Ejemplo n.º 1
0
        /// <summary>
        ///     Creates a new <c>Object</c> that stores some external data.
        /// </summary>
        /// <remarks>
        ///     Requires an active script context.
        /// </remarks>
        /// <param name="data">External data that the object will represent. May be null.</param>
        /// <param name="finalizer">
        ///     A callback for when the object is finalized. May be null.
        /// </param>
        /// <returns>The new <c>Object</c>.</returns>
        public static JavaScriptValue CreateExternalObject(IntPtr data, JavaScriptObjectFinalizeCallback finalizer)
        {
            JavaScriptValue reference;

            Native.ThrowIfError(Native.JsCreateExternalObject(data, finalizer, out reference));
            return(reference);
        }
Ejemplo n.º 2
0
        public JsArrayBuffer CreateArrayBufferUtf8(string data)
        {
            if (data == null)
            {
                data = String.Empty;
            }

            JavaScriptValueSafeHandle externalArrayHandle;

            int len = Encoding.UTF8.GetByteCount(data);

            byte[] buffer = new byte[len + 1];
            Encoding.UTF8.GetBytes(data, 0, data.Length, buffer, 0);
            IntPtr ptrData = Marshal.AllocHGlobal(buffer.Length);

            try
            {
                Marshal.Copy(buffer, 0, ptrData, buffer.Length);

                GCHandle freeCallbackHandle = default(GCHandle);
                JavaScriptObjectFinalizeCallback freeCallback = (ptr) =>
                {
                    Marshal.FreeHGlobal(ptrData);
                    if (freeCallbackHandle.IsAllocated)
                    {
                        freeCallbackHandle.Free();
                    }
                };
                freeCallbackHandle = GCHandle.Alloc(freeCallback);

                externalArrayHandle = m_engine.JsCreateExternalArrayBuffer(ptrData, (uint)data.Length, freeCallback, IntPtr.Zero);
            }
            catch (Exception)
            {
                //If anything goes wrong, free the unmanaged memory.
                //This is not a finally as if success, the memory will be freed automagially.
                Marshal.FreeHGlobal(ptrData);
                throw;
            }

            var result = m_valuePool.GetOrAdd(externalArrayHandle, () =>
            {
                return(new JsManagedExternalArrayBuffer(m_engine, Context, externalArrayHandle, ptrData));
            });

            var resultArrayBuffer = result as JsArrayBuffer;

            if (resultArrayBuffer == null)
            {
                throw new InvalidOperationException($"Expected the result object to be a JsArrayBuffer, however the value was {result.GetType()}");
            }

            return((JsArrayBuffer)result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes a script, automatically storing the script value within an ExternalArrayBuffer.
        /// </summary>
        /// <remarks>
        ///     This extension method exists for simplicity.
        /// </remarks>
        /// <param name="jsrt"></param>
        /// <param name="script"></param>
        /// <param name="sourceContext"></param>
        /// <param name="sourceUrl"></param>
        /// <param name="parseAttributes"></param>
        /// <param name="result"></param>
        /// <returns>A JavaScriptValueSafeHandle containing the result.</returns>
        public static JavaScriptValueSafeHandle JsRunScript(this IJavaScriptEngine jsrt, string script, JavaScriptSourceContext sourceContext = default(JavaScriptSourceContext), string sourceUrl = null, JavaScriptParseScriptAttributes parseAttributes = JavaScriptParseScriptAttributes.None)
        {
            var ptrScript = Marshal.StringToHGlobalAnsi(script);

            JavaScriptObjectFinalizeCallback callback = (IntPtr ptr) =>
            {
                //Release pinned string.
                Marshal.FreeHGlobal(ptrScript);
            };

            var scriptHandle = jsrt.JsCreateExternalArrayBuffer(ptrScript, (uint)script.Length, callback, IntPtr.Zero);

            if (sourceContext == default(JavaScriptSourceContext))
            {
                sourceContext = JavaScriptSourceContext.None;
            }

            JavaScriptValueSafeHandle sourceUrlHandle;

            if (string.IsNullOrWhiteSpace(sourceUrl))
            {
                sourceUrl = "[eval code]";
            }

            sourceUrlHandle = jsrt.JsCreateString(EvalSourceUrl, (ulong)EvalSourceUrl.Length);

            if (parseAttributes == default(JavaScriptParseScriptAttributes))
            {
                parseAttributes = JavaScriptParseScriptAttributes.None;
            }

            try
            {
                return(jsrt.JsRun(scriptHandle, sourceContext, sourceUrlHandle, parseAttributes));
            }
            finally
            {
                //Release variables created during this operation.
                sourceUrlHandle.Dispose();
                scriptHandle.Dispose();
            }
        }
Ejemplo n.º 4
0
 internal static extern JavaScriptErrorCode JsCreateExternalObject(IntPtr data, JavaScriptObjectFinalizeCallback finalizeCallback, out JavaScriptValue obj);
Ejemplo n.º 5
0
 internal static extern JavaScriptErrorCode JsCreateExternalObject(IntPtr data, JavaScriptObjectFinalizeCallback finalizeCallback, out JavaScriptValue obj);
Ejemplo n.º 6
0
 internal static extern JavaScriptErrorCode JsCreateExternalArrayBuffer(IntPtr data, uint byteLength, JavaScriptObjectFinalizeCallback finalizeCallback, IntPtr callbackState, out bool result);
 internal static extern JavaScriptErrorCode JsCreateExternalArrayBuffer(IntPtr data, uint byteLength, JavaScriptObjectFinalizeCallback finalizeCallback, IntPtr callbackState, out bool result);
 /// <summary>
 ///     Creates a new <c>Object</c> that stores some external data.
 /// </summary>
 /// <remarks>
 ///     Requires an active script context.
 /// </remarks>
 /// <param name="data">External data that the object will represent. May be null.</param>
 /// <param name="finalizer">
 ///     A callback for when the object is finalized. May be null.
 /// </param>
 /// <returns>The new <c>Object</c>.</returns>
 public static JavaScriptValue CreateExternalObject(IntPtr data, JavaScriptObjectFinalizeCallback finalizer)
 {
     JavaScriptValue reference;
     Native.ThrowIfError(Native.JsCreateExternalObject(data, finalizer, out reference));
     return reference;
 }
Ejemplo n.º 9
0
 public static extern JavaScriptErrorCode JsCreateExternalArrayBuffer(IntPtr data, uint byteLength, JavaScriptObjectFinalizeCallback finalizeCallback, IntPtr callbackState, out JavaScriptValue obj);
Ejemplo n.º 10
0
 /// <summary>
 /// map internal memory to chakracore engine
 /// </summary>
 /// <param name="data">data block</param>
 /// <param name="byteLength">length of data</param>
 /// <param name="finalizeCallback">callback for object disposed in js engine</param>
 /// <param name="callbackState">reference, can be null</param>
 /// <returns></returns>
 public static JavaScriptValue CreateExternalArrayBuffer(IntPtr data, uint byteLength, JavaScriptObjectFinalizeCallback finalizeCallback, IntPtr callbackState)
 {
     Native.ThrowIfError(Native.JsCreateExternalArrayBuffer(data, byteLength, finalizeCallback, callbackState, out JavaScriptValue result));
     return(result);
 }