summaryrefslogtreecommitdiff
path: root/lib/internal/modules/esm/loader.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/modules/esm/loader.js')
-rw-r--r--lib/internal/modules/esm/loader.js29
1 files changed, 26 insertions, 3 deletions
diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js
index aff1211368..465775f56f 100644
--- a/lib/internal/modules/esm/loader.js
+++ b/lib/internal/modules/esm/loader.js
@@ -11,10 +11,12 @@ const { URL } = require('url');
const { validateString } = require('internal/validators');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');
+
const defaultResolve = require('internal/modules/esm/default_resolve');
const createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
-const translators = require('internal/modules/esm/translators');
+const { translators } = require('internal/modules/esm/translators');
+const { ModuleWrap } = internalBinding('module_wrap');
const FunctionBind = Function.call.bind(Function.prototype.bind);
@@ -32,6 +34,9 @@ class Loader {
// Registry of loaded modules, akin to `require.cache`
this.moduleMap = new ModuleMap();
+ // Map of already-loaded CJS modules to use
+ this.cjsCache = new Map();
+
// The resolver has the signature
// (specifier : string, parentURL : string, defaultResolve)
// -> Promise<{ url : string, format: string }>
@@ -48,6 +53,8 @@ class Loader {
// an object with the same keys as `exports`, whose values are get/set
// functions for the actual exported values.
this._dynamicInstantiate = undefined;
+ // The index for assigning unique URLs to anonymous module evaluation
+ this.evalIndex = 0;
}
async resolve(specifier, parentURL) {
@@ -95,9 +102,25 @@ class Loader {
return { url, format };
}
+ async eval(source, url = `eval:${++this.evalIndex}`) {
+ const evalInstance = async (url) => {
+ return {
+ module: new ModuleWrap(source, url),
+ reflect: undefined
+ };
+ };
+ const job = new ModuleJob(this, url, evalInstance, false);
+ this.moduleMap.set(url, job);
+ const { module, result } = await job.run();
+ return {
+ namespace: module.namespace(),
+ result
+ };
+ }
+
async import(specifier, parent) {
const job = await this.getModuleJob(specifier, parent);
- const module = await job.run();
+ const { module } = await job.run();
return module.namespace();
}
@@ -143,4 +166,4 @@ class Loader {
Object.setPrototypeOf(Loader.prototype, null);
-module.exports = Loader;
+exports.Loader = Loader;