Skip to content

Commit

Permalink
Throw real JavascriptException location on Module (#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
huanent committed Apr 3, 2023
1 parent 1445918 commit 6560653
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
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

0 comments on commit 6560653

Please sign in to comment.