Skip to content

Commit afd30d2

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore improved a performance in version for Unix
1 parent 5c4a109 commit afd30d2

File tree

7 files changed

+128
-58
lines changed

7 files changed

+128
-58
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23-
<releaseNotes>ChakraCore was updated to version 1.11.0.</releaseNotes>
23+
<releaseNotes>Improved a performance in version for Unix.</releaseNotes>
2424
<copyright>Copyright (c) 2013-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2525
<language>en-US</language>
2626
<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
@@ -30,7 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
ChakraCore was updated to version 1.11.0.
33+
Improved a performance in version for Unix.
3434

3535
=============
3636
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore.Net4/JavaScriptEngineSwitcher.ChakraCore.Net40.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\ChakraCoreSettings.cs">
8080
<Link>ChakraCoreSettings.cs</Link>
8181
</Compile>
82+
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\DefaultExternalBufferFinalizeCallback.cs">
83+
<Link>JsRt\DefaultExternalBufferFinalizeCallback.cs</Link>
84+
</Compile>
8285
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\Helpers\NumericHelpers.cs">
8386
<Link>Helpers\NumericHelpers.cs</Link>
8487
</Compile>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
4+
{
5+
/// <summary>
6+
/// Default callback for finalization of external buffer
7+
/// </summary>
8+
internal static class DefaultExternalBufferFinalizeCallback
9+
{
10+
/// <summary>
11+
/// Gets a instance of default callback for finalization of external buffer
12+
/// </summary>
13+
public static readonly JsObjectFinalizeCallback Instance = Marshal.FreeHGlobal;
14+
}
15+
}

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsContext.cs

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23

34
using JavaScriptEngineSwitcher.Core.Utilities;
45

@@ -144,32 +145,33 @@ public static uint Idle()
144145
}
145146

