예제 #1
0
파일: JSObject.cs 프로젝트: reshadi2/mcjs
        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);
          }
        }
예제 #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);
            }
        }