Skip to content

Commit 34407fd

Browse files
committed
Fixed a error #59 “Unhandled exception when JS exception is thrown”
1 parent 94931d5 commit 34407fd

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

Diff for: src/JavaScriptEngineSwitcher.Core/Helpers/JsErrorHelpers.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,14 @@ private static string GetSourceFragment(string source, int position, int length,
604604

605605
var leftOffset = (int)Math.Floor((double)maxFragmentLength / 2);
606606
int fragmentStartPosition = columnNumber - leftOffset - 1;
607-
if (fragmentStartPosition < position)
607+
if (fragmentStartPosition > position)
608+
{
609+
if (length - fragmentStartPosition < maxFragmentLength)
610+
{
611+
fragmentStartPosition = length - maxFragmentLength;
612+
}
613+
}
614+
else
608615
{
609616
fragmentStartPosition = position;
610617
}
@@ -613,12 +620,10 @@ private static string GetSourceFragment(string source, int position, int length,
613620
if (fragmentStartPosition > position)
614621
{
615622
startPart = ellipsisSymbol;
616-
fragmentLength--;
617623
}
618-
if (fragmentStartPosition + maxFragmentLength < length)
624+
if (fragmentStartPosition + fragmentLength < length)
619625
{
620626
endPart = ellipsisSymbol;
621-
fragmentLength--;
622627
}
623628

624629
StringBuilder fragmentBuilder = StringBuilderPool.GetBuilder();

Diff for: src/JavaScriptEngineSwitcher.Core/JavaScriptEngineSwitcher.Core.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Product>JS Engine Switcher: Core</Product>
55
<VersionPrefix>3.0.0</VersionPrefix>
6-
<VersionSuffix>beta4</VersionSuffix>
6+
<VersionSuffix>beta9</VersionSuffix>
77
<TargetFrameworks>net40-client;net45;net471;netstandard1.3;netstandard2.0</TargetFrameworks>
88
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.0</NetStandardImplicitPackageVersion>
99
<OutputType>Library</OutputType>
@@ -14,7 +14,7 @@
1414
<Description>JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines (MSIE JavaScript Engine for .NET, Microsoft ClearScript.V8, Jurassic, Jint, ChakraCore, VroomJs and NiL.JS). This library allows you to quickly and easily switch to using of another JavaScript engine.</Description>
1515
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_Core_Logo128x128.png</PackageIconUrl>
1616
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript</PackageTags>
17-
<PackageReleaseNotes>Fixed a error #54 “System.Runtime.InteropServices.RuntimeInformation not required for .NET Framework 4.7.1”. Special thanks to David Gardiner.</PackageReleaseNotes>
17+
<PackageReleaseNotes>Fixed a error #59 “Unhandled exception when JS exception is thrown”.</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<Import Project="../../build/common.props" />

Diff for: src/JavaScriptEngineSwitcher.Core/readme.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
--------------------------------------------------------------------------------
4-
README file for JS Engine Switcher: Core v3.0.0 Beta 4
4+
README file for JS Engine Switcher: Core v3.0.0 Beta 9
55

66
--------------------------------------------------------------------------------
77

@@ -20,8 +20,7 @@
2020
=============
2121
RELEASE NOTES
2222
=============
23-
Fixed a error #54 “System.Runtime.InteropServices.RuntimeInformation not
24-
required for .NET Framework 4.7.1”. Special thanks to David Gardiner.
23+
Fixed a error #59 “Unhandled exception when JS exception is thrown”.
2524

2625
=============
2726
DOCUMENTATION
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Xunit;
2+
3+
using JavaScriptEngineSwitcher.Core.Helpers;
4+
5+
namespace JavaScriptEngineSwitcher.Tests
6+
{
7+
public class ErrorFormattingTests
8+
{
9+
[Fact]
10+
public void GettingSourceFragmentFromLineIsCorrect()
11+
{
12+
// Arrange
13+
const string input1 = "";
14+
const string targetOutput1 = input1;
15+
16+
const string input2 = " \n";
17+
const string targetOutput2 = input2;
18+
19+
const string input3 = "var @variable3 = 678;";
20+
const string targetOutput3 = input3;
21+
22+
const string input4 = " Math.hasOwnProperty(\"log2\")||(Math.log2=function(n){" +
23+
"return Math.log(@n)*Math.LOG2E});";
24+
const string targetOutput4 = "…Math.hasOwnProperty(\"log2\")||(Math.log2=function(n){" +
25+
"return Math.log(@n)*Math.LOG2E});";
26+
27+
const string input5 = "function mix(destination,source){var propertyName;destination=destination||{};" +
28+
"for(propertyName in source){if(source.hasOwnProperty(propertyName){" +
29+
"destination[propertyName]=source[propertyName]}}return destination}"
30+
;
31+
const string targetOutput5 = "… in source){if(source.hasOwnProperty(propertyName){" +
32+
"destination[propertyName]=source[propertyName]}}r…";
33+
34+
const string input6 = "Object.hasOwnProperty(\"assign)||(Object.assign=function(n){" +
35+
"var u,i,f,t,r;if(typeof n==\"undefined\"||n===null)" +
36+
"throw new TypeError(\"Object.assign: argument is not an Object.\");" +
37+
"for(u=Object(n),f=arguments.length,i=1;i<f;i++)" +
38+
"if(t=arguments[i],typeof t!=\"undefined\"&&t!==null)for(r in t)" +
39+
"Object.prototype.hasOwnProperty.call(t,r)&&(u[r]=t[r]);return u});"
40+
;
41+
const string targetOutput6 = "Object.hasOwnProperty(\"assign)||(Object.assign=function(n){" +
42+
"var u,i,f,t,r;if(typeof n==\"undefined\"||n…";
43+
44+
const string input7 = "function base64_encode(a){" +
45+
"var b=\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\";" +
46+
"var c,o2,o3,h1,h2,h3,h4,bits,i=0,enc='';do{c=a.charCodeAt(i++);o2=a.charCodeAt(i++);" +
47+
"o3=a.charCodeAt(i++);bits=c<<16|o2<<8|o3;h1=bits>>18&0x3f;h2=bits>>12&0x3f;" +
48+
"h3=bits>>6&0x3f;h4=bits&0x3f;enc+=b.charAt(h1)+b.charAt(h2)+b.charAt(h3)+b.charAt(h4)}" +
49+
"while(i<a.length);switch(a.length%3){case 1:enc=enc.slice(0,-2)+'==';break;" +
50+
"case 2:enc=enc.slice(0,-1)+'=';break}return @enc}"
51+
;
52+
const string targetOutput7 = "…(a.length%3){case 1:enc=enc.slice(0,-2)+'==';break;" +
53+
"case 2:enc=enc.slice(0,-1)+'=';break}return @enc}";
54+
55+
// Act
56+
string output1 = JsErrorHelpers.GetSourceFragmentFromLine(input1, 1, 100);
57+
string output2 = JsErrorHelpers.GetSourceFragmentFromLine(input2, 1, 100);
58+
string output3 = JsErrorHelpers.GetSourceFragmentFromLine(input3, 5, 100);
59+
string output4 = JsErrorHelpers.GetSourceFragmentFromLine(input4, 70, 85);
60+
string output5 = JsErrorHelpers.GetSourceFragmentFromLine(input5, 145, 100);
61+
string output6 = JsErrorHelpers.GetSourceFragmentFromLine(input6, 23, 100);
62+
string output7 = JsErrorHelpers.GetSourceFragmentFromLine(input7, 465, 100);
63+
64+
// Assert
65+
Assert.Equal(targetOutput1, output1);
66+
Assert.Equal(targetOutput2, output2);
67+
Assert.Equal(targetOutput3, output3);
68+
Assert.Equal(targetOutput4, output4);
69+
Assert.Equal(targetOutput5, output5);
70+
Assert.Equal(targetOutput6, output6);
71+
Assert.Equal(targetOutput7, output7);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)