146147
/// <summary>
147-
/// Parses a script and returns a <c>Function</c> representing the script
148+
/// Parses a script and returns a function representing the script
148149
/// </summary>
149150
/// <remarks>
150151
/// Requires an active script context.
151152
/// </remarks>
152153
/// <param name="script">The script to parse</param>
153-
/// <param name="sourceContext">The cookie identifying the script that can be used
154-
/// by script contexts that have debugging enabled</param>
155-
/// <param name="sourceName">The location the script came from</param>
156-
/// <returns>The <c>Function</c> representing the script code</returns>
157-
public static JsValue ParseScript(string script, JsSourceContext sourceContext, string sourceName)
154+
/// <param name="sourceContext">A cookie identifying the script that can be used
155+
/// by debuggable script contexts</param>
156+
/// <param name="sourceUrl">The location the script came from</param>
157+
/// <returns>A function representing the script code</returns>
158+
public static JsValue ParseScript(string script, JsSourceContext sourceContext, string sourceUrl)
158159
{
159160
JsValue result;
160161
JsErrorCode errorCode;
161162

162163
if (Utils.IsWindows())
163164
{
164-
errorCode = NativeMethods.JsParseScript(script, sourceContext, sourceName, out result);
165+
errorCode = NativeMethods.JsParseScript(script, sourceContext, sourceUrl, out result);
165166
JsErrorHelpers.ThrowIfError(errorCode);
166167
}
167168
else
168169
{
169-
JsValue scriptValue = JsValue.FromString(script);
170+
byte[] scriptBytes = Encoding.GetEncoding(0).GetBytes(script);
171+
JsValue scriptValue = JsValue.CreateExternalArrayBuffer(scriptBytes);
170172
scriptValue.AddRef();
171173

172-
JsValue sourceUrlValue = JsValue.FromString(sourceName);
174+
JsValue sourceUrlValue = JsValue.FromString(sourceUrl);
173175
sourceUrlValue.AddRef();
174176

175177
try
@@ -188,46 +190,34 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
188190
return result;
189191
}
190192

191-
/// <summary>
192-
/// Parses a script and returns a <c>Function</c> representing the script
193-
/// </summary>
194-
/// <remarks>
195-
/// Requires an active script context.
196-
/// </remarks>
197-
/// <param name="script">The script to parse</param>
198-
/// <returns>The <c>Function</c> representing the script code</returns>
199-
public static JsValue ParseScript(string script)
200-
{
201-
return ParseScript(script, JsSourceContext.None, string.Empty);
202-
}
203-
204193
/// <summary>
205194
/// Executes a script
206195
/// </summary>
207196
/// <remarks>
208197
/// Requires an active script context.
209198
/// </remarks>
210199
/// <param name="script">The script to run</param>
211-
/// <param name="sourceContext">The cookie identifying the script that can be used
212-
/// by script contexts that have debugging enabled</param>
213-
/// <param name="sourceName">The location the script came from</param>
200+
/// <param name="sourceContext">A cookie identifying the script that can be used
201+
/// by debuggable script contexts</param>
202+
/// <param name="sourceUrl">The location the script came from</param>
214203
/// <returns>The result of the script, if any</returns>
215-
public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceName)
204+
public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceUrl)
216205
{
217206
JsValue result;
218207
JsErrorCode errorCode;
219208

220209
if (Utils.IsWindows())
221210
{
222-
errorCode = NativeMethods.JsRunScript(script, sourceContext, sourceName, out result);
211+
errorCode = NativeMethods.JsRunScript(script, sourceContext, sourceUrl, out result);
223212
JsErrorHelpers.ThrowIfError(errorCode);
224213
}
225214
else
226215
{
227-
JsValue scriptValue = JsValue.FromString(script);
216+
byte[] scriptBytes = Encoding.GetEncoding(0).GetBytes(script);
217+
JsValue scriptValue = JsValue.CreateExternalArrayBuffer(scriptBytes);
228218
scriptValue.AddRef();
229219

230-
JsValue sourceUrlValue = JsValue.FromString(sourceName);
220+
JsValue sourceUrlValue = JsValue.FromString(sourceUrl);
231221
sourceUrlValue.AddRef();
232222

233223
try
@@ -246,25 +236,12 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
246236
return result;
247237
}
248238

249-
/// <summary>
250-
/// Executes a script
251-
/// </summary>
252-
/// <remarks>
253-
/// Requires an active script context
254-
/// </remarks>
255-
/// <param name="script">The script to run</param>
256-
/// <returns>The result of the script, if any</returns>
257-
public static JsValue RunScript(string script)
258-
{
259-
return RunScript(script, JsSourceContext.None, string.Empty);
260-
}
261-
262239
/// <summary>
263240
/// Serializes a parsed script to a buffer than can be reused
264241
/// </summary>
265242
/// <remarks>
266243
/// <para>
267-
/// SerializeScript parses a script and then stores the parsed form of the script in a
244+
/// <c>SerializeScript</c> parses a script and then stores the parsed form of the script in a
268245
/// runtime-independent format. The serialized script then can be deserialized in any
269246
/// runtime without requiring the script to be re-parsed.
270247
/// </para>
@@ -273,41 +250,47 @@ public static JsValue RunScript(string script)
273250
/// </para>
274251
/// </remarks>
275252
/// <param name="script">The script to serialize</param>
276-
/// <param name="buffer">The buffer to put the serialized script into. Can be null</param>
277-
/// <returns>The size of the buffer, in bytes, required to hold the serialized script</returns>
278-
public static ulong SerializeScript(string script, byte[] buffer)
253+
/// <returns>The buffer to put the serialized script into</returns>
254+
public static byte[] SerializeScript(string script)
279255
{
280-
var bufferSize = (ulong)buffer.Length;
256+
byte[] buffer;
281257
JsErrorCode errorCode;
282258

283259
if (Utils.IsWindows())
284260
{
261+
buffer = null;
262+
uint bufferSize = 0;
263+
264+
errorCode = NativeMethods.JsSerializeScript(script, buffer, ref bufferSize);
265+
JsErrorHelpers.ThrowIfError(errorCode);
266+
267+
buffer = new byte[(int)bufferSize];
268+
285269
errorCode = NativeMethods.JsSerializeScript(script, buffer, ref bufferSize);
286270
JsErrorHelpers.ThrowIfError(errorCode);
287271
}
288272
else
289273
{
290-
JsValue scriptValue = JsValue.FromString(script);
274+
byte[] scriptBytes = Encoding.GetEncoding(0).GetBytes(script);
275+
JsValue scriptValue = JsValue.CreateExternalArrayBuffer(scriptBytes);
291276
scriptValue.AddRef();
292277

293278
JsValue bufferValue;
294279

295280
try
296281
{
297-
errorCode = NativeMethods.JsSerialize(scriptValue, out bufferValue,
298-
JsParseScriptAttributes.None);
282+
errorCode = NativeMethods.JsSerialize(scriptValue, out bufferValue, JsParseScriptAttributes.None);
299283
JsErrorHelpers.ThrowIfError(errorCode);
300284
}
301285
finally
302286
{
303287
scriptValue.Release();
304288
}
305289

306-
JsValue lengthValue = bufferValue.GetProperty("length");
307-
bufferSize = Convert.ToUInt64(lengthValue.ConvertToNumber().ToDouble());
290+
buffer = bufferValue.ArrayBufferBytes;
308291
}
309292

310-
return bufferSize;
293+
return buffer;
311294
}
312295

313296
/// <summary>

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsValue.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,39 @@ public IntPtr ExternalData
234234
}
235235
}
236236

