Ejemplo n.º 1
0
        //ECMA-262 section 15.2.3.5
        void create(ref mdr.CallFrame callFrame)
        {
            Debug.WriteLine("calling JSObject.create");
            var O = callFrame.Arg0.AsDObject();

            if (O == null)
            {
                Trace.Fail("TypeError");
            }
            var obj   = new DObject(O);
            var props = callFrame.Arg1.AsDObject();

            if (!ValueTypesHelper.IsUndefined(props.ValueType))
            {
                DefineProperties(obj, props);
            }

            callFrame.Return.Set(obj);
        }
Ejemplo n.º 2
0
        private void DefineProperty(DObject O, string name, DObject desc)
        {
            if (desc == null || O == null || name == null)
            {
                Trace.Fail("TypeError");
            }

            var getter     = new DValue();
            var setter     = new DValue();
            var value      = new DValue();
            var attributes = PropertyDescriptor.Attributes.NotEnumerable | PropertyDescriptor.Attributes.NotWritable | PropertyDescriptor.Attributes.NotConfigurable;

            attributes = PropertyDescriptor.Attributes.NotEnumerable | PropertyDescriptor.Attributes.NotWritable | PropertyDescriptor.Attributes.NotConfigurable;

            getter.SetUndefined();
            setter.SetUndefined();
            value.SetUndefined();

            value = desc.HasProperty("value") ? desc.GetField("value") : value;

            if (desc.HasProperty("enumerable"))
            {
                attributes &= desc.GetField("enumerable").AsBoolean() ? ~PropertyDescriptor.Attributes.NotEnumerable : attributes;
            }


            if (desc.HasProperty("configurable"))
            {
                attributes &= desc.GetField("configurable").AsBoolean() ? ~PropertyDescriptor.Attributes.NotConfigurable : attributes;
            }

            if (desc.HasProperty("writable"))
            {
                attributes &= desc.GetField("writable").AsBoolean() ? ~PropertyDescriptor.Attributes.NotWritable : attributes;
            }

            if (desc.HasProperty("get"))
            {
                getter = desc.GetField("get");
                if (!ValueTypesHelper.IsUndefined(getter.ValueType) && !ValueTypesHelper.IsFunction(getter.ValueType))
                {
                    Trace.Fail("TypeError");
                }
            }

            if (desc.HasProperty("set"))
            {
                setter = desc.GetField("set");
                if (!ValueTypesHelper.IsUndefined(setter.ValueType) && !ValueTypesHelper.IsFunction(setter.ValueType))
                {
                    Trace.Fail("TypeError");
                }
            }

            Trace.Assert(
                !((desc.HasProperty("get") || desc.HasProperty("set")) &&
                  (desc.HasProperty("value") || desc.HasProperty("writable"))),
                "If either getter or setter needs to be defined, value or writable shouldn't be defined.");

            if (desc.HasProperty("value"))
            {
                O.DefineOwnProperty(name, ref value, attributes | PropertyDescriptor.Attributes.Data);
            }
            else
            {
                var property = new DProperty();
                if (ValueTypesHelper.IsFunction(getter.ValueType))
                {
                    property.Getter = getter.AsDFunction();
                }
                if (ValueTypesHelper.IsFunction(setter.ValueType))
                {
                    property.Setter = setter.AsDFunction();
                }

                O.DefineOwnProperty(name, property, attributes | PropertyDescriptor.Attributes.Accessor);
            }
        }