Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // Flags: --expose-debug-as debug
     29 // Test the mirror object for objects
     30 
     31 function MirrorRefCache(json_refs) {
     32   var tmp = eval('(' + json_refs + ')');
     33   this.refs_ = [];
     34   for (var i = 0; i < tmp.length; i++) {
     35     this.refs_[tmp[i].handle] = tmp[i];
     36   }
     37 }
     38 
     39 MirrorRefCache.prototype.lookup = function(handle) {
     40   return this.refs_[handle];
     41 };
     42 
     43 function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) {
     44   // Create mirror and JSON representation.
     45   var mirror = debug.MakeMirror(obj);
     46   var serializer = debug.MakeMirrorSerializer();
     47   var json = JSON.stringify(serializer.serializeValue(mirror));
     48   var refs = new MirrorRefCache(
     49       JSON.stringify(serializer.serializeReferencedObjects()));
     50 
     51   // Check the mirror hierachy.
     52   assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy');
     53   assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierachy');
     54   assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
     55 
     56   // Check the mirror properties.
     57   assertTrue(mirror.isObject(), 'Unexpected mirror');
     58   assertEquals('object', mirror.type(), 'Unexpected mirror type');
     59   assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror');
     60   assertEquals(cls_name, mirror.className(), 'Unexpected mirror class name');
     61   assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
     62   assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected constructor function name');
     63   assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
     64   assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
     65   assertFalse(mirror.hasNamedInterceptor(), 'No named interceptor expected');
     66   assertFalse(mirror.hasIndexedInterceptor(), 'No indexed interceptor expected');
     67 
     68   var names = mirror.propertyNames();
     69   var properties = mirror.properties();
     70   assertEquals(names.length, properties.length);
     71   for (var i = 0; i < properties.length; i++) {
     72     assertTrue(properties[i] instanceof debug.Mirror, 'Unexpected mirror hierachy');
     73     assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy');
     74     assertEquals('property', properties[i].type(), 'Unexpected mirror type');
     75     assertEquals(names[i], properties[i].name(), 'Unexpected property name');
     76   }
     77 
     78   for (var p in obj) {
     79     var property_mirror = mirror.property(p);
     80     assertTrue(property_mirror instanceof debug.PropertyMirror);
     81     assertEquals(p, property_mirror.name());
     82     // If the object has some special properties don't test for these.
     83     if (!hasSpecialProperties) {
     84       assertEquals(0, property_mirror.attributes(), property_mirror.name());
     85       assertFalse(property_mirror.isReadOnly());
     86       assertTrue(property_mirror.isEnum());
     87       assertTrue(property_mirror.canDelete());
     88     }
     89   }
     90 
     91   // Parse JSON representation and check.
     92   var fromJSON = eval('(' + json + ')');
     93   assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON');
     94   assertEquals(cls_name, fromJSON.className, 'Unexpected mirror class name in JSON');
     95   assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.ref, 'Unexpected constructor function handle in JSON');
     96   assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, 'Unexpected constructor function type in JSON');
     97   assertEquals(ctor_name, refs.lookup(fromJSON.constructorFunction.ref).name, 'Unexpected constructor function name in JSON');
     98   assertEquals(mirror.protoObject().handle(), fromJSON.protoObject.ref, 'Unexpected proto object handle in JSON');
     99   assertEquals(mirror.protoObject().type(), refs.lookup(fromJSON.protoObject.ref).type, 'Unexpected proto object type in JSON');
    100   assertEquals(mirror.prototypeObject().handle(), fromJSON.prototypeObject.ref, 'Unexpected prototype object handle in JSON');
    101   assertEquals(mirror.prototypeObject().type(), refs.lookup(fromJSON.prototypeObject.ref).type, 'Unexpected prototype object type in JSON');
    102   assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON');
    103   assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expected in JSON');
    104 
    105   // Check that the serialization contains all properties.
    106   assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON');
    107   for (var i = 0; i < fromJSON.properties.length; i++) {
    108     var name = fromJSON.properties[i].name;
    109     if (typeof name == 'undefined') name = fromJSON.properties[i].index;
    110     var found = false;
    111     for (var j = 0; j < names.length; j++) {
    112       if (names[j] == name) {
    113         // Check that serialized handle is correct.
    114         assertEquals(properties[i].value().handle(), fromJSON.properties[i].ref, 'Unexpected serialized handle');
    115 
    116         // Check that serialized name is correct.
    117         assertEquals(properties[i].name(), fromJSON.properties[i].name, 'Unexpected serialized name');
    118 
    119         // If property type is normal property type is not serialized.
    120         if (properties[i].propertyType() != debug.PropertyType.Normal) {
    121           assertEquals(properties[i].propertyType(), fromJSON.properties[i].propertyType, 'Unexpected serialized property type');
    122         } else {
    123           assertTrue(typeof(fromJSON.properties[i].propertyType) === 'undefined', 'Unexpected serialized property type');
    124         }
    125 
    126         // If there are no attributes attributes are not serialized.
    127         if (properties[i].attributes() != debug.PropertyAttribute.None) {
    128           assertEquals(properties[i].attributes(), fromJSON.properties[i].attributes, 'Unexpected serialized attributes');
    129         } else {
    130           assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined', 'Unexpected serialized attributes');
    131         }
    132 
    133         // Lookup the serialized object from the handle reference.
    134         var o = refs.lookup(fromJSON.properties[i].ref);
    135         assertTrue(o != void 0, 'Referenced object is not serialized');
    136 
    137         assertEquals(properties[i].value().type(), o.type, 'Unexpected serialized property type for ' + name);
    138         if (properties[i].value().isPrimitive()) {
    139           if (properties[i].value().type() == "null" ||
    140               properties[i].value().type() == "undefined") {
    141             // Null and undefined has no value property.
    142             assertFalse("value" in o, 'Unexpected value property for ' + name);
    143           } else if (properties[i].value().type() == "number" &&
    144                      !isFinite(properties[i].value().value())) {
    145             assertEquals(String(properties[i].value().value()), o.value,
    146                          'Unexpected serialized property value for ' + name);
    147           } else {
    148             assertEquals(properties[i].value().value(), o.value, 'Unexpected serialized property value for ' + name);
    149           }
    150         } else if (properties[i].value().isFunction()) {
    151           assertEquals(properties[i].value().source(), o.source, 'Unexpected serialized property value for ' + name);
    152         }
    153         found = true;
    154       }
    155     }
    156     assertTrue(found, '"' + name + '" not found (' + json + ')');
    157   }
    158 }
    159 
    160 
    161 function Point(x,y) {
    162   this.x_ = x;
    163   this.y_ = y;
    164 }
    165 
    166 // Test a number of different objects.
    167 testObjectMirror({}, 'Object', 'Object');
    168 testObjectMirror({'a':1,'b':2}, 'Object', 'Object');
    169 testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y);}}, 'Object', 'Object');
    170 testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point');
    171 testObjectMirror(this, 'global', '', true);  // Global object has special properties
    172 testObjectMirror(this.__proto__, 'Object', '');
    173 testObjectMirror([], 'Array', 'Array');
    174 testObjectMirror([1,2], 'Array', 'Array');
    175 
    176 // Test circular references.
    177 o = {};
    178 o.o = o;
    179 testObjectMirror(o, 'Object', 'Object');
    180 
    181 // Test that non enumerable properties are part of the mirror
    182 global_mirror = debug.MakeMirror(this);
    183 assertEquals('property', global_mirror.property("Math").type());
    184 assertFalse(global_mirror.property("Math").isEnum(), "Math is enumerable" + global_mirror.property("Math").attributes());
    185 
    186 math_mirror = global_mirror.property("Math").value();
    187 assertEquals('property', math_mirror.property("E").type());
    188 assertFalse(math_mirror.property("E").isEnum(), "Math.E is enumerable");
    189 assertTrue(math_mirror.property("E").isReadOnly());
    190 assertFalse(math_mirror.property("E").canDelete());
    191 
    192 // Test objects with JavaScript accessors.
    193 o = {}
    194 o.__defineGetter__('a', function(){return 'a';});
    195 o.__defineSetter__('b', function(){});
    196 o.__defineGetter__('c', function(){throw 'c';});
    197 o.__defineSetter__('c', function(){throw 'c';});
    198 testObjectMirror(o, 'Object', 'Object');
    199 mirror = debug.MakeMirror(o);
    200 // a has getter but no setter.
    201 assertTrue(mirror.property('a').hasGetter());
    202 assertFalse(mirror.property('a').hasSetter());
    203 assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType());
    204 assertEquals('function', mirror.property('a').getter().type());
    205 assertEquals('undefined', mirror.property('a').setter().type());
    206 assertEquals('function (){return \'a\';}', mirror.property('a').getter().source());
    207 // b has setter but no getter.
    208 assertFalse(mirror.property('b').hasGetter());
    209 assertTrue(mirror.property('b').hasSetter());
    210 assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType());
    211 assertEquals('undefined', mirror.property('b').getter().type());
    212 assertEquals('function', mirror.property('b').setter().type());
    213 assertEquals('function (){}', mirror.property('b').setter().source());
    214 assertFalse(mirror.property('b').isException());
    215 // c has both getter and setter. The getter throws an exception.
    216 assertTrue(mirror.property('c').hasGetter());
    217 assertTrue(mirror.property('c').hasSetter());
    218 assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType());
    219 assertEquals('function', mirror.property('c').getter().type());
    220 assertEquals('function', mirror.property('c').setter().type());
    221 assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source());
    222 assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source());
    223 
    224 // Test objects with native accessors.
    225 mirror = debug.MakeMirror(new String('abc'));
    226 assertTrue(mirror instanceof debug.ObjectMirror);
    227 assertFalse(mirror.property('length').hasGetter());
    228 assertFalse(mirror.property('length').hasSetter());
    229 assertTrue(mirror.property('length').isNative());
    230 assertEquals('a', mirror.property(0).value().value());
    231 assertEquals('b', mirror.property(1).value().value());
    232 assertEquals('c', mirror.property(2).value().value());
    233