| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,7 +88,7 @@ changes: | |||
| 88 | 88 | This option is part of the experimental modules API, and should not be | |
| 89 | 89 | considered stable. | |
| 90 | 90 | * `specifier` {string} specifier passed to `import()` | |
| 91 | - * `module` {vm.Module} | ||
| 91 | + * `script` {vm.Script} | ||
| 92 | 92 | * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is | |
| 93 | 93 | recommended in order to take advantage of error tracking, and to avoid | |
| 94 | 94 | issues with namespaces that contain `then` function exports. | |
@@ -773,6 +773,10 @@ const vm = require('vm'); | |||
| 773 | 773 | ## `vm.compileFunction(code[, params[, options]])` | |
| 774 | 774 | <!-- YAML | |
| 775 | 775 | added: v10.10.0 | |
| 776 | + changes: | ||
| 777 | + - version: REPLACEME | ||
| 778 | + pr-url: https://github.com/nodejs/node/pull/32985 | ||
| 779 | + description: The `importModuleDynamically` option is now supported. | ||
| 776 | 780 | --> | |
| 777 | 781 | ||
| 778 | 782 | * `code` {string} The body of the function to compile. | |
@@ -795,6 +799,16 @@ added: v10.10.0 | |||
| 795 | 799 | * `contextExtensions` {Object[]} An array containing a collection of context | |
| 796 | 800 | extensions (objects wrapping the current scope) to be applied while | |
| 797 | 801 | compiling. **Default:** `[]`. | |
| 802 | + * `importModuleDynamically` {Function} Called during evaluation of this module | ||
| 803 | + when `import()` is called. If this option is not specified, calls to | ||
| 804 | + `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. | ||
| 805 | + This option is part of the experimental modules API, and should not be | ||
| 806 | + considered stable. | ||
| 807 | + * `specifier` {string} specifier passed to `import()` | ||
| 808 | + * `function` {Function} | ||
| 809 | + * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is | ||
| 810 | + recommended in order to take advantage of error tracking, and to avoid | ||
| 811 | + issues with namespaces that contain `then` function exports. | ||
| 798 | 812 | * Returns: {Function} | |
| 799 | 813 | ||
| 800 | 814 | Compiles the given code into the provided context (if no context is | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,7 +77,6 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); | |||
| 77 | 77 | const manifest = getOptionValue('--experimental-policy') ? | |
| 78 | 78 | require('internal/process/policy').manifest : | |
| 79 | 79 | null; | |
| 80 | - const { compileFunction } = internalBinding('contextify'); | ||
| 81 | 80 | ||
| 82 | 81 | // Whether any user-provided CJS modules had been loaded (executed). | |
| 83 | 82 | // Used for internal assertions. | |
@@ -1100,40 +1099,25 @@ function wrapSafe(filename, content, cjsModuleInstance) { | |||
| 1100 | 1099 | }, | |
| 1101 | 1100 | }); | |
| 1102 | 1101 | } | |
| 1103 | - let compiled; | ||
| 1104 | 1102 | try { | |
| 1105 | - compiled = compileFunction( | ||
| 1106 | - content, | ||
| 1103 | + return vm.compileFunction(content, [ | ||
| 1104 | + 'exports', | ||
| 1105 | + 'require', | ||
| 1106 | + 'module', | ||
| 1107 | + '__filename', | ||
| 1108 | + '__dirname', | ||
| 1109 | + ], { | ||
| 1107 | 1110 | filename, | |
| 1108 | - 0, | ||
| 1109 | - 0, | ||
| 1110 | - undefined, | ||
| 1111 | - false, | ||
| 1112 | - undefined, | ||
| 1113 | - [], | ||
| 1114 | - [ | ||
| 1115 | - 'exports', | ||
| 1116 | - 'require', | ||
| 1117 | - 'module', | ||
| 1118 | - '__filename', | ||
| 1119 | - '__dirname', | ||
| 1120 | - ] | ||
| 1121 | - ); | ||
| 1111 | + importModuleDynamically(specifier) { | ||
| 1112 | + const loader = asyncESM.ESMLoader; | ||
| 1113 | + return loader.import(specifier, normalizeReferrerURL(filename)); | ||
| 1114 | + }, | ||
| 1115 | + }); | ||
| 1122 | 1116 | } catch (err) { | |
| 1123 | 1117 | if (process.mainModule === cjsModuleInstance) | |
| 1124 | 1118 | enrichCJSError(err); | |
| 1125 | 1119 | throw err; | |
| 1126 | 1120 | } | |
| 1127 | - | ||
| 1128 | - const { callbackMap } = internalBinding('module_wrap'); | ||
| 1129 | - callbackMap.set(compiled.cacheKey, { | ||
| 1130 | - importModuleDynamically: async (specifier) => { | ||
| 1131 | - const loader = asyncESM.ESMLoader; | ||
| 1132 | - return loader.import(specifier, normalizeReferrerURL(filename)); | ||
| 1133 | - } | ||
| 1134 | - }); | ||
| 1135 | - | ||
| 1136 | - return compiled.function; | ||
| 1137 | 1121 | } | |
| 1138 | 1122 | ||
| 1139 | 1123 | // Run the file contents in the correct scope or sandbox. Expose | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -313,6 +313,7 @@ function compileFunction(code, params, options = {}) { | |||
| 313 | 313 | produceCachedData = false, | |
| 314 | 314 | parsingContext = undefined, | |
| 315 | 315 | contextExtensions = [], | |
| 316 | + importModuleDynamically, | ||
| 316 | 317 | } = options; | |
| 317 | 318 | ||
| 318 | 319 | validateString(filename, 'options.filename'); | |
@@ -360,6 +361,22 @@ function compileFunction(code, params, options = {}) { | |||
| 360 | 361 | result.function.cachedData = result.cachedData; | |
| 361 | 362 | } | |
| 362 | 363 | ||
| 364 | + if (importModuleDynamically !== undefined) { | ||
| 365 | + if (typeof importModuleDynamically !== 'function') { | ||
| 366 | + throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically', | ||
| 367 | + 'function', | ||
| 368 | + importModuleDynamically); | ||
| 369 | + } | ||
| 370 | + const { importModuleDynamicallyWrap } = | ||
| 371 | + require('internal/vm/module'); | ||
| 372 | + const { callbackMap } = internalBinding('module_wrap'); | ||
| 373 | + const wrapped = importModuleDynamicallyWrap(importModuleDynamically); | ||
| 374 | + const func = result.function; | ||
| 375 | + callbackMap.set(result.cacheKey, { | ||
| 376 | + importModuleDynamically: (s, _k) => wrapped(s, func), | ||
| 377 | + }); | ||
| 378 | + } | ||
| 379 | + | ||
| 363 | 380 | return result.function; | |
| 364 | 381 | } | |
| 365 | 382 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,8 @@ const { | |||
| 8 | 8 | Module, | |
| 9 | 9 | SourceTextModule, | |
| 10 | 10 | SyntheticModule, | |
| 11 | - createContext | ||
| 11 | + createContext, | ||
| 12 | + compileFunction, | ||
| 12 | 13 | } = require('vm'); | |
| 13 | 14 | const util = require('util'); | |
| 14 | 15 | ||
@@ -147,3 +148,19 @@ const util = require('util'); | |||
| 147 | 148 | name: 'TypeError' | |
| 148 | 149 | }); | |
| 149 | 150 | } | |
| 151 | + | ||
| 152 | + // Test compileFunction importModuleDynamically | ||
| 153 | + { | ||
| 154 | + const module = new SyntheticModule([], () => {}); | ||
| 155 | + module.link(() => {}); | ||
| 156 | + const f = compileFunction('return import("x")', [], { | ||
| 157 | + importModuleDynamically(specifier, referrer) { | ||
| 158 | + assert.strictEqual(specifier, 'x'); | ||
| 159 | + assert.strictEqual(referrer, f); | ||
| 160 | + return module; | ||
| 161 | + }, | ||
| 162 | + }); | ||
| 163 | + f().then((ns) => { | ||
| 164 | + assert.strictEqual(ns, module.namespace); | ||
| 165 | + }); | ||
| 166 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,6 +148,7 @@ const customTypesMap = { | |||
| 148 | 148 | 'URLSearchParams': 'url.html#url_class_urlsearchparams', | |
| 149 | 149 | ||
| 150 | 150 | 'vm.Module': 'vm.html#vm_class_vm_module', | |
| 151 | + 'vm.Script': 'vm.html#vm_class_vm_script', | ||
| 151 | 152 | 'vm.SourceTextModule': 'vm.html#vm_class_vm_sourcetextmodule', | |
| 152 | 153 | ||
| 153 | 154 | 'MessagePort': 'worker_threads.html#worker_threads_class_messageport', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments