Ejemplo n.º 1
0
            /// <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
                    );
                }
            }
Ejemplo n.º 2
0
        private JsObject BuildObjectClass(JsObject prototype)
        {
            prototype.DefineProperty(Id.toString, ObjectFunctions.ToString, 0, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.toLocaleString, ObjectFunctions.ToString, 0, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.valueOf, ObjectFunctions.ValueOf, 0, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.hasOwnProperty, ObjectFunctions.HasOwnProperty, 1, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.isPrototypeOf, ObjectFunctions.IsPrototypeOf, 1, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.propertyIsEnumerable, ObjectFunctions.PropertyIsEnumerable, 1, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.getPrototypeOf, ObjectFunctions.GetPrototypeOf, 1, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.defineProperty, ObjectFunctions.DefineProperty, 1, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.__lookupGetter__, ObjectFunctions.LookupGetter, 1, PropertyAttributes.DontEnum);
            prototype.DefineProperty(Id.__lookupSetter__, ObjectFunctions.LookupSetter, 1, PropertyAttributes.DontEnum);

            return CreateNakedFunction("Object", ObjectFunctions.Constructor, 0, prototype);
        }