| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a3b6095 commit 8adfb54
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,12 @@ added: v12.16.0 | |||
| 62 | 62 | process via the `__wasi_proc_exit()` function. Setting this option to `true` | |
| 63 | 63 | causes `wasi.start()` to return the exit code rather than terminate the | |
| 64 | 64 | process. **Default:** `false`. | |
| 65 | + * `stdin` {integer} The file descriptor used as standard input in the | ||
| 66 | + WebAssembly application. **Default:** `0`. | ||
| 67 | + * `stdout` {integer} The file descriptor used as standard output in the | ||
| 68 | + WebAssembly application. **Default:** `1`. | ||
| 69 | + * `stderr` {integer} The file descriptor used as standard error in the | ||
| 70 | + WebAssembly application. **Default:** `2`. | ||
| 65 | 71 | ||
| 66 | 72 | ### `wasi.start(instance)` | |
| 67 | 73 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ const { isArrayBuffer } = require('internal/util/types'); | |||
| 15 | 15 | const { | |
| 16 | 16 | validateArray, | |
| 17 | 17 | validateBoolean, | |
| 18 | + validateInt32, | ||
| 18 | 19 | validateObject, | |
| 19 | 20 | } = require('internal/validators'); | |
| 20 | 21 | const { WASI: _WASI } = internalBinding('wasi'); | |
@@ -50,7 +51,13 @@ class WASI { | |||
| 50 | 51 | } | |
| 51 | 52 | } | |
| 52 | 53 | ||
| 53 | - const wrap = new _WASI(args, env, preopens); | ||
| 54 | + const { stdin = 0, stdout = 1, stderr = 2 } = options; | ||
| 55 | + validateInt32(stdin, 'options.stdin', 0); | ||
| 56 | + validateInt32(stdout, 'options.stdout', 0); | ||
| 57 | + validateInt32(stderr, 'options.stderr', 0); | ||
| 58 | + const stdio = [stdin, stdout, stderr]; | ||
| 59 | + | ||
| 60 | + const wrap = new _WASI(args, env, preopens, stdio); | ||
| 54 | 61 | ||
| 55 | 62 | for (const prop in wrap) { | |
| 56 | 63 | wrap[prop] = FunctionPrototypeBind(wrap[prop], wrap); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -162,20 +162,27 @@ void WASI::DecreaseAllocatedSize(size_t size) { | |||
| 162 | 162 | ||
| 163 | 163 | void WASI::New(const FunctionCallbackInfo<Value>& args) { | |
| 164 | 164 | CHECK(args.IsConstructCall()); | |
| 165 | - CHECK_EQ(args.Length(), 3); | ||
| 165 | + CHECK_EQ(args.Length(), 4); | ||
| 166 | 166 | CHECK(args[0]->IsArray()); | |
| 167 | 167 | CHECK(args[1]->IsArray()); | |
| 168 | 168 | CHECK(args[2]->IsArray()); | |
| 169 | + CHECK(args[3]->IsArray()); | ||
| 169 | 170 | ||
| 170 | 171 | Environment* env = Environment::GetCurrent(args); | |
| 171 | 172 | Local<Context> context = env->context(); | |
| 172 | 173 | Local<Array> argv = args[0].As<Array>(); | |
| 173 | 174 | const uint32_t argc = argv->Length(); | |
| 174 | 175 | uvwasi_options_t options; | |
| 175 | 176 | ||
| 176 | - options.in = 0; | ||
| 177 | - options.out = 1; | ||
| 178 | - options.err = 2; | ||
| 177 | + Local<Array> stdio = args[3].As<Array>(); | ||
| 178 | + CHECK_EQ(stdio->Length(), 3); | ||
| 179 | + options.in = stdio->Get(context, 0).ToLocalChecked()-> | ||
| 180 | + Int32Value(context).FromJust(); | ||
| 181 | + options.out = stdio->Get(context, 1).ToLocalChecked()-> | ||
| 182 | + Int32Value(context).FromJust(); | ||
| 183 | + options.err = stdio->Get(context, 2).ToLocalChecked()-> | ||
| 184 | + Int32Value(context).FromJust(); | ||
| 185 | + | ||
| 179 | 186 | options.fd_table_size = 3; | |
| 180 | 187 | options.argc = argc; | |
| 181 | 188 | options.argv = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,18 @@ assert.throws(() => { new WASI({ preopens: 'fhqwhgads' }); }, | |||
| 25 | 25 | assert.throws(() => { new WASI({ returnOnExit: 'fhqwhgads' }); }, | |
| 26 | 26 | { code: 'ERR_INVALID_ARG_TYPE', message: /\breturnOnExit\b/ }); | |
| 27 | 27 | ||
| 28 | + // If stdin is not an int32 and not undefined, it should throw. | ||
| 29 | + assert.throws(() => { new WASI({ stdin: 'fhqwhgads' }); }, | ||
| 30 | + { code: 'ERR_INVALID_ARG_TYPE', message: /\bstdin\b/ }); | ||
| 31 | + | ||
| 32 | + // If stdout is not an int32 and not undefined, it should throw. | ||
| 33 | + assert.throws(() => { new WASI({ stdout: 'fhqwhgads' }); }, | ||
| 34 | + { code: 'ERR_INVALID_ARG_TYPE', message: /\bstdout\b/ }); | ||
| 35 | + | ||
| 36 | + // If stderr is not an int32 and not undefined, it should throw. | ||
| 37 | + assert.throws(() => { new WASI({ stderr: 'fhqwhgads' }); }, | ||
| 38 | + { code: 'ERR_INVALID_ARG_TYPE', message: /\bstderr\b/ }); | ||
| 39 | + | ||
| 28 | 40 | // If options is provided, but not an object, the constructor should throw. | |
| 29 | 41 | [null, 'foo', '', 0, NaN, Symbol(), true, false, () => {}].forEach((value) => { | |
| 30 | 42 | assert.throws(() => { new WASI(value); }, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + // Flags: --experimental-wasi-unstable-preview1 --experimental-wasm-bigint | ||
| 2 | + 'use strict'; | ||
| 3 | + require('../common'); | ||
| 4 | + const tmpdir = require('../common/tmpdir'); | ||
| 5 | + const { strictEqual } = require('assert'); | ||
| 6 | + const { closeSync, openSync, readFileSync, writeFileSync } = require('fs'); | ||
| 7 | + const { join } = require('path'); | ||
| 8 | + const { WASI } = require('wasi'); | ||
| 9 | + const modulePath = join(__dirname, 'wasm', 'stdin.wasm'); | ||
| 10 | + const buffer = readFileSync(modulePath); | ||
| 11 | + const stdinFile = join(tmpdir.path, 'stdin.txt'); | ||
| 12 | + const stdoutFile = join(tmpdir.path, 'stdout.txt'); | ||
| 13 | + const stderrFile = join(tmpdir.path, 'stderr.txt'); | ||
| 14 | + | ||
| 15 | + tmpdir.refresh(); | ||
| 16 | + // Write 33 x's. The test's buffer only holds 31 x's + a terminator. | ||
| 17 | + writeFileSync(stdinFile, 'x'.repeat(33)); | ||
| 18 | + | ||
| 19 | + const stdin = openSync(stdinFile, 'r'); | ||
| 20 | + const stdout = openSync(stdoutFile, 'a'); | ||
| 21 | + const stderr = openSync(stderrFile, 'a'); | ||
| 22 | + const wasi = new WASI({ stdin, stdout, stderr, returnOnExit: true }); | ||
| 23 | + const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; | ||
| 24 | + | ||
| 25 | + (async () => { | ||
| 26 | + const { instance } = await WebAssembly.instantiate(buffer, importObject); | ||
| 27 | + | ||
| 28 | + strictEqual(wasi.start(instance), 0); | ||
| 29 | + closeSync(stdin); | ||
| 30 | + closeSync(stdout); | ||
| 31 | + closeSync(stderr); | ||
| 32 | + strictEqual(readFileSync(stdoutFile, 'utf8').trim(), 'x'.repeat(31)); | ||
| 33 | + strictEqual(readFileSync(stderrFile, 'utf8').trim(), ''); | ||
| 34 | + })(); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments