Skip to content

Commit 5319c39

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore fixed a text encoding errors
1 parent 99c94f6 commit 5319c39

File tree

13 files changed

+146
-69
lines changed

13 files changed

+146
-69
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
For correct working of the ChakraCore require assemblies `msvcp120.dll` and `msvcr120.dll` from the Visual C++ Redistributable Packages for Visual Studio 2013.</description>
1616
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore version 1.3).</summary>
17-
<releaseNotes>Was made switch to new ChakraCore API.</releaseNotes>
17+
<releaseNotes>Fixed a text encoding errors.</releaseNotes>
1818
<copyright>Copyright (c) 2013-2016 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1919
<language>en-US</language>
2020
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
=============
2525
RELEASE NOTES
2626
=============
27-
Was made switch to new ChakraCore API.
27+
Fixed a text encoding errors.
2828

2929
====================
3030
POST-INSTALL ACTIONS

NuGet/JavaScriptEngineSwitcher.Core/JavaScriptEngineSwitcher.Core.nuspec

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<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 and ChakraCore). This library allows you to quickly and easily switch to using of another JavaScript engine.</description>
1414
<summary>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 and ChakraCore).</summary>
15-
<releaseNotes>1. Removed dependency on `System.Configuration.dll` (no longer supported configuration by using the `Web.config` and `App.config` files);
16-
2. Added support of .NET Core 1.0.1 and .NET Framework 4.5.1;
17-
3. In `IJsEngine` interface was added `SupportsGarbageCollection` property and `CollectGarbage` method;
18-
4. `JsRuntimeErrorHelpers` class was renamed to `JsErrorHelpers` class.</releaseNotes>
1915
<copyright>Copyright (c) 2013-2016 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2016
<language>en-US</language>
2117
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript</tags>
@@ -33,6 +29,7 @@
3329
<dependency id="System.Resources.ResourceManager" version="4.0.1" />
3430
<dependency id="System.Runtime.Extensions" version="4.1.0" />
3531
<dependency id="System.Runtime.InteropServices" version="4.1.0" />
32+
<dependency id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" />
3633
<dependency id="System.Text.Encoding" version="4.0.11" />
3734
<dependency id="System.Text.RegularExpressions" version="4.1.0" />
3835
<dependency id="System.Threading" version="4.0.11" />

NuGet/JavaScriptEngineSwitcher.Core/readme.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
Microsoft ClearScript.V8, Jurassic, Jint and ChakraCore). This library allows
1717
you to quickly and easily switch to using of another JavaScript engine.
1818

