コード例 #1
0
        private void ProjectMethods(JsValue 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();

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

                    object thisObj = null;

                    if (instance)
                    {
                        if (!thisValue.HasExternalData)
                        {
                            JsValue errorValue = JsErrorHelpers.CreateTypeError(
                                string.Format(Strings.Runtime_InvalidThisContextForHostObjectMethod, methodName));
                            JsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        thisObj = MapToHostType(thisValue);
                    }

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

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        JsValue errorValue = JsErrorHelpers.CreateReferenceError(
                            string.Format(Strings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        JsErrorHelpers.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(
                            Strings.Runtime_HostObjectMethodInvocationFailed, methodName, e.Message)
                            :
                                              string.Format(
                            Strings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName, e.Message)
                        ;

                        JsValue errorValue = JsErrorHelpers.CreateError(errorMessage);
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    JsValue resultValue = MapToScriptType(result);

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

                JsValue methodValue = JsValue.CreateFunction(nativeFunction);
                target.SetProperty(methodName, methodValue, true);
            }
        }
コード例 #2
0
        private void ProjectProperties(JsValue 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;

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

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

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                JsValue errorValue = JsErrorHelpers.CreateTypeError(
                                    string.Format(Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                JsErrorHelpers.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(
                                Strings.Runtime_HostObjectPropertyGettingFailed, propertyName, e.Message)
                                :
                                                  string.Format(
                                Strings.Runtime_HostTypePropertyGettingFailed, propertyName, typeName, e.Message)
                            ;

                            JsValue errorValue = JsErrorHelpers.CreateError(errorMessage);
                            JsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        JsValue resultValue = MapToScriptType(result);

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

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

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

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                JsValue errorValue = JsErrorHelpers.CreateTypeError(
                                    string.Format(Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                JsErrorHelpers.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(
                                Strings.Runtime_HostObjectPropertySettingFailed, propertyName, e.Message)
                                :
                                                  string.Format(
                                Strings.Runtime_HostTypePropertySettingFailed, propertyName, typeName, e.Message)
                            ;

                            JsValue errorValue = JsErrorHelpers.CreateError(errorMessage);
                            JsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

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

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

                target.DefineProperty(propertyName, descriptorValue);
            }
        }