Skip to content

Commit e181acd

Browse files
committed
In JavaScriptEngineSwitcher.Jurassic now script error contains a full stack trace
1 parent 1a2d657 commit e181acd

File tree

6 files changed

+208
-6
lines changed

6 files changed

+208
-6
lines changed

NuGet/JavaScriptEngineSwitcher.Jurassic/JavaScriptEngineSwitcher.Jurassic.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>JavaScriptEngineSwitcher.Jurassic contains adapter `JurassicJsEngine` (wrapper for the Jurassic JavaScript Engine (http://github.com/paulbartrum/jurassic) version of February 8, 2017).</description>
1414
<summary>JavaScriptEngineSwitcher.Jurassic contains adapter `JurassicJsEngine` (wrapper for the Jurassic JavaScript Engine version of February 8, 2017).</summary>
15+
<releaseNotes>Now script error contains a full stack trace.</releaseNotes>
1516
<copyright>Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1617
<language>en-US</language>
1718
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript Jurassic</tags>

NuGet/JavaScriptEngineSwitcher.Jurassic/readme.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
for the Jurassic JavaScript Engine (http://github.com/paulbartrum/jurassic)
1616
version of February 8, 2017).
1717

18+
=============
19+
RELEASE NOTES
20+
=============
21+
Now script error contains a full stack trace.
22+
1823
=============
1924
DOCUMENTATION
2025
=============

src/JavaScriptEngineSwitcher.Core/JsEngineBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ public virtual void ExecuteResource(string resourceName, Type type)
226226
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
227227
}
228228

229-
string code = Utils.GetResourceAsString(resourceName, type);
229+
Assembly assembly = type.GetTypeInfo().Assembly;
230+
string nameSpace = type.Namespace;
231+
string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName;
232+
233+
string code = Utils.GetResourceAsString(resourceFullName, assembly);
230234
Execute(code, resourceName);
231235
}
232236

src/JavaScriptEngineSwitcher.Jurassic.Net4/JavaScriptEngineSwitcher.Jurassic.Net40.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
<Compile Include="..\JavaScriptEngineSwitcher.Jurassic\Properties\AssemblyInfo.cs">
7272
<Link>AssemblyInfo.cs</Link>
7373
</Compile>
74+
<Compile Include="..\JavaScriptEngineSwitcher.Jurassic\ResourceScriptSource.cs">
75+
<Link>ResourceScriptSource.cs</Link>
76+
</Compile>
7477
</ItemGroup>
7578
<ItemGroup>
7679
<Folder Include="Properties\" />

src/JavaScriptEngineSwitcher.Jurassic/JurassicJsEngine.cs

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using System;
22
using System.IO;
3+
using System.Reflection;
34
using System.Text;
45

56
using OriginalCompatibilityMode = Jurassic.CompatibilityMode;
67
using OriginalConcatenatedString = Jurassic.ConcatenatedString;
8+
using OriginalErrorInstance = Jurassic.Library.ErrorInstance;
9+
using OriginalFileScriptSource = Jurassic.FileScriptSource;
710
using OriginalJsEngine = Jurassic.ScriptEngine;
811
using OriginalJsException = Jurassic.JavaScriptException;
912
using OriginalNull = Jurassic.Null;
13+
using OriginalStringScriptSource = Jurassic.StringScriptSource;
1014
using OriginalTypeConverter = Jurassic.TypeConverter;
1115
using OriginalUndefined = Jurassic.Undefined;
1216

1317
using JavaScriptEngineSwitcher.Core;
18+
using JavaScriptEngineSwitcher.Core.Utilities;
1419
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
1520

1621
namespace JavaScriptEngineSwitcher.Jurassic
@@ -147,7 +152,14 @@ private static object MapToHostType(object value)
147152
private JsRuntimeException ConvertJavascriptExceptionToJsRuntimeException(
148153
OriginalJsException jsException)
149154
{
150-
var jsRuntimeException = new JsRuntimeException(jsException.Message, EngineName, EngineVersion,
155+
var jsError = jsException.ErrorObject as OriginalErrorInstance;
156+
string message = jsException.Message;
157+
if (jsError != null)
158+
{
159+
message = !string.IsNullOrEmpty(jsError.Stack) ? jsError.Stack : jsError.Message;
160+
}
161+
162+
var jsRuntimeException = new JsRuntimeException(message, EngineName, EngineVersion,
151163
jsException)
152164
{
153165
Category = jsException.Name,
@@ -162,14 +174,20 @@ private JsRuntimeException ConvertJavascriptExceptionToJsRuntimeException(
162174
#region JsEngineBase implementation
163175

164176
protected override object InnerEvaluate(string expression)
177+
{
178+
return InnerEvaluate(expression, null);
179+
}
180+
181+
protected override object InnerEvaluate(string expression, string documentName)
165182
{
166183
object result;
167184

168185
lock (_executionSynchronizer)
169186
{
170187
try
171188
{
172-
result = _jsEngine.Evaluate(expression);
189+
var source = new OriginalStringScriptSource(expression, documentName);
190+
result = _jsEngine.Evaluate(source);
173191
}
174192
catch (OriginalJsException e)
175193
{
@@ -188,18 +206,29 @@ protected override object InnerEvaluate(string expression)
188206

189207
protected override T InnerEvaluate<T>(string expression)
190208
{
191-
object result = InnerEvaluate(expression);
209+
return InnerEvaluate<T>(expression, null);
210+
}
211+
212+
protected override T InnerEvaluate<T>(string expression, string documentName)
213+
{
214+
object result = InnerEvaluate(expression, documentName);
192215

193216
return OriginalTypeConverter.ConvertTo<T>(_jsEngine, result);
194217
}
195218

196219
protected override void InnerExecute(string code)
220+
{
221+
InnerExecute(code, null);
222+
}
223+
224+
protected override void InnerExecute(string code, string documentName)
197225
{
198226
lock (_executionSynchronizer)
199227
{
200228
try
201229
{
202-
_jsEngine.Execute(code);
230+
var source = new OriginalStringScriptSource(code, documentName);
231+
_jsEngine.Execute(source);
203232
}
204233
catch (OriginalJsException e)
205234
{
@@ -382,7 +411,84 @@ public override void ExecuteFile(string path, Encoding encoding = null)
382411
{
383412
try
384413
{
385-
_jsEngine.ExecuteFile(path, encoding);
414+
var source = new OriginalFileScriptSource(path, encoding);
415+
_jsEngine.Execute(source);
416+
}
417+
catch (OriginalJsException e)
418+
{
419+
throw ConvertJavascriptExceptionToJsRuntimeException(e);
420+
}
421+
}
422+
}
423+
424+
public override void ExecuteResource(string resourceName, Type type)
425+
{
426+
VerifyNotDisposed();
427+
428+
if (resourceName == null)
429+
{
430+
throw new ArgumentNullException(
431+
"resourceName", string.Format(CoreStrings.Common_ArgumentIsNull, "resourceName"));
432+
}
433+
434+
if (type == null)
435+
{
436+
throw new ArgumentNullException(
437+
"type", string.Format(CoreStrings.Common_ArgumentIsNull, "type"));
438+
}
439+
440+
if (string.IsNullOrWhiteSpace(resourceName))
441+
{
442+
throw new ArgumentException(
443+
string.Format(CoreStrings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
444+
}
445+
446+
Assembly assembly = type.GetTypeInfo().Assembly;
447+
string nameSpace = type.Namespace;
448+
string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName;
449+
450+
lock (_executionSynchronizer)
451+
{
452+
try
453+
{
454+
var source = new ResourceScriptSource(resourceFullName, assembly);
455+
_jsEngine.Execute(source);
456+
}
457+
catch (OriginalJsException e)
458+
{
459+
throw ConvertJavascriptExceptionToJsRuntimeException(e);
460+
}
461+
}
462+
}
463+
464+
public override void ExecuteResource(string resourceName, Assembly assembly)
465+
{
466+
VerifyNotDisposed();
467+
468+
if (resourceName == null)
469+
{
470+
throw new ArgumentNullException(
471+
"resourceName", string.Format(CoreStrings.Common_ArgumentIsNull, "resourceName"));
472+
}
473+
474+
if (assembly == null)
475+
{
476+
throw new ArgumentNullException(
477+
"assembly", string.Format(CoreStrings.Common_ArgumentIsNull, "assembly"));
478+
}
479+
480+
if (string.IsNullOrWhiteSpace(resourceName))
481+
{
482+
throw new ArgumentException(
483+
string.Format(CoreStrings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
484+
}
485+
486+
lock (_executionSynchronizer)
487+
{
488+
try
489+
{
490+
var source = new ResourceScriptSource(resourceName, assembly);
491+
_jsEngine.Execute(source);
386492
}
387493
catch (OriginalJsException e)
388494
{
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
5+
using Jurassic;
6+
7+
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
8+
9+
namespace JavaScriptEngineSwitcher.Jurassic
10+
{
11+
/// <summary>
12+
/// Represents a embedded JS-resource
13+
/// </summary>
14+
internal sealed class ResourceScriptSource : ScriptSource
15+
{
16+
/// <summary>
17+
/// The case-sensitive resource name
18+
/// </summary>
19+
private readonly string _resourceName;
20+
21+
/// <summary>
22+
/// The assembly, which contains the embedded resource
23+
/// </summary>
24+
private readonly Assembly _assembly;
25+
26+
/// <summary>
27+
/// Gets a path to the embedded JS-resource
28+
/// </summary>
29+
public override string Path
30+
{
31+
get { return _resourceName; }
32+
}
33+
34+
35+
/// <summary>
36+
/// Constructs a instance of <see cref="ResourceScriptSource"/>
37+
/// </summary>
38+
/// <param name="resourceName">The case-sensitive resource name</param>
39+
/// <param name="assembly">The assembly, which contains the embedded resource</param>
40+
public ResourceScriptSource(string resourceName, Assembly assembly)
41+
{
42+
if (resourceName == null)
43+
{
44+
throw new ArgumentNullException(
45+
"resourceName", string.Format(CoreStrings.Common_ArgumentIsNull, "resourceName"));
46+
}
47+
48+
if (assembly == null)
49+
{
50+
throw new ArgumentNullException(
51+
"assembly", string.Format(CoreStrings.Common_ArgumentIsNull, "assembly"));
52+
}
53+
54+
if (string.IsNullOrWhiteSpace(resourceName))
55+
{
56+
throw new ArgumentException(
57+
string.Format(CoreStrings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
58+
}
59+
60+
_resourceName = resourceName;
61+
_assembly = assembly;
62+
}
63+
64+
65+
/// <summary>
66+
/// Gets a reader that can be used to read the source code from the embedded JS-resource
67+
/// </summary>
68+
/// <returns>The reader that can be used to read the source code from the embedded
69+
/// JS-resource, positioned at the start of the source code</returns>
70+
public override TextReader GetReader()
71+
{
72+
Stream stream = _assembly.GetManifestResourceStream(_resourceName);
73+
74+
if (stream == null)
75+
{
76+
throw new NullReferenceException(
77+
string.Format(CoreStrings.Resources_ResourceIsNull, _resourceName));
78+
}
79+
80+
return new StreamReader(stream);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)