11cb0ef41Sopenharmony_ci{
21cb0ef41Sopenharmony_ci  "type": "module",
31cb0ef41Sopenharmony_ci  "source": "doc/api/wasi.md",
41cb0ef41Sopenharmony_ci  "modules": [
51cb0ef41Sopenharmony_ci    {
61cb0ef41Sopenharmony_ci      "textRaw": "WebAssembly System Interface (WASI)",
71cb0ef41Sopenharmony_ci      "name": "webassembly_system_interface_(wasi)",
81cb0ef41Sopenharmony_ci      "introduced_in": "v12.16.0",
91cb0ef41Sopenharmony_ci      "stability": 1,
101cb0ef41Sopenharmony_ci      "stabilityText": "Experimental",
111cb0ef41Sopenharmony_ci      "desc": "<p><strong>Source Code:</strong> <a href=\"https://github.com/nodejs/node/blob/v18.20.1/lib/wasi.js\">lib/wasi.js</a></p>\n<p>The WASI API provides an implementation of the <a href=\"https://wasi.dev/\">WebAssembly System Interface</a>\nspecification. WASI gives sandboxed WebAssembly applications access to the\nunderlying operating system via a collection of POSIX-like functions.</p>\n<pre><code class=\"language-mjs\">import { readFile } from 'node:fs/promises';\nimport { WASI } from 'wasi';\nimport { argv, env } from 'node:process';\n\nconst wasi = new WASI({\n  args: argv,\n  env,\n  preopens: {\n    '/sandbox': '/some/real/path/that/wasm/can/access',\n  },\n});\n\n// Some WASI binaries require:\n//   const importObject = { wasi_unstable: wasi.wasiImport };\nconst importObject = { wasi_snapshot_preview1: wasi.wasiImport };\n\nconst wasm = await WebAssembly.compile(\n  await readFile(new URL('./demo.wasm', import.meta.url)),\n);\nconst instance = await WebAssembly.instantiate(wasm, importObject);\n\nwasi.start(instance);\n</code></pre>\n<pre><code class=\"language-cjs\">'use strict';\nconst { readFile } = require('node:fs/promises');\nconst { WASI } = require('wasi');\nconst { argv, env } = require('node:process');\nconst { join } = require('node:path');\n\nconst wasi = new WASI({\n  args: argv,\n  env,\n  preopens: {\n    '/sandbox': '/some/real/path/that/wasm/can/access',\n  },\n});\n\n// Some WASI binaries require:\n//   const importObject = { wasi_unstable: wasi.wasiImport };\nconst importObject = { wasi_snapshot_preview1: wasi.wasiImport };\n\n(async () => {\n  const wasm = await WebAssembly.compile(\n    await readFile(join(__dirname, 'demo.wasm')),\n  );\n  const instance = await WebAssembly.instantiate(wasm, importObject);\n\n  wasi.start(instance);\n})();\n</code></pre>\n<p>To run the above example, create a new WebAssembly text format file named\n<code>demo.wat</code>:</p>\n<pre><code class=\"language-text\">(module\n    ;; Import the required fd_write WASI function which will write the given io vectors to stdout\n    ;; The function signature for fd_write is:\n    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written\n    (import \"wasi_snapshot_preview1\" \"fd_write\" (func $fd_write (param i32 i32 i32 i32) (result i32)))\n\n    (memory 1)\n    (export \"memory\" (memory 0))\n\n    ;; Write 'hello world\\n' to memory at an offset of 8 bytes\n    ;; Note the trailing newline which is required for the text to appear\n    (data (i32.const 8) \"hello world\\n\")\n\n    (func $main (export \"_start\")\n        ;; Creating a new io vector within linear memory\n        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\\n' string\n        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\\n' string\n\n        (call $fd_write\n            (i32.const 1) ;; file_descriptor - 1 for stdout\n            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0\n            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.\n            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written\n        )\n        drop ;; Discard the number of bytes written from the top of the stack\n    )\n)\n</code></pre>\n<p>Use <a href=\"https://github.com/WebAssembly/wabt\">wabt</a> to compile <code>.wat</code> to <code>.wasm</code></p>\n<pre><code class=\"language-console\">$ wat2wasm demo.wat\n</code></pre>",
121cb0ef41Sopenharmony_ci      "classes": [
131cb0ef41Sopenharmony_ci        {
141cb0ef41Sopenharmony_ci          "textRaw": "Class: `WASI`",
151cb0ef41Sopenharmony_ci          "type": "class",
161cb0ef41Sopenharmony_ci          "name": "WASI",
171cb0ef41Sopenharmony_ci          "meta": {
181cb0ef41Sopenharmony_ci            "added": [
191cb0ef41Sopenharmony_ci              "v13.3.0",
201cb0ef41Sopenharmony_ci              "v12.16.0"
211cb0ef41Sopenharmony_ci            ],
221cb0ef41Sopenharmony_ci            "changes": []
231cb0ef41Sopenharmony_ci          },
241cb0ef41Sopenharmony_ci          "desc": "<p>The <code>WASI</code> class provides the WASI system call API and additional convenience\nmethods for working with WASI-based applications. Each <code>WASI</code> instance\nrepresents a distinct sandbox environment. For security purposes, each <code>WASI</code>\ninstance must have its command-line arguments, environment variables, and\nsandbox directory structure configured explicitly.</p>",
251cb0ef41Sopenharmony_ci          "methods": [
261cb0ef41Sopenharmony_ci            {
271cb0ef41Sopenharmony_ci              "textRaw": "`wasi.start(instance)`",
281cb0ef41Sopenharmony_ci              "type": "method",
291cb0ef41Sopenharmony_ci              "name": "start",
301cb0ef41Sopenharmony_ci              "meta": {
311cb0ef41Sopenharmony_ci                "added": [
321cb0ef41Sopenharmony_ci                  "v13.3.0",
331cb0ef41Sopenharmony_ci                  "v12.16.0"
341cb0ef41Sopenharmony_ci                ],
351cb0ef41Sopenharmony_ci                "changes": []
361cb0ef41Sopenharmony_ci              },
371cb0ef41Sopenharmony_ci              "signatures": [
381cb0ef41Sopenharmony_ci                {
391cb0ef41Sopenharmony_ci                  "params": [
401cb0ef41Sopenharmony_ci                    {
411cb0ef41Sopenharmony_ci                      "textRaw": "`instance` {WebAssembly.Instance}",
421cb0ef41Sopenharmony_ci                      "name": "instance",
431cb0ef41Sopenharmony_ci                      "type": "WebAssembly.Instance"
441cb0ef41Sopenharmony_ci                    }
451cb0ef41Sopenharmony_ci                  ]
461cb0ef41Sopenharmony_ci                }
471cb0ef41Sopenharmony_ci              ],
481cb0ef41Sopenharmony_ci              "desc": "<p>Attempt to begin execution of <code>instance</code> as a WASI command by invoking its\n<code>_start()</code> export. If <code>instance</code> does not contain a <code>_start()</code> export, or if\n<code>instance</code> contains an <code>_initialize()</code> export, then an exception is thrown.</p>\n<p><code>start()</code> requires that <code>instance</code> exports a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory\"><code>WebAssembly.Memory</code></a> named\n<code>memory</code>. If <code>instance</code> does not have a <code>memory</code> export an exception is thrown.</p>\n<p>If <code>start()</code> is called more than once, an exception is thrown.</p>"
491cb0ef41Sopenharmony_ci            },
501cb0ef41Sopenharmony_ci            {
511cb0ef41Sopenharmony_ci              "textRaw": "`wasi.initialize(instance)`",
521cb0ef41Sopenharmony_ci              "type": "method",
531cb0ef41Sopenharmony_ci              "name": "initialize",
541cb0ef41Sopenharmony_ci              "meta": {
551cb0ef41Sopenharmony_ci                "added": [
561cb0ef41Sopenharmony_ci                  "v14.6.0",
571cb0ef41Sopenharmony_ci                  "v12.19.0"
581cb0ef41Sopenharmony_ci                ],
591cb0ef41Sopenharmony_ci                "changes": []
601cb0ef41Sopenharmony_ci              },
611cb0ef41Sopenharmony_ci              "signatures": [
621cb0ef41Sopenharmony_ci                {
631cb0ef41Sopenharmony_ci                  "params": [
641cb0ef41Sopenharmony_ci                    {
651cb0ef41Sopenharmony_ci                      "textRaw": "`instance` {WebAssembly.Instance}",
661cb0ef41Sopenharmony_ci                      "name": "instance",
671cb0ef41Sopenharmony_ci                      "type": "WebAssembly.Instance"
681cb0ef41Sopenharmony_ci                    }
691cb0ef41Sopenharmony_ci                  ]
701cb0ef41Sopenharmony_ci                }
711cb0ef41Sopenharmony_ci              ],
721cb0ef41Sopenharmony_ci              "desc": "<p>Attempt to initialize <code>instance</code> as a WASI reactor by invoking its\n<code>_initialize()</code> export, if it is present. If <code>instance</code> contains a <code>_start()</code>\nexport, then an exception is thrown.</p>\n<p><code>initialize()</code> requires that <code>instance</code> exports a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory\"><code>WebAssembly.Memory</code></a> named\n<code>memory</code>. If <code>instance</code> does not have a <code>memory</code> export an exception is thrown.</p>\n<p>If <code>initialize()</code> is called more than once, an exception is thrown.</p>"
731cb0ef41Sopenharmony_ci            }
741cb0ef41Sopenharmony_ci          ],
751cb0ef41Sopenharmony_ci          "properties": [
761cb0ef41Sopenharmony_ci            {
771cb0ef41Sopenharmony_ci              "textRaw": "`wasiImport` {Object}",
781cb0ef41Sopenharmony_ci              "type": "Object",
791cb0ef41Sopenharmony_ci              "name": "wasiImport",
801cb0ef41Sopenharmony_ci              "meta": {
811cb0ef41Sopenharmony_ci                "added": [
821cb0ef41Sopenharmony_ci                  "v13.3.0",
831cb0ef41Sopenharmony_ci                  "v12.16.0"
841cb0ef41Sopenharmony_ci                ],
851cb0ef41Sopenharmony_ci                "changes": []
861cb0ef41Sopenharmony_ci              },
871cb0ef41Sopenharmony_ci              "desc": "<p><code>wasiImport</code> is an object that implements the WASI system call API. This object\nshould be passed as the <code>wasi_snapshot_preview1</code> import during the instantiation\nof a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance\"><code>WebAssembly.Instance</code></a>.</p>"
881cb0ef41Sopenharmony_ci            }
891cb0ef41Sopenharmony_ci          ],
901cb0ef41Sopenharmony_ci          "signatures": [
911cb0ef41Sopenharmony_ci            {
921cb0ef41Sopenharmony_ci              "params": [
931cb0ef41Sopenharmony_ci                {
941cb0ef41Sopenharmony_ci                  "textRaw": "`options` {Object}",
951cb0ef41Sopenharmony_ci                  "name": "options",
961cb0ef41Sopenharmony_ci                  "type": "Object",
971cb0ef41Sopenharmony_ci                  "options": [
981cb0ef41Sopenharmony_ci                    {
991cb0ef41Sopenharmony_ci                      "textRaw": "`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:** `[]`.",
1001cb0ef41Sopenharmony_ci                      "name": "args",
1011cb0ef41Sopenharmony_ci                      "type": "Array",
1021cb0ef41Sopenharmony_ci                      "default": "`[]`",
1031cb0ef41Sopenharmony_ci                      "desc": "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."
1041cb0ef41Sopenharmony_ci                    },
1051cb0ef41Sopenharmony_ci                    {
1061cb0ef41Sopenharmony_ci                      "textRaw": "`env` {Object} An object similar to `process.env` that the WebAssembly application will see as its environment. **Default:** `{}`.",
1071cb0ef41Sopenharmony_ci                      "name": "env",
1081cb0ef41Sopenharmony_ci                      "type": "Object",
1091cb0ef41Sopenharmony_ci                      "default": "`{}`",
1101cb0ef41Sopenharmony_ci                      "desc": "An object similar to `process.env` that the WebAssembly application will see as its environment."
1111cb0ef41Sopenharmony_ci                    },
1121cb0ef41Sopenharmony_ci                    {
1131cb0ef41Sopenharmony_ci                      "textRaw": "`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.",
1141cb0ef41Sopenharmony_ci                      "name": "preopens",
1151cb0ef41Sopenharmony_ci                      "type": "Object",
1161cb0ef41Sopenharmony_ci                      "desc": "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."
1171cb0ef41Sopenharmony_ci                    },
1181cb0ef41Sopenharmony_ci                    {
1191cb0ef41Sopenharmony_ci                      "textRaw": "`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`.",
1201cb0ef41Sopenharmony_ci                      "name": "returnOnExit",
1211cb0ef41Sopenharmony_ci                      "type": "boolean",
1221cb0ef41Sopenharmony_ci                      "default": "`false`",
1231cb0ef41Sopenharmony_ci                      "desc": "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."
1241cb0ef41Sopenharmony_ci                    },
1251cb0ef41Sopenharmony_ci                    {
1261cb0ef41Sopenharmony_ci                      "textRaw": "`stdin` {integer} The file descriptor used as standard input in the WebAssembly application. **Default:** `0`.",
1271cb0ef41Sopenharmony_ci                      "name": "stdin",
1281cb0ef41Sopenharmony_ci                      "type": "integer",
1291cb0ef41Sopenharmony_ci                      "default": "`0`",
1301cb0ef41Sopenharmony_ci                      "desc": "The file descriptor used as standard input in the WebAssembly application."
1311cb0ef41Sopenharmony_ci                    },
1321cb0ef41Sopenharmony_ci                    {
1331cb0ef41Sopenharmony_ci                      "textRaw": "`stdout` {integer} The file descriptor used as standard output in the WebAssembly application. **Default:** `1`.",
1341cb0ef41Sopenharmony_ci                      "name": "stdout",
1351cb0ef41Sopenharmony_ci                      "type": "integer",
1361cb0ef41Sopenharmony_ci                      "default": "`1`",
1371cb0ef41Sopenharmony_ci                      "desc": "The file descriptor used as standard output in the WebAssembly application."
1381cb0ef41Sopenharmony_ci                    },
1391cb0ef41Sopenharmony_ci                    {
1401cb0ef41Sopenharmony_ci                      "textRaw": "`stderr` {integer} The file descriptor used as standard error in the WebAssembly application. **Default:** `2`.",
1411cb0ef41Sopenharmony_ci                      "name": "stderr",
1421cb0ef41Sopenharmony_ci                      "type": "integer",
1431cb0ef41Sopenharmony_ci                      "default": "`2`",
1441cb0ef41Sopenharmony_ci                      "desc": "The file descriptor used as standard error in the WebAssembly application."
1451cb0ef41Sopenharmony_ci                    }
1461cb0ef41Sopenharmony_ci                  ]
1471cb0ef41Sopenharmony_ci                }
1481cb0ef41Sopenharmony_ci              ]
1491cb0ef41Sopenharmony_ci            }
1501cb0ef41Sopenharmony_ci          ]
1511cb0ef41Sopenharmony_ci        }
1521cb0ef41Sopenharmony_ci      ],
1531cb0ef41Sopenharmony_ci      "type": "module",
1541cb0ef41Sopenharmony_ci      "displayName": "WebAssembly System Interface (WASI)"
1551cb0ef41Sopenharmony_ci    }
1561cb0ef41Sopenharmony_ci  ]
1571cb0ef41Sopenharmony_ci}