Skip to content

Commit 7c723af

Browse files
lundibunditargos
authored andcommitted
wasi: clean up options validation
PR-URL: #31797 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent b7d264e commit 7c723af

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

lib/wasi.js

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
/* global WebAssembly */
33
const {
4-
ArrayIsArray,
54
ArrayPrototypeMap,
65
ArrayPrototypePush,
76
FunctionPrototypeBind,
@@ -13,6 +12,11 @@ const {
1312
ERR_WASI_ALREADY_STARTED
1413
} = require('internal/errors').codes;
1514
const { emitExperimentalWarning } = require('internal/util');
15+
const {
16+
validateArray,
17+
validateBoolean,
18+
validateObject,
19+
} = require('internal/validators');
1620
const { WASI: _WASI } = internalBinding('wasi');
1721
const kExitCode = Symbol('exitCode');
1822
const kSetMemory = Symbol('setMemory');
@@ -23,52 +27,39 @@ emitExperimentalWarning('WASI');
2327

2428
class WASI {
2529
constructor(options = {}) {
26-
if (options === null || typeof options !== 'object')
27-
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
28-
29-
const { env, preopens, returnOnExit = false } = options;
30-
let { args = [] } = options;
30+
validateObject(options, 'options');
3131

32-
if (ArrayIsArray(args))
33-
args = ArrayPrototypeMap(args, (arg) => { return String(arg); });
34-
else
35-
throw new ERR_INVALID_ARG_TYPE('options.args', 'Array', args);
32+
if (options.args !== undefined)
33+
validateArray(options.args, 'options.args');
34+
const args = ArrayPrototypeMap(options.args || [], String);
3635

37-
const envPairs = [];
38-
39-
if (env !== null && typeof env === 'object') {
40-
for (const key in env) {
41-
const value = env[key];
36+
const env = [];
37+
if (options.env !== undefined) {
38+
validateObject(options.env, 'options.env');
39+
for (const [key, value] of ObjectEntries(options.env)) {
4240
if (value !== undefined)
43-
ArrayPrototypePush(envPairs, `${key}=${value}`);
41+
ArrayPrototypePush(env, `${key}=${value}`);
4442
}
45-
} else if (env !== undefined) {
46-
throw new ERR_INVALID_ARG_TYPE('options.env', 'Object', env);
4743
}
4844

49-
const preopenArray = [];
50-
51-
if (typeof preopens === 'object' && preopens !== null) {
52-
for (const [key, value] of ObjectEntries(preopens)) {
53-
ArrayPrototypePush(preopenArray, String(key), String(value));
45+
const preopens = [];
46+
if (options.preopens !== undefined) {
47+
validateObject(options.preopens, 'options.preopens');
48+
for (const [key, value] of ObjectEntries(options.preopens)) {
49+
ArrayPrototypePush(preopens, String(key), String(value));
5450
}
55-
} else if (preopens !== undefined) {
56-
throw new ERR_INVALID_ARG_TYPE('options.preopens', 'Object', preopens);
57-
}
58-
59-
if (typeof returnOnExit !== 'boolean') {
60-
throw new ERR_INVALID_ARG_TYPE(
61-
'options.returnOnExit', 'boolean', returnOnExit);
6251
}
6352

64-
const wrap = new _WASI(args, envPairs, preopenArray);
53+
const wrap = new _WASI(args, env, preopens);
6554

6655
for (const prop in wrap) {
6756
wrap[prop] = FunctionPrototypeBind(wrap[prop], wrap);
6857
}
6958

70-
if (returnOnExit) {
71-
wrap.proc_exit = FunctionPrototypeBind(wasiReturnOnProcExit, this);
59+
if (options.returnOnExit !== undefined) {
60+
validateBoolean(options.returnOnExit, 'options.returnOnExit');
61+
if (options.returnOnExit)
62+
wrap.proc_exit = FunctionPrototypeBind(wasiReturnOnProcExit, this);
7263
}
7364

7465
this[kSetMemory] = wrap._setMemory;
@@ -86,8 +77,7 @@ class WASI {
8677

8778
const exports = instance.exports;
8879

89-
if (exports === null || typeof exports !== 'object')
90-
throw new ERR_INVALID_ARG_TYPE('instance.exports', 'Object', exports);
80+
validateObject(exports, 'instance.exports');
9181

9282
const { memory } = exports;
9383

0 commit comments

Comments
 (0)