-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure Acornima ParseErrorException is exposed as JavasScriptException (
- Loading branch information
Showing
10 changed files
with
93 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Jint.Runtime; | ||
|
||
namespace Jint; | ||
|
||
internal static class AcornimaExtensions | ||
{ | ||
public static Script ParseScriptGuarded(this Parser parser, Realm realm, string code, string? source = null, bool strict = false) | ||
{ | ||
try | ||
{ | ||
return parser.ParseScript(code, source, strict); | ||
} | ||
catch (ParseErrorException e) | ||
{ | ||
ExceptionHelper.ThrowSyntaxError(realm, e.Message, ToLocation(e, source)); | ||
return default; | ||
} | ||
} | ||
|
||
public static Module ParseModuleGuarded(this Parser parser, Engine engine, string code, string? source = null) | ||
{ | ||
try | ||
{ | ||
return parser.ParseModule(code, source); | ||
} | ||
catch (ParseErrorException ex) | ||
{ | ||
ExceptionHelper.ThrowSyntaxError(engine.Realm, $"Error while loading module: error in module '{source}': {ex.Error}", ToLocation(ex, source)); | ||
return default; | ||
} | ||
catch (Exception) | ||
{ | ||
ExceptionHelper.ThrowJavaScriptException(engine, $"Could not load module {source}", AstExtensions.DefaultLocation); | ||
return default; | ||
} | ||
} | ||
|
||
private static SourceLocation ToLocation(ParseErrorException ex, string? source) | ||
{ | ||
return SourceLocation.From(Position.From(ex.LineNumber, ex.Column), Position.From(ex.LineNumber, ex.Column), source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters