diff options
author | Michaël Zasso <targos@protonmail.com> | 2018-04-04 22:11:11 +0200 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2018-04-07 15:10:27 +0200 |
commit | 77b52fd58f7398a81999c81afd21fe2e156c0766 (patch) | |
tree | 8b8998b9dfbd89661e82aa044464c32cb56b07cc /lib/internal/vm/module.js | |
parent | 0ac6ced2e9e09fdfe4b5c9aec8fb9a3f570f63e7 (diff) | |
download | android-node-v8-77b52fd58f7398a81999c81afd21fe2e156c0766.tar.gz android-node-v8-77b52fd58f7398a81999c81afd21fe2e156c0766.tar.bz2 android-node-v8-77b52fd58f7398a81999c81afd21fe2e156c0766.zip |
module: move options checks from C++ to JS
PR-URL: https://github.com/nodejs/node/pull/19822
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/vm/module.js')
-rw-r--r-- | lib/internal/vm/module.js | 80 |
1 files changed, 54 insertions, 26 deletions
diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index a64ccfe3b2..7284c8bd61 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -3,9 +3,10 @@ const { internalBinding } = require('internal/bootstrap/loaders'); const { emitExperimentalWarning } = require('internal/util'); const { URL } = require('internal/url'); -const { kParsingContext, isContext } = process.binding('contextify'); +const { isContext } = process.binding('contextify'); const { ERR_INVALID_ARG_TYPE, + ERR_OUT_OF_RANGE, ERR_VM_MODULE_ALREADY_LINKED, ERR_VM_MODULE_DIFFERENT_CONTEXT, ERR_VM_MODULE_LINKING_ERRORED, @@ -55,23 +56,26 @@ class Module { if (typeof src !== 'string') throw new ERR_INVALID_ARG_TYPE('src', 'string', src); if (typeof options !== 'object' || options === null) - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); - let context; - if (options.context !== undefined) { - if (typeof options.context !== 'object' || options.context === null) { - throw new ERR_INVALID_ARG_TYPE('options.context', 'object', - options.context); + const { + context, + lineOffset = 0, + columnOffset = 0, + initializeImportMeta + } = options; + + if (context !== undefined) { + if (typeof context !== 'object' || context === null) { + throw new ERR_INVALID_ARG_TYPE('options.context', 'Object', context); } - if (isContext(options.context)) { - context = options.context; - } else { - throw new ERR_INVALID_ARG_TYPE('options.context', - 'vm.Context', options.context); + if (!isContext(context)) { + throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context', + context); } } - let url = options.url; + let { url } = options; if (url !== undefined) { if (typeof url !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url); @@ -88,22 +92,19 @@ class Module { perContextModuleId.set(context, 1); } - if (options.initializeImportMeta !== undefined) { - if (typeof options.initializeImportMeta === 'function') { - initImportMetaMap.set(this, options.initializeImportMeta); + validateInteger(lineOffset, 'options.lineOffset'); + validateInteger(columnOffset, 'options.columnOffset'); + + if (initializeImportMeta !== undefined) { + if (typeof initializeImportMeta === 'function') { + initImportMetaMap.set(this, initializeImportMeta); } else { throw new ERR_INVALID_ARG_TYPE( - 'options.initializeImportMeta', 'function', - options.initializeImportMeta); + 'options.initializeImportMeta', 'function', initializeImportMeta); } } - const wrap = new ModuleWrap(src, url, { - [kParsingContext]: context, - lineOffset: options.lineOffset, - columnOffset: options.columnOffset - }); - + const wrap = new ModuleWrap(src, url, context, lineOffset, columnOffset); wrapMap.set(this, wrap); linkingStatusMap.set(this, 'unlinked'); wrapToModuleMap.set(wrap, this); @@ -194,7 +195,25 @@ class Module { wrap.instantiate(); } - async evaluate(options) { + async evaluate(options = {}) { + if (typeof options !== 'object' || options === null) { + throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); + } + + let timeout = options.timeout; + if (timeout === undefined) { + timeout = -1; + } else if (!Number.isInteger(timeout) || timeout <= 0) { + throw new ERR_INVALID_ARG_TYPE('options.timeout', 'a positive integer', + timeout); + } + + const { breakOnSigint = false } = options; + if (typeof breakOnSigint !== 'boolean') { + throw new ERR_INVALID_ARG_TYPE('options.breakOnSigint', 'boolean', + breakOnSigint); + } + const wrap = wrapMap.get(this); const status = wrap.getStatus(); if (status !== kInstantiated && @@ -204,7 +223,7 @@ class Module { 'must be one of instantiated, evaluated, and errored' ); } - const result = wrap.evaluate(options); + const result = wrap.evaluate(timeout, breakOnSigint); return { result, __proto__: null }; } @@ -224,6 +243,15 @@ class Module { } } +function validateInteger(prop, propName) { + if (!Number.isInteger(prop)) { + throw new ERR_INVALID_ARG_TYPE(propName, 'integer', prop); + } + if ((prop >> 0) !== prop) { + throw new ERR_OUT_OF_RANGE(propName, '32-bit integer', prop); + } +} + module.exports = { Module, initImportMetaMap, |