11using System ;
2+ using System . Text ;
23
34using JavaScriptEngineSwitcher . Core . Utilities ;
45
@@ -144,32 +145,33 @@ public static uint Idle()
144145 }
145146
146147 /// <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
148149 /// </summary>
149150 /// <remarks>
150151 /// Requires an active script context.
151152 /// </remarks>
152153 /// <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 )
158159 {
159160 JsValue result ;
160161 JsErrorCode errorCode ;
161162
162163 if ( Utils . IsWindows ( ) )
163164 {
164- errorCode = NativeMethods . JsParseScript ( script , sourceContext , sourceName , out result ) ;
165+ errorCode = NativeMethods . JsParseScript ( script , sourceContext , sourceUrl , out result ) ;
165166 JsErrorHelpers . ThrowIfError ( errorCode ) ;
166167 }
167168 else
168169 {
169- JsValue scriptValue = JsValue . FromString ( script ) ;
170+ byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
171+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
170172 scriptValue . AddRef ( ) ;
171173
172- JsValue sourceUrlValue = JsValue . FromString ( sourceName ) ;
174+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
173175 sourceUrlValue . AddRef ( ) ;
174176
175177 try
@@ -188,46 +190,34 @@ public static JsValue ParseScript(string script, JsSourceContext sourceContext,
188190 return result ;
189191 }
190192
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-
204193 /// <summary>
205194 /// Executes a script
206195 /// </summary>
207196 /// <remarks>
208197 /// Requires an active script context.
209198 /// </remarks>
210199 /// <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>
214203 /// <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 )
216205 {
217206 JsValue result ;
218207 JsErrorCode errorCode ;
219208
220209 if ( Utils . IsWindows ( ) )
221210 {
222- errorCode = NativeMethods . JsRunScript ( script , sourceContext , sourceName , out result ) ;
211+ errorCode = NativeMethods . JsRunScript ( script , sourceContext , sourceUrl , out result ) ;
223212 JsErrorHelpers . ThrowIfError ( errorCode ) ;
224213 }
225214 else
226215 {
227- JsValue scriptValue = JsValue . FromString ( script ) ;
216+ byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
217+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
228218 scriptValue . AddRef ( ) ;
229219
230- JsValue sourceUrlValue = JsValue . FromString ( sourceName ) ;
220+ JsValue sourceUrlValue = JsValue . FromString ( sourceUrl ) ;
231221 sourceUrlValue . AddRef ( ) ;
232222
233223 try
@@ -246,25 +236,12 @@ public static JsValue RunScript(string script, JsSourceContext sourceContext, st
246236 return result ;
247237 }
248238
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-
262239 /// <summary>
263240 /// Serializes a parsed script to a buffer than can be reused
264241 /// </summary>
265242 /// <remarks>
266243 /// <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
268245 /// runtime-independent format. The serialized script then can be deserialized in any
269246 /// runtime without requiring the script to be re-parsed.
270247 /// </para>
@@ -273,41 +250,47 @@ public static JsValue RunScript(string script)
273250 /// </para>
274251 /// </remarks>
275252 /// <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 )
279255 {
280- var bufferSize = ( ulong ) buffer . Length ;
256+ byte [ ] buffer ;
281257 JsErrorCode errorCode ;
282258
283259 if ( Utils . IsWindows ( ) )
284260 {
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+
285269 errorCode = NativeMethods . JsSerializeScript ( script , buffer , ref bufferSize ) ;
286270 JsErrorHelpers . ThrowIfError ( errorCode ) ;
287271 }
288272 else
289273 {
290- JsValue scriptValue = JsValue . FromString ( script ) ;
274+ byte [ ] scriptBytes = Encoding . GetEncoding ( 0 ) . GetBytes ( script ) ;
275+ JsValue scriptValue = JsValue . CreateExternalArrayBuffer ( scriptBytes ) ;
291276 scriptValue . AddRef ( ) ;
292277
293278 JsValue bufferValue ;
294279
295280 try
296281 {
297- errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue ,
298- JsParseScriptAttributes . None ) ;
282+ errorCode = NativeMethods . JsSerialize ( scriptValue , out bufferValue , JsParseScriptAttributes . None ) ;
299283 JsErrorHelpers . ThrowIfError ( errorCode ) ;
300284 }
301285 finally
302286 {
303287 scriptValue . Release ( ) ;
304288 }
305289
306- JsValue lengthValue = bufferValue . GetProperty ( "length" ) ;
307- bufferSize = Convert . ToUInt64 ( lengthValue . ConvertToNumber ( ) . ToDouble ( ) ) ;
290+ buffer = bufferValue . ArrayBufferBytes ;
308291 }
309292
310- return bufferSize ;
293+ return buffer ;
311294 }
312295
313296 /// <summary>
0 commit comments