Skip to content

Commit

Permalink
wasi: use missing validator
Browse files Browse the repository at this point in the history
The `wasi` lib module's `initialize()` method is missing a validator.

PR-URL: #39070
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
VoltrexKeyva authored and aduh95 committed Jun 26, 2021
1 parent ffda9a8 commit 46a7e61
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
6 changes: 6 additions & 0 deletions lib/internal/validators.js
Expand Up @@ -229,6 +229,11 @@ const validateFunction = hideStackFrames((value, name) => {
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
});

const validateUndefined = hideStackFrames((value, name) => {
if (value !== undefined)
throw new ERR_INVALID_ARG_TYPE(name, 'undefined', value);
});

module.exports = {
isInt32,
isUint32,
Expand All @@ -247,6 +252,7 @@ module.exports = {
validateSignalName,
validateString,
validateUint32,
validateUndefined,
validateCallback,
validateAbortSignal,
};
17 changes: 4 additions & 13 deletions lib/wasi.js
Expand Up @@ -21,6 +21,7 @@ const {
validateFunction,
validateInt32,
validateObject,
validateUndefined,
} = require('internal/validators');
const { WASI: _WASI } = internalBinding('wasi');
const kExitCode = Symbol('kExitCode');
Expand Down Expand Up @@ -120,10 +121,7 @@ class WASI {
const { _start, _initialize } = this[kInstance].exports;

validateFunction(_start, 'instance.exports._start');
if (_initialize !== undefined) {
throw new ERR_INVALID_ARG_TYPE(
'instance.exports._initialize', 'undefined', _initialize);
}
validateUndefined(_initialize, 'instance.exports._initialize');

try {
_start();
Expand All @@ -147,16 +145,9 @@ class WASI {

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

if (typeof _initialize !== 'function' && _initialize !== undefined) {
throw new ERR_INVALID_ARG_TYPE(
'instance.exports._initialize', 'function', _initialize);
}
if (_start !== undefined) {
throw new ERR_INVALID_ARG_TYPE(
'instance.exports._start', 'undefined', _initialize);
}

validateUndefined(_start, 'instance.exports._start');
if (_initialize !== undefined) {
validateFunction(_initialize, 'instance.exports._initialize');
_initialize();
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/wasi/test-wasi-initialize-validation.js
Expand Up @@ -78,7 +78,8 @@ const bufferSource = fixtures.readSync('simple.wasm');
() => { wasi.initialize(instance); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: /"instance\.exports\._start" property must be undefined/
message: 'The "instance.exports._start" property must be' +
' undefined. Received function _start',
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion test/wasi/test-wasi-start-validation.js
Expand Up @@ -78,7 +78,8 @@ const bufferSource = fixtures.readSync('simple.wasm');
() => { wasi.start(instance); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: /"instance\.exports\._initialize" property must be undefined/
message: 'The "instance.exports._initialize" property must be' +
' undefined. Received function _initialize',
}
);
}
Expand Down

0 comments on commit 46a7e61

Please sign in to comment.