1
1
using System ;
2
+ using System . Runtime . CompilerServices ;
2
3
#if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
3
4
using System . Runtime . InteropServices ;
4
5
#endif
@@ -164,7 +165,8 @@ public static uint Idle()
164
165
public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
165
166
ref JsParseScriptAttributes parseAttributes )
166
167
{
167
- JsExternalByteArrayBuffer scriptBuffer = CreateByteArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
168
+ JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
169
+ scriptValue . AddRef ( ) ;
168
170
169
171
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
170
172
sourceUrlValue . AddRef ( ) ;
@@ -173,13 +175,13 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
173
175
174
176
try
175
177
{
176
- JsErrorCode errorCode = NativeMethods . JsParse ( scriptBuffer . Value , sourceContext , sourceUrlValue ,
178
+ JsErrorCode errorCode = NativeMethods . JsParse ( scriptValue , sourceContext , sourceUrlValue ,
177
179
parseAttributes , out result ) ;
178
180
JsErrorHelpers . ThrowIfError ( errorCode ) ;
179
181
}
180
182
finally
181
183
{
182
- scriptBuffer . Dispose ( ) ;
184
+ scriptValue . Release ( ) ;
183
185
sourceUrlValue . Release ( ) ;
184
186
}
185
187
@@ -204,7 +206,8 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
204
206
public static JsValue ParseSerializedScript ( string script , byte [ ] buffer ,
205
207
JsSerializedLoadScriptCallback scriptLoadCallback , JsSourceContext sourceContext , string sourceUrl )
206
208
{
207
- JsExternalByteArrayBuffer scriptBuffer = JsExternalByteArrayBuffer . FromBytes ( buffer ) ;
209
+ JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
210
+ bufferValue . AddRef ( ) ;
208
211
209
212
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
210
213
sourceUrlValue . AddRef ( ) ;
@@ -213,13 +216,13 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
213
216
214
217
try
215
218
{
216
- JsErrorCode errorCode = NativeMethods . JsParseSerialized ( scriptBuffer . Value , scriptLoadCallback ,
219
+ JsErrorCode errorCode = NativeMethods . JsParseSerialized ( bufferValue , scriptLoadCallback ,
217
220
sourceContext , sourceUrlValue , out result ) ;
218
221
JsErrorHelpers . ThrowIfError ( errorCode ) ;
219
222
}
220
223
finally
221
224
{
222
- scriptBuffer . Dispose ( ) ;
225
+ bufferValue . Release ( ) ;
223
226
sourceUrlValue . Release ( ) ;
224
227
}
225
228
@@ -241,7 +244,8 @@ public static JsValue ParseSerializedScript(string script, byte[] buffer,
241
244
public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceUrl ,
242
245
ref JsParseScriptAttributes parseAttributes )
243
246
{
244
- JsExternalByteArrayBuffer scriptBuffer = CreateByteArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
247
+ JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
248
+ scriptValue . AddRef ( ) ;
245
249
246
250
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
247
251
sourceUrlValue . AddRef ( ) ;
@@ -250,13 +254,13 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
250
254
251
255
try
252
256
{
253
- JsErrorCode errorCode = NativeMethods . JsRun ( scriptBuffer . Value , sourceContext , sourceUrlValue ,
257
+ JsErrorCode errorCode = NativeMethods . JsRun ( scriptValue , sourceContext , sourceUrlValue ,
254
258
parseAttributes , out result ) ;
255
259
JsErrorHelpers . ThrowIfError ( errorCode ) ;
256
260
}
257
261
finally
258
262
{
259
- scriptBuffer . Dispose ( ) ;
263
+ scriptValue . Release ( ) ;
260
264
sourceUrlValue . Release ( ) ;
261
265
}
262
266
@@ -281,7 +285,8 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
281
285
public static JsValue RunSerializedScript ( string script , byte [ ] buffer ,
282
286
JsSerializedLoadScriptCallback scriptLoadCallback , JsSourceContext sourceContext , string sourceUrl )
283
287
{
284
- JsExternalByteArrayBuffer scriptBuffer = JsExternalByteArrayBuffer . FromBytes ( buffer ) ;
288
+ JsValue bufferValue = JsValue . CreateExternalArrayBuffer ( buffer ) ;
289
+ bufferValue . AddRef ( ) ;
285
290
286
291
JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
287
292
sourceUrlValue . AddRef ( ) ;
@@ -290,13 +295,13 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
290
295
291
296
try
292
297
{
293
- JsErrorCode errorCode = NativeMethods . JsRunSerialized ( scriptBuffer . Value , scriptLoadCallback ,
298
+ JsErrorCode errorCode = NativeMethods . JsRunSerialized ( bufferValue , scriptLoadCallback ,
294
299
sourceContext , sourceUrlValue , out result ) ;
295
300
JsErrorHelpers . ThrowIfError ( errorCode ) ;
296
301
}
297
302
finally
298
303
{
299
- scriptBuffer . Dispose ( ) ;
304
+ bufferValue . Release ( ) ;
300
305
sourceUrlValue . Release ( ) ;
301
306
}
302
307
@@ -321,17 +326,19 @@ public static JsValue RunSerializedScript(string script, byte[] buffer,
321
326
/// <returns>The buffer to put the serialized script into</returns>
322
327
public static byte [ ] SerializeScript ( string script , ref JsParseScriptAttributes parseAttributes )
323
328
{
324
- JsExternalByteArrayBuffer scriptBuffer = CreateByteArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
329
+ JsValue scriptValue = CreateExternalArrayBufferFromScriptCode ( script , ref parseAttributes ) ;
330
+ scriptValue . AddRef ( ) ;
331
+
325
332
JsValue bufferValue ;
326
333
327
334
try
328
335
{
329
- JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptBuffer . Value , out bufferValue , parseAttributes ) ;
336
+ JsErrorCode errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , parseAttributes ) ;
330
337
JsErrorHelpers . ThrowIfError ( errorCode ) ;
331
338
}
332
339
finally
333
340
{
334
- scriptBuffer . Dispose ( ) ;
341
+ scriptValue . Release ( ) ;
335
342
}
336
343
337
344
byte [ ] buffer = bufferValue . ArrayBufferBytes ;
@@ -340,12 +347,13 @@ public static byte[] SerializeScript(string script, ref JsParseScriptAttributes
340
347
}
341
348
342
349
/// <summary>
343
- /// Creates a wrapper for the Javascript ArrayBuffer from script code
350
+ /// Creates a Javascript <c> ArrayBuffer</c> object from script code
344
351
/// </summary>
345
352
/// <param name="script">Script code</param>
346
353
/// <param name="parseAttributes">Attribute mask for parsing the script</param>
347
- /// <returns>Instance of wrapper for the Javascript ArrayBuffer</returns>
348
- private static JsExternalByteArrayBuffer CreateByteArrayBufferFromScriptCode ( string script ,
354
+ /// <returns>The new <c>ArrayBuffer</c> object</returns>
355
+ [ MethodImpl ( ( MethodImplOptions ) 256 /* AggressiveInlining */ ) ]
356
+ private static JsValue CreateExternalArrayBufferFromScriptCode ( string script ,
349
357
ref JsParseScriptAttributes parseAttributes )
350
358
{
351
359
Encoding encoding ;
@@ -360,9 +368,9 @@ private static JsExternalByteArrayBuffer CreateByteArrayBufferFromScriptCode(str
360
368
encoding = Encoding . UTF8 ;
361
369
}
362
370
363
- JsExternalByteArrayBuffer scriptBuffer = JsExternalByteArrayBuffer . FromString ( script , encoding ) ;
371
+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( script , encoding ) ;
364
372
365
- return scriptBuffer ;
373
+ return scriptValue ;
366
374
}
367
375
368
376
/// <summary>
0 commit comments