| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 05e702d commit b0f114d
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // exposes ModuleWrap for testing | ||
| 4 | + | ||
| 5 | + module.exports = internalBinding('module_wrap').ModuleWrap; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ const { | |||
| 8 | 8 | getConstructorOf, | |
| 9 | 9 | customInspectSymbol, | |
| 10 | 10 | } = require('internal/util'); | |
| 11 | + const { SafePromise } = require('internal/safe_globals'); | ||
| 11 | 12 | ||
| 12 | 13 | const { | |
| 13 | 14 | ModuleWrap, | |
@@ -131,27 +132,26 @@ class Module { | |||
| 131 | 132 | const wrap = wrapMap.get(this); | |
| 132 | 133 | if (wrap.getStatus() !== kUninstantiated) | |
| 133 | 134 | throw new errors.Error('ERR_VM_MODULE_STATUS', 'must be uninstantiated'); | |
| 135 | + | ||
| 134 | 136 | linkingStatusMap.set(this, 'linking'); | |
| 135 | - const promises = []; | ||
| 136 | - wrap.link((specifier) => { | ||
| 137 | - const p = (async () => { | ||
| 138 | - const m = await linker(specifier, this); | ||
| 139 | - if (!m || !wrapMap.has(m)) | ||
| 140 | - throw new errors.Error('ERR_VM_MODULE_NOT_MODULE'); | ||
| 141 | - if (m.context !== this.context) | ||
| 142 | - throw new errors.Error('ERR_VM_MODULE_DIFFERENT_CONTEXT'); | ||
| 143 | - const childLinkingStatus = linkingStatusMap.get(m); | ||
| 144 | - if (childLinkingStatus === 'errored') | ||
| 145 | - throw new errors.Error('ERR_VM_MODULE_LINKING_ERRORED'); | ||
| 146 | - if (childLinkingStatus === 'unlinked') | ||
| 147 | - await m.link(linker); | ||
| 148 | - return wrapMap.get(m); | ||
| 149 | - })(); | ||
| 150 | - promises.push(p); | ||
| 151 | - return p; | ||
| 137 | + | ||
| 138 | + const promises = wrap.link(async (specifier) => { | ||
| 139 | + const m = await linker(specifier, this); | ||
| 140 | + if (!m || !wrapMap.has(m)) | ||
| 141 | + throw new errors.Error('ERR_VM_MODULE_NOT_MODULE'); | ||
| 142 | + if (m.context !== this.context) | ||
| 143 | + throw new errors.Error('ERR_VM_MODULE_DIFFERENT_CONTEXT'); | ||
| 144 | + const childLinkingStatus = linkingStatusMap.get(m); | ||
| 145 | + if (childLinkingStatus === 'errored') | ||
| 146 | + throw new errors.Error('ERR_VM_MODULE_LINKING_ERRORED'); | ||
| 147 | + if (childLinkingStatus === 'unlinked') | ||
| 148 | + await m.link(linker); | ||
| 149 | + return wrapMap.get(m); | ||
| 152 | 150 | }); | |
| 151 | + | ||
| 153 | 152 | try { | |
| 154 | - await Promise.all(promises); | ||
| 153 | + if (promises !== undefined) | ||
| 154 | + await SafePromise.all(promises); | ||
| 155 | 155 | linkingStatusMap.set(this, 'linked'); | |
| 156 | 156 | } catch (err) { | |
| 157 | 157 | linkingStatusMap.set(this, 'errored'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -107,6 +107,7 @@ | |||
| 107 | 107 | 'lib/internal/loader/DefaultResolve.js', | |
| 108 | 108 | 'lib/internal/loader/ModuleJob.js', | |
| 109 | 109 | 'lib/internal/loader/ModuleMap.js', | |
| 110 | + 'lib/internal/loader/ModuleWrap.js', | ||
| 110 | 111 | 'lib/internal/loader/Translators.js', | |
| 111 | 112 | 'lib/internal/safe_globals.js', | |
| 112 | 113 | 'lib/internal/net.js', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -209,7 +209,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) { | |||
| 209 | 209 | Local<Promise> resolve_promise = resolve_return_value.As<Promise>(); | |
| 210 | 210 | obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise); | |
| 211 | 211 | ||
| 212 | - promises->Set(mod_context, specifier, resolve_promise).FromJust(); | ||
| 212 | + promises->Set(mod_context, i, resolve_promise).FromJust(); | ||
| 213 | 213 | } | |
| 214 | 214 | ||
| 215 | 215 | args.GetReturnValue().Set(promises); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,29 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // Flags: --expose-internals | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + common.crashOnUnhandledRejection(); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + | ||
| 9 | + const ModuleWrap = require('internal/loader/ModuleWrap'); | ||
| 10 | + const { getPromiseDetails, isPromise } = process.binding('util'); | ||
| 11 | + const setTimeoutAsync = require('util').promisify(setTimeout); | ||
| 12 | + | ||
| 13 | + const foo = new ModuleWrap('export * from "bar"; 6;', 'foo'); | ||
| 14 | + const bar = new ModuleWrap('export const five = 5', 'bar'); | ||
| 15 | + | ||
| 16 | + (async () => { | ||
| 17 | + const promises = foo.link(() => setTimeoutAsync(1000).then(() => bar)); | ||
| 18 | + assert.strictEqual(promises.length, 1); | ||
| 19 | + assert(isPromise(promises[0])); | ||
| 20 | + | ||
| 21 | + await Promise.all(promises); | ||
| 22 | + | ||
| 23 | + assert.strictEqual(getPromiseDetails(promises[0])[1], bar); | ||
| 24 | + | ||
| 25 | + foo.instantiate(); | ||
| 26 | + | ||
| 27 | + assert.strictEqual(await foo.evaluate(), 6); | ||
| 28 | + assert.strictEqual(foo.namespace().five, 5); | ||
| 29 | + })(); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments