Skip to content

Commit afe40ec

Browse files
committed
Fixed a minor errors
1 parent a85276a commit afe40ec

File tree

20 files changed

+434
-226
lines changed

20 files changed

+434
-226
lines changed

NuGet/MsieJavaScriptEngine.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine). Project was based on the code of SassAndCoffee.JavaScript (http://github.com/paulcbetts/SassAndCoffee), Chakra Sample Hosts (http://github.com/panopticoncentral/chakra-host) and jsrt-dotnet (http://github.com/robpaveza/jsrt-dotnet).</description>
1414
<summary>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine).</summary>
15-
<releaseNotes>In JsRT modes during calling of the `CollectGarbage` method is no longer performed blocking.</releaseNotes>
15+
<releaseNotes>Fixed a minor errors.</releaseNotes>
1616
<copyright>Copyright (c) 2012-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1717
<language>en-US</language>
1818
<tags>JavaScript ECMAScript MSIE IE Edge Chakra</tags>

NuGet/readme.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
=============
2222
RELEASE NOTES
2323
=============
24-
In JsRT modes during calling of the `CollectGarbage` method is no longer
25-
performed blocking.
24+
Fixed a minor errors.
2625

2726
============
2827
PROJECT SITE

src/MsieJavaScriptEngine.Net4/MsieJavaScriptEngine.Net40.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<Compile Include="..\MsieJavaScriptEngine\ActiveScript\ActiveScriptJsEngineBase.ScriptSite.cs">
5555
<Link>ActiveScript\ActiveScriptJsEngineBase.ScriptSite.cs</Link>
5656
</Compile>
57+
<Compile Include="..\MsieJavaScriptEngine\ActiveScript\ActiveScriptJsErrorHelpers.cs">
58+
<Link>ActiveScript\ActiveScriptJsErrorHelpers.cs</Link>
59+
</Compile>
5760
<Compile Include="..\MsieJavaScriptEngine\ActiveScript\ActiveScriptWrapper32.cs">
5861
<Link>ActiveScript\ActiveScriptWrapper32.cs</Link>
5962
</Compile>
@@ -498,6 +501,9 @@
498501
<Compile Include="..\MsieJavaScriptEngine\Utilities\StringBuilderExtensions.cs">
499502
<Link>Utilities\StringBuilderExtensions.cs</Link>
500503
</Compile>
504+
<Compile Include="..\MsieJavaScriptEngine\Utilities\StringExtensions.cs">
505+
<Link>Utilities\StringExtensions.cs</Link>
506+
</Compile>
501507
<Compile Include="..\MsieJavaScriptEngine\Utilities\TypeConverter.cs">
502508
<Link>Utilities\TypeConverter.cs</Link>
503509
</Compile>

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.ScriptSite.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private ActiveScriptException CreateActiveScriptException(IActiveScriptError err
5252

5353
int hResult = exceptionInfo.scode;
5454
int errorCode = HResultHelpers.GetCode(hResult);
55-
bool isSyntaxError = IsSyntaxError(hResult);
55+
bool isSyntaxError = ActiveScriptJsErrorHelpers.IsSyntaxError(hResult);
5656
string category = exceptionInfo.bstrSource;
5757
string description = exceptionInfo.bstrDescription;
5858
string helpLink = string.Empty;
@@ -97,24 +97,6 @@ private ActiveScriptException CreateActiveScriptException(IActiveScriptError err
9797
return activeScriptException;
9898
}
9999

100-
/// <summary>
101-
/// Checks whether the specified HRESULT value is syntax error
102-
/// </summary>
103-
/// <param name="hResult">The HRESULT value</param>
104-
/// <returns>Result of check (true - is syntax error; false - is not syntax error)</returns>
105-
private static bool IsSyntaxError(int hResult)
106-
{
107-
bool isSyntaxError = false;
108-
109-
if (HResultHelpers.GetFacility(hResult) == HResultHelpers.FACILITY_CONTROL)
110-
{
111-
int errorCode = HResultHelpers.GetCode(hResult);
112-
isSyntaxError = errorCode >= 1002 && errorCode <= 1035;
113-
}
114-
115-
return isSyntaxError;
116-
}
117-
118100
/// <summary>
119101
/// Gets a error details
120102
/// </summary>
@@ -241,10 +223,8 @@ private bool TryWriteStackTrace(StringBuilder buffer)
241223
string description;
242224
stackFrame.GetDescriptionString(true, out description);
243225

244-
if (string.Equals(description, "JScript global code", StringComparison.Ordinal))
245-
{
246-
description = "Global code";
247-
}
226+
description = ActiveScriptJsErrorHelpers.ShortenErrorItemName(
227+
description, "JScript ");
248228

249229
IDebugCodeContext codeContext;
250230
stackFrame.GetCodeContext(out codeContext);

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.cs

Lines changed: 35 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ namespace MsieJavaScriptEngine.ActiveScript
1919
/// </summary>
2020
internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
2121
{
22-
/// <summary>
23-
/// Name of resource, which contains a ECMAScript 5 Polyfill
24-
/// </summary>
25-
private const string ES5_POLYFILL_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.ES5.min.js";
26-
27-
/// <summary>
28-
/// Name of resource, which contains a JSON2 library
29-
/// </summary>
30-
private const string JSON2_LIBRARY_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.json2.min.js";
31-
3222
/// <summary>
3323
/// Instance of Active Script wrapper
3424
/// </summary>
@@ -96,10 +86,7 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
9686
/// </summary>
9787
/// <param name="engineMode">JS engine mode</param>
9888
/// <param name="enableDebugging">Flag for whether to enable script debugging features</param>
99-
/// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param>
100-
/// <param name="useJson2Library">Flag for whether to use the JSON2 library</param>
101-
protected ActiveScriptJsEngineBase(JsEngineMode engineMode, bool enableDebugging,
102-
bool useEcmaScript5Polyfill, bool useJson2Library)
89+
protected ActiveScriptJsEngineBase(JsEngineMode engineMode, bool enableDebugging)
10390
: base(engineMode)
10491
{
10592
string lowerIeVersion;
@@ -119,36 +106,41 @@ protected ActiveScriptJsEngineBase(JsEngineMode engineMode, bool enableDebugging
119106
throw new NotSupportedException();
120107
}
121108

122-
_dispatcher.Invoke(() =>
109+
try
123110
{
124-
try
111+
_dispatcher.Invoke(() =>
125112
{
126113
_activeScriptWrapper = Utils.Is64BitProcess() ?
127114
(IActiveScriptWrapper)new ActiveScriptWrapper64(engineMode, enableDebugging)
128115
:
129116
new ActiveScriptWrapper32(engineMode, enableDebugging)
130117
;
131-
}
132-
catch (Exception e)
133-
{
134-
throw new JsEngineLoadException(
135-
string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded,
136-
_engineModeName, lowerIeVersion, e.Message), _engineModeName);
137-
}
138-
139-
if (enableDebugging)
140-
{
141-
StartDebugging();
142-
}
143118

144-
_activeScriptWrapper.SetScriptSite(new ScriptSite(this));
145-
_activeScriptWrapper.InitNew();
146-
_activeScriptWrapper.SetScriptState(ScriptState.Started);
119+
if (enableDebugging)
120+
{
121+
StartDebugging();
122+
}
147123

148-
InitScriptDispatch();
149-
});
124+
_activeScriptWrapper.SetScriptSite(new ScriptSite(this));
125+
_activeScriptWrapper.InitNew();
126+
_activeScriptWrapper.SetScriptState(ScriptState.Started);
150127

151-
LoadResources(useEcmaScript5Polyfill, useJson2Library);
128+
_dispatch = WrapScriptDispatch(_activeScriptWrapper.GetScriptDispatch());
129+
});
130+
}
131+
catch (Exception e)
132+
{
133+
throw new JsEngineLoadException(
134+
string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded,
135+
_engineModeName, lowerIeVersion, e.Message), _engineModeName);
136+
}
137+
finally
138+
{
139+
if (_dispatch == null)
140+
{
141+
Dispose();
142+
}
143+
}
152144
}
153145

154146
/// <summary>
@@ -276,24 +268,7 @@ private JsRuntimeException ConvertActiveScriptExceptionToJsRuntimeException(
276268
/// <returns>Short name of error category</returns>
277269
private string ShortenErrorCategoryName(string categoryName)
278270
{
279-
if (categoryName == null)
280-
{
281-
throw new ArgumentNullException("categoryName");
282-
}
283-
284-
string shortCategoryName = categoryName;
285-
if (categoryName.StartsWith(_errorCategoryNamePrefix, StringComparison.Ordinal))
286-
{
287-
shortCategoryName = categoryName.Substring(_errorCategoryNamePrefix.Length);
288-
if (shortCategoryName.Length > 0)
289-
{
290-
char[] chars = shortCategoryName.ToCharArray();
291-
chars[0] = char.ToUpperInvariant(chars[0]);
292-
shortCategoryName = new string(chars);
293-
}
294-
}
295-
296-
return shortCategoryName;
271+
return ActiveScriptJsErrorHelpers.ShortenErrorItemName(categoryName, _errorCategoryNamePrefix);
297272
}
298273

299274
/// <summary>
@@ -319,27 +294,21 @@ private void StartDebugging()
319294
}
320295
}
321296

322-
/// <summary>
323-
/// Initializes a script dispatch
324-
/// </summary>
325-
private void InitScriptDispatch()
297+
private static IExpando WrapScriptDispatch(object dispatch)
326298
{
327-
IExpando dispatch = null;
328-
object obj;
329-
330-
_activeScriptWrapper.GetScriptDispatch(null, out obj);
331-
332-
if (obj != null && obj.GetType().IsCOMObject)
299+
IExpando wrappedDispatch = null;
300+
if (dispatch != null && dispatch.GetType().IsCOMObject)
333301
{
334-
dispatch = obj as IExpando;
302+
wrappedDispatch = dispatch as IExpando;
335303
}
336304

337-
if (dispatch == null)
305+
if (wrappedDispatch == null)
338306
{
339-
throw new InvalidOperationException(NetFrameworkStrings.Runtime_ActiveScriptDispatcherNotInitialized);
307+
throw new InvalidOperationException(
308+
NetFrameworkStrings.Runtime_ActiveScriptDispatcherNotInitialized);
340309
}
341310

342-
_dispatch = dispatch;
311+
return wrappedDispatch;
343312
}
344313

