diff options
author | Timothy O. Peters <timotewpeters@gmail.com> | 2018-02-12 19:58:15 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-02-22 12:08:01 +0100 |
commit | 3cb3618973bcf7817b55eb4fd96cc5fe7284cbd9 (patch) | |
tree | 25adf8168600e84a521b8c44c11555a96bc9cf7f /lib/vm.js | |
parent | ca79fc5373f2fde4a3a3cfd86c34c3848df37a1a (diff) | |
download | android-node-v8-3cb3618973bcf7817b55eb4fd96cc5fe7284cbd9.tar.gz android-node-v8-3cb3618973bcf7817b55eb4fd96cc5fe7284cbd9.tar.bz2 android-node-v8-3cb3618973bcf7817b55eb4fd96cc5fe7284cbd9.zip |
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>
Diffstat (limited to 'lib/vm.js')
-rw-r--r-- | lib/vm.js | 34 |
1 files changed, 16 insertions, 18 deletions
@@ -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 (contextOptions.origin !== undefined && - typeof contextOptions.origin !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.contextOrigin', - 'string', contextOptions.origin); + if (options) { + const contextOptions = { + name: options.contextName, + origin: options.contextOrigin + }; + validateString(contextOptions.name, 'options.contextName'); + validateString(contextOptions.origin, 'options.contextOrigin'); + return contextOptions; } - 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++}` |