Skip to content

Commit 509ccc3

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore in some parts of the code now are used a Javascript String values instead of the ArrayBuffer objects
1 parent 2df240e commit 509ccc3

File tree

2 files changed

+3
-78
lines changed

2 files changed

+3
-78
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsContext.cs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
using System;
2-
using System.Runtime.CompilerServices;
3-
#if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
4-
using System.Runtime.InteropServices;
5-
#endif
6-
using System.Text;
7-
#if NET40
8-
9-
using PolyfillsForOldDotNet.System.Runtime.InteropServices;
10-
#endif
112

123
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
134
{
@@ -165,7 +156,7 @@ public static uint Idle()
165156
public static JsValue ParseScript(string script, JsSourceContext sourceContext, string sourceUrl,
166157
ref JsParseScriptAttributes parseAttributes)
167158
{
168-
JsValue scriptValue = CreateExternalArrayBufferFromScriptCode(script, ref parseAttributes);
159+
JsValue scriptValue = JsValue.FromString(script);
169160
scriptValue.AddRef();
170161

171162
JsValue sourceUrlValue = JsValue.FromString(sourceUrl);
@@ -244,7 +235,7 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
244235
public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceUrl,
245236
ref JsParseScriptAttributes parseAttributes)
246237
{
247-
JsValue scriptValue = CreateExternalArrayBufferFromScriptCode(script, ref parseAttributes);
238+
JsValue scriptValue = JsValue.FromString(script);
248239
scriptValue.AddRef();
249240

250241
JsValue sourceUrlValue = JsValue.FromString(sourceUrl);
@@ -326,7 +317,7 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
326317
/// <returns>The buffer to put the serialized script into</returns>
327318
public static byte[] SerializeScript(string script, ref JsParseScriptAttributes parseAttributes)
328319
{
329-
JsValue scriptValue = CreateExternalArrayBufferFromScriptCode(script, ref parseAttributes);
320+
JsValue scriptValue = JsValue.FromString(script);
330321
scriptValue.AddRef();
331322

332323
JsValue bufferValue;
@@ -346,33 +337,6 @@ public static byte[] SerializeScript(string script, ref JsParseScriptAttributes
346337
return buffer;
347338
}
348339

349-
/// <summary>
350-
/// Creates a Javascript <c>ArrayBuffer</c> object from script code
351-
/// </summary>
352-
/// <param name="script">Script code</param>
353-
/// <param name="parseAttributes">Attribute mask for parsing the script</param>
354-
/// <returns>The new <c>ArrayBuffer</c> object</returns>
355-
[MethodImpl((MethodImplOptions)256 /* AggressiveInlining */)]
356-
private static JsValue CreateExternalArrayBufferFromScriptCode(string script,
357-
ref JsParseScriptAttributes parseAttributes)
358-
{
359-
Encoding encoding;
360-
361-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
362-
{
363-
encoding = Encoding.Unicode;
364-
parseAttributes |= JsParseScriptAttributes.ArrayBufferIsUtf16Encoded;
365-
}
366-
else
367-
{
368-
encoding = Encoding.UTF8;
369-
}
370-
371-
JsValue scriptValue = JsValue.CreateExternalArrayBuffer(script, encoding);
372-
373-
return scriptValue;
374-
}
375-
376340
/// <summary>
377341
/// Returns a exception that caused the runtime of the current context to be in the
378342
/// exception state and resets the exception state for that runtime

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsValue.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -469,45 +469,6 @@ public static JsValue CreateExternalArrayBuffer(byte[] buffer)
469469
return reference;
470470
}
471471

472-
/// <summary>
473-
/// Creates a Javascript <c>ArrayBuffer</c> object to access external memory
474-
/// </summary>
475-
/// <remarks>Requires an active script context.</remarks>
476-
/// <param name="value">String value</param>
477-
/// <param name="encoding">Character encoding</param>
478-
/// <returns>The new <c>ArrayBuffer</c> object</returns>
479-
public static unsafe JsValue CreateExternalArrayBuffer(string value, Encoding encoding)
480-
{
481-
if (value == null)
482-
{
483-
throw new ArgumentNullException(nameof(value));
484-
}
485-
486-
if (encoding == null)
487-
{
488-
throw new ArgumentNullException(nameof(encoding));
489-
}
490-
491-
int valueLength = value.Length;
492-
int bufferLength = encoding.GetByteCount(value);
493-
IntPtr bufferPtr = Marshal.AllocHGlobal(bufferLength + 1);
494-
495-
fixed (char* pValue = value)
496-
{
497-
var pBuffer = (byte*)bufferPtr;
498-
pBuffer[bufferLength] = 0;
499-
500-
encoding.GetBytes(pValue, valueLength, pBuffer, bufferLength);
501-
}
502-
503-
JsValue reference;
504-
JsErrorCode errorCode = NativeMethods.JsCreateExternalArrayBuffer(bufferPtr, (uint)bufferLength,
505-
DefaultExternalBufferFinalizeCallback.Instance, bufferPtr, out reference);
506-
JsErrorHelpers.ThrowIfError(errorCode);
507-
508-
return reference;
509-
}
510-
511472
/// <summary>
512473
/// Creates a new JavaScript error object
513474
/// </summary>

0 commit comments

Comments
 (0)