Skip to content

Commit 7eac3e3

Browse files
committed
Dynamic import should return different promise objects
1 parent 5270164 commit 7eac3e3

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

lib/Runtime/Language/SourceTextModuleRecord.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ namespace Js
202202

203203
if (this->promise != nullptr)
204204
{
205-
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, this->scriptContext, this);
205+
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, this->scriptContext, this, false);
206206
}
207207

208208
// Notify host if current module is dynamically-loaded module, or is root module and the host hasn't been notified
@@ -307,7 +307,7 @@ namespace Js
307307
{
308308
// Cleanup in case of error.
309309
this->ReleaseParserResources();
310-
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, scriptContext, this);
310+
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, scriptContext, this, false);
311311
}
312312
else
313313
{
@@ -342,7 +342,7 @@ namespace Js
342342
}
343343
}
344344

345-
return this->promise;
345+
return JavascriptPromise::CreatePassThroughPromise(this->promise, scriptContext);
346346
}
347347

348348
HRESULT SourceTextModuleRecord::PrepareForModuleDeclarationInitialization()
@@ -401,7 +401,7 @@ namespace Js
401401

402402
if (this->promise != nullptr)
403403
{
404-
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, this->scriptContext, this);
404+
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, this->scriptContext, this, false);
405405
}
406406

407407
if (this->promise != nullptr || (isRootModule && !hadNotifyHostReady))
@@ -967,7 +967,7 @@ namespace Js
967967

968968
if (this->promise != nullptr)
969969
{
970-
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, this->scriptContext, this);
970+
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, this->errorObject, this->scriptContext, this, false);
971971
return scriptContext->GetLibrary()->GetUndefined();
972972
}
973973
else
@@ -1029,7 +1029,7 @@ namespace Js
10291029
this->errorObject = errorObject;
10301030
if (this->promise != nullptr)
10311031
{
1032-
ResolveOrRejectDynamicImportPromise(false, errorObject, scriptContext, this);
1032+
ResolveOrRejectDynamicImportPromise(false, errorObject, scriptContext, this, false);
10331033
return scriptContext->GetLibrary()->GetUndefined();
10341034
}
10351035
}
@@ -1041,7 +1041,7 @@ namespace Js
10411041

10421042
if (this->promise != nullptr)
10431043
{
1044-
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(true, this->GetNamespace(), this->GetScriptContext(), this);
1044+
SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(true, this->GetNamespace(), this->GetScriptContext(), this, false);
10451045
}
10461046

10471047
return ret;
@@ -1268,7 +1268,7 @@ namespace Js
12681268
}
12691269

12701270
// static
1271-
Var SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(bool isResolve, Var value, ScriptContext *scriptContext, SourceTextModuleRecord *moduleRecord)
1271+
Var SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(bool isResolve, Var value, ScriptContext *scriptContext, SourceTextModuleRecord *moduleRecord, bool useReturn)
12721272
{
12731273
bool isScriptActive = scriptContext->GetThreadContext()->IsScriptActive();
12741274
JavascriptPromise *promise = nullptr;
@@ -1297,8 +1297,11 @@ namespace Js
12971297
if (moduleRecord != nullptr)
12981298
{
12991299
moduleRecord->SetPromise(nullptr);
1300+
if (useReturn)
1301+
{
1302+
return JavascriptPromise::CreatePassThroughPromise(promise, scriptContext);
1303+
}
13001304
}
1301-
13021305
return promise;
13031306
}
13041307
}

lib/Runtime/Language/SourceTextModuleRecord.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace Js
108108

109109
void SetParent(SourceTextModuleRecord* parentRecord, LPCOLESTR moduleName);
110110
Utf8SourceInfo* GetSourceInfo() { return this->pSourceInfo; }
111-
static Var ResolveOrRejectDynamicImportPromise(bool isResolve, Var value, ScriptContext *scriptContext, SourceTextModuleRecord *mr = nullptr);
111+
static Var ResolveOrRejectDynamicImportPromise(bool isResolve, Var value, ScriptContext *scriptContext, SourceTextModuleRecord *mr = nullptr, bool useReturn = true);
112112
Var PostProcessDynamicModuleImport();
113113

114114
private:

test/es6module/dynamic-module-functionality.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ var tests = [
406406
body: function() {
407407
assert.throws(()=>{eval('new import("ModuleSimpleExport.js")')}, SyntaxError);
408408
}
409+
},
410+
{
411+
name : "Test that import() always gives different promise objects - Bug Issue #5795",
412+
body: function () {
413+
WScript.RegisterModuleSource("testModule", "export const a = 5;");
414+
let functionBody =
415+
`testDynamicImport(function () {
416+
const first = import ('ModuleSimpleExport.js');
417+
const second = import ('ModuleSimpleExport.js');
418+
assert.isTrue(first !== second, 'import() should not return the same promise');
419+
return Promise.all([first, second]).then ((results) => ({first, second, results}));
420+
}, function (imports) {
421+
assert.isTrue(imports.first !== imports.second, 'import() should not return the same promise after resolution');
422+
assert.isTrue(imports.results[0] === imports.results[1], 'import() should return the same namespace object');
423+
}, function (e) {
424+
print ("Test should not throw, threw " + e);
425+
}, _asyncEnter, _asyncExit);`;
426+
testScript(functionBody, "Test that import() always gives different promise objects", false, true);
427+
testModuleScript(functionBody, "Test that import() always gives different promise objects", false, true);
428+
}
409429
}
410430
];
411431

0 commit comments

Comments
 (0)