1
1
using System ;
2
+ using System . Text ;
2
3
3
4
using JavaScriptEngineSwitcher . Core . Utilities ;
4
5
@@ -144,32 +145,33 @@ public static uint Idle()
144
145
}
145
146
146
147
/// <summary>
147
- /// Parses a script and returns a <c>Function</c> representing the script
148
+ /// Parses a script and returns a function representing the script
148
149
/// </summary>
149
150
/// <remarks>
150
151
/// Requires an active script context.
151
152
/// </remarks>
152
153
/// <param name="script">The script to parse</param>
153
- /// <param name="sourceContext">The cookie identifying the script that can be used
154
- /// by script contexts that have debugging enabled </param>
155
- /// <param name="sourceName ">The location the script came from</param>
156
- /// <returns>The <c>Function</c> representing the script code</returns>
157
- public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceName )
154
+ /// <param name="sourceContext">A cookie identifying the script that can be used
155
+ /// by debuggable script contexts</param>
156
+ /// <param name="sourceUrl ">The location the script came from</param>
157
+ /// <returns>A function representing the script code</returns>
158
+ public static JsValue ParseScript ( string script , JsSourceContext sourceContext , string sourceUrl )
158
159
{
159
160
JsValue result ;
160
161
JsErrorCode errorCode ;
161
162
162
163
if ( Utils . IsWindows ( ) )
163
164
{
164
- errorCode = NativeMethods . JsParseScript ( script , sourceContext , sourceName , out result ) ;
165
+ errorCode = NativeMethods . JsParseScript ( script , sourceContext , sourceUrl , out result ) ;
165
166
JsErrorHelpers . ThrowIfError ( errorCode ) ;
166
167
}
167
168
else
168
169
{
169
- JsValue scriptValue = JsValue . FromString ( script ) ;
170
+ byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
171
+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
170
172
scriptValue . AddRef ( ) ;
171
173
172
- JsValue sourceUrlValue = JsValue . FromString ( sourceName ) ;
174
+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
173
175
sourceUrlValue . AddRef ( ) ;
174
176
175
177
try
@@ -188,46 +190,34 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
188
190
return result ;
189
191
}
190
192
191
- /// <summary>
192
- /// Parses a script and returns a <c>Function</c> representing the script
193
- /// </summary>
194
- /// <remarks>
195
- /// Requires an active script context.
196
- /// </remarks>
197
- /// <param name="script">The script to parse</param>
198
- /// <returns>The <c>Function</c> representing the script code</returns>
199
- public static JsValue ParseScript ( string script )
200
- {
201
- return ParseScript ( script , JsSourceContext . None , string . Empty ) ;
202
- }
203
-
204
193
/// <summary>
205
194
/// Executes a script
206
195
/// </summary>
207
196
/// <remarks>
208
197
/// Requires an active script context.
209
198
/// </remarks>
210
199
/// <param name="script">The script to run</param>
211
- /// <param name="sourceContext">The cookie identifying the script that can be used
212
- /// by script contexts that have debugging enabled </param>
213
- /// <param name="sourceName ">The location the script came from</param>
200
+ /// <param name="sourceContext">A cookie identifying the script that can be used
201
+ /// by debuggable script contexts</param>
202
+ /// <param name="sourceUrl ">The location the script came from</param>
214
203
/// <returns>The result of the script, if any</returns>
215
- public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceName )
204
+ public static JsValue RunScript ( string script , JsSourceContext sourceContext , string sourceUrl )
216
205
{
217
206
JsValue result ;
218
207
JsErrorCode errorCode ;
219
208
220
209
if ( Utils . IsWindows ( ) )
221
210
{
222
- errorCode = NativeMethods . JsRunScript ( script , sourceContext , sourceName , out result ) ;
211
+ errorCode = NativeMethods . JsRunScript ( script , sourceContext , sourceUrl , out result ) ;
223
212
JsErrorHelpers . ThrowIfError ( errorCode ) ;
224
213
}
225
214
else
226
215
{
227
- JsValue scriptValue = JsValue . FromString ( script ) ;
216
+ byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
217
+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
228
218
scriptValue . AddRef ( ) ;
229
219
230
- JsValue sourceUrlValue = JsValue . FromString ( sourceName ) ;
220
+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
231
221
sourceUrlValue . AddRef ( ) ;
232
222
233
223
try
@@ -246,25 +236,12 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
246
236
return result ;
247
237
}
248
238
249
- /// <summary>
250
- /// Executes a script
251
- /// </summary>
252
- /// <remarks>
253
- /// Requires an active script context
254
- /// </remarks>
255
- /// <param name="script">The script to run</param>
256
- /// <returns>The result of the script, if any</returns>
257
- public static JsValue RunScript ( string script )
258
- {
259
- return RunScript ( script , JsSourceContext . None , string . Empty ) ;
260
- }
261
-
262
239
/// <summary>
263
240
/// Serializes a parsed script to a buffer than can be reused
264
241
/// </summary>
265
242
/// <remarks>
266
243
/// <para>
267
- /// SerializeScript parses a script and then stores the parsed form of the script in a
244
+ /// <c> SerializeScript</c> parses a script and then stores the parsed form of the script in a
268
245
/// runtime-independent format. The serialized script then can be deserialized in any
269
246
/// runtime without requiring the script to be re-parsed.
270
247
/// </para>
@@ -273,41 +250,47 @@ public static JsValue RunScript(string script)
273
250
/// </para>
274
251
/// </remarks>
275
252
/// <param name="script">The script to serialize</param>
276
- /// <param name="buffer">The buffer to put the serialized script into. Can be null</param>
277
- /// <returns>The size of the buffer, in bytes, required to hold the serialized script</returns>
278
- public static ulong SerializeScript ( string script , byte [ ] buffer )
253
+ /// <returns>The buffer to put the serialized script into</returns>
254
+ public static byte [ ] SerializeScript ( string script )
279
255
{
280
- var bufferSize = ( ulong ) buffer . Length ;
256
+ byte [ ] buffer ;
281
257
JsErrorCode errorCode ;
282
258
283
259
if ( Utils . IsWindows ( ) )
284
260
{
261
+ buffer = null ;
262
+ uint bufferSize = 0 ;
263
+
264
+ errorCode = NativeMethods . JsSerializeScript ( script , buffer , ref bufferSize ) ;
265
+ JsErrorHelpers . ThrowIfError ( errorCode ) ;
266
+
267
+ buffer = new byte [ ( int ) bufferSize ] ;
268
+
285
269
errorCode = NativeMethods . JsSerializeScript ( script , buffer , ref bufferSize ) ;
286
270
JsErrorHelpers . ThrowIfError ( errorCode ) ;
287
271
}
288
272
else
289
273
{
290
- JsValue scriptValue = JsValue . FromString ( script ) ;
274
+ byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
275
+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
291
276
scriptValue . AddRef ( ) ;
292
277
293
278
JsValue bufferValue ;
294
279
295
280
try
296
281
{
297
- errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue ,
298
- JsParseScriptAttributes . None ) ;
282
+ errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , JsParseScriptAttributes . None ) ;
299
283
JsErrorHelpers . ThrowIfError ( errorCode ) ;
300
284
}
301
285
finally
302
286
{
303
287
scriptValue . Release ( ) ;
304
288
}
305
289
306
- JsValue lengthValue = bufferValue . GetProperty ( "length" ) ;
307
- bufferSize = Convert . ToUInt64 ( lengthValue . ConvertToNumber ( ) . ToDouble ( ) ) ;
290
+ buffer = bufferValue . ArrayBufferBytes ;
308
291
}
309
292
310
- return bufferSize ;
293
+ return buffer ;
311
294
}
312
295
313
296
/// <summary>
0 commit comments