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); }
public void SetGlobalFunction(string functionName, JavaScriptCallableFunction hostFunction) { if (hostFunction == null) { throw new ArgumentNullException(nameof(hostFunction)); } GlobalObject.SetPropertyByName(functionName, CreateFunction(hostFunction, functionName)); }
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); }
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); }
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; }
public void SetGlobalFunction(string functionName, JavaScriptCallableFunction hostFunction) { if (hostFunction == null) throw new ArgumentNullException(nameof(hostFunction)); GlobalObject.SetPropertyByName(functionName, CreateFunction(hostFunction, functionName)); }