@@ -159,37 +159,29 @@ public static uint Idle()
159159 /// <param name="sourceContext">A cookie identifying the script that can be used
160160 /// by debuggable script contexts</param>
161161 /// <param name="sourceUrl">The location the script came from</param>
162+ /// <param name="parseAttributes">Attribute mask for parsing the script</param>
162163 /// <returns>A function representing the script code</returns>
163- public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceUrl )
164+ public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
165+ ref JsParseScriptAttributes parseAttributes )
164166 {
167+ JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
168+ scriptValue . AddRef ( ) ;
169+
170+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
171+ sourceUrlValue . AddRef ( ) ;
172+
165173 JsValue result ;
166- JsErrorCode errorCode ;
167174
168- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
175+ try
169176 {
170- errorCode = NativeMethods . JsParseScript ( script , sourceContext , sourceUrl , out result ) ;
177+ JsErrorCode errorCode = NativeMethods . JsParse ( scriptValue , sourceContext , sourceUrlValue ,
178+ parseAttributes , out result ) ;
171179 JsErrorHelpers . ThrowIfError ( errorCode ) ;
172180 }
173- else
181+ finally
174182 {
175- byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
176- JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
177- scriptValue . AddRef ( ) ;
178-
179- JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
180- sourceUrlValue . AddRef ( ) ;
181-
182- try
183- {
184- errorCode = NativeMethods . JsParse ( scriptValue , sourceContext , sourceUrlValue ,
185- JsParseScriptAttributes . None , out result ) ;
186- JsErrorHelpers . ThrowIfError ( errorCode ) ;
187- }
188- finally
189- {
190- scriptValue . Release ( ) ;
191- sourceUrlValue . Release ( ) ;
192- }
183+ scriptValue . Release ( ) ;
184+ sourceUrlValue . Release ( ) ;
193185 }
194186
195187 return result ;
@@ -213,33 +205,24 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
213205 public static JsValue ParseSerializedScript ( string script , byte [ ] buffer ,
214206 JsSerializedLoadScriptCallback scriptLoadCallback , JsSourceContext sourceContext , string sourceUrl )
215207 {
208+ JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
209+ bufferValue . AddRef ( ) ;
210+
211+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
212+ sourceUrlValue . AddRef ( ) ;
213+
216214 JsValue result ;
217- JsErrorCode errorCode ;
218215
219- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
216+ try
220217 {
221- errorCode = NativeMethods . JsParseSerializedScript ( script , buffer , sourceContext , sourceUrl , out result ) ;
218+ JsErrorCode errorCode = NativeMethods . JsParseSerialized ( bufferValue , scriptLoadCallback , sourceContext ,
219+ sourceUrlValue , out result ) ;
222220 JsErrorHelpers . ThrowIfError ( errorCode ) ;
223221 }
224- else
222+ finally
225223 {
226- JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
227- bufferValue . AddRef ( ) ;
228-
229- JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
230- sourceUrlValue . AddRef ( ) ;
231-
232- try
233- {
234- errorCode = NativeMethods . JsParseSerialized ( bufferValue , scriptLoadCallback , sourceContext ,
235- sourceUrlValue , out result ) ;
236- JsErrorHelpers . ThrowIfError ( errorCode ) ;
237- }
238- finally
239- {
240- bufferValue . Release ( ) ;
241- sourceUrlValue . Release ( ) ;
242- }
224+ bufferValue . Release ( ) ;
225+ sourceUrlValue . Release ( ) ;
243226 }
244227
245228 return result ;
@@ -255,37 +238,29 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
255238 /// <param name="sourceContext">A cookie identifying the script that can be used
256239 /// by debuggable script contexts</param>
257240 /// <param name="sourceUrl">The location the script came from</param>
241+ /// <param name="parseAttributes">Attribute mask for parsing the script</param>
258242 /// <returns>The result of the script, if any</returns>
259- public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceUrl )
243+ public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
244+ ref JsParseScriptAttributes parseAttributes )
260245 {
246+ JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
247+ scriptValue . AddRef ( ) ;
248+
249+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
250+ sourceUrlValue . AddRef ( ) ;
251+
261252 JsValue result ;
262- JsErrorCode errorCode ;
263253
264- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
254+ try
265255 {
266- errorCode = NativeMethods . JsRunScript ( script , sourceContext , sourceUrl , out result ) ;
256+ JsErrorCode errorCode = NativeMethods . JsRun ( scriptValue , sourceContext , sourceUrlValue ,
257+ parseAttributes , out result ) ;
267258 JsErrorHelpers . ThrowIfError ( errorCode ) ;
268259 }
269- else
260+ finally
270261 {
271- byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
272- JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
273- scriptValue . AddRef ( ) ;
274-
275- JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
276- sourceUrlValue . AddRef ( ) ;
277-
278- try
279- {
280- errorCode = NativeMethods . JsRun ( scriptValue , sourceContext , sourceUrlValue ,
281- JsParseScriptAttributes . None , out result ) ;
282- JsErrorHelpers . ThrowIfError ( errorCode ) ;
283- }
284- finally
285- {
286- scriptValue . Release ( ) ;
287- sourceUrlValue . Release ( ) ;
288- }
262+ scriptValue . Release ( ) ;
263+ sourceUrlValue . Release ( ) ;
289264 }
290265
291266 return result ;
@@ -310,32 +285,23 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
310285 JsSerializedLoadScriptCallback scriptLoadCallback , JsSourceContext sourceContext , string sourceUrl )
311286 {
312287 JsValue result ;
313- JsErrorCode errorCode ;
314288
315- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
289+ JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
290+ bufferValue . AddRef ( ) ;
291+
292+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
293+ sourceUrlValue . AddRef ( ) ;
294+
295+ try
316296 {
317- errorCode = NativeMethods . JsRunSerializedScript ( script , buffer , sourceContext , sourceUrl , out result ) ;
297+ JsErrorCode errorCode = NativeMethods . JsRunSerialized ( bufferValue , scriptLoadCallback , sourceContext ,
298+ sourceUrlValue , out result ) ;
318299 JsErrorHelpers . ThrowIfError ( errorCode ) ;
319300 }
320- else
301+ finally
321302 {
322- JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
323- bufferValue . AddRef ( ) ;
324-
325- JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
326- sourceUrlValue . AddRef ( ) ;
327-
328- try
329- {
330- errorCode = NativeMethods . JsRunSerialized ( bufferValue , scriptLoadCallback , sourceContext ,
331- sourceUrlValue , out result ) ;
332- JsErrorHelpers . ThrowIfError ( errorCode ) ;
333- }
334- finally
335- {
336- bufferValue . Release ( ) ;
337- sourceUrlValue . Release ( ) ;
338- }
303+ bufferValue . Release ( ) ;
304+ sourceUrlValue . Release ( ) ;
339305 }
340306
341307 return result ;
@@ -355,47 +321,55 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
355321 /// </para>
356322 /// </remarks>
357323 /// <param name="script">The script to serialize</param>
324+ /// <param name="parseAttributes">Attribute mask for parsing the script</param>
358325 /// <returns>The buffer to put the serialized script into</returns>
359- public static byte [ ] SerializeScript ( string script )
326+ public static byte [ ] SerializeScript ( string script , ref JsParseScriptAttributes parseAttributes )
360327 {
361- byte [ ] buffer ;
362- JsErrorCode errorCode ;
328+ JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
329+ scriptValue . AddRef ( ) ;
363330
364- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
365- {
366- buffer = null ;
367- uint bufferSize = 0 ;
331+ JsValue bufferValue ;
368332
369- errorCode = NativeMethods . JsSerializeScript ( script , buffer , ref bufferSize ) ;
333+ try
334+ {
335+ JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , parseAttributes ) ;
370336 JsErrorHelpers . ThrowIfError ( errorCode ) ;
337+ }
338+ finally
339+ {
340+ scriptValue . Release ( ) ;
341+ }
371342
372- buffer = new byte [ ( int ) bufferSize ] ;
343+ byte [ ] buffer = bufferValue . ArrayBufferBytes ;
373344
374- errorCode = NativeMethods . JsSerializeScript ( script , buffer , ref bufferSize ) ;
375- JsErrorHelpers . ThrowIfError ( errorCode ) ;
345+ return buffer ;
346+ }
347+
348+ /// <summary>
349+ /// Creates a Javascript <c>ArrayBuffer</c> object from script code
350+ /// </summary>
351+ /// <param name="script">Script code</param>
352+ /// <param name="parseAttributes">Attribute mask for parsing the script</param>
353+ /// <returns>The new <c>ArrayBuffer</c> object</returns>
354+ private static JsValue CreateExternalArrayBufferFromScriptCode ( string script ,
355+ ref JsParseScriptAttributes parseAttributes )
356+ {
357+ Encoding encoding ;
358+
359+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
360+ {
361+ encoding = Encoding . Unicode ;
362+ parseAttributes |= JsParseScriptAttributes . ArrayBufferIsUtf16Encoded ;
376363 }
377364 else
378365 {
379- byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
380- JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
381- scriptValue . AddRef ( ) ;
382-
383- JsValue bufferValue ;
384-
385- try
386- {
387- errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , JsParseScriptAttributes . None ) ;
388- JsErrorHelpers . ThrowIfError ( errorCode ) ;
389- }
390- finally
391- {
392- scriptValue . Release ( ) ;
393- }
394-
395- buffer = bufferValue . ArrayBufferBytes ;
366+ encoding = Encoding . UTF8 ;
396367 }
397368
398- return buffer ;
369+ byte [ ] scriptBytes = encoding . GetBytes ( script ) ;
370+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
371+
372+ return scriptValue ;
399373 }
400374
401375 /// <summary>
0 commit comments