Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw real JavascriptException location on Module #1522

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Jint.Tests/Runtime/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,23 @@ public void InvokingDelegateShouldContainJavascriptExceptionAsInnerException()
Assert.Equal("foo is not defined", exception.Message);
}

[Fact]
public void JavaScriptExceptionLocationOnModuleShouldBeRight()
{
var engine = new Engine();
engine.AddModule("my_module", @"
function throw_error(){
throw Error(""custom error"")
}

throw_error();
");

var ex= Assert.Throws<JavaScriptException>(() => engine.ImportModule("my_module"));
Assert.Equal(ex.Location.Start.Line, 3);
Assert.Equal(ex.Location.Start.Column, 10);
}

private static void EqualIgnoringNewLineDifferences(string expected, string actual)
{
expected = expected.Replace("\r\n", "\n");
Expand Down
6 changes: 5 additions & 1 deletion Jint/Engine.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ private JsValue EvaluateModule(string specifier, ModuleRecord cyclicModule)
}
else if (promise.State == PromiseState.Rejected)
{
var node = EsprimaExtensions.CreateLocationNode(Location.From(new Position(), new Position(), specifier));
var location = cyclicModule is CyclicModuleRecord cyclicModuleRecord
? cyclicModuleRecord.AbnormalCompletionLocation
: Location.From(new Position(), new Position());

var node = EsprimaExtensions.CreateLocationNode(location);
ExceptionHelper.ThrowJavaScriptException(this, promise.Value, node.Location);
}
else if (promise.State != PromiseState.Fulfilled)
Expand Down
6 changes: 5 additions & 1 deletion Jint/Runtime/Modules/CyclicModuleRecord.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable disable

using Esprima;
using Esprima.Ast;
using Jint.Native;
using Jint.Native.Promise;
Expand Down Expand Up @@ -40,6 +41,8 @@ internal CyclicModuleRecord(Engine engine, Realm realm, Module source, string lo

internal ModuleStatus Status { get; private set; }

internal Location AbnormalCompletionLocation { get; private set; }

/// <summary>
/// https://tc39.es/ecma262/#sec-moduledeclarationlinking
/// </summary>
Expand Down Expand Up @@ -121,7 +124,7 @@ public override JsValue Evaluate()
module._topLevelCapability = capability;

var result = module.InnerModuleEvaluation(stack, 0, ref asyncEvalOrder);

if (result.Type != CompletionType.Normal)
{
foreach (var m in stack)
Expand All @@ -130,6 +133,7 @@ public override JsValue Evaluate()
m._evalError = result;
}

AbnormalCompletionLocation = result.Location;
capability.Reject.Call(Undefined, new[] { result.Value });
}
else
Expand Down