Skip to content

Commit e7f6fb7

Browse files
committed
In JavaScriptEngineSwitcher.Jint improved a handling of JS runtime errors
1 parent e257246 commit e7f6fb7

File tree

4 files changed

+59
-96
lines changed

4 files changed

+59
-96
lines changed

src/JavaScriptEngineSwitcher.Jint/JavaScriptEngineSwitcher.Jint.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageIcon>icon.png</PackageIcon>
1515
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;Jint</PackageTags>
1616
<PackageReleaseNotes>1. Jint was updated to version 3.0.0 Beta 1715;
17-
2. Simplified a handling of JS runtime errors.</PackageReleaseNotes>
17+
2. Simplified and improved a handling of JS runtime errors.</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<Import Project="../../build/common.props" />
@@ -52,8 +52,4 @@
5252
</Content>
5353
</ItemGroup>
5454

55-
<ItemGroup>
56-
<Folder Include="Extensions\" />
57-
</ItemGroup>
58-
5955
</Project>

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 56 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
using IOriginalPrimitiveInstance = Jint.Native.IPrimitiveInstance;
66
using OriginalEngine = Jint.Engine;
77
using OriginalJavaScriptException = Jint.Runtime.JavaScriptException;
8-
using OriginalJintException = Jint.Runtime.JintException;
98
using OriginalMemoryLimitExceededException = Jint.Runtime.MemoryLimitExceededException;
109
using OriginalObjectInstance = Jint.Native.Object.ObjectInstance;
1110
using OriginalParser = Esprima.JavaScriptParser;
1211
using OriginalParserException = Esprima.ParserException;
1312
using OriginalParserOptions = Esprima.ParserOptions;
1413
using OriginalProgram = Esprima.Ast.Program;
1514
using OriginalRecursionDepthOverflowException = Jint.Runtime.RecursionDepthOverflowException;
15+
using OriginalRuntimeException = Jint.Runtime.JintException;
1616
using OriginalStatementsCountOverflowException = Jint.Runtime.StatementsCountOverflowException;
1717
using OriginalTypeReference = Jint.Runtime.Interop.TypeReference;
1818
using OriginalTypes = Jint.Runtime.Types;
@@ -27,7 +27,6 @@
2727

2828
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
2929
using WrapperCompilationException = JavaScriptEngineSwitcher.Core.JsCompilationException;
30-
using WrapperException = JavaScriptEngineSwitcher.Core.JsException;
3130
using WrapperRuntimeException = JavaScriptEngineSwitcher.Core.JsRuntimeException;
3231
using WrapperTimeoutException = JavaScriptEngineSwitcher.Core.JsTimeoutException;
3332
using WrapperUsageException = JavaScriptEngineSwitcher.Core.JsUsageException;
@@ -188,24 +187,26 @@ private static WrapperCompilationException WrapParserException(OriginalParserExc
188187
return wrapperCompilationException;
189188
}
190189

