| 36 | |
| 37 | class WASI { |
| 38 | constructor(options = kEmptyObject) { |
| 39 | validateObject(options, 'options'); |
| 40 | |
| 41 | let _WASI; |
| 42 | validateString(options.version, 'options.version'); |
| 43 | switch (options.version) { |
| 44 | case 'unstable': |
| 45 | ({ WASI: _WASI } = internalBinding('wasi')); |
| 46 | this[kBindingName] = 'wasi_unstable'; |
| 47 | break; |
| 48 | case 'preview1': |
| 49 | ({ WASI: _WASI } = internalBinding('wasi')); |
| 50 | this[kBindingName] = 'wasi_snapshot_preview1'; |
| 51 | break; |
| 52 | // When adding support for additional wasi versions add case here |
| 53 | default: |
| 54 | throw new ERR_INVALID_ARG_VALUE('options.version', |
| 55 | options.version, |
| 56 | 'unsupported WASI version'); |
| 57 | } |
| 58 | |
| 59 | if (options.args !== undefined) |
| 60 | validateArray(options.args, 'options.args'); |
| 61 | const args = ArrayPrototypeMap(options.args || [], String); |
| 62 | |
| 63 | const env = []; |
| 64 | if (options.env !== undefined) { |
| 65 | validateObject(options.env, 'options.env'); |
| 66 | ArrayPrototypeForEach( |
| 67 | ObjectEntries(options.env), |
| 68 | ({ 0: key, 1: value }) => { |
| 69 | if (value !== undefined) |
| 70 | ArrayPrototypePush(env, `${key}=${value}`); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | const preopens = []; |
| 75 | if (options.preopens !== undefined) { |
| 76 | validateObject(options.preopens, 'options.preopens'); |
| 77 | ArrayPrototypeForEach( |
| 78 | ObjectEntries(options.preopens), |
| 79 | ({ 0: key, 1: value }) => |
| 80 | ArrayPrototypePush(preopens, String(key), String(value)), |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | const { stdin = 0, stdout = 1, stderr = 2 } = options; |
| 85 | validateInt32(stdin, 'options.stdin', 0); |
| 86 | validateInt32(stdout, 'options.stdout', 0); |
| 87 | validateInt32(stderr, 'options.stderr', 0); |
| 88 | const stdio = [stdin, stdout, stderr]; |
| 89 | |
| 90 | const wrap = new _WASI(args, env, preopens, stdio); |
| 91 | |
| 92 | for (const prop in wrap) { |
| 93 | wrap[prop] = FunctionPrototypeBind(wrap[prop], wrap); |
| 94 | } |
| 95 | |