vm: consolidate validation

PR-URL: https://github.com/nodejs/node/pull/18816
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Timothy O. Peters 2018-02-12 19:58:15 +01:00 committed by Ruben Bridgewater
parent ca79fc5373
commit 3cb3618973
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -79,22 +79,23 @@ Script.prototype.runInNewContext = function(sandbox, options) {
return this.runInContext(context, options); return this.runInContext(context, options);
}; };
function validateString(prop, propName) {
if (prop !== undefined && typeof prop !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', propName,
'string', prop);
}
function getContextOptions(options) { function getContextOptions(options) {
const contextOptions = options ? { if (options) {
name: options.contextName, const contextOptions = {
origin: options.contextOrigin name: options.contextName,
} : {}; origin: options.contextOrigin
if (contextOptions.name !== undefined && };
typeof contextOptions.name !== 'string') { validateString(contextOptions.name, 'options.contextName');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextName', validateString(contextOptions.origin, 'options.contextOrigin');
'string', contextOptions.name); return contextOptions;
} }
if (contextOptions.origin !== undefined && return {};
typeof contextOptions.origin !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin',
'string', contextOptions.origin);
}
return contextOptions;
} }
let defaultContextNameIndex = 1; let defaultContextNameIndex = 1;
@ -120,10 +121,7 @@ function createContext(sandbox, options) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.name', throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.name',
'string', options.name); 'string', options.name);
} }
if (options.origin !== undefined && typeof options.origin !== 'string') { validateString(options.origin, 'options.origin');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.origin',
'string', options.origin);
}
} else { } else {
options = { options = {
name: `VM Context ${defaultContextNameIndex++}` name: `VM Context ${defaultContextNameIndex++}`