diff options
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) { |