Пример #1
0
 public void ShouldHandleDictionaryObjects() {
     var dic = new JsObject();
     dic["prop1"] = new JsNumber(1, JsNull.Instance);
     Assert.IsTrue(dic.HasProperty(new JsString("prop1", JsNull.Instance)));
     Assert.IsTrue(dic.HasProperty("prop1"));
     Assert.AreEqual(1, dic["prop1"].ToNumber());
 }
Пример #2
0
        internal static Descriptor ToPropertyDesciptor(
            IGlobal global,
            JsDictionaryObject owner,
            string name,
            JsInstance jsInstance)
        {
            if (jsInstance.Class != "Object")
            {
                throw new JsException((JsInstance)global.TypeErrorClass.New("The target object has to be an instance of an object"));
            }
            JsObject jsObject = (JsObject)jsInstance;

            if ((jsObject.HasProperty("value") || jsObject.HasProperty("writable")) && (jsObject.HasProperty("set") || jsObject.HasProperty("get")))
            {
                throw new JsException((JsInstance)global.TypeErrorClass.New("The property cannot be both writable and have get/set accessors or cannot have both a value and an accessor defined"));
            }
            JsInstance result     = (JsInstance)null;
            Descriptor descriptor = !jsObject.HasProperty("value") ? (Descriptor) new PropertyDescriptor(global, owner, name) : (Descriptor) new ValueDescriptor(owner, name, jsObject["value"]);

            if (jsObject.TryGetProperty("enumerable", out result))
            {
                descriptor.Enumerable = result.ToBoolean();
            }
            if (jsObject.TryGetProperty("configurable", out result))
            {
                descriptor.Configurable = result.ToBoolean();
            }
            if (jsObject.TryGetProperty("writable", out result))
            {
                descriptor.Writable = result.ToBoolean();
            }
            if (jsObject.TryGetProperty("get", out result))
            {
                if (!(result is JsFunction))
                {
                    throw new JsException((JsInstance)global.TypeErrorClass.New("The getter has to be a function"));
                }
                ((PropertyDescriptor)descriptor).GetFunction = (JsFunction)result;
            }
            if (jsObject.TryGetProperty("set", out result))
            {
                if (!(result is JsFunction))
                {
                    throw new JsException((JsInstance)global.TypeErrorClass.New("The setter has to be a function"));
                }
                ((PropertyDescriptor)descriptor).SetFunction = (JsFunction)result;
            }
            return(descriptor);
        }
        /// <summary>
        /// 8.10.5
        /// </summary>
        /// <param name="global"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal static Descriptor ToPropertyDesciptor(IGlobal global, JsDictionaryObject owner, string name, JsInstance jsInstance)
        {
            if (jsInstance.Class != JsObject.TYPEOF)
            {
                throw new JsException(global.TypeErrorClass.New("The target object has to be an instance of an object"));
            }

            JsObject obj = (JsObject)jsInstance;

            if ((obj.HasProperty("value") || obj.HasProperty("writable")) && (obj.HasProperty("set") || obj.HasProperty("get")))
            {
                throw new JsException(global.TypeErrorClass.New("The property cannot be both writable and have get/set accessors or cannot have both a value and an accessor defined"));
            }

            Descriptor desc;
            JsInstance result = null;

            if (obj.HasProperty("value"))
            {
                desc = new ValueDescriptor(owner, name, obj["value"]);
            }
            else
            {
                desc = new PropertyDescriptor(global, owner, name);
            }

            if (obj.TryGetProperty("enumerable", out result))
            {
                desc.Enumerable = result.ToBoolean();
            }

            if (obj.TryGetProperty("configurable", out result))
            {
                desc.Configurable = result.ToBoolean();
            }

            if (obj.TryGetProperty("writable", out result))
            {
                desc.Writable = result.ToBoolean();
            }

            if (obj.TryGetProperty("get", out result))
            {
                if (result.Class != JsFunction.TYPEOF)
                {
                    throw new JsException(global.TypeErrorClass.New("The getter has to be a function"));
                }

                ((PropertyDescriptor)desc).GetFunction = (JsFunction)result;
            }

            if (obj.TryGetProperty("set", out result))
            {
                if (result.Class != JsFunction.TYPEOF)
                {
                    throw new JsException(global.TypeErrorClass.New("The setter has to be a function"));
                }

                ((PropertyDescriptor)desc).SetFunction = (JsFunction)result;
            }

            return(desc);
        }