Skip to content

Commit eaf6723

Browse files
lundibundiMylesBorins
authored andcommitted
vm: refactor value validation with internal/validators.js
PR-URL: #31480 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 4589863 commit eaf6723

File tree

1 file changed

+40
-86
lines changed

1 file changed

+40
-86
lines changed

lib/vm.js

+40-86
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
'use strict';
2323

2424
const {
25-
ArrayIsArray,
2625
ArrayPrototypeForEach,
2726
Symbol,
2827
PromiseReject
@@ -47,7 +46,11 @@ const {
4746
const {
4847
validateInt32,
4948
validateUint32,
50-
validateString
49+
validateString,
50+
validateArray,
51+
validateBoolean,
52+
validateBuffer,
53+
validateObject,
5154
} = require('internal/validators');
5255
const {
5356
kVmBreakFirstLineSymbol,
@@ -147,30 +150,14 @@ class Script extends ContextifyScript {
147150
}
148151

149152
function validateContext(contextifiedObject) {
150-
if (typeof contextifiedObject !== 'object' || contextifiedObject === null) {
151-
throw new ERR_INVALID_ARG_TYPE('contextifiedObject', 'Object',
152-
contextifiedObject);
153-
}
154-
if (!_isContext(contextifiedObject)) {
153+
if (!isContext(contextifiedObject)) {
155154
throw new ERR_INVALID_ARG_TYPE('contextifiedObject', 'vm.Context',
156155
contextifiedObject);
157156
}
158157
}
159158

160-
function validateBool(prop, propName) {
161-
if (prop !== undefined && typeof prop !== 'boolean')
162-
throw new ERR_INVALID_ARG_TYPE(propName, 'boolean', prop);
163-
}
164-
165-
function validateObject(prop, propName) {
166-
if (prop !== undefined && (typeof prop !== 'object' || prop === null))
167-
throw new ERR_INVALID_ARG_TYPE(propName, 'Object', prop);
168-
}
169-
170159
function getRunInContextArgs(options = {}) {
171-
if (typeof options !== 'object' || options === null) {
172-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
173-
}
160+
validateObject(options, 'options');
174161

175162
let timeout = options.timeout;
176163
if (timeout === undefined) {
@@ -185,14 +172,8 @@ function getRunInContextArgs(options = {}) {
185172
[kVmBreakFirstLineSymbol]: breakFirstLine = false,
186173
} = options;
187174

188-
if (typeof displayErrors !== 'boolean') {
189-
throw new ERR_INVALID_ARG_TYPE('options.displayErrors', 'boolean',
190-
displayErrors);
191-
}
192-
if (typeof breakOnSigint !== 'boolean') {
193-
throw new ERR_INVALID_ARG_TYPE('options.breakOnSigint', 'boolean',
194-
breakOnSigint);
195-
}
175+
validateBoolean(displayErrors, 'options.displayErrors');
176+
validateBoolean(breakOnSigint, 'options.breakOnSigint');
196177

197178
return {
198179
breakOnSigint,
@@ -201,30 +182,28 @@ function getRunInContextArgs(options = {}) {
201182
}
202183

203184
function getContextOptions(options) {
204-
if (options) {
185+
if (!options)
186+
return {};
187+
const contextOptions = {
188+
name: options.contextName,
189+
origin: options.contextOrigin,
190+
codeGeneration: undefined,
191+
};
192+
if (contextOptions.name !== undefined)
193+
validateString(contextOptions.name, 'options.contextName');
194+
if (contextOptions.origin !== undefined)
195+
validateString(contextOptions.origin, 'options.contextOrigin');
196+
if (options.contextCodeGeneration !== undefined) {
205197
validateObject(options.contextCodeGeneration,
206198
'options.contextCodeGeneration');
207-
const contextOptions = {
208-
name: options.contextName,
209-
origin: options.contextOrigin,
210-
codeGeneration: typeof options.contextCodeGeneration === 'object' ? {
211-
strings: options.contextCodeGeneration.strings,
212-
wasm: options.contextCodeGeneration.wasm,
213-
} : undefined,
214-
};
215-
if (contextOptions.name !== undefined)
216-
validateString(contextOptions.name, 'options.contextName');
217-
if (contextOptions.origin !== undefined)
218-
validateString(contextOptions.origin, 'options.contextOrigin');
219-
if (contextOptions.codeGeneration) {
220-
validateBool(contextOptions.codeGeneration.strings,
221-
'options.contextCodeGeneration.strings');
222-
validateBool(contextOptions.codeGeneration.wasm,
223-
'options.contextCodeGeneration.wasm');
224-
}
225-
return contextOptions;
199+
const { strings, wasm } = options.contextCodeGeneration;
200+
if (strings !== undefined)
201+
validateBoolean(strings, 'options.contextCodeGeneration.strings');
202+
if (wasm !== undefined)
203+
validateBoolean(wasm, 'options.contextCodeGeneration.wasm');
204+
contextOptions.codeGeneration = { strings, wasm };
226205
}
227-
return {};
206+
return contextOptions;
228207
}
229208

230209
function isContext(object) {
@@ -240,9 +219,7 @@ function createContext(contextObject = {}, options = {}) {
240219
return contextObject;
241220
}
242221

243-
if (typeof options !== 'object' || options === null) {
244-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
245-
}
222+
validateObject(options, 'options');
246223

247224
const {
248225
name = `VM Context ${defaultContextNameIndex++}`,
@@ -253,14 +230,15 @@ function createContext(contextObject = {}, options = {}) {
253230
validateString(name, 'options.name');
254231
if (origin !== undefined)
255232
validateString(origin, 'options.origin');
256-
validateObject(codeGeneration, 'options.codeGeneration');
233+
if (codeGeneration !== undefined)
234+
validateObject(codeGeneration, 'options.codeGeneration');
257235

258236
let strings = true;
259237
let wasm = true;
260238
if (codeGeneration !== undefined) {
261239
({ strings = true, wasm = true } = codeGeneration);
262-
validateBool(strings, 'options.codeGeneration.strings');
263-
validateBool(wasm, 'options.codeGeneration.wasm');
240+
validateBoolean(strings, 'options.codeGeneration.strings');
241+
validateBoolean(wasm, 'options.codeGeneration.wasm');
264242
}
265243

266244
makeContext(contextObject, name, origin, strings, wasm);
@@ -322,9 +300,7 @@ function runInThisContext(code, options) {
322300
function compileFunction(code, params, options = {}) {
323301
validateString(code, 'code');
324302
if (params !== undefined) {
325-
if (!ArrayIsArray(params)) {
326-
throw new ERR_INVALID_ARG_TYPE('params', 'Array', params);
327-
}
303+
validateArray(params, 'params');
328304
ArrayPrototypeForEach(params,
329305
(param, i) => validateString(param, `params[${i}]`));
330306
}
@@ -342,20 +318,9 @@ function compileFunction(code, params, options = {}) {
342318
validateString(filename, 'options.filename');
343319
validateUint32(columnOffset, 'options.columnOffset');
344320
validateUint32(lineOffset, 'options.lineOffset');
345-
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
346-
throw new ERR_INVALID_ARG_TYPE(
347-
'options.cachedData',
348-
['Buffer', 'TypedArray', 'DataView'],
349-
cachedData
350-
);
351-
}
352-
if (typeof produceCachedData !== 'boolean') {
353-
throw new ERR_INVALID_ARG_TYPE(
354-
'options.produceCachedData',
355-
'boolean',
356-
produceCachedData
357-
);
358-
}
321+
if (cachedData !== undefined)
322+
validateBuffer(cachedData, 'options.cachedData');
323+
validateBoolean(produceCachedData, 'options.produceCachedData');
359324
if (parsingContext !== undefined) {
360325
if (
361326
typeof parsingContext !== 'object' ||
@@ -369,21 +334,10 @@ function compileFunction(code, params, options = {}) {
369334
);
370335
}
371336
}
372-
if (!ArrayIsArray(contextExtensions)) {
373-
throw new ERR_INVALID_ARG_TYPE(
374-
'options.contextExtensions',
375-
'Array',
376-
contextExtensions
377-
);
378-
}
337+
validateArray(contextExtensions, 'options.contextExtensions');
379338
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
380-
if (typeof extension !== 'object') {
381-
throw new ERR_INVALID_ARG_TYPE(
382-
`options.contextExtensions[${i}]`,
383-
'object',
384-
extension
385-
);
386-
}
339+
const name = `options.contextExtensions[${i}]`;
340+
validateObject(extension, name, { nullable: true });
387341
});
388342

389343
const result = _compileFunction(

0 commit comments

Comments
 (0)