Skip to content

Commit

Permalink
vm: refactor value validation with internal/validators.js
Browse files Browse the repository at this point in the history
PR-URL: #31480
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
  • Loading branch information
lundibundi authored and MylesBorins committed Mar 10, 2020
1 parent 4589863 commit eaf6723
Showing 1 changed file with 40 additions and 86 deletions.
126 changes: 40 additions & 86 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'use strict';

const {
ArrayIsArray,
ArrayPrototypeForEach,
Symbol,
PromiseReject
Expand All @@ -47,7 +46,11 @@ const {
const {
validateInt32,
validateUint32,
validateString
validateString,
validateArray,
validateBoolean,
validateBuffer,
validateObject,
} = require('internal/validators');
const {
kVmBreakFirstLineSymbol,
Expand Down Expand Up @@ -147,30 +150,14 @@ class Script extends ContextifyScript {
}

function validateContext(contextifiedObject) {
if (typeof contextifiedObject !== 'object' || contextifiedObject === null) {
throw new ERR_INVALID_ARG_TYPE('contextifiedObject', 'Object',
contextifiedObject);
}
if (!_isContext(contextifiedObject)) {
if (!isContext(contextifiedObject)) {
throw new ERR_INVALID_ARG_TYPE('contextifiedObject', 'vm.Context',
contextifiedObject);
}
}

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

function validateObject(prop, propName) {
if (prop !== undefined && (typeof prop !== 'object' || prop === null))
throw new ERR_INVALID_ARG_TYPE(propName, 'Object', prop);
}

function getRunInContextArgs(options = {}) {
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');

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

if (typeof displayErrors !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE('options.displayErrors', 'boolean',
displayErrors);
}
if (typeof breakOnSigint !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE('options.breakOnSigint', 'boolean',
breakOnSigint);
}
validateBoolean(displayErrors, 'options.displayErrors');
validateBoolean(breakOnSigint, 'options.breakOnSigint');

return {
breakOnSigint,
Expand All @@ -201,30 +182,28 @@ function getRunInContextArgs(options = {}) {
}

function getContextOptions(options) {
if (options) {
if (!options)
return {};
const contextOptions = {
name: options.contextName,
origin: options.contextOrigin,
codeGeneration: undefined,
};
if (contextOptions.name !== undefined)
validateString(contextOptions.name, 'options.contextName');
if (contextOptions.origin !== undefined)
validateString(contextOptions.origin, 'options.contextOrigin');
if (options.contextCodeGeneration !== undefined) {
validateObject(options.contextCodeGeneration,
'options.contextCodeGeneration');
const contextOptions = {
name: options.contextName,
origin: options.contextOrigin,
codeGeneration: typeof options.contextCodeGeneration === 'object' ? {
strings: options.contextCodeGeneration.strings,
wasm: options.contextCodeGeneration.wasm,
} : undefined,
};
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');
validateBool(contextOptions.codeGeneration.wasm,
'options.contextCodeGeneration.wasm');
}
return contextOptions;
const { strings, wasm } = options.contextCodeGeneration;
if (strings !== undefined)
validateBoolean(strings, 'options.contextCodeGeneration.strings');
if (wasm !== undefined)
validateBoolean(wasm, 'options.contextCodeGeneration.wasm');
contextOptions.codeGeneration = { strings, wasm };
}
return {};
return contextOptions;
}

function isContext(object) {
Expand All @@ -240,9 +219,7 @@ function createContext(contextObject = {}, options = {}) {
return contextObject;
}

if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');

const {
name = `VM Context ${defaultContextNameIndex++}`,
Expand All @@ -253,14 +230,15 @@ function createContext(contextObject = {}, options = {}) {
validateString(name, 'options.name');
if (origin !== undefined)
validateString(origin, 'options.origin');
validateObject(codeGeneration, 'options.codeGeneration');
if (codeGeneration !== undefined)
validateObject(codeGeneration, 'options.codeGeneration');

let strings = true;
let wasm = true;
if (codeGeneration !== undefined) {
({ strings = true, wasm = true } = codeGeneration);
validateBool(strings, 'options.codeGeneration.strings');
validateBool(wasm, 'options.codeGeneration.wasm');
validateBoolean(strings, 'options.codeGeneration.strings');
validateBoolean(wasm, 'options.codeGeneration.wasm');
}

makeContext(contextObject, name, origin, strings, wasm);
Expand Down Expand Up @@ -322,9 +300,7 @@ function runInThisContext(code, options) {
function compileFunction(code, params, options = {}) {
validateString(code, 'code');
if (params !== undefined) {
if (!ArrayIsArray(params)) {
throw new ERR_INVALID_ARG_TYPE('params', 'Array', params);
}
validateArray(params, 'params');
ArrayPrototypeForEach(params,
(param, i) => validateString(param, `params[${i}]`));
}
Expand All @@ -342,20 +318,9 @@ function compileFunction(code, params, options = {}) {
validateString(filename, 'options.filename');
validateUint32(columnOffset, 'options.columnOffset');
validateUint32(lineOffset, 'options.lineOffset');
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
throw new ERR_INVALID_ARG_TYPE(
'options.cachedData',
['Buffer', 'TypedArray', 'DataView'],
cachedData
);
}
if (typeof produceCachedData !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE(
'options.produceCachedData',
'boolean',
produceCachedData
);
}
if (cachedData !== undefined)
validateBuffer(cachedData, 'options.cachedData');
validateBoolean(produceCachedData, 'options.produceCachedData');
if (parsingContext !== undefined) {
if (
typeof parsingContext !== 'object' ||
Expand All @@ -369,21 +334,10 @@ function compileFunction(code, params, options = {}) {
);
}
}
if (!ArrayIsArray(contextExtensions)) {
throw new ERR_INVALID_ARG_TYPE(
'options.contextExtensions',
'Array',
contextExtensions
);
}
validateArray(contextExtensions, 'options.contextExtensions');
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
if (typeof extension !== 'object') {
throw new ERR_INVALID_ARG_TYPE(
`options.contextExtensions[${i}]`,
'object',
extension
);
}
const name = `options.contextExtensions[${i}]`;
validateObject(extension, name, { nullable: true });
});

const result = _compileFunction(
Expand Down

0 comments on commit eaf6723

Please sign in to comment.