CreateObject() public static method

Creates a new Object
Requires an active script context.
public static CreateObject ( ) : JsValue
return JsValue
Ejemplo n.º 1
0
        private static void SetNonEnumerableProperty(JsValue objValue, string name, JsValue value)
        {
            JsValue descriptorValue = JsValue.CreateObject();

            descriptorValue.SetProperty("enumerable", JsValue.False, true);
            descriptorValue.SetProperty("writable", JsValue.True, true);

            JsPropertyId id = JsPropertyId.FromString(name);

            objValue.DefineProperty(id, descriptorValue);
            objValue.SetProperty(id, value, true);
        }
Ejemplo n.º 2
0
        private static JsValue CreateErrorFromWrapperException(WrapperException exception)
        {
            var         originalException = exception.InnerException as JsException;
            JsErrorCode errorCode         = originalException != null ?
                                            originalException.ErrorCode : JsErrorCode.NoError;
            string description = exception.Description;

            JsValue innerErrorValue = JsErrorHelpers.CreateError(description);

            innerErrorValue.SetProperty("description", JsValue.FromString(description), true);

            JsValue metadataValue = JsValue.CreateObject();

            var scriptException = exception as WrapperScriptException;

            if (scriptException != null)
            {
                string type         = scriptException.Type;
                string documentName = scriptException.DocumentName;
                int    lineNumber   = scriptException.LineNumber;
                if (lineNumber > 0)
                {
                    lineNumber--;
                }
                int columnNumber = scriptException.ColumnNumber;
                if (columnNumber > 0)
                {
                    columnNumber--;
                }
                string sourceFragment = scriptException.SourceFragment;

                innerErrorValue.SetProperty("name", JsValue.FromString(type), true);

                var runtimeException = scriptException as WrapperRuntimeException;
                if (runtimeException != null)
                {
                    var    errorNumber = (int)errorCode;
                    string callStack   = runtimeException.CallStack;
                    string messageWithTypeAndCallStack = CoreErrorHelpers.GenerateScriptErrorMessage(type,
                                                                                                     description, callStack);

                    innerErrorValue.SetProperty("number", JsValue.FromInt32(errorNumber), true);
                    if (!string.IsNullOrWhiteSpace(callStack))
                    {
                        innerErrorValue.SetProperty("stack", JsValue.FromString(messageWithTypeAndCallStack), true);
                    }
                }
                else
                {
                    innerErrorValue.SetProperty("url", JsValue.FromString(documentName), true);
                    innerErrorValue.SetProperty("line", JsValue.FromInt32(lineNumber), true);
                    innerErrorValue.SetProperty("column", JsValue.FromInt32(columnNumber), true);
                    innerErrorValue.SetProperty("source", JsValue.FromString(sourceFragment), true);
                }

                metadataValue.SetProperty("url", JsValue.FromString(documentName), true);
                metadataValue.SetProperty("line", JsValue.FromInt32(lineNumber), true);
                metadataValue.SetProperty("column", JsValue.FromInt32(columnNumber), true);
                metadataValue.SetProperty("source", JsValue.FromString(sourceFragment), true);
            }

            innerErrorValue.SetProperty("metadata", metadataValue, true);

            JsValue errorValue = JsErrorHelpers.CreateError(description);

            errorValue.SetProperty("innerException", innerErrorValue, true);

            return(errorValue);
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
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);
            }
        }