Esempio n. 1
0
        public void JsWeakReferenceValueBeRetrieved()
        {
            var str     = "Hello, World!";
            var weakRef = JavaScriptWeakReferenceSafeHandle.Invalid;

            using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null))
            {
                using (var contextHandle = Engine.JsCreateContext(runtimeHandle))
                {
                    Engine.JsSetCurrentContext(contextHandle);

                    var stringHandle = Engine.JsCreateString(str, (ulong)str.Length);
                    Assert.True(stringHandle != JavaScriptValueSafeHandle.Invalid);

                    weakRef = Engine.JsCreateWeakReference(stringHandle);
                    Assert.True(weakRef != JavaScriptWeakReferenceSafeHandle.Invalid);

                    var valueHandle = Engine.JsGetWeakReferenceValue(weakRef);
                    Assert.True(valueHandle != JavaScriptValueSafeHandle.Invalid);

                    Assert.True(valueHandle == stringHandle);

                    valueHandle.Dispose();
                    stringHandle.Dispose();
                }

                //TODO: even after a collect, JsGetWeakReferenceValue still returns a handle.
                //Engine.JsCollectGarbage(runtimeHandle);
                //var outOfScopeValueHandle = Engine.JsGetWeakReferenceValue(weakRef);
                //Assert.True(outOfScopeValueHandle == JavaScriptValueSafeHandle.Invalid);
            }
        }