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

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters);

                object result;

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

                    JsErrorHelpers.SetException(errorValue);

                    return(JsValue.Undefined);
                }

                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);
        }
        private static void CreateAndSetTypeError(string message)
        {
            JsValue errorValue = JsErrorHelpers.CreateTypeError(message);

            JsErrorHelpers.SetException(errorValue);
        }
        private void ProjectMethods(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string                   typeName            = type.FullName;
            BindingFlags             defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
            IEnumerable <MethodInfo> methods             = type.GetMethods(defaultBindingFlags)
                                                           .Where(ReflectionHelpers.IsFullyFledgedMethod);
            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) =>
                {
                    if (instance && obj == null)
                    {
                        CreateAndSetTypeError(string.Format(
                                                  Strings.Runtime_InvalidThisContextForHostObjectMethod, methodName));
                        return(JsValue.Undefined);
                    }

                    object[] processedArgs = GetHostItemMemberArguments(args);

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        CreateAndSetReferenceError(string.Format(
                                                       Strings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        return(JsValue.Undefined);
                    }

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

                    object result;

                    try
                    {
                        result = bestFitMethod.Invoke(obj, processedArgs);
                    }
                    catch (Exception e)
                    {
                        Exception exception        = UnwrapException(e);
                        var       wrapperException = exception as WrapperException;
                        JsValue   errorValue;

                        if (wrapperException != null)
                        {
                            errorValue = CreateErrorFromWrapperException(wrapperException);
                        }
                        else
                        {
                            string errorMessage = instance ?
                                                  string.Format(Strings.Runtime_HostObjectMethodInvocationFailed, methodName,
                                                                exception.Message)
                                                                :
                                                  string.Format(Strings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName,
                                                                exception.Message)
                            ;
                            errorValue = JsErrorHelpers.CreateError(errorMessage);
                        }
                        JsErrorHelpers.SetException(errorValue);

                        return(JsValue.Undefined);
                    }

                    JsValue resultValue = MapToScriptType(result);

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

                JsValue methodValue = JsValue.CreateFunction(nativeFunction);
                typeValue.SetProperty(methodName, methodValue, true);
            }
        }
        private void ProjectProperties(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            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 nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        if (instance && obj == null)
                        {
                            CreateAndSetTypeError(string.Format(
                                                      Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                            return(JsValue.Undefined);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(obj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as WrapperException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      string.Format(Strings.Runtime_HostObjectPropertyGettingFailed, propertyName,
                                                                    exception.Message)
                                                                        :
                                                      string.Format(Strings.Runtime_HostTypePropertyGettingFailed, propertyName,
                                                                    typeName, exception.Message)
                                ;
                                errorValue = JsErrorHelpers.CreateError(errorMessage);
                            }
                            JsErrorHelpers.SetException(errorValue);

                            return(JsValue.Undefined);
                        }

                        JsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    };
                    nativeFunctions.Add(nativeGetFunction);

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

                if (property.GetSetMethod() != null)
                {
                    JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        if (instance && obj == null)
                        {
                            CreateAndSetTypeError(string.Format(
                                                      Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                            return(JsValue.Undefined);
                        }

                        object value = MapToHostType(args[1]);
                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(obj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as WrapperException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      string.Format(Strings.Runtime_HostObjectPropertySettingFailed, propertyName,
                                                                    exception.Message)
                                                                        :
                                                      string.Format(Strings.Runtime_HostTypePropertySettingFailed, propertyName,
                                                                    typeName, exception.Message)
                                ;
                                errorValue = JsErrorHelpers.CreateError(errorMessage);
                            }
                            JsErrorHelpers.SetException(errorValue);

                            return(JsValue.Undefined);
                        }

                        return(JsValue.Undefined);
                    };
                    nativeFunctions.Add(nativeSetFunction);

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

                typeValue.DefineProperty(propertyName, descriptorValue);
            }
        }
        private EmbeddedType CreateEmbeddedType(Type type)
        {
#if NET40
            Type typeInfo = type;
#else
            TypeInfo typeInfo = type.GetTypeInfo();
#endif
            string            typeName            = type.FullName;
            BindingFlags      defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true);
            ConstructorInfo[] constructors        = type.GetConstructors(defaultBindingFlags);

            JsNativeFunction nativeConstructorFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                object   result;
                JsValue  resultValue;
                object[] processedArgs = GetHostItemMemberArguments(args);

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

                    return(resultValue);
                }

                if (constructors.Length == 0)
                {
                    CreateAndSetError(string.Format(Strings.Runtime_HostTypeConstructorNotFound, typeName));
                    return(JsValue.Undefined);
                }

                var bestFitConstructor = (ConstructorInfo)ReflectionHelpers.GetBestFitMethod(
                    constructors, processedArgs);
                if (bestFitConstructor == null)
                {
                    CreateAndSetReferenceError(string.Format(
                                                   Strings.Runtime_SuitableConstructorOfHostTypeNotFound, typeName));
                    return(JsValue.Undefined);
                }

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

                try
                {
                    result = bestFitConstructor.Invoke(processedArgs);
                }
                catch (Exception e)
                {
                    Exception exception        = UnwrapException(e);
                    var       wrapperException = exception as WrapperException;
                    JsValue   errorValue       = wrapperException != null?
                                                 CreateErrorFromWrapperException(wrapperException)
                                                     :
                                                     JsErrorHelpers.CreateError(string.Format(
                                                                                    Strings.Runtime_HostTypeConstructorInvocationFailed, typeName, exception.Message))
                    ;

                    JsErrorHelpers.SetException(errorValue);

                    return(JsValue.Undefined);
                }

                resultValue = MapToScriptType(result);

                return(resultValue);
            };

            GCHandle embeddedTypeHandle = GCHandle.Alloc(type);
            IntPtr   embeddedTypePtr    = GCHandle.ToIntPtr(embeddedTypeHandle);
            JsValue  objValue           = JsValue.CreateExternalObject(embeddedTypePtr, _embeddedTypeFinalizeCallback);

            JsValue typeValue = JsValue.CreateFunction(nativeConstructorFunction);
            SetNonEnumerableProperty(typeValue, ExternalObjectPropertyName, objValue);

            var embeddedType = new EmbeddedType(type, typeValue,
                                                new List <JsNativeFunction> {
                nativeConstructorFunction
            });

            ProjectFields(embeddedType);
            ProjectProperties(embeddedType);
            ProjectMethods(embeddedType);
            FreezeObject(typeValue);

            return(embeddedType);
        }
Example #6
0
        private void ProjectFields(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            bool    instance  = externalItem.IsInstance;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            FieldInfo[] fields = type.GetFields(defaultBindingFlags);

            foreach (FieldInfo field in fields)
            {
                string fieldName = field.Name;

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

                JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        JsValue undefinedValue = JsValue.Undefined;
                        JsValue errorValue     = JsErrorHelpers.CreateTypeError(
                            string.Format(Strings.Runtime_InvalidThisContextForHostObjectField, fieldName));
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    object result;

                    try
                    {
                        result = field.GetValue(obj);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = instance ?
                                              string.Format(Strings.Runtime_HostObjectFieldGettingFailed, fieldName, e.Message)
                                                        :
                                              string.Format(Strings.Runtime_HostTypeFieldGettingFailed, fieldName, typeName, e.Message)
                        ;

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

                        return(undefinedValue);
                    }

                    JsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                nativeFunctions.Add(nativeGetFunction);

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

                JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        JsValue undefinedValue = JsValue.Undefined;
                        JsValue errorValue     = JsErrorHelpers.CreateTypeError(
                            string.Format(Strings.Runtime_InvalidThisContextForHostObjectField, fieldName));
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    object value = MapToHostType(args[1]);
                    ReflectionHelpers.FixFieldValueType(ref value, field);

                    try
                    {
                        field.SetValue(obj, value);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = instance ?
                                              string.Format(Strings.Runtime_HostObjectFieldSettingFailed, fieldName, e.Message)
                                                        :
                                              string.Format(Strings.Runtime_HostTypeFieldSettingFailed, fieldName, typeName, e.Message)
                        ;

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

                        return(undefinedValue);
                    }

                    return(JsValue.Undefined);
                };
                nativeFunctions.Add(nativeSetFunction);

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

                typeValue.DefineProperty(fieldName, descriptorValue);
            }
        }