Ejemplo n.º 1
0
        protected override void Dispose(bool disposing)
        {
            bool shouldCallAfterDispose = false;

            if (disposing && !IsDisposed)
            {
                if (m_contextFactory != null)
                {
                    m_contextFactory.Dispose();
                    m_contextFactory = null;
                }

                shouldCallAfterDispose = true;
            }

            if (!IsDisposed)
            {
                //We don't need no more steekin' memory monitoring.
                Engine.JsSetRuntimeMemoryAllocationCallback(Handle, IntPtr.Zero, null);
                m_runtimeMemoryAllocationChangingDelegateHandle.Free();

                //Don't need no before collect monitoring either!
                Engine.JsSetRuntimeBeforeCollectCallback(Handle, IntPtr.Zero, null);
                m_beforeCollectCallbackDelegateHandle.Free();
            }

            base.Dispose(disposing);

            if (shouldCallAfterDispose)
            {
                OnAfterDispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of a Barista Runtime.
        /// </summary>
        public BaristaRuntime(IJavaScriptEngine engine, IBaristaContextFactory contextFactory, JavaScriptRuntimeSafeHandle runtimeHandle)
            : base(engine, runtimeHandle)
        {
            m_contextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));

            JavaScriptMemoryAllocationCallback runtimeMemoryAllocationChanging = (IntPtr callbackState, JavaScriptMemoryEventType allocationEvent, UIntPtr allocationSize) =>
            {
                try
                {
                    return(OnRuntimeMemoryAllocationChanging(callbackState, allocationEvent, allocationSize));
                }
                catch
                {
                    //Do Nothing.
                    return(true);
                }
            };

            m_runtimeMemoryAllocationChangingDelegateHandle = GCHandle.Alloc(runtimeMemoryAllocationChanging);
            Engine.JsSetRuntimeMemoryAllocationCallback(runtimeHandle, IntPtr.Zero, runtimeMemoryAllocationChanging);

            JavaScriptBeforeCollectCallback beforeCollectCallback = (IntPtr callbackState) =>
            {
                try
                {
                    OnBeforeCollect(IntPtr.Zero, callbackState);
                }
                catch
                {
                    //Do Nothing.
                }
            };

            m_beforeCollectCallbackDelegateHandle = GCHandle.Alloc(beforeCollectCallback);
            Engine.JsSetRuntimeBeforeCollectCallback(runtimeHandle, IntPtr.Zero, beforeCollectCallback);
        }