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:
parent
ca79fc5373
commit
3cb3618973
34
lib/vm.js
34
lib/vm.js
@ -79,22 +79,23 @@ Script.prototype.runInNewContext = function(sandbox, 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) {
|
||||
const contextOptions = options ? {
|
||||
name: options.contextName,
|
||||
origin: options.contextOrigin
|
||||
} : {};
|
||||
if (contextOptions.name !== undefined &&
|
||||
typeof contextOptions.name !== 'string') {
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextName',
|
||||
'string', contextOptions.name);
|
||||
if (options) {
|
||||
const contextOptions = {
|
||||
name: options.contextName,
|
||||
origin: options.contextOrigin
|
||||
};
|
||||
validateString(contextOptions.name, 'options.contextName');
|
||||
validateString(contextOptions.origin, 'options.contextOrigin');
|
||||
return contextOptions;
|
||||
}
|
||||
if (contextOptions.origin !== undefined &&
|
||||
typeof contextOptions.origin !== 'string') {
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin',
|
||||
'string', contextOptions.origin);
|
||||
}
|
||||
return contextOptions;
|
||||
return {};
|
||||
}
|
||||
|
||||
let defaultContextNameIndex = 1;
|
||||
@ -120,10 +121,7 @@ function createContext(sandbox, options) {
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.name',
|
||||
'string', options.name);
|
||||
}
|
||||
if (options.origin !== undefined && typeof options.origin !== 'string') {
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.origin',
|
||||
'string', options.origin);
|
||||
}
|
||||
validateString(options.origin, 'options.origin');
|
||||
} else {
|
||||
options = {
|
||||
name: `VM Context ${defaultContextNameIndex++}`
|
||||
|
Loading…
x
Reference in New Issue
Block a user