Permalink
Browse files

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>
  • Loading branch information...
ZYSzys authored and MylesBorins committed Dec 16, 2018
1 parent c6388ed commit 7df59f824b2490512cd104d11f55bf1910d181af
Showing with 16 additions and 26 deletions.
  1. +16 −26 lib/vm.js
@@ -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);
@@ -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)) {
@@ -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);
@@ -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');
@@ -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;
@@ -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 {
@@ -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)) {

0 comments on commit 7df59f8

Please sign in to comment.