From 46a7e619005c10de01be2c439fe1ff6ca8df8054 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Fri, 18 Jun 2021 06:39:40 +0430 Subject: [PATCH] wasi: use missing validator The `wasi` lib module's `initialize()` method is missing a validator. PR-URL: https://github.com/nodejs/node/pull/39070 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Darshan Sen --- lib/internal/validators.js | 6 ++++++ lib/wasi.js | 17 ++++------------- test/wasi/test-wasi-initialize-validation.js | 3 ++- test/wasi/test-wasi-start-validation.js | 3 ++- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 6abe332fc5fdc0..9bca0aa747725e 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -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, @@ -247,6 +252,7 @@ module.exports = { validateSignalName, validateString, validateUint32, + validateUndefined, validateCallback, validateAbortSignal, }; diff --git a/lib/wasi.js b/lib/wasi.js index 63209ce716cb9a..43ecf94b4cb050 100644 --- a/lib/wasi.js +++ b/lib/wasi.js @@ -21,6 +21,7 @@ const { validateFunction, validateInt32, validateObject, + validateUndefined, } = require('internal/validators'); const { WASI: _WASI } = internalBinding('wasi'); const kExitCode = Symbol('kExitCode'); @@ -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(); @@ -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(); } } diff --git a/test/wasi/test-wasi-initialize-validation.js b/test/wasi/test-wasi-initialize-validation.js index 79b0bd8485a483..40dfd864d1874e 100644 --- a/test/wasi/test-wasi-initialize-validation.js +++ b/test/wasi/test-wasi-initialize-validation.js @@ -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', } ); } diff --git a/test/wasi/test-wasi-start-validation.js b/test/wasi/test-wasi-start-validation.js index 5c6a1ede5d4fd7..2059ff081e88dd 100644 --- a/test/wasi/test-wasi-start-validation.js +++ b/test/wasi/test-wasi-start-validation.js @@ -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', } ); }