예제 #1
0
        public JavaScriptFunction CreateFunction(JavaScriptCallableFunction hostFunction, string name)
        {
            if (hostFunction == null)
            {
                throw new ArgumentNullException(nameof(hostFunction));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            ClaimContext();

            var nameVal = Converter.FromString(name);

            NativeFunctionThunkData td = new NativeFunctionThunkData()
            {
                callback = hostFunction, engine = new WeakReference <JavaScriptEngine>(this)
            };
            GCHandle handle = GCHandle.Alloc(td, GCHandleType.Weak);

            nativeFunctionThunks_.Add(td);

            JavaScriptValueSafeHandle resultHandle;

            Errors.ThrowIfIs(api_.JsCreateNamedFunction(nameVal.handle_, NativeCallbackPtr, GCHandle.ToIntPtr(handle), out resultHandle));

            return(CreateObjectFromHandle(resultHandle) as JavaScriptFunction);
        }
예제 #2
0
        public void SetGlobalFunction(string functionName, JavaScriptCallableFunction hostFunction)
        {
            if (hostFunction == null)
            {
                throw new ArgumentNullException(nameof(hostFunction));
            }

            GlobalObject.SetPropertyByName(functionName, CreateFunction(hostFunction, functionName));
        }
예제 #3
0
        public JavaScriptFunction CreateFunction(JavaScriptCallableFunction hostFunction)
        {
            if (hostFunction == null)
            {
                throw new ArgumentNullException(nameof(hostFunction));
            }

            NativeFunctionThunkData td = new NativeFunctionThunkData()
            {
                callback = hostFunction, engine = new WeakReference <JavaScriptEngine>(this)
            };
            GCHandle handle = GCHandle.Alloc(td, GCHandleType.Weak);

            nativeFunctionThunks_.Add(td);

            JavaScriptValueSafeHandle resultHandle;

            Errors.ThrowIfIs(api_.JsCreateFunction(NativeCallbackPtr, GCHandle.ToIntPtr(handle), out resultHandle));

            return(CreateObjectFromHandle(resultHandle) as JavaScriptFunction);
        }
예제 #4
0
        public void TestMethod1()
        {
            bool ok = false;
            JavaScriptCallableFunction callback = (eng, construct, thisObj, args) =>
            {
                ok = true;
                return(eng.UndefinedValue);
            };

            using (var rt = new JavaScriptRuntime())
            {
                rt.MemoryChanging += Rt_MemoryChanging;
                using (var eng = rt.CreateEngine())
                {
                    eng.SetGlobalFunction("hitIt", callback);

                    eng.Execute(new ScriptSource("[eval code]", "hitIt();"));
                }
            }
            Assert.IsTrue(ok);
        }
예제 #5
0
        public JavaScriptFunction CreateFunction(JavaScriptCallableFunction hostFunction, string name)
        {
            if (hostFunction == null)
                throw new ArgumentNullException(nameof(hostFunction));
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name));

            ClaimContext();

            var nameVal = Converter.FromString(name);

            NativeFunctionThunkData td = new NativeFunctionThunkData() { callback = hostFunction, engine = new WeakReference<JavaScriptEngine>(this) };
            GCHandle handle = GCHandle.Alloc(td, GCHandleType.Weak);
            nativeFunctionThunks_.Add(td);

            JavaScriptValueSafeHandle resultHandle;
            Errors.ThrowIfIs(api_.JsCreateNamedFunction(nameVal.handle_, NativeCallbackPtr, GCHandle.ToIntPtr(handle), out resultHandle));

            return CreateObjectFromHandle(resultHandle) as JavaScriptFunction;
        }
예제 #6
0
        public void SetGlobalFunction(string functionName, JavaScriptCallableFunction hostFunction)
        {
            if (hostFunction == null)
                throw new ArgumentNullException(nameof(hostFunction));

            GlobalObject.SetPropertyByName(functionName, CreateFunction(hostFunction, functionName));
        }