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

‎lib/internal/validators.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ const validateFunction = hideStackFrames((value, name) =>{
229229
thrownewERR_INVALID_ARG_TYPE(name,'Function',value);
230230
});
231231

232+
constvalidateUndefined=hideStackFrames((value,name)=>{
233+
if(value!==undefined)
234+
thrownewERR_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
};

‎lib/wasi.js‎

Lines changed: 4 additions & 13 deletions
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
constkExitCode=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-
thrownewERR_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-
thrownewERR_INVALID_ARG_TYPE(
152-
'instance.exports._initialize','function',_initialize);
153-
}
154-
if(_start!==undefined){
155-
thrownewERR_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
}

‎test/wasi/test-wasi-initialize-validation.js‎

Lines changed: 2 additions & 1 deletion
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"propertymustbeundefined/
81+
message: 'The "instance.exports._start" property must be'+
82+
' undefined. Received function _start',
8283
}
8384
);
8485
}

‎test/wasi/test-wasi-start-validation.js‎

Lines changed: 2 additions & 1 deletion
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"propertymustbeundefined/
81+
message: 'The "instance.exports._initialize" property must be'+
82+
' undefined. Received function _initialize',
8283
}
8384
);
8485
}

0 commit comments

Comments
(0)