Ejemplo n.º 1
0
        public static EcmaValue IsPrototypeOf([This] EcmaValue thisValue, EcmaValue obj)
        {
            Guard.RequireObjectCoercible(thisValue);
            if (obj.Type != EcmaValueType.Object)
            {
                return(false);
            }
            RuntimeObject thisObj = thisValue.ToObject();

            for (RuntimeObject v = obj.ToObject(); v != null; v = v.GetPrototypeOf())
            {
                if (v == thisObj)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        public void ObjectHosting()
        {
            It("should return different instances on different execution thread", () => {
                TestObject obj   = new TestObject();
                RuntimeObject v0 = RuntimeRealm.Current.ResolveRuntimeObject(obj);

                RuntimeExecution.CreateWorkerThread(parentRealm => {
                    Assume.That(parentRealm, Is.Not.EqualTo(RuntimeRealm.Current), "realm should be different");

                    RuntimeObject v1 = RuntimeRealm.Current.ResolveRuntimeObject(obj);
                    That(v0 != v1, "returned object should be different");
                    That(v0.Realm, Is.EqualTo(parentRealm));
                    That(v1.Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf() != v1.GetPrototypeOf(), "the prototype of returned object should be different");
                    That(v0.GetPrototypeOf().Realm, Is.EqualTo(parentRealm));
                    That(v1.GetPrototypeOf().Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf().GetPrototypeOf() != v1.GetPrototypeOf().GetPrototypeOf(), "all prototype ancestors of returned object should be different");

                    That(v0["MyMethod"], Is.TypeOf("function"));
                    That(v0["MyMethod"], Is.Not.EqualTo(v1["MyMethod"]), "property value of object type should be different");
                }, true).Thread.Join();
            });

            It("should return different instances on different realm of the same execution thread", () => {
                TestObject obj           = new TestObject();
                RuntimeObject v0         = RuntimeRealm.Current.ResolveRuntimeObject(obj);
                RuntimeRealm parentRealm = RuntimeRealm.Current;
                RuntimeRealm other       = new RuntimeRealm();

                other.Enqueue(() => {
                    Assume.That(RuntimeRealm.Current, Is.Not.EqualTo(parentRealm), "realm should be different");
                    Assume.That(RuntimeRealm.Current, Is.EqualTo(other));

                    RuntimeObject v1 = RuntimeRealm.Current.ResolveRuntimeObject(obj);
                    That(v0 != v1, "returned object should be different");
                    That(v0.Realm, Is.EqualTo(parentRealm));
                    That(v1.Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf() != v1.GetPrototypeOf(), "the prototype of returned object should be different");
                    That(v0.GetPrototypeOf().Realm, Is.EqualTo(parentRealm));
                    That(v1.GetPrototypeOf().Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf().GetPrototypeOf() != v1.GetPrototypeOf().GetPrototypeOf(), "all prototype ancestors of returned object should be different");

                    That(v0["MyMethod"], Is.TypeOf("function"));
                    That(v0["MyMethod"], Is.Not.EqualTo(v1["MyMethod"]), "property value of object type should be different");
                });
                RuntimeExecution.ContinueUntilEnd();
            });

            It("should expose native properties as getters and setters on protoype", () => {
                TestObject obj  = new TestObject();
                EcmaValue value = new EcmaValue(obj);
                value["Value"]  = 1;

                That(value["Value"], Is.EqualTo(1));
                That(obj.Value, Is.EqualTo(1), "Value should be reflected on native object");
                That(() => value["ReadOnlyValue"] = 2, Throws.Nothing, "Setting a readonly property does not throw exception");
                That(value["ReadOnlyValue"], Is.EqualTo(1), "Value should not be changed");

                RuntimeObject testObjectPrototype = value.ToObject().GetPrototypeOf();
                That(testObjectPrototype.GetOwnProperty("Value"), Is.Not.EqualTo(EcmaValue.Undefined), "Property should be defined on the reflected prototype object");
                That(testObjectPrototype.GetOwnProperty("Value").Get, Is.Not.EqualTo(EcmaValue.Undefined), "Property should be defined as getter/setter");
                That(testObjectPrototype.GetOwnProperty("ReadOnlyValue").Set, Is.Undefined, "Readonly property should have no setter");
            });

            It("should expose IList as an Array exotic object", () => {
                EcmaValue list = new EcmaValue(new List <object> {
                    1, 2, 3, new EcmaObject()
                });
                That(list["length"], Is.EqualTo(4));
                That(list.ToString(), Is.EqualTo("1,2,3,[object Object]"));
                That(Global.Json.Invoke("stringify", list), Is.EqualTo("[1,2,3,{}]"));
            });

            It("should expose IDictionary as an ordinary object for valid EcmaPropertyKey", () => {
                Hashtable ht  = new Hashtable();
                EcmaValue obj = new EcmaValue(ht);
                obj["prop"]   = 1;
                That(obj["prop"], Is.EqualTo(1));
                That(Global.Json.Invoke("stringify", obj), Is.EqualTo("{\"prop\":1}"));
            });
        }
Ejemplo n.º 3
0
 public InspectorMetaObject(RuntimeObject obj)
 {
     Guard.ArgumentNotNull(obj, "obj");
     this.EnumerableProperties    = new InspectorMetaObjectEntryCollection();
     this.NonEnumerableProperties = new InspectorMetaObjectEntryCollection();
     if (!(obj is RuntimeObjectProxy))
     {
         this.Prototype = new EcmaValue(obj.GetPrototypeOf());
         if (this.Prototype.IsNullOrUndefined)
         {
             this.Prototype = EcmaValue.Null;
         }
         for (RuntimeObject cur = obj; cur != null; cur = cur.GetPrototypeOf())
         {
             foreach (EcmaPropertyKey property in cur.GetOwnPropertyKeys())
             {
                 EcmaPropertyDescriptor descriptor = cur.GetOwnProperty(property);
                 if (descriptor == null || (descriptor.IsDataDescriptor && cur != obj))
                 {
                     continue;
                 }
                 EcmaValue value;
                 if (descriptor.IsDataDescriptor)
                 {
                     value = descriptor.Value;
                 }
                 else
                 {
                     try {
                         value = descriptor.Get.Call(obj);
                     } catch (EcmaException ex) {
                         value = String.Format("{0}: {1}", ex.ErrorType, ex.Message);
                     } catch (Exception ex) {
                         value = ex.Message;
                     }
                     if (cur == obj)
                     {
                         if (descriptor.Get)
                         {
                             NonEnumerableProperties.Add("get " + property, descriptor.Get);
                         }
                         if (descriptor.Set)
                         {
                             NonEnumerableProperties.Add("set " + property, descriptor.Get);
                         }
                     }
                 }
                 if (descriptor.Enumerable)
                 {
                     EnumerableProperties.Add(property, value);
                 }
                 else
                 {
                     NonEnumerableProperties.Add(property, value);
                 }
             }
         }
     }
     if (obj is IInspectorMetaProvider provider)
     {
         try {
             provider.FillInInspectorMetaObject(this);
         } catch { }
     }
 }