AddRef() public method

Adds a reference to the object
This only needs to be called on objects that are not going to be stored somewhere on the stack. Calling AddRef ensures that the JavaScript object the value refers to will not be freed until Release is called
public AddRef ( ) : uint
return uint
        /// <summary>
        /// Runs a serialized script
        /// </summary>
        /// <remarks>
        /// <para>Requires an active script context.</para>
        /// <para>The runtime will detach the data from the buffer and hold on to it until all
        /// instances of any functions created from the buffer are garbage collected.</para>
        /// </remarks>
        /// <param name="script">The source code of the serialized script</param>
        /// <param name="buffer">The serialized script</param>
        /// <param name="scriptLoadCallback">Callback to load the source code of the serialized script</param>
        /// <param name="sourceContext">A cookie identifying the script that can be used
        /// by debuggable script contexts</param>
        /// <param name="sourceUrl">The location the script came from</param>
        /// <returns>The result of running the script, if any</returns>
        public static JsValue RunSerializedScript(string script, byte[] buffer,
                                                  JsSerializedLoadScriptCallback scriptLoadCallback, JsSourceContext sourceContext, string sourceUrl)
        {
            JsValue bufferValue = JsValue.CreateExternalArrayBuffer(buffer);

            bufferValue.AddRef();

            JsValue sourceUrlValue = JsValue.FromString(sourceUrl);

            sourceUrlValue.AddRef();

            JsValue result;

            try
            {
                JsErrorCode errorCode = NativeMethods.JsRunSerialized(bufferValue, scriptLoadCallback,
                                                                      sourceContext, sourceUrlValue, out result);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            finally
            {
                bufferValue.Release();
                sourceUrlValue.Release();
            }

            return(result);
        }
        /// <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);
        }
        /// <summary>
        /// Executes a script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to run</param>
        /// <param name="sourceContext">A cookie identifying the script that can be used
        /// by debuggable script contexts</param>
        /// <param name="sourceUrl">The location the script came from</param>
        /// <param name="parseAttributes">Attribute mask for parsing the script</param>
        /// <returns>The result of the script, if any</returns>
        public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceUrl,
                                        ref JsParseScriptAttributes parseAttributes)
        {
            JsValue scriptValue = JsValue.FromString(script);

            scriptValue.AddRef();

            JsValue sourceUrlValue = JsValue.FromString(sourceUrl);

            sourceUrlValue.AddRef();

            JsValue result;

            try
            {
                JsErrorCode errorCode = NativeMethods.JsRun(scriptValue, sourceContext, sourceUrlValue,
                                                            parseAttributes, out result);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            finally
            {
                scriptValue.Release();
                sourceUrlValue.Release();
            }

            return(result);
        }
        /// <summary>
        /// Executes a script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to run</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The result of the script, if any</returns>
        public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceName)
        {
            JsValue     result;
            JsErrorCode errorCode;

            if (Utils.IsWindows())
            {
                errorCode = NativeMethods.JsRunScript(script, sourceContext, sourceName, out result);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            else
            {
                JsValue scriptValue = JsValue.FromString(script);
                scriptValue.AddRef();

                JsValue sourceUrlValue = JsValue.FromString(sourceName);
                sourceUrlValue.AddRef();

                try
                {
                    errorCode = NativeMethods.JsRun(scriptValue, sourceContext, sourceUrlValue,
                                                    JsParseScriptAttributes.None, out result);
                    JsErrorHelpers.ThrowIfError(errorCode);
                }
                finally
                {
                    scriptValue.Release();
                    sourceUrlValue.Release();
                }
            }

            return(result);
        }
        /// <summary>
        /// Serializes a parsed script to a buffer than can be reused
        /// </summary>
        /// <remarks>
        /// <para>
        /// <c>SerializeScript</c> 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="parseAttributes">Attribute mask for parsing the script</param>
        /// <returns>The buffer to put the serialized script into</returns>
        public static byte[] SerializeScript(string script, ref JsParseScriptAttributes parseAttributes)
        {
            JsValue scriptValue = JsValue.FromString(script);

            scriptValue.AddRef();

            JsValue bufferValue;

            try
            {
                JsErrorCode errorCode = NativeMethods.JsSerialize(scriptValue, out bufferValue, parseAttributes);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            finally
            {
                scriptValue.Release();
            }

            byte[] buffer = bufferValue.ArrayBufferBytes;

            return(buffer);
        }