@@ -164,8 +164,7 @@ public static uint Idle()
164164 public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
165165 ref JsParseScriptAttributes parseAttributes )
166166 {
167- JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
168- scriptValue . AddRef ( ) ;
167+ JsPooledArrayBuffer scriptBuffer = CreatePooledArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
169168
170169 JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
171170 sourceUrlValue . AddRef ( ) ;
@@ -174,13 +173,13 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
174173
175174 try
176175 {
177- JsErrorCode errorCode = NativeMethods . JsParse ( scriptValue , sourceContext , sourceUrlValue ,
176+ JsErrorCode errorCode = NativeMethods . JsParse ( scriptBuffer . Value , sourceContext , sourceUrlValue ,
178177 parseAttributes , out result ) ;
179178 JsErrorHelpers . ThrowIfError ( errorCode ) ;
180179 }
181180 finally
182181 {
183- scriptValue . Release ( ) ;
182+ scriptBuffer . Dispose ( ) ;
184183 sourceUrlValue . Release ( ) ;
185184 }
186185
@@ -225,6 +224,8 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
225224 sourceUrlValue . Release ( ) ;
226225 }
227226
227+ GC . KeepAlive ( buffer ) ;
228+
228229 return result ;
229230 }
230231
@@ -243,8 +244,7 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
243244 public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
244245 ref JsParseScriptAttributes parseAttributes )
245246 {
246- JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
247- scriptValue . AddRef ( ) ;
247+ JsPooledArrayBuffer scriptBuffer = CreatePooledArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
248248
249249 JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
250250 sourceUrlValue . AddRef ( ) ;
@@ -253,13 +253,13 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
253253
254254 try
255255 {
256- JsErrorCode errorCode = NativeMethods . JsRun ( scriptValue , sourceContext , sourceUrlValue ,
256+ JsErrorCode errorCode = NativeMethods . JsRun ( scriptBuffer . Value , sourceContext , sourceUrlValue ,
257257 parseAttributes , out result ) ;
258258 JsErrorHelpers . ThrowIfError ( errorCode ) ;
259259 }
260260 finally
261261 {
262- scriptValue . Release ( ) ;
262+ scriptBuffer . Dispose ( ) ;
263263 sourceUrlValue . Release ( ) ;
264264 }
265265
@@ -284,14 +284,14 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
284284 public static JsValue RunSerializedScript ( string script , byte [ ] buffer ,
285285 JsSerializedLoadScriptCallback scriptLoadCallback , JsSourceContext sourceContext , string sourceUrl )
286286 {
287- JsValue result ;
288-
289287 JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
290288 bufferValue . AddRef ( ) ;
291289
292290 JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
293291 sourceUrlValue . AddRef ( ) ;
294292
293+ JsValue result ;
294+
295295 try
296296 {
297297 JsErrorCode errorCode = NativeMethods . JsRunSerialized ( bufferValue , scriptLoadCallback , sourceContext ,
@@ -304,6 +304,8 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
304304 sourceUrlValue . Release ( ) ;
305305 }
306306
307+ GC . KeepAlive ( buffer ) ;
308+
307309 return result ;
308310 }
309311
@@ -325,19 +327,17 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
325327 /// <returns>The buffer to put the serialized script into</returns>
326328 public static byte [ ] SerializeScript ( string script , ref JsParseScriptAttributes parseAttributes )
327329 {
328- JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
329- scriptValue . AddRef ( ) ;
330-
330+ JsPooledArrayBuffer scriptBuffer = CreatePooledArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
331331 JsValue bufferValue ;
332332
333333 try
334334 {
335- JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , parseAttributes ) ;
335+ JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptBuffer . Value , out bufferValue , parseAttributes ) ;
336336 JsErrorHelpers . ThrowIfError ( errorCode ) ;
337337 }
338338 finally
339339 {
340- scriptValue . Release ( ) ;
340+ scriptBuffer . Dispose ( ) ;
341341 }
342342
343343 byte [ ] buffer = bufferValue . ArrayBufferBytes ;
@@ -346,12 +346,12 @@ public static byte[] SerializeScript(string script, ref JsParseScriptAttributes
346346 }
347347
348348 /// <summary>
349- /// Creates a Javascript <c>ArrayBuffer</c> object from script code
349+ /// Creates a pooled Javascript <c>ArrayBuffer</c> object from script code
350350 /// </summary>
351351 /// <param name="script">Script code</param>
352352 /// <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 ,
353+ /// <returns>Instance of <see cref="JsPooledArrayBuffer"/> </returns>
354+ private static JsPooledArrayBuffer CreatePooledArrayBufferFromScriptCode ( string script ,
355355 ref JsParseScriptAttributes parseAttributes )
356356 {
357357 Encoding encoding ;
@@ -366,9 +366,9 @@ private static JsValue CreateExternalArrayBufferFromScriptCode(string script,
366366 encoding = Encoding . UTF8 ;
367367 }
368368
369- JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( script , encoding ) ;
369+ JsPooledArrayBuffer scriptBuffer = JsPooledArrayBuffer . Create ( script , encoding ) ;
370370
371- return scriptValue ;
371+ return scriptBuffer ;
372372 }
373373
374374 /// <summary>
0 commit comments