/// <summary>
            /// 8.10.5
            /// </summary>
            private static void ToPropertyDescriptor(JsGlobal global, JsObject owner, string name, object value)
            {
                var @object = value as JsObject;
                if (@object == null)
                    throw new JsException(JsErrorType.TypeError, "The target object has to be an instance of an object");

                if (
                    (@object.HasProperty(Id.value) || @object.HasProperty(Id.writable)) &&
                    (@object.HasProperty(Id.set) || @object.HasProperty(Id.get))
                )
                    throw new JsException(JsErrorType.TypeError, "The property cannot be both writable and have get/set accessors or cannot have both a value and an accessor defined");

                var attributes = PropertyAttributes.None;
                JsObject getFunction = null;
                JsObject setFunction = null;
                object result;

                if (
                    @object.TryGetProperty(Id.enumerable, out result) &&
                    !JsValue.ToBoolean(result)
                )
                    attributes |= PropertyAttributes.DontEnum;

                if (
                    @object.TryGetProperty(Id.configurable, out result) &&
                    !JsValue.ToBoolean(result)
                )
                    attributes |= PropertyAttributes.DontDelete;

                if (
                    @object.TryGetProperty(Id.writable, out result) &&
                    !JsValue.ToBoolean(result)
                )
                    attributes |= PropertyAttributes.ReadOnly;

                if (@object.TryGetProperty(Id.get, out result))
                {
                    if (!JsValue.IsFunction(result))
                        throw new JsException(JsErrorType.TypeError, "The getter has to be a function");

                    getFunction = (JsObject)result;
                }

                if (@object.TryGetProperty(Id.set, out result))
                {
                    if (!JsValue.IsFunction(result))
                        throw new JsException(JsErrorType.TypeError, "The setter has to be a function");

                    setFunction = (JsObject)result;
                }

                if (@object.HasProperty(Id.value))
                {
                    owner.DefineProperty(
                        global.ResolveIdentifier(name),
                        @object.GetProperty(Id.value),
                        PropertyAttributes.None
                    );
                }
                else
                {
                    owner.DefineAccessor(
                        global.ResolveIdentifier(name),
                        getFunction,
                        setFunction,
                        attributes
                    );
                }
            }
Example #2
0
File: Id.cs Project: pvginkel/Jint2
 private static void SeedToken(JsGlobal global, string name, int id)
 {
     int resolvedId = global.ResolveIdentifier(name);
     Debug.Assert(id == resolvedId);
 }