Ejemplo n.º 1
0
        /// <summary>
        /// This method creates a <see cref="GCHandle"/> for a host object, converts it into a pointer, then
        /// adds a 'before collect callback' passing that pointer. This callback will execute whenever the
        /// JavaScript object is going to be garbage collected, which releases the host object GC handle,
        /// allowing it to also be garbage collected should no external references exist.
        /// </summary>
        /// <param name="jsValue">The <see cref="JavaScriptValue"/> to add the before collect callback for.</param>
        /// <param name="instance">The host object to link to the JavaScript object.</param>
        /// <returns>The resulting pointer of the <see cref="GCHandle"/> for the host object.</returns>
        public IntPtr Link <T>(JavaScriptValue jsValue, T instance)
        {
            var gcHandle = GCHandle.Alloc(instance, GCHandleType.Normal);
            var ptr      = GCHandle.ToIntPtr(gcHandle);

            _scope.Run(() =>
            {
                JavaScriptContext.SetObjectBeforeCollectCallback(jsValue, ptr, _jsGcCollect);
            });

            return(ptr);
        }