SOLFIND
Web Lens
Portal home

WebAssembly System Interface (WASI) | Node.js v19.9.0 Documentation

https://nodejs.org/docs/latest-v19.x/api/wasi.html • 30 KB fetched
Open original page


WebAssembly System Interface (WASI) | Node.js v19.9.0 Documentation Node.js * About this documentation * Usage and example * Assertion testing * Asynchronous context tracking * Async hooks * Buffer * C++ addons * C/C++ addons with Node-API * C++ embedder API * Child processes * Cluster * Command-line options * Console * Corepack * Crypto * Debugger * Deprecated APIs * Diagnostics Channel * DNS * Domain * Errors * Events * File system * Globals * HTTP * HTTP/2 * HTTPS * Inspector * Internationalization * Modules: CommonJS modules * Modules: ECMAScript modules * Modules: node:module API * Modules: Packages * Net * OS * Path * Performance hooks * Permissions * Process * Punycode * Query strings * Readline * REPL * Report * Single executable applications * Stream * String decoder * Test runner * Timers * TLS/SSL * Trace events * TTY * UDP/datagram * URL * Utilities * V8 * VM * WASI * Web Crypto API * Web Streams API * Worker threads * Zlib * Code repository and issue tracker Node.js v19.9.0 documentation * Node.js v19.9.0 * ► ▼ Table of contents * WebAssembly System Interface (WASI) * Class: WASI * new WASI([options]) * wasi.getImportObject() * wasi.start(instance) * wasi.initialize(instance) * wasi.wasiImport * ► ▼ Index * About this documentation * Usage and example * Index * Assertion testing * Asynchronous context tracking * Async hooks * Buffer * C++ addons * C/C++ addons with Node-API * C++ embedder API * Child processes * Cluster * Command-line options * Console * Corepack * Crypto * Debugger * Deprecated APIs * Diagnostics Channel * DNS * Domain * Errors * Events * File system * Globals * HTTP * HTTP/2 * HTTPS * Inspector * Internationalization * Modules: CommonJS modules * Modules: ECMAScript modules * Modules: node:module API * Modules: Packages * Net * OS * Path * Performance hooks * Permissions * Process * Punycode * Query strings * Readline * REPL * Report * Single executable applications * Stream * String decoder * Test runner * Timers * TLS/SSL * Trace events * TTY * UDP/datagram * URL * Utilities * V8 * VM * WASI * Web Crypto API * Web Streams API * Worker threads * Zlib * Code repository and issue tracker * ► ▼ Other versions * 19.x * 18.x LTS * 17.x * 16.x LTS * 15.x * 14.x LTS * 13.x * 12.x * ► ▼ Options * View on single page * View as JSON * Edit on GitHub Table of contents * WebAssembly System Interface (WASI) * Class: WASI * new WASI([options]) * wasi.getImportObject() * wasi.start(instance) * wasi.initialize(instance) * wasi.wasiImport WebAssembly System Interface (WASI) # Stability: 1 - Experimental Source Code: lib/wasi.js The WASI API provides an implementation of the WebAssembly System Interface specification. WASI gives sandboxed WebAssembly applications access to the underlying operating system via a collection of POSIX-like functions. import { readFile } from 'node:fs/promises' ; import { WASI } from 'wasi' ; import { argv, env } from 'node:process' ; const wasi = new WASI ({ version : 'preview1' , args : argv, env, preopens : { '/sandbox' : '/some/real/path/that/wasm/can/access' , }, }); const wasm = await WebAssembly . compile ( await readFile ( new URL ( './demo.wasm' , import . meta . url )), ); const instance = await WebAssembly . instantiate (wasm, wasi. getImportObject ()); wasi. start (instance); 'use strict' ; const { readFile } = require ( 'node:fs/promises' ); const { WASI } = require ( 'wasi' ); const { argv, env } = require ( 'node:process' ); const { join } = require ( 'node:path' ); const wasi = new WASI ({ version : 'preview1' , args : argv, env, preopens : { '/sandbox' : '/some/real/path/that/wasm/can/access' , }, }); ( async () => { const wasm = await WebAssembly . compile ( await readFile ( join (__dirname, 'demo.wasm' )), ); const instance = await WebAssembly . instantiate (wasm, wasi. getImportObject ()); wasi. start (instance); })(); copy To run the above example, create a new WebAssembly text format file named demo.wat : (module ;; Import the required fd_write WASI function which will write the given io vectors to stdout ;; The function signature for fd_write is: ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32))) (memory 1) (export "memory" (memory 0)) ;; Write 'hello world\n' to memory at an offset of 8 bytes ;; Note the trailing newline which is required for the text to appear (data (i32.const 8) "hello world\n") (func $main (export "_start") ;; Creating a new io vector within linear memory (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string (call $fd_write (i32.const 1) ;; file_descriptor - 1 for stdout (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written ) drop ;; Discard the number of bytes written from the top of the stack ) ) copy Use wabt to compile .wat to .wasm $ wat2wasm demo.wat copy The --experimental-wasi-unstable-preview1 CLI argument is needed for this example to run. Class: WASI # Added in: v13.3.0, v12.16.0 The WASI class provides the WASI system call API and additional convenience methods for working with WASI-based applications. Each WASI instance represents a distinct sandbox environment. For security purposes, each WASI instance must have its command-line arguments, environment variables, and sandbox directory structure configured explicitly. new WASI([options]) # History Version Changes v19.8.0 version field added to options. v13.3.0, v12.16.0 Added in: v13.3.0, v12.16.0 * options <Object> * args <Array> An array of strings that the WebAssembly application will see as command-line arguments. The first argument is the virtual path to the WASI command itself. Default: [] . * env <Object> An object similar to process.env that the WebAssembly application will see as its environment. Default: {} . * preopens <Object> This object represents the WebAssembly application's sandbox directory structure. The string keys of preopens are treated as directories within the sandbox. The corresponding values in preopens are the real paths to those directories on the host machine. * returnOnExit <boolean> By default, WASI applications terminate the Node.js process via the __wasi_proc_exit() function. Setting this option to true causes wasi.start() to return the exit code rather than terminate the process. Default: false . * stdin <integer> The file descriptor used as standard input in the WebAssembly application. Default: 0 . * stdout <integer> The file descriptor used as standard output in the WebAssembly application. Default: 1 . * stderr <integer> The file descriptor used as standard error in the WebAssembly application. Default: 2 . * version <string> The version of WASI requested. Currently the only supported versions are unstable and preview1 . Default: preview1 . wasi.getImportObject() # Added in: v19.8.0 Return an import object that can be passed to WebAssembly.instantiate() if no other WASM imports are needed beyond those provided by WASI. If version unstable was passed into the constructor it will return: { wasi_unstable : wasi.wasiImport } copy If version preview1 was passed into the constructor or no version was specified it will return: { wasi_snapshot_preview1 : wasi.wasiImport } copy wasi.start(instance) # Added in: v13.3.0, v12.16.0 * instance <WebAssembly.Instance> Attempt to begin execution of instance as a WASI command by invoking its _start() export. If instance does not contain a _start() export, or if instance contains an _initialize() export, then an exception is thrown. start() requires that instance exports a WebAssembly.Memory named memory . If instance does not have a memory export an exception is thrown. If start() is called more than once, an exception is thrown. wasi.initialize(instance) # Added in: v14.6.0, v12.19.0 * instance <WebAssembly.Instance> Attempt to initialize instance as a WASI reactor by invoking its _initialize() export, if it is present. If instance contains a _start() export, then an exception is thrown. initialize() requires that instance exports a WebAssembly.Memory named memory . If instance does not have a memory export an exception is thrown. If initialize() is called more than once, an exception is thrown. wasi.wasiImport # Added in: v13.3.0, v12.16.0 * <Object> wasiImport is an object that implements the WASI system call API. This object should be passed as the wasi_snapshot_preview1 import during the instantiation of a WebAssembly.Instance .