191-
private static WrapperException WrapJintException(
192-
OriginalJintException originalJintException)
190+
private static WrapperRuntimeException WrapRuntimeException(OriginalRuntimeException originalRuntimeException)
193191
{
194-
WrapperException wrapperException;
195-
string message = originalJintException.Message;
192+
string message = originalRuntimeException.Message;
196193
if (string.IsNullOrWhiteSpace(message))
197194
{
198195
message = "An unknown error occurred";
199196
}
200197
string description = message;
198+
string type = string.Empty;
199+
string documentName = string.Empty;
200+
int lineNumber = 0;
201+
int columnNumber = 0;
202+
string callStack = string.Empty;
201203

202-
if (originalJintException is OriginalJavaScriptException)
204+
if (originalRuntimeException is OriginalJavaScriptException)
203205
{
204-
var originalJavaScriptException = (OriginalJavaScriptException)originalJintException;
205-
string type = string.Empty;
206-
string documentName = originalJavaScriptException.Location.Source;
207-
int lineNumber = originalJavaScriptException.LineNumber;
208-
int columnNumber = originalJavaScriptException.Column + 1;
206+
var originalJavaScriptException = (OriginalJavaScriptException)originalRuntimeException;
207+
documentName = originalJavaScriptException.Location.Source;
208+
lineNumber = originalJavaScriptException.LineNumber;
209+
columnNumber = originalJavaScriptException.Column + 1;
209210

210211
OriginalValue errorValue = originalJavaScriptException.Error;
211212
if (errorValue.IsObject())
@@ -219,46 +220,22 @@ private static WrapperException WrapJintException(
219220
}
220221
}
221222

222-
if (!string.IsNullOrEmpty(type))
223+
if (string.IsNullOrEmpty(type))
223224
{
224-
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
225-
columnNumber);
226-
227-
var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
228-
originalJavaScriptException)
229-
{
230-
Type = type,
231-
DocumentName = documentName,
232-
LineNumber = lineNumber,
233-
ColumnNumber = columnNumber
234-
};
235-
236-
wrapperException = wrapperRuntimeException;
237-
}
238-
else
239-
{
240-
wrapperException = new WrapperException(message, EngineName, EngineVersion,
241-
originalJavaScriptException);
225+
type = JsErrorType.Common;
242226
}
227+
228+
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
229+
columnNumber);
243230
}
244-
else if (originalJintException is OriginalMemoryLimitExceededException)
231+
else if (originalRuntimeException is OriginalMemoryLimitExceededException)
245232
{
246-
var originalMemoryException = (OriginalMemoryLimitExceededException)originalJintException;
247-
string type = JsErrorType.Common;
233+
type = JsErrorType.Common;
248234
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);
249-
250-
var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
251-
originalMemoryException)
252-
{
253-
Description = description
254-
};
255-
256-
wrapperException = wrapperRuntimeException;
257235
}
258-
else if (originalJintException is OriginalRecursionDepthOverflowException)
236+
else if (originalRuntimeException is OriginalRecursionDepthOverflowException)
259237
{
260-
var originalRecursionException = (OriginalRecursionDepthOverflowException)originalJintException;
261-
string callStack = string.Empty;
238+
var originalRecursionException = (OriginalRecursionDepthOverflowException)originalRuntimeException;
262239
string[] callChainItems = originalRecursionException.CallChain
263240
.Split(new string[] { "->" }, StringSplitOptions.None)
264241
;
@@ -287,42 +264,32 @@ private static WrapperException WrapJintException(
287264
stringBuilderPool.Return(stackBuilder);
288265
}
289266

290-
string type = JsErrorType.Range;
267+
type = JsErrorType.Range;
291268
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);
292-
293-
var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
294-
originalRecursionException)
295-
{
296-
Description = description,
297-
Type = type,
298-
CallStack = callStack
299-
};
300-
301-
wrapperException = wrapperRuntimeException;
302269
}
303-
else if (originalJintException is OriginalStatementsCountOverflowException)
270+
else if (originalRuntimeException is OriginalStatementsCountOverflowException)
304271
{
305-
var originalStatementsException = (OriginalStatementsCountOverflowException)originalJintException;
306-
string type = JsErrorType.Range;
272+
type = JsErrorType.Range;
307273
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);
308-
309-
var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
310-
originalStatementsException)
311-
{
312-
Description = description
313-
};
314-
315-
wrapperException = wrapperRuntimeException;
316274
}
317275
else
318276
{
319-
wrapperException = new WrapperException(message, EngineName, EngineVersion,
320-
originalJintException);
277+
type = JsErrorType.Common;
278+
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);
321279
}
322280

323-
wrapperException.Description = description;
281+
var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
282+
originalRuntimeException)
283+
{
284+
Description = description,
285+
Type = type,
286+
DocumentName = documentName,
287+
LineNumber = lineNumber,
288+
ColumnNumber = columnNumber,
289+
CallStack = callStack
290+
};
324291

325-
return wrapperException;
292+
return wrapperRuntimeException;
326293
}
327294