237+
/// <summary>
238+
/// Gets a bytes from an <c>ArrayBuffer</c>
239+
/// </summary>
240+
/// <remarks>
241+
/// Requires an active script context.
242+
/// </remarks>
243+
public byte[] ArrayBufferBytes
244+
{
245+
get
246+
{
247+
IntPtr bufferPtr;
248+
uint bufferLength;
249+
250+
JsErrorCode errorCode = NativeMethods.JsGetArrayBufferStorage(this, out bufferPtr, out bufferLength);
251+
JsErrorHelpers.ThrowIfError(errorCode);
252+
253+
if (bufferPtr == IntPtr.Zero)
254+
{
255+
return null;
256+
}
257+
258+
if (bufferLength == 0)
259+
{
260+
return new byte[0];
261+
}
262+
263+
byte[] buffer = new byte[bufferLength];
264+
Marshal.Copy(bufferPtr, buffer, 0, (int)bufferLength);
265+
266+
return buffer;
267+
}
268+
}
269+
237270

238271
/// <summary>
239272
/// Initializes a new instance of the <see cref="JsValue"/> struct
@@ -403,6 +436,42 @@ public static JsValue CreateArray(uint length)
403436
return reference;
404437
}
405438

439+
/// <summary>
440+
/// Creates a Javascript <c>ArrayBuffer</c> object to access external memory
441+
/// </summary>
442+
/// <remarks>Requires an active script context.</remarks>
443+
/// <param name="buffer">Buffer for an external memory</param>
444+
/// <returns>The new <c>ArrayBuffer</c> object</returns>
445+
public static JsValue CreateExternalArrayBuffer(byte[] buffer)
446+
{
447+
IntPtr bufferPtr;
448+
int bufferLength;
449+
JsObjectFinalizeCallback finalizeCallback;
450+
451+
if (buffer != null)
452+
{
453+
bufferLength = buffer.Length;
454+
455+
bufferPtr = Marshal.AllocHGlobal(bufferLength);
456+
Marshal.Copy(buffer, 0, bufferPtr, bufferLength);
457+
458+
finalizeCallback = DefaultExternalBufferFinalizeCallback.Instance;
459+
}
460+
else
461+
{
462+
bufferLength = 0;
463+
bufferPtr = IntPtr.Zero;
464+
finalizeCallback = null;
465+
}
466+
467+
JsValue reference;
468+
JsErrorCode errorCode = NativeMethods.JsCreateExternalArrayBuffer(bufferPtr, (uint)bufferLength,
469+
finalizeCallback, bufferPtr, out reference);
470+
JsErrorHelpers.ThrowIfError(errorCode);
471+
472+
return reference;
473+
}
474+
406475
/// <summary>
407476
/// Creates a new JavaScript error object
408477
/// </summary>

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/NativeMethods.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ internal static extern JsErrorCode JsSetIndexedPropertiesToExternalData(JsValue
240240

241241
[DllImport(DllName)]
242242
internal static extern JsErrorCode JsCreateExternalArrayBuffer(IntPtr data, uint byteLength,
243-
JsObjectFinalizeCallback finalizeCallback, IntPtr callbackState, out bool result);
243+
JsObjectFinalizeCallback finalizeCallback, IntPtr callbackState, out JsValue result);
244244

245245
[DllImport(DllName)]
246246
internal static extern JsErrorCode JsCreateTypedArray(JsTypedArrayType arrayType, JsValue arrayBuffer,
@@ -255,7 +255,7 @@ internal static extern JsErrorCode JsGetTypedArrayInfo(JsValue typedArray, out J
255255
out JsValue arrayBuffer, out uint byteOffset, out uint byteLength);
256256

257257
[DllImport(DllName)]
258-
internal static extern JsErrorCode JsGetArrayBufferStorage(JsValue arrayBuffer, out byte[] buffer,
258+
internal static extern JsErrorCode JsGetArrayBufferStorage(JsValue arrayBuffer, out IntPtr buffer,
259259
out uint bufferLength);
260260

261261
[DllImport(DllName)]
@@ -375,7 +375,7 @@ internal static extern JsErrorCode JsRunScript(string script, JsSourceContext so
375375
string sourceUrl, out JsValue result);
376376

377377
[DllImport(DllName, CharSet = CharSet.Unicode)]
378-
internal static extern JsErrorCode JsSerializeScript(string script, byte[] buffer, ref ulong bufferSize);
378+
internal static extern JsErrorCode JsSerializeScript(string script, byte[] buffer, ref uint bufferSize);
379379

380380
[DllImport(DllName)]
381381
internal static extern JsErrorCode JsParseSerializedScriptWithCallback(

0 commit comments

Comments
 (0)