]> git.proxmox.com Git - rustc.git/blob - debian/wasi-node
Update upstream source from tag 'upstream/1.60.0+dfsg1'
[rustc.git] / debian / wasi-node
1 #!/usr/bin/node --experimental-wasi-unstable-preview1
2 ///
3 /// Simple WASI executor, adapted from the NodeJS WASI module API docs [1].
4 ///
5 /// Usage: wasi-node <command> [<args> .. ]
6 ///
7 /// Environment variables:
8 ///
9 /// WASI_NODE_PREOPENS - optional JSON file defining the application sandbox
10 /// directory structure. See [1] for details.
11 ///
12 /// WASI_NODE_ENV - optional JSON file defining the application environment.
13 /// If omitted then the process's POSIX environment is used; this may leak
14 /// information. If a clean environment is required then set this to /dev/null
15 /// or some other empty file.
16 ///
17 /// [1] https://nodejs.org/api/wasi.html
18
19 'use strict';
20 const fs = require('fs');
21 const { WASI } = require('wasi');
22
23 // argv[0] is nodejs
24 // argv[1] is this script
25 var args = process.argv.slice(2); // inner argv includes cmd
26
27 if (!args[0]) {
28 console.warn(process.argv[1] + ": no command given");
29 process.exit(1);
30 }
31
32 var preopens = {};
33 var preopens_json = process.env["WASI_NODE_PREOPENS"];
34 if (preopens_json) {
35 var preopens_data = fs.readFileSync(preopens_json);
36 preopens = preopens_data.length ? JSON.parse(preopens_data) : {};
37 }
38
39 var env = process.env;
40 var env_json = process.env["WASI_NODE_ENV"];
41 if (env_json) {
42 var env_data = fs.readFileSync(env_json);
43 env = env_data.length ? JSON.parse(env_data) : {};
44 }
45
46 const wasi = new WASI({ args: args, env: env, preopens: preopens });
47 const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
48
49 (async () => {
50 const wasm = await WebAssembly.compile(fs.readFileSync(args[0]));
51 const instance = await WebAssembly.instantiate(wasm, importObject);
52
53 wasi.start(instance);
54 })();