@@ -164,8 +164,7 @@ public static uint Idle()
164
164
public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
165
165
ref JsParseScriptAttributes parseAttributes )
166
166
{
167
- JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
168
- scriptValue . AddRef ( ) ;
167
+ JsPooledArrayBuffer scriptBuffer = CreatePooledArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
169
168
170
169
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
171
170
sourceUrlValue . AddRef ( ) ;
@@ -174,13 +173,13 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
174
173
175
174
try
176
175
{
177
- JsErrorCode errorCode = NativeMethods . JsParse ( scriptValue , sourceContext , sourceUrlValue ,
176
+ JsErrorCode errorCode = NativeMethods . JsParse ( scriptBuffer . Value , sourceContext , sourceUrlValue ,
178
177
parseAttributes , out result ) ;
179
178
JsErrorHelpers . ThrowIfError ( errorCode ) ;
180
179
}
181
180
finally
182
181
{
183
- scriptValue . Release ( ) ;
182
+ scriptBuffer . Dispose ( ) ;
184
183
sourceUrlValue . Release ( ) ;
185
184
}
186
185
@@ -225,6 +224,8 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
225
224
sourceUrlValue . Release ( ) ;
226
225
}
227
226
227
+ GC . KeepAlive ( buffer ) ;
228
+
228
229
return result ;
229
230
}
230
231
@@ -243,8 +244,7 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
243
244
public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
244
245
ref JsParseScriptAttributes parseAttributes )
245
246
{
246
- JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
247
- scriptValue . AddRef ( ) ;
247
+ JsPooledArrayBuffer scriptBuffer = CreatePooledArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
248
248
249
249
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
250
250
sourceUrlValue . AddRef ( ) ;
@@ -253,13 +253,13 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
253
253
254
254
try
255
255
{
256
- JsErrorCode errorCode = NativeMethods . JsRun ( scriptValue , sourceContext , sourceUrlValue ,
256
+ JsErrorCode errorCode = NativeMethods . JsRun ( scriptBuffer . Value , sourceContext , sourceUrlValue ,
257
257
parseAttributes , out result ) ;
258
258
JsErrorHelpers . ThrowIfError ( errorCode ) ;
259
259
}
260
260
finally
261
261
{
262
- scriptValue . Release ( ) ;
262
+ scriptBuffer . Dispose ( ) ;
263
263
sourceUrlValue . Release ( ) ;
264
264
}
265
265
@@ -284,14 +284,14 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
284
284
public static JsValue RunSerializedScript ( string script , byte [ ] buffer ,
285
285
JsSerializedLoadScriptCallback scriptLoadCallback , JsSourceContext sourceContext , string sourceUrl )
286
286
{
287
- JsValue result ;
288
-
289
287
JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
290
288
bufferValue . AddRef ( ) ;
291
289
292
290
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
293
291
sourceUrlValue . AddRef ( ) ;
294
292
293
+ JsValue result ;
294
+
295
295
try
296
296
{
297
297
JsErrorCode errorCode = NativeMethods . JsRunSerialized ( bufferValue , scriptLoadCallback , sourceContext ,
@@ -304,6 +304,8 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
304
304
sourceUrlValue . Release ( ) ;
305
305
}
306
306
307
+ GC . KeepAlive ( buffer ) ;
308
+
307
309
return result ;
308
310
}
309
311
@@ -325,19 +327,17 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
325
327
/// <returns>The buffer to put the serialized script into</returns>
326
328
public static byte [ ] SerializeScript ( string script , ref JsParseScriptAttributes parseAttributes )
327
329
{
328
- JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
329
- scriptValue . AddRef ( ) ;
330
-
330
+ JsPooledArrayBuffer scriptBuffer = CreatePooledArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
331
331
JsValue bufferValue ;
332
332
333
333
try
334
334
{
335
- JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , parseAttributes ) ;
335
+ JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptBuffer . Value , out bufferValue , parseAttributes ) ;
336
336
JsErrorHelpers . ThrowIfError ( errorCode ) ;
337
337
}
338
338
finally
339
339
{
340
- scriptValue . Release ( ) ;
340
+ scriptBuffer . Dispose ( ) ;
341
341
}
342
342
343
343
byte [ ] buffer = bufferValue . ArrayBufferBytes ;
@@ -346,12 +346,12 @@ public static byte[] SerializeScript(string script, ref JsParseScriptAttributes
346
346
}
347
347
348
348
/// <summary>
349
- /// Creates a Javascript <c>ArrayBuffer</c> object from script code
349
+ /// Creates a pooled Javascript <c>ArrayBuffer</c> object from script code
350
350
/// </summary>
351
351
/// <param name="script">Script code</param>
352
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 ,
353
+ /// <returns>Instance of <see cref="JsPooledArrayBuffer"/> </returns>
354
+ private static JsPooledArrayBuffer CreatePooledArrayBufferFromScriptCode ( string script ,
355
355
ref JsParseScriptAttributes parseAttributes )
356
356
{
357
357
Encoding encoding ;
@@ -366,9 +366,9 @@ private static JsValue CreateExternalArrayBufferFromScriptCode(string script,
366
366
encoding = Encoding . UTF8 ;
367
367
}
368
368
369
- JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( script , encoding ) ;
369
+ JsPooledArrayBuffer scriptBuffer = JsPooledArrayBuffer . Create ( script , encoding ) ;
370
370
371
- return scriptValue ;
371
+ return scriptBuffer ;
372
372
}
373
373
374
374
/// <summary>
0 commit comments