diff options
author | Gus Caplan <me@gus.host> | 2018-08-17 17:26:34 -0500 |
---|---|---|
committer | Gus Caplan <me@gus.host> | 2018-10-06 17:33:25 -0500 |
commit | 4c37df779cf944b5666fc72e2a27fbf2e745881f (patch) | |
tree | 80d135f6cbd7cd9545bee950c280659985c276b8 /lib/vm.js | |
parent | 124a8e21238f8452028614625fe491b3049f7244 (diff) | |
download | android-node-v8-4c37df779cf944b5666fc72e2a27fbf2e745881f.tar.gz android-node-v8-4c37df779cf944b5666fc72e2a27fbf2e745881f.tar.bz2 android-node-v8-4c37df779cf944b5666fc72e2a27fbf2e745881f.zip |
vm: add dynamic import support
PR-URL: https://github.com/nodejs/node/pull/22381
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Diffstat (limited to 'lib/vm.js')
-rw-r--r-- | lib/vm.js | 34 |
1 files changed, 30 insertions, 4 deletions
@@ -27,9 +27,12 @@ const { isContext: _isContext, compileFunction: _compileFunction } = internalBinding('contextify'); - -const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; -const { isUint8Array } = require('internal/util/types'); +const { callbackMap } = internalBinding('module_wrap'); +const { + ERR_INVALID_ARG_TYPE, + ERR_VM_MODULE_NOT_MODULE, +} = require('internal/errors').codes; +const { isModuleNamespaceObject, isUint8Array } = require('util').types; const { validateInt32, validateUint32 } = require('internal/validators'); const kParsingContext = Symbol('script parsing context'); @@ -52,7 +55,8 @@ class Script extends ContextifyScript { columnOffset = 0, cachedData, produceCachedData = false, - [kParsingContext]: parsingContext + importModuleDynamically, + [kParsingContext]: parsingContext, } = options; if (typeof filename !== 'string') { @@ -83,6 +87,28 @@ class Script extends ContextifyScript { } catch (e) { throw e; /* node-do-not-add-exception-line */ } + + if (importModuleDynamically !== undefined) { + if (typeof importModuleDynamically !== 'function') { + throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically', + 'function', + importModuleDynamically); + } + const { wrapMap, linkingStatusMap } = + require('internal/vm/source_text_module'); + callbackMap.set(this, { importModuleDynamically: async (...args) => { + const m = await importModuleDynamically(...args); + if (isModuleNamespaceObject(m)) { + return m; + } + if (!m || !wrapMap.has(m)) + throw new ERR_VM_MODULE_NOT_MODULE(); + const childLinkingStatus = linkingStatusMap.get(m); + if (childLinkingStatus === 'errored') + throw m.error; + return m.namespace; + } }); + } } runInThisContext(options) { |