/// <summary> /// Serializes a parsed script to a buffer than can be reused /// </summary> /// <remarks> /// <para> /// SerializeScript parses a script and then stores the parsed form of the script in a /// runtime-independent format. The serialized script then can be deserialized in any /// runtime without requiring the script to be re-parsed. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <param name="script">The script to serialize</param> /// <param name="buffer">The buffer to put the serialized script into. Can be null</param> /// <returns>The size of the buffer, in bytes, required to hold the serialized script</returns> public static ulong SerializeScript(string script, byte[] buffer) { var bufferSize = (ulong)buffer.Length; JsErrorCode errorCode; if (Utils.IsWindows()) { errorCode = NativeMethods.JsSerializeScript(script, buffer, ref bufferSize); JsErrorHelpers.ThrowIfError(errorCode); } else { JsValue scriptValue = JsValue.FromString(script); scriptValue.AddRef(); JsValue bufferValue; try { errorCode = NativeMethods.JsSerialize(scriptValue, out bufferValue, JsParseScriptAttributes.None); JsErrorHelpers.ThrowIfError(errorCode); } finally { scriptValue.Release(); } JsValue lengthValue = bufferValue.GetProperty("length"); bufferSize = Convert.ToUInt64(lengthValue.ConvertToNumber().ToDouble()); } return(bufferSize); }