Links found on this page

  1. Node.js [direct]
  2. About this documentation [direct]
  3. Usage and example [direct]
  4. Assertion testing [direct]
  5. Asynchronous context tracking [direct]
  6. Async hooks [direct]
  7. Buffer [direct]
  8. C++ addons [direct]
  9. C/C++ addons with Node-API [direct]
  10. C++ embedder API [direct]
  11. Child processes [direct]
  12. Cluster [direct]
  13. Command-line options [direct]
  14. Console [direct]
  15. Corepack [direct]
  16. Crypto [direct]
  17. Debugger [direct]
  18. Deprecated APIs [direct]
  19. Diagnostics Channel [direct]
  20. DNS [direct]
  21. Domain [direct]
  22. Errors [direct]
  23. Events [direct]
  24. File system [direct]
  25. Globals [direct]
  26. HTTP [direct]
  27. HTTP/2 [direct]
  28. HTTPS [direct]
  29. Inspector [direct]
  30. Internationalization [direct]
  31. Modules: CommonJS modules [direct]
  32. Modules: ECMAScript modules [direct]
  33. Modules: node:module API [direct]
  34. Modules: Packages [direct]
  35. Net [direct]
  36. OS [direct]
  37. Path [direct]
  38. Performance hooks [direct]
  39. Permissions [direct]
  40. Process [direct]
  41. Punycode [direct]
  42. Query strings [direct]
  43. Readline [direct]
  44. REPL [direct]
  45. Report [direct]
  46. Single executable applications [direct]
  47. Stream [direct]
  48. String decoder [direct]
  49. Test runner [direct]
  50. Timers [direct]
  51. TLS/SSL [direct]
  52. Trace events [direct]
  53. TTY [direct]
  54. UDP/datagram [direct]
  55. URL [direct]
  56. Utilities [direct]
  57. V8 [direct]
  58. VM [direct]
  59. WASI [direct]
  60. Web Crypto API [direct]
  61. Web Streams API [direct]
  62. Worker threads [direct]
  63. Zlib [direct]
  64. Code repository and issue tracker [direct]
  65. Index [direct]
  66. 18.x LTS [direct]
  67. 17.x [direct]
  68. 16.x LTS [direct]
  69. 15.x [direct]
  70. 14.x LTS [direct]
  71. 13.x [direct]
  72. 12.x [direct]
  73. View on single page [direct]
  74. View as JSON [direct]
  75. Edit on GitHub [direct]
  76. lib/wasi.js [direct]
  77. WebAssembly System Interface [direct]
  78. wabt [direct]
  79. <Object> [direct]
  80. <Array> [direct]