Skip to content

Commit fe1c81f

Browse files
VoltrexKeyvatargos
authored andcommitted
wasi: use missing validator
The `wasi` lib module's `initialize()` method is missing a validator. PR-URL: #39070 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 21e8720 commit fe1c81f

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

Diff for: lib/internal/validators.js

+6
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ const validateFunction = hideStackFrames((value, name) => {
229229
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
230230
});
231231

232+
const validateUndefined = hideStackFrames((value, name) => {
233+
if (value !== undefined)
234+
throw new ERR_INVALID_ARG_TYPE(name, 'undefined', value);
235+
});
236+
232237
module.exports = {
233238
isInt32,
234239
isUint32,
@@ -247,6 +252,7 @@ module.exports = {
247252
validateSignalName,
248253
validateString,
249254
validateUint32,
255+
validateUndefined,
250256
validateCallback,
251257
validateAbortSignal,
252258
};

Diff for: lib/wasi.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {
2121
validateFunction,
2222
validateInt32,
2323
validateObject,
24+
validateUndefined,
2425
} = require('internal/validators');
2526
const { WASI: _WASI } = internalBinding('wasi');
2627
const kExitCode = Symbol('kExitCode');
@@ -120,10 +121,7 @@ class WASI {
120121
const { _start, _initialize } = this[kInstance].exports;
121122

122123
validateFunction(_start, 'instance.exports._start');
123-
if (_initialize !== undefined) {
124-
throw new ERR_INVALID_ARG_TYPE(
125-
'instance.exports._initialize', 'undefined', _initialize);
126-
}
124+
validateUndefined(_initialize, 'instance.exports._initialize');
127125

128126
try {
129127
_start();
@@ -147,16 +145,9 @@ class WASI {
147145

148146
const { _start, _initialize } = this[kInstance].exports;
149147

150-
if (typeof _initialize !== 'function' && _initialize !== undefined) {
151-
throw new ERR_INVALID_ARG_TYPE(
152-
'instance.exports._initialize', 'function', _initialize);
153-
}
154-
if (_start !== undefined) {
155-
throw new ERR_INVALID_ARG_TYPE(
156-
'instance.exports._start', 'undefined', _initialize);
157-
}
158-
148+
validateUndefined(_start, 'instance.exports._start');
159149
if (_initialize !== undefined) {
150+
validateFunction(_initialize, 'instance.exports._initialize');
160151
_initialize();
161152
}
162153
}

Diff for: test/wasi/test-wasi-initialize-validation.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ const bufferSource = fixtures.readSync('simple.wasm');
7878
() => { wasi.initialize(instance); },
7979
{
8080
code: 'ERR_INVALID_ARG_TYPE',
81-
message: /"instance\.exports\._start" property must be undefined/
81+
message: 'The "instance.exports._start" property must be' +
82+
' undefined. Received function _start',
8283
}
8384
);
8485
}

Diff for: test/wasi/test-wasi-start-validation.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ const bufferSource = fixtures.readSync('simple.wasm');
7878
() => { wasi.start(instance); },
7979
{
8080
code: 'ERR_INVALID_ARG_TYPE',
81-
message: /"instance\.exports\._initialize" property must be undefined/
81+
message: 'The "instance.exports._initialize" property must be' +
82+
' undefined. Received function _initialize',
8283
}
8384
);
8485
}

0 commit comments

Comments
 (0)