From 1b536db124b217fc54c7228de63e4206bba2659f Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 21 Aug 2024 21:24:50 +0200 Subject: [PATCH] Don't overwrite default export from `SourceTextModule` --- Jint.Tests/Runtime/ModuleTests.cs | 16 ++++++++++++++++ .../Statements/JintExportDefaultDeclaration.cs | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/Jint.Tests/Runtime/ModuleTests.cs b/Jint.Tests/Runtime/ModuleTests.cs index cc0a6f5136..65b5804578 100644 --- a/Jint.Tests/Runtime/ModuleTests.cs +++ b/Jint.Tests/Runtime/ModuleTests.cs @@ -43,6 +43,22 @@ public void ShouldExportDefault() Assert.Equal("exported value", ns.Get("default").AsString()); } + [Fact] + public void ShouldExportDefaultFunctionWithoutName() + { + _engine.Modules.Add("module1", "export default function main() { return 1; }"); + _engine.Modules.Add("module2", "export default function () { return 1; }"); + var ns = _engine.Modules.Import("module1"); + + var func = ns.Get("default"); + Assert.Equal(1, func.Call()); + + ns = _engine.Modules.Import("module2"); + + func = ns.Get("default"); + Assert.Equal(1, func.Call()); + } + [Fact] public void ShouldExportAll() { diff --git a/Jint/Runtime/Interpreter/Statements/JintExportDefaultDeclaration.cs b/Jint/Runtime/Interpreter/Statements/JintExportDefaultDeclaration.cs index b40772ad3f..3d0d5dd04a 100644 --- a/Jint/Runtime/Interpreter/Statements/JintExportDefaultDeclaration.cs +++ b/Jint/Runtime/Interpreter/Statements/JintExportDefaultDeclaration.cs @@ -42,6 +42,13 @@ protected override void Initialize(EvaluationContext context) protected override Completion ExecuteInternal(EvaluationContext context) { var env = context.Engine.ExecutionContext.LexicalEnvironment; + if (env.HasBinding("*default*")) + { + // We already have the default binding. + // Initialized in SourceTextModule.InitializeEnvironment. + return Completion.Empty(); + } + JsValue value; if (_classDefinition is not null) {