SetException() public static method

Sets a runtime of the current context to an exception state

If the runtime of the current context is already in an exception state, this API will throw JsErrorInExceptionState.

Requires an active script context.

public static SetException ( JsValue exception ) : void
exception JsValue The JavaScript exception to set for the runtime of the current context
return void
        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);
        }
        private static void CreateAndSetTypeError(string message)
        {
            JsValue errorValue = JsErrorHelpers.CreateTypeError(message);

            JsContext.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);
                        }
                        JsContext.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);
                            }
                            JsContext.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);
                            }
                            JsContext.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))
                    ;

                    JsContext.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);
        }