public JsFunction CreateFunction(Delegate func, string name = null) { if (func == null) { throw new ArgumentNullException(nameof(func)); } JavaScriptNativeFunction fnDelegate = null; switch (func) { case BaristaFunctionDelegate fnBaristaFunctionDelegate: fnDelegate = CreateNativeFunctionForDelegate(fnBaristaFunctionDelegate); break; default: fnDelegate = CreateNativeFunctionForDelegate(func); break; } JavaScriptValueSafeHandle fnHandle; if (String.IsNullOrWhiteSpace(name)) { fnHandle = m_engine.JsCreateFunction(fnDelegate, IntPtr.Zero); } else { var nameValue = CreateString(name); fnHandle = m_engine.JsCreateNamedFunction(nameValue.Handle, fnDelegate, IntPtr.Zero); } //this is a special case where we cannot use our CreateValue<> method. return(m_valuePool.GetOrAdd(fnHandle, () => { var jsNativeFunction = new JsNativeFunction(m_engine, Context, fnHandle, fnDelegate); jsNativeFunction.BeforeCollect += JsValueBeforeCollectCallback; return jsNativeFunction; }) as JsNativeFunction); }