328295
private static WrapperTimeoutException WrapTimeoutException(TimeoutException originalTimeoutException)
@@ -390,9 +357,9 @@ protected override object InnerEvaluate(string expression, string documentName)
390357
{
391358
throw WrapParserException(e);
392359
}
393-
catch (OriginalJintException e)
360+
catch (OriginalRuntimeException e)
394361
{
395-
throw WrapJintException(e);
362+
throw WrapRuntimeException(e);
396363
}
397364
catch (TimeoutException e)
398365
{
@@ -437,9 +404,9 @@ protected override void InnerExecute(string code, string documentName)
437404
{
438405
throw WrapParserException(e);
439406
}
440-
catch (OriginalJintException e)
407+
catch (OriginalRuntimeException e)
441408
{
442-
throw WrapJintException(e);
409+
throw WrapRuntimeException(e);
443410
}
444411
catch (TimeoutException e)
445412
{
@@ -466,9 +433,9 @@ protected override void InnerExecute(IPrecompiledScript precompiledScript)
466433
{
467434
_jsEngine.Execute(jintPrecompiledScript.Program);
468435
}
469-
catch (OriginalJintException e)
436+
catch (OriginalRuntimeException e)
470437
{
471-
throw WrapJintException(e);
438+
throw WrapRuntimeException(e);
472439
}
473440
catch (TimeoutException e)
474441
{
@@ -489,9 +456,9 @@ protected override object InnerCallFunction(string functionName, params object[]
489456
{
490457
functionValue = _jsEngine.GetValue(functionName);
491458
}
492-
catch (OriginalJintException e)
459+
catch (OriginalRuntimeException e)
493460
{
494-
throw WrapJintException(e);
461+
throw WrapRuntimeException(e);
495462
}
496463

497464
OriginalValue resultValue;
@@ -500,9 +467,9 @@ protected override object InnerCallFunction(string functionName, params object[]
500467
{
501468
resultValue = _jsEngine.Invoke(functionValue, args);
502469
}
503-
catch (OriginalJintException e)
470+
catch (OriginalRuntimeException e)
504471
{
505-
throw WrapJintException(e);
472+
throw WrapRuntimeException(e);
506473
}
507474
catch (TimeoutException e)
508475
{
@@ -559,9 +526,9 @@ protected override object InnerGetVariableValue(string variableName)
559526
{
560527
variableValue = _jsEngine.GetValue(variableName);
561528
}
562-
catch (OriginalJintException e)
529+
catch (OriginalRuntimeException e)
563530
{
564-
throw WrapJintException(e);
531+
throw WrapRuntimeException(e);
565532
}
566533

567534
result = MapToHostType(variableValue);
@@ -587,9 +554,9 @@ protected override void InnerSetVariableValue(string variableName, object value)
587554
{
588555
_jsEngine.SetValue(variableName, processedValue);
589556
}
590-
catch (OriginalJintException e)
557+
catch (OriginalRuntimeException e)
591558
{
592-
throw WrapJintException(e);
559+
throw WrapRuntimeException(e);
593560
}
594561
}
595562
}
@@ -609,9 +576,9 @@ protected override void InnerEmbedHostObject(string itemName, object value)
609576
{
610577
_jsEngine.SetValue(itemName, processedValue);
611578
}
612-
catch (OriginalJintException e)
579+
catch (OriginalRuntimeException e)
613580
{
614-
throw WrapJintException(e);
581+
throw WrapRuntimeException(e);
615582
}
616583
}
617584
}
@@ -626,9 +593,9 @@ protected override void InnerEmbedHostType(string itemName, Type type)
626593
{
627594
_jsEngine.SetValue(itemName, typeReference);
628595
}
629-
catch (OriginalJintException e)
596+
catch (OriginalRuntimeException e)
630597
{
631-
throw WrapJintException(e);
598+
throw WrapRuntimeException(e);
632599
}
633600
}
634601
}

src/JavaScriptEngineSwitcher.Jint/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
RELEASE NOTES
1919
=============
2020
1. Jint was updated to version 3.0.0 Beta 1715;
21-
2. Simplified a handling of JS runtime errors.
21+
2. Simplified and improved a handling of JS runtime errors.
2222

2323
=============
2424
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/Jint/CommonTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void MappingRuntimeErrorDuringStatementsCountOverflowIsCorrect()
306306
Assert.NotNull(exception);
307307
Assert.Equal("Runtime error", exception.Category);
308308
Assert.Equal("The maximum number of statements executed have been reached.", exception.Description);
309-
Assert.Empty(exception.Type);
309+
Assert.Equal("RangeError", exception.Type);
310310
Assert.Empty(exception.DocumentName);
311311
Assert.Equal(0, exception.LineNumber);
312312
Assert.Equal(0, exception.ColumnNumber);

0 commit comments

Comments
 (0)