19-
=============
20-
RELEASE NOTES
21-
=============
22-
1. Removed dependency on `System.Configuration.dll` (no longer supported
23-
configuration by using the `Web.config` and `App.config` files);
24-
2. Added support of .NET Core 1.0.1 and .NET Framework 4.5.1;
25-
3. In `IJsEngine` interface was added `SupportsGarbageCollection` property and
26-
`CollectGarbage` method;
27-
4. `JsRuntimeErrorHelpers` class was renamed to `JsErrorHelpers` class.
28-
2919
=============
3020
DOCUMENTATION
3121
=============

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsContext.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using JavaScriptEngineSwitcher.Core.Utilities;
4+
35
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
46
{
57
/// <summary>
@@ -155,7 +157,13 @@ public static uint Idle()
155157
public static JsValue ParseScript(string script, JsSourceContext sourceContext, string sourceName)
156158
{
157159
JsValue result;
158-
JsErrorHelpers.ThrowIfError(NativeMethods.JsParseScriptUtf8(script, sourceContext, sourceName, out result));
160+
JsErrorCode errorCode = Utils.IsWindows() ?
161+
NativeMethods.JsParseScript(script, sourceContext, sourceName, out result)
162+
:
163+
NativeMethods.JsParseScriptUtf8(script, sourceContext, sourceName, out result)
164+
;
165+
166+
JsErrorHelpers.ThrowIfError(errorCode);
159167

160168
return result;
161169
}
@@ -187,7 +195,13 @@ public static JsValue ParseScript(string script)
187195
public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceName)
188196
{
189197
JsValue result;
190-
JsErrorHelpers.ThrowIfError(NativeMethods.JsRunScriptUtf8(script, sourceContext, sourceName, out result));
198+
JsErrorCode errorCode = Utils.IsWindows() ?
199+
NativeMethods.JsRunScript(script, sourceContext, sourceName, out result)
200+
:
201+
NativeMethods.JsRunScriptUtf8(script, sourceContext, sourceName, out result)
202+
;
203+
204+
JsErrorHelpers.ThrowIfError(errorCode);
191205

192206
return result;
193207
}
@@ -224,7 +238,13 @@ public static JsValue RunScript(string script)
224238
public static ulong SerializeScript(string script, byte[] buffer)
225239
{
226240
var bufferSize = (ulong)buffer.Length;
227-
JsErrorHelpers.ThrowIfError(NativeMethods.JsSerializeScriptUtf8(script, buffer, ref bufferSize));
241+
JsErrorCode errorCode = Utils.IsWindows() ?
242+
NativeMethods.JsSerializeScript(script, buffer, ref bufferSize)
243+
:
244+
NativeMethods.JsSerializeScriptUtf8(script, buffer, ref bufferSize)
245+
;
246+
247+
JsErrorHelpers.ThrowIfError(errorCode);
228248

229249
return bufferSize;
230250
}

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsPropertyId.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using JavaScriptEngineSwitcher.Core.Utilities;
4+
35
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
46
{
57
/// <summary>
@@ -37,7 +39,13 @@ public string Name
3739
get
3840
{
3941
string name;
40-
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetPropertyNameFromIdUtf8Copy(this, out name));
42+
JsErrorCode errorCode = Utils.IsWindows() ?
43+
NativeMethods.JsGetPropertyNameFromId(this, out name)
44+
:
45+
NativeMethods.JsGetPropertyNameFromIdUtf8Copy(this, out name)
46+
;
47+
48+
JsErrorHelpers.ThrowIfError(errorCode);
4149

4250
return name;
4351
}
@@ -71,7 +79,13 @@ internal JsPropertyId(IntPtr id)
7179
public static JsPropertyId FromString(string name)
7280
{
7381
JsPropertyId id;
74-
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetPropertyIdFromNameUtf8(name, out id));
82+
JsErrorCode errorCode = Utils.IsWindows() ?
83+
NativeMethods.JsGetPropertyIdFromName(name, out id)
84+
:
85+
NativeMethods.JsGetPropertyIdFromNameUtf8(name, out id)
86+
;
87+
88+
JsErrorHelpers.ThrowIfError(errorCode);
7589

7690
return id;
7791
}

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsValue.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Runtime.InteropServices;
33

