JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue.CreateFunction C# (CSharp) Method

CreateFunction() public static method

Creates a new JavaScript function
Requires an active script context.
public static CreateFunction ( JsNativeFunction function ) : JsValue
function JsNativeFunction The method to call when the function is invoked
return JsValue
		public static JsValue CreateFunction(JsNativeFunction function)
		{
			JsValue reference;
			JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateFunction(function, IntPtr.Zero, out reference));

			return reference;
		}

Same methods

JsValue::CreateFunction ( JsNativeFunction function, IntPtr callbackData ) : JsValue

Usage Example

        private EmbeddedObject CreateEmbeddedFunction(Delegate del)
        {
            JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
#if NET40
                MethodInfo method = del.Method;
#else
                MethodInfo method = del.GetMethodInfo();
#endif
                ParameterInfo[] parameters    = method.GetParameters();
                object[]        processedArgs = GetHostItemMemberArguments(args, parameters.Length);

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters);

                object result;

                try
                {
                    result = del.DynamicInvoke(processedArgs);
                }
                catch (Exception e)
                {
                    JsValue   undefinedValue   = JsValue.Undefined;
                    Exception exception        = UnwrapException(e);
                    var       wrapperException = exception as WrapperException;
                    JsValue   errorValue       = wrapperException != null?
                                                 CreateErrorFromWrapperException(wrapperException)
                                                     :
                                                     JsErrorHelpers.CreateError(string.Format(
                                                                                    Strings.Runtime_HostDelegateInvocationFailed, exception.Message))
                    ;

                    JsContext.SetException(errorValue);

                    return(undefinedValue);
                }

                JsValue resultValue = MapToScriptType(result);

                return(resultValue);
            };

            GCHandle delHandle = GCHandle.Alloc(del);
            IntPtr   delPtr    = GCHandle.ToIntPtr(delHandle);
            JsValue  objValue  = JsValue.CreateExternalObject(delPtr, _embeddedObjectFinalizeCallback);

            JsValue functionValue = JsValue.CreateFunction(nativeFunction);

            SetNonEnumerableProperty(functionValue, ExternalObjectPropertyName, objValue);

            var embeddedObject = new EmbeddedObject(del, functionValue,
                                                    new List <JsNativeFunction> {
                nativeFunction
            });

            return(embeddedObject);
        }
All Usage Examples Of JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue::CreateFunction