Skip to content

Commit

Permalink
vm: reuse validateString of internal/validators
Browse files Browse the repository at this point in the history
PR-URL: #25074
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
  • Loading branch information
ZYSzys authored and MylesBorins committed Dec 26, 2018
1 parent c6388ed commit 7df59f8
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const {
ERR_VM_MODULE_NOT_MODULE,
} = require('internal/errors').codes;
const { isModuleNamespaceObject, isArrayBufferView } = require('util').types;
const { validateInt32, validateUint32 } = require('internal/validators');
const {
validateInt32,
validateUint32,
validateString
} = require('internal/validators');
const kParsingContext = Symbol('script parsing context');

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

if (typeof filename !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename);
}
validateString(filename, 'options.filename');
validateInt32(lineOffset, 'options.lineOffset');
validateInt32(columnOffset, 'options.columnOffset');
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
Expand Down Expand Up @@ -149,11 +151,6 @@ function validateContext(sandbox) {
}
}

function validateString(prop, propName) {
if (prop !== undefined && typeof prop !== 'string')
throw new ERR_INVALID_ARG_TYPE(propName, 'string', prop);
}

function validateBool(prop, propName) {
if (prop !== undefined && typeof prop !== 'boolean')
throw new ERR_INVALID_ARG_TYPE(propName, 'boolean', prop);
Expand Down Expand Up @@ -208,8 +205,10 @@ function getContextOptions(options) {
wasm: options.contextCodeGeneration.wasm,
} : undefined,
};
validateString(contextOptions.name, 'options.contextName');
validateString(contextOptions.origin, 'options.contextOrigin');
if (contextOptions.name !== undefined)
validateString(contextOptions.name, 'options.contextName');
if (contextOptions.origin !== undefined)
validateString(contextOptions.origin, 'options.contextOrigin');
if (contextOptions.codeGeneration) {
validateBool(contextOptions.codeGeneration.strings,
'options.contextCodeGeneration.strings');
Expand Down Expand Up @@ -244,10 +243,9 @@ function createContext(sandbox = {}, options = {}) {
codeGeneration
} = options;

if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.name', 'string', options.name);
}
validateString(origin, 'options.origin');
validateString(name, 'options.name');
if (origin !== undefined)
validateString(origin, 'options.origin');
validateObject(codeGeneration, 'options.codeGeneration');

let strings = true;
Expand Down Expand Up @@ -319,18 +317,12 @@ function runInThisContext(code, options) {
}

function compileFunction(code, params, options = {}) {
if (typeof code !== 'string') {
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
}
validateString(code, 'code');
if (params !== undefined) {
if (!ArrayIsArray(params)) {
throw new ERR_INVALID_ARG_TYPE('params', 'Array', params);
}
ArrayForEach(params, (param, i) => {
if (typeof param !== 'string') {
throw new ERR_INVALID_ARG_TYPE(`params[${i}]`, 'string', param);
}
});
ArrayForEach(params, (param, i) => validateString(param, `params[${i}]`));
}

const {
Expand All @@ -343,9 +335,7 @@ function compileFunction(code, params, options = {}) {
contextExtensions = [],
} = options;

if (typeof filename !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename);
}
validateString(filename, 'options.filename');
validateUint32(columnOffset, 'options.columnOffset');
validateUint32(lineOffset, 'options.lineOffset');
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
Expand Down

0 comments on commit 7df59f8

Please sign in to comment.