4+
using JavaScriptEngineSwitcher.Core.Utilities;
5+
46
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
57
{
68
/// <summary>
@@ -301,7 +303,15 @@ public static JsValue FromInt32(int value)
301303
public static JsValue FromString(string value)
302304
{
303305
JsValue reference;
304-
JsErrorHelpers.ThrowIfError(NativeMethods.JsPointerToStringUtf8(value, new UIntPtr((uint)value.Length), out reference));
306+
JsErrorCode errorCode;
307+
308+
errorCode = Utils.IsWindows() ?
309+
NativeMethods.JsPointerToString(value, new UIntPtr((uint)value.Length), out reference)
310+
:
311+
NativeMethods.JsPointerToStringUtf8(value, new UIntPtr((uint)value.Length), out reference)
312+
;
313+
314+
JsErrorHelpers.ThrowIfError(errorCode);
305315

306316
return reference;
307317
}
@@ -568,10 +578,25 @@ public double ToDouble()
568578
{
569579
IntPtr buffer;
570580
UIntPtr length;
581+
JsErrorCode errorCode;
582+
string result;
583+
584+
if (Utils.IsWindows())
585+
{
586+
errorCode = NativeMethods.JsStringToPointer(this, out buffer, out length);
587+
JsErrorHelpers.ThrowIfError(errorCode);
571588

572-
JsErrorHelpers.ThrowIfError(NativeMethods.JsStringToPointerUtf8Copy(this, out buffer, out length));
589+
result = Marshal.PtrToStringUni(buffer, (int)length);
590+
}
591+
else
592+
{
593+
errorCode = NativeMethods.JsStringToPointerUtf8Copy(this, out buffer, out length);
594+
JsErrorHelpers.ThrowIfError(errorCode);
573595

574-
return Marshal.PtrToStringAnsi(buffer, (int)length);
596+
result = Marshal.PtrToStringAnsi(buffer, (int)length);
597+
}
598+
599+
return result;
575600
}
576601

577602
/// <summary>

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/NativeMethods.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,44 @@ internal static extern JsErrorCode JsCreateTypedArray(JsTypedArrayType arrayType
324324

325325
[DllImport(DllName)]
326326
internal static extern JsErrorCode JsSetContextData(JsContext context, IntPtr data);
327+
328+
#region Windows only
329+
330+
[DllImport(DllName, CharSet = CharSet.Unicode)]
331+
internal static extern JsErrorCode JsParseScript(string script, JsSourceContext sourceContext, string sourceUrl, out JsValue result);
332+
333+
[DllImport(DllName, CharSet = CharSet.Unicode)]
334+
internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext, string sourceUrl, out JsValue result);
335+
336+
[DllImport(DllName, CharSet = CharSet.Unicode)]
337+
internal static extern JsErrorCode JsSerializeScript(string script, byte[] buffer, ref ulong bufferSize);
338+
339+
[DllImport(DllName, CharSet = CharSet.Unicode)]
340+
internal static extern JsErrorCode JsParseSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out JsValue result);
341+
342+
[DllImport(DllName, CharSet = CharSet.Unicode)]
343+
internal static extern JsErrorCode JsRunSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out JsValue result);
344+
345+
[DllImport(DllName)]
346+
internal static extern JsErrorCode JsParseSerializedScriptWithCallback(JsSerializedScriptLoadSourceCallback scriptLoadCallback,
347+
JsSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out JsValue result);
348+
349+
[DllImport(DllName)]
350+
internal static extern JsErrorCode JsRunSerializedScriptWithCallback(JsSerializedScriptLoadSourceCallback scriptLoadCallback,
351+
JsSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out JsValue result);
352+
353+
[DllImport(DllName, CharSet = CharSet.Unicode)]
354+
internal static extern JsErrorCode JsGetPropertyIdFromName(string name, out JsPropertyId propertyId);
355+
356+
[DllImport(DllName, CharSet = CharSet.Unicode)]
357+
internal static extern JsErrorCode JsGetPropertyNameFromId(JsPropertyId propertyId, out string name);
358+
359+
[DllImport(DllName, CharSet = CharSet.Unicode)]
360+
internal static extern JsErrorCode JsPointerToString(string value, UIntPtr stringLength, out JsValue stringValue);
361+
362+
[DllImport(DllName)]
363+
internal static extern JsErrorCode JsStringToPointer(JsValue value, out IntPtr stringValue, out UIntPtr stringLength);
364+
365+
#endregion
327366
}
328367
}

src/JavaScriptEngineSwitcher.Core/Utilities/Utils.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
using System;
22
using System.IO;
3+
#if !NETSTANDARD1_3
4+
using System.Linq;
5+
#endif
36
using System.Reflection;
47
using System.Runtime.CompilerServices;
8+
#if NETSTANDARD1_3
9+
using System.Runtime.InteropServices;
10+
#endif
511
using System.Text;
612

713
using JavaScriptEngineSwitcher.Core.Resources;
@@ -10,6 +16,35 @@ namespace JavaScriptEngineSwitcher.Core.Utilities
1016
{
1117
public static class Utils
1218
{
19+
#if !NETSTANDARD1_3
20+
/// <summary>
21+
/// List of Windows platform identifiers
22+
/// </summary>
23+
private static readonly PlatformID[] _windowsPlatformIDs =
24+
{
25+
PlatformID.Win32NT,
26+
PlatformID.Win32S,
27+
PlatformID.Win32Windows,
28+
PlatformID.WinCE
29+
};
30+
#endif
31+
32+
33+
/// <summary>
34+
/// Determines whether the current operating system is Windows
35+
/// </summary>
36+
/// <returns>true if the operating system is Windows; otherwise, false</returns>
37+
public static bool IsWindows()
38+
{
39+
#if NETSTANDARD1_3
40+
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
41+
#else
42+
bool isWindows = _windowsPlatformIDs.Contains(Environment.OSVersion.Platform);
43+
#endif
44+
45+
return isWindows;
46+
}
47+
1348
/// <summary>
1449
/// Determines whether the current process is a 64-bit process
1550
/// </summary>

src/JavaScriptEngineSwitcher.Core/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"System.Resources.ResourceManager": "4.0.1",
2424
"System.Runtime.Extensions": "4.1.0",
2525
"System.Runtime.InteropServices": "4.1.0",
26+
"System.Runtime.InteropServices.RuntimeInformation": "4.0.0",
2627
"System.Text.Encoding": "4.0.11",
2728
"System.Text.RegularExpressions": "4.1.0",
2829
"System.Threading": "4.0.11"

0 commit comments

Comments
 (0)