Skip to content

Commit 7df59f8

Browse files
ZYSzysMylesBorins
authored andcommitted
vm: reuse validateString of internal/validators
PR-URL: #25074 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
1 parent c6388ed commit 7df59f8

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

lib/vm.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ const {
3333
ERR_VM_MODULE_NOT_MODULE,
3434
} = require('internal/errors').codes;
3535
const { isModuleNamespaceObject, isArrayBufferView } = require('util').types;
36-
const { validateInt32, validateUint32 } = require('internal/validators');
36+
const {
37+
validateInt32,
38+
validateUint32,
39+
validateString
40+
} = require('internal/validators');
3741
const kParsingContext = Symbol('script parsing context');
3842

3943
const ArrayForEach = Function.call.bind(Array.prototype.forEach);
@@ -58,9 +62,7 @@ class Script extends ContextifyScript {
5862
[kParsingContext]: parsingContext,
5963
} = options;
6064

61-
if (typeof filename !== 'string') {
62-
throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename);
63-
}
65+
validateString(filename, 'options.filename');
6466
validateInt32(lineOffset, 'options.lineOffset');
6567
validateInt32(columnOffset, 'options.columnOffset');
6668
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
@@ -149,11 +151,6 @@ function validateContext(sandbox) {
149151
}
150152
}
151153

152-
function validateString(prop, propName) {
153-
if (prop !== undefined && typeof prop !== 'string')
154-
throw new ERR_INVALID_ARG_TYPE(propName, 'string', prop);
155-
}
156-
157154
function validateBool(prop, propName) {
158155
if (prop !== undefined && typeof prop !== 'boolean')
159156
throw new ERR_INVALID_ARG_TYPE(propName, 'boolean', prop);
@@ -208,8 +205,10 @@ function getContextOptions(options) {
208205
wasm: options.contextCodeGeneration.wasm,
209206
} : undefined,
210207
};
211-
validateString(contextOptions.name, 'options.contextName');
212-
validateString(contextOptions.origin, 'options.contextOrigin');
208+
if (contextOptions.name !== undefined)
209+
validateString(contextOptions.name, 'options.contextName');
210+
if (contextOptions.origin !== undefined)
211+
validateString(contextOptions.origin, 'options.contextOrigin');
213212
if (contextOptions.codeGeneration) {
214213
validateBool(contextOptions.codeGeneration.strings,
215214
'options.contextCodeGeneration.strings');
@@ -244,10 +243,9 @@ function createContext(sandbox = {}, options = {}) {
244243
codeGeneration
245244
} = options;
246245

247-
if (typeof name !== 'string') {
248-
throw new ERR_INVALID_ARG_TYPE('options.name', 'string', options.name);
249-
}
250-
validateString(origin, 'options.origin');
246+
validateString(name, 'options.name');
247+
if (origin !== undefined)
248+
validateString(origin, 'options.origin');
251249
validateObject(codeGeneration, 'options.codeGeneration');
252250

253251
let strings = true;
@@ -319,18 +317,12 @@ function runInThisContext(code, options) {
319317
}
320318

321319
function compileFunction(code, params, options = {}) {
322-
if (typeof code !== 'string') {
323-
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
324-
}
320+
validateString(code, 'code');
325321
if (params !== undefined) {
326322
if (!ArrayIsArray(params)) {
327323
throw new ERR_INVALID_ARG_TYPE('params', 'Array', params);
328324
}
329-
ArrayForEach(params, (param, i) => {
330-
if (typeof param !== 'string') {
331-
throw new ERR_INVALID_ARG_TYPE(`params[${i}]`, 'string', param);
332-
}
333-
});
325+
ArrayForEach(params, (param, i) => validateString(param, `params[${i}]`));
334326
}
335327

336328
const {
@@ -343,9 +335,7 @@ function compileFunction(code, params, options = {}) {
343335
contextExtensions = [],
344336
} = options;
345337

346-
if (typeof filename !== 'string') {
347-
throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename);
348-
}
338+
validateString(filename, 'options.filename');
349339
validateUint32(columnOffset, 'options.columnOffset');
350340
validateUint32(lineOffset, 'options.lineOffset');
351341
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {

0 commit comments

Comments
 (0)