345314
/// <summary>
@@ -536,55 +505,6 @@ private void InnerCollectGarbage(ScriptGCType type)
536505
_activeScriptWrapper.CollectGarbage(type);
537506
}
538507

539-
/// <summary>
540-
/// Loads a resources
541-
/// </summary>
542-
/// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param>
543-
/// <param name="useJson2Library">Flag for whether to use the JSON2 library</param>
544-
private void LoadResources(bool useEcmaScript5Polyfill, bool useJson2Library)
545-
{
546-
Assembly assembly = GetType().GetTypeInfo().Assembly;
547-
548-
if (useEcmaScript5Polyfill)
549-
{
550-
ExecuteResource(ES5_POLYFILL_RESOURCE_NAME, assembly);
551-
}
552-
553-
if (useJson2Library)
554-
{
555-
ExecuteResource(JSON2_LIBRARY_RESOURCE_NAME, assembly);
556-
}
557-
}
558-
559-
/// <summary>
560-
/// Executes a code from embedded JS-resource
561-
/// </summary>
562-
/// <param name="resourceName">The case-sensitive resource name</param>
563-
/// <param name="assembly">The assembly, which contains the embedded resource</param>
564-
private void ExecuteResource(string resourceName, Assembly assembly)
565-
{
566-
if (resourceName == null)
567-
{
568-
throw new ArgumentNullException(
569-
"resourceName", string.Format(CommonStrings.Common_ArgumentIsNull, "resourceName"));
570-
}
571-
572-
if (assembly == null)
573-
{
574-
throw new ArgumentNullException(
575-
"assembly", string.Format(CommonStrings.Common_ArgumentIsNull, "assembly"));
576-
}
577-
578-
if (string.IsNullOrWhiteSpace(resourceName))
579-
{
580-
throw new ArgumentException(
581-
string.Format(CommonStrings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
582-
}
583-
584-
string code = Utils.GetResourceAsString(resourceName, assembly);
585-
Execute(code, resourceName);
586-
}
587-
588508
#region IInnerJsEngine implementation
589509

590510
public override string Mode
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#if !NETSTANDARD1_3
2+
using System;
3+
4+
using MsieJavaScriptEngine.Utilities;
5+
6+
using HResultHelpers = MsieJavaScriptEngine.Helpers.ComHelpers.HResult;
7+
8+
namespace MsieJavaScriptEngine.ActiveScript
9+
{
10+
/// <summary>
11+
/// Active Script error helpers
12+
/// </summary>
13+
internal static class ActiveScriptJsErrorHelpers
14+
{
15+
/// <summary>
16+
/// Checks whether the specified HRESULT value is syntax error
17+
/// </summary>
18+
/// <param name="hResult">The HRESULT value</param>
19+
/// <returns>Result of check (true - is syntax error; false - is not syntax error)</returns>
20+
public static bool IsSyntaxError(int hResult)
21+
{
22+
bool isSyntaxError = false;
23+
24+
if (HResultHelpers.GetFacility(hResult) == HResultHelpers.FACILITY_CONTROL)
25+
{
26+
int errorCode = HResultHelpers.GetCode(hResult);
27+
isSyntaxError = errorCode >= 1002 && errorCode <= 1035;
28+
}
29+
30+
return isSyntaxError;
31+
}
32+
33+
/// <summary>
34+
/// Shortens a name of error item
35+
/// </summary>
36+
/// <param name="itemName">Name of error item</param>
37+
/// <param name="prefix">Prefix</param>
38+
/// <returns>Short name of error item</returns>
39+
public static string ShortenErrorItemName(string itemName, string prefix)
40+
{
41+
if (itemName == null)
42+
{
43+
throw new ArgumentNullException("itemName");
44+
}
45+
46+
if (prefix == null)
47+
{
48+
throw new ArgumentNullException("prefix");
49+
}
50+
51+
int itemNameLength = itemName.Length;
52+
if (itemNameLength == 0 || prefix.Length == 0)
53+
{
54+
return itemName;
55+
}
56+
57+
string shortItemName = itemName.TrimStart(prefix);
58+
int shortItemNameLength = shortItemName.Length;
59+
60+
if (shortItemNameLength > 0 && shortItemNameLength < itemNameLength)
61+
{
62+
shortItemName = shortItemName.CapitalizeFirstLetter();
63+
}
64+
65+
return shortItemName;
66+
}
67+
}
68+
}
69+
#endif

0 commit comments

Comments
 (0)