Skip to content

Commit

Permalink
wasi: make returnOnExit true by default
Browse files Browse the repository at this point in the history
Refs: nodejs#46923

Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Apr 6, 2023
1 parent 7efae93 commit 5583c30
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
12 changes: 8 additions & 4 deletions doc/api/wasi.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47391
description: The version option is now required and has no default value.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47390
description: default for returnOnExit changed to true.
- version: v19.8.0
pr-url: https://github.com/nodejs/node/pull/46469
description: version field added to options.
Expand All @@ -136,10 +139,11 @@ changes:
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`.
* `returnOnExit` {boolean} By default, when WASI applications call
`__wasi_proc_exit()` `wasi.start()` will returns with the exit code
specified rather than terminatng the process. Setting this option to
`false` will cause the Node.js process to exit with the specified
exit code instead. **Default:** `true`.
* `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
Expand Down
8 changes: 5 additions & 3 deletions lib/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ class WASI {
wrap[prop] = FunctionPrototypeBind(wrap[prop], wrap);
}

let returnOnExit = true;
if (options.returnOnExit !== undefined) {
validateBoolean(options.returnOnExit, 'options.returnOnExit');
if (options.returnOnExit)
wrap.proc_exit = FunctionPrototypeBind(wasiReturnOnProcExit, this);
returnOnExit = options.returnOnExit;
validateBoolean(returnOnExit, 'options.returnOnExit');
}
if (returnOnExit === true)
wrap.proc_exit = FunctionPrototypeBind(wasiReturnOnProcExit, this);

this[kSetMemory] = wrap._setMemory;
delete wrap._setMemory;
Expand Down
16 changes: 15 additions & 1 deletion test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
'use strict';
const common = require('../common');

function returnOnExitEnvToValue(env) {
const envValue = env.RETURN_ON_EXIT;
if (envValue !== undefined)
return envValue === 'true';

return undefined;
}

if (process.argv[2] === 'wasi-child-preview1') {
// Test version set to preview1
const assert = require('assert');
Expand All @@ -23,6 +31,7 @@ if (process.argv[2] === 'wasi-child-preview1') {
'/sandbox': fixtures.path('wasi'),
'/tmp': tmpdir.path,
},
returnOnExit: returnOnExitEnvToValue(process.env),
});

// Validate the getImportObject helper
Expand Down Expand Up @@ -56,6 +65,9 @@ if (process.argv[2] === 'wasi-child-preview1') {
if (options.stdin !== undefined)
opts.input = options.stdin;

if (options.returnOnExit === false)
opts.env.RETURN_ON_EXIT = 'false';

const child = cp.spawnSync(process.execPath, [
...args,
__filename,
Expand All @@ -79,7 +91,9 @@ if (process.argv[2] === 'wasi-child-preview1') {
if (!common.isIBMi) {
runWASI({ test: 'clock_getres' });
}
runWASI({ test: 'exitcode', exitCode: 120 });
runWASI({ test: 'exitcode' });
runWASI({ test: 'exitcode', returnOnExit: true });
runWASI({ test: 'exitcode', exitCode: 120, returnOnExit: false });
runWASI({ test: 'fd_prestat_get_refresh' });
runWASI({ test: 'freopen', stdout: `hello from input2.txt${checkoutEOL}` });
runWASI({ test: 'ftruncate' });
Expand Down

0 comments on commit 5583c30

Please sign in to comment.