DefineProperty() публичный Метод

Defines a new object's own property from a property descriptor
Requires an active script context.
public DefineProperty ( JsPropertyId propertyId, JsValue propertyDescriptor ) : bool
propertyId JsPropertyId The ID of the property
propertyDescriptor JsValue The property descriptor
Результат bool
Пример #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);
        }
Пример #2
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);
            }
        }
Пример #3
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);
            }
        }