CreateFunction() public static method

Creates a new JavaScript function
Requires an active script context.
public static CreateFunction ( IeJsNativeFunction function ) : IeJsValue
function IeJsNativeFunction The method to call when the function is invoked
return IeJsValue
Esempio n. 1
0
        private IeJsValue CreateFunctionFromDelegate(Delegate value)
        {
            IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                object[]        processedArgs  = MapToHostType(args.Skip(1).ToArray());
                ParameterInfo[] parameters     = value.GetMethodInfo().GetParameters();
                IeJsValue       undefinedValue = IeJsValue.Undefined;

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters);

                object result;

                try
                {
                    result = value.DynamicInvoke(processedArgs);
                }
                catch (Exception e)
                {
                    IeJsValue errorValue = IeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostDelegateInvocationFailed, e.Message));
                    IeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                IeJsValue resultValue = MapToScriptType(result);

                return(resultValue);
            };

            _nativeFunctions.Add(nativeFunction);

            IeJsValue functionValue = IeJsValue.CreateFunction(nativeFunction);

            return(functionValue);
        }
Esempio n. 2
0
        private void ProjectMethods(IeJsValue target, Type type, bool instance)
        {
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            MethodInfo[] methods = type.GetMethods(defaultBindingFlags);
            IEnumerable <IGrouping <string, MethodInfo> > methodGroups = methods.GroupBy(m => m.Name);

            foreach (IGrouping <string, MethodInfo> methodGroup in methodGroups)
            {
                string       methodName       = methodGroup.Key;
                MethodInfo[] methodCandidates = methodGroup.ToArray();

                IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    IeJsValue thisValue      = args[0];
                    IeJsValue undefinedValue = IeJsValue.Undefined;

                    object thisObj = null;

                    if (instance)
                    {
                        if (!thisValue.HasExternalData)
                        {
                            IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectMethod, methodName));
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        thisObj = MapToHostType(thisValue);
                    }

                    object[] processedArgs = MapToHostType(args.Skip(1).ToArray());

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        IeJsValue errorValue = IeJsErrorHelpers.CreateReferenceError(
                            string.Format(NetCoreStrings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        IeJsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitMethod.GetParameters());

                    object result;

                    try
                    {
                        result = bestFitMethod.Invoke(thisObj, processedArgs);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = instance ?
                                              string.Format(
                            NetCoreStrings.Runtime_HostObjectMethodInvocationFailed, methodName, e.Message)
                                                        :
                                              string.Format(
                            NetCoreStrings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName, e.Message)
                        ;

                        IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                        IeJsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    IeJsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                _nativeFunctions.Add(nativeFunction);

                IeJsValue methodValue = IeJsValue.CreateFunction(nativeFunction);
                target.SetProperty(methodName, methodValue, true);
            }
        }
Esempio n. 3
0
        private void ProjectProperties(IeJsValue target, Type type, bool instance)
        {
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            PropertyInfo[] properties = type.GetProperties(defaultBindingFlags);

            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;

                IeJsValue descriptorValue = IeJsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", IeJsValue.True, true);

                if (property.GetGetMethod() != null)
                {
                    IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        IeJsValue thisValue      = args[0];
                        IeJsValue undefinedValue = IeJsValue.Undefined;

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                IeJsErrorHelpers.SetException(errorValue);

                                return(undefinedValue);
                            }

                            thisObj = MapToHostType(thisValue);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(thisObj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            string errorMessage = instance ?
                                                  string.Format(
                                NetCoreStrings.Runtime_HostObjectPropertyGettingFailed, propertyName, e.Message)
                                                                :
                                                  string.Format(
                                NetCoreStrings.Runtime_HostTypePropertyGettingFailed, propertyName, typeName, e.Message)
                            ;

                            IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        IeJsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    };
                    _nativeFunctions.Add(nativeFunction);

                    IeJsValue getMethodValue = IeJsValue.CreateFunction(nativeFunction);
                    descriptorValue.SetProperty("get", getMethodValue, true);
                }

                if (property.GetSetMethod() != null)
                {
                    IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        IeJsValue thisValue      = args[0];
                        IeJsValue undefinedValue = IeJsValue.Undefined;

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                IeJsErrorHelpers.SetException(errorValue);

                                return(undefinedValue);
                            }

                            thisObj = MapToHostType(thisValue);
                        }

                        object value = MapToHostType(args.Skip(1).First());
                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(thisObj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            string errorMessage = instance ?
                                                  string.Format(
                                NetCoreStrings.Runtime_HostObjectPropertySettingFailed, propertyName, e.Message)
                                                                :
                                                  string.Format(
                                NetCoreStrings.Runtime_HostTypePropertySettingFailed, propertyName, typeName, e.Message)
                            ;

                            IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        return(undefinedValue);
                    };
                    _nativeFunctions.Add(nativeFunction);

                    IeJsValue setMethodValue = IeJsValue.CreateFunction(nativeFunction);
                    descriptorValue.SetProperty("set", setMethodValue, true);
                }

                target.DefineProperty(propertyName, descriptorValue);
            }
        }
Esempio n. 4
0
        private IeJsValue CreateConstructor(Type type)
        {
            TypeInfo     typeInfo            = type.GetTypeInfo();
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true);

            ConstructorInfo[] constructors = type.GetConstructors(defaultBindingFlags);

            IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                IeJsValue resultValue;
                IeJsValue undefinedValue = IeJsValue.Undefined;

                object[] processedArgs = MapToHostType(args.Skip(1).ToArray());
                object   result;

                if (processedArgs.Length == 0 && typeInfo.IsValueType)
                {
                    result      = Activator.CreateInstance(type);
                    resultValue = MapToScriptType(result);

                    return(resultValue);
                }

                if (constructors.Length == 0)
                {
                    IeJsValue errorValue = IeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostTypeConstructorNotFound, typeName));
                    IeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                var bestFitConstructor = (ConstructorInfo)ReflectionHelpers.GetBestFitMethod(
                    constructors, processedArgs);
                if (bestFitConstructor == null)
                {
                    IeJsValue errorValue = IeJsErrorHelpers.CreateReferenceError(
                        string.Format(NetCoreStrings.Runtime_SuitableConstructorOfHostTypeNotFound, typeName));
                    IeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitConstructor.GetParameters());

                try
                {
                    result = bestFitConstructor.Invoke(processedArgs);
                }
                catch (Exception e)
                {
                    IeJsValue errorValue = IeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostTypeConstructorInvocationFailed, typeName, e.Message));
                    IeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                resultValue = MapToScriptType(result);

                return(resultValue);
            };

            _nativeFunctions.Add(nativeFunction);

            IeJsValue constructorValue = IeJsValue.CreateFunction(nativeFunction);

            return(constructorValue);
        }