ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 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) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 44 | // Create mirror and JSON representation. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 45 | var mirror = debug.MakeMirror(obj); |
| 46 | var serializer = debug.MakeMirrorSerializer(); |
| 47 | var json = serializer.serializeValue(mirror); |
| 48 | var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 49 | |
| 50 | // Check the mirror hierachy. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 51 | assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy'); |
| 52 | assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierachy'); |
| 53 | assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierachy'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 54 | |
| 55 | // Check the mirror properties. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 56 | assertTrue(mirror.isObject(), 'Unexpected mirror'); |
| 57 | assertEquals('object', mirror.type(), 'Unexpected mirror type'); |
| 58 | assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror'); |
| 59 | assertEquals(cls_name, mirror.className(), 'Unexpected mirror class name'); |
| 60 | assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierachy'); |
| 61 | assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected constructor function name'); |
| 62 | assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierachy'); |
| 63 | assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierachy'); |
| 64 | assertFalse(mirror.hasNamedInterceptor(), 'No named interceptor expected'); |
| 65 | assertFalse(mirror.hasIndexedInterceptor(), 'No indexed interceptor expected'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 66 | |
| 67 | var names = mirror.propertyNames(); |
| 68 | var properties = mirror.properties() |
| 69 | assertEquals(names.length, properties.length); |
| 70 | for (var i = 0; i < properties.length; i++) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 71 | assertTrue(properties[i] instanceof debug.Mirror, 'Unexpected mirror hierachy'); |
| 72 | assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy'); |
| 73 | assertEquals('property', properties[i].type(), 'Unexpected mirror type'); |
| 74 | assertEquals(names[i], properties[i].name(), 'Unexpected property name'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 75 | } |
| 76 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 77 | for (var p in obj) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 78 | var property_mirror = mirror.property(p); |
| 79 | assertTrue(property_mirror instanceof debug.PropertyMirror); |
| 80 | assertEquals(p, property_mirror.name()); |
| 81 | // If the object has some special properties don't test for these. |
| 82 | if (!hasSpecialProperties) { |
| 83 | assertEquals(0, property_mirror.attributes(), property_mirror.name()); |
| 84 | assertFalse(property_mirror.isReadOnly()); |
| 85 | assertTrue(property_mirror.isEnum()); |
| 86 | assertTrue(property_mirror.canDelete()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Parse JSON representation and check. |
| 91 | var fromJSON = eval('(' + json + ')'); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 92 | assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON'); |
| 93 | assertEquals(cls_name, fromJSON.className, 'Unexpected mirror class name in JSON'); |
| 94 | assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.ref, 'Unexpected constructor function handle in JSON'); |
| 95 | assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, 'Unexpected constructor function type in JSON'); |
| 96 | assertEquals(ctor_name, refs.lookup(fromJSON.constructorFunction.ref).name, 'Unexpected constructor function name in JSON'); |
| 97 | assertEquals(mirror.protoObject().handle(), fromJSON.protoObject.ref, 'Unexpected proto object handle in JSON'); |
| 98 | assertEquals(mirror.protoObject().type(), refs.lookup(fromJSON.protoObject.ref).type, 'Unexpected proto object type in JSON'); |
| 99 | assertEquals(mirror.prototypeObject().handle(), fromJSON.prototypeObject.ref, 'Unexpected prototype object handle in JSON'); |
| 100 | assertEquals(mirror.prototypeObject().type(), refs.lookup(fromJSON.prototypeObject.ref).type, 'Unexpected prototype object type in JSON'); |
| 101 | assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON'); |
| 102 | assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expected in JSON'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 103 | |
| 104 | // Check that the serialization contains all properties. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 105 | assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 106 | for (var i = 0; i < fromJSON.properties.length; i++) { |
| 107 | var name = fromJSON.properties[i].name; |
| 108 | if (!name) name = fromJSON.properties[i].index; |
| 109 | var found = false; |
| 110 | for (var j = 0; j < names.length; j++) { |
| 111 | if (names[j] == name) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 112 | // Check that serialized handle is correct. |
| 113 | assertEquals(properties[i].value().handle(), fromJSON.properties[i].ref, 'Unexpected serialized handle'); |
| 114 | |
| 115 | // Check that serialized name is correct. |
| 116 | assertEquals(properties[i].name(), fromJSON.properties[i].name, 'Unexpected serialized name'); |
| 117 | |
| 118 | // If property type is normal property type is not serialized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 119 | if (properties[i].propertyType() != debug.PropertyType.Normal) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 120 | assertEquals(properties[i].propertyType(), fromJSON.properties[i].propertyType, 'Unexpected serialized property type'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 121 | } else { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 122 | assertTrue(typeof(fromJSON.properties[i].propertyType) === 'undefined', 'Unexpected serialized property type'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 123 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 124 | |
| 125 | // If there are no attributes attributes are not serialized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 126 | if (properties[i].attributes() != debug.PropertyAttribute.None) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 127 | assertEquals(properties[i].attributes(), fromJSON.properties[i].attributes, 'Unexpected serialized attributes'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 128 | } else { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 129 | assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined', 'Unexpected serialized attributes'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 130 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 131 | |
| 132 | // Lookup the serialized object from the handle reference. |
| 133 | var o = refs.lookup(fromJSON.properties[i].ref); |
| 134 | assertTrue(o != void 0, 'Referenced object is not serialized'); |
| 135 | |
| 136 | assertEquals(properties[i].value().type(), o.type, 'Unexpected serialized property type for ' + name); |
| 137 | if (properties[i].value().isPrimitive()) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 138 | // Special check for NaN as NaN == NaN is false. |
| 139 | if (properties[i].value().isNumber() && isNaN(properties[i].value().value())) { |
| 140 | assertEquals('NaN', o.value, 'Unexpected serialized property value for ' + name); |
| 141 | } else { |
| 142 | assertEquals(properties[i].value().value(), o.value, 'Unexpected serialized property value for ' + name); |
| 143 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 144 | } else if (properties[i].value().isFunction()) { |
| 145 | assertEquals(properties[i].value().source(), o.source, 'Unexpected serialized property value for ' + name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 146 | } |
| 147 | found = true; |
| 148 | } |
| 149 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 150 | assertTrue(found, '"' + name + '" not found (' + json + ')'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | function Point(x,y) { |
| 156 | this.x_ = x; |
| 157 | this.y_ = y; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | // Test a number of different objects. |
| 162 | testObjectMirror({}, 'Object', 'Object'); |
| 163 | testObjectMirror({'a':1,'b':2}, 'Object', 'Object'); |
| 164 | testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y);}}, 'Object', 'Object'); |
| 165 | testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point'); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 166 | testObjectMirror(this, 'global', '', true); // Global object has special properties |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 167 | testObjectMirror(this.__proto__, 'Object', ''); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 168 | testObjectMirror([], 'Array', 'Array'); |
| 169 | testObjectMirror([1,2], 'Array', 'Array'); |
| 170 | |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 171 | // Test circular references. |
| 172 | o = {}; |
| 173 | o.o = o; |
| 174 | testObjectMirror(o, 'Object', 'Object'); |
| 175 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 176 | // Test that non enumerable properties are part of the mirror |
| 177 | global_mirror = debug.MakeMirror(this); |
| 178 | assertEquals('property', global_mirror.property("Math").type()); |
| 179 | assertFalse(global_mirror.property("Math").isEnum(), "Math is enumerable" + global_mirror.property("Math").attributes()); |
| 180 | |
| 181 | math_mirror = global_mirror.property("Math").value(); |
| 182 | assertEquals('property', math_mirror.property("E").type()); |
| 183 | assertFalse(math_mirror.property("E").isEnum(), "Math.E is enumerable"); |
| 184 | assertTrue(math_mirror.property("E").isReadOnly()); |
| 185 | assertFalse(math_mirror.property("E").canDelete()); |
| 186 | |
| 187 | // Test objects with JavaScript accessors. |
| 188 | o = {} |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 189 | o.__defineGetter__('a', function(){return 'a';}); |
| 190 | o.__defineSetter__('b', function(){}); |
| 191 | o.__defineGetter__('c', function(){throw 'c';}); |
| 192 | o.__defineSetter__('c', function(){throw 'c';}); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 193 | testObjectMirror(o, 'Object', 'Object'); |
| 194 | mirror = debug.MakeMirror(o); |
| 195 | // a has getter but no setter. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 196 | assertTrue(mirror.property('a').hasGetter()); |
| 197 | assertFalse(mirror.property('a').hasSetter()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 198 | assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 199 | assertEquals('function', mirror.property('a').getter().type()); |
| 200 | assertEquals('undefined', mirror.property('a').setter().type()); |
| 201 | assertEquals('function (){return \'a\';}', mirror.property('a').getter().source()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 202 | // b has setter but no getter. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 203 | assertFalse(mirror.property('b').hasGetter()); |
| 204 | assertTrue(mirror.property('b').hasSetter()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 205 | assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 206 | assertEquals('undefined', mirror.property('b').getter().type()); |
| 207 | assertEquals('function', mirror.property('b').setter().type()); |
| 208 | assertEquals('function (){}', mirror.property('b').setter().source()); |
| 209 | assertFalse(mirror.property('b').isException()); |
| 210 | // c has both getter and setter. The getter throws an exception. |
| 211 | assertTrue(mirror.property('c').hasGetter()); |
| 212 | assertTrue(mirror.property('c').hasSetter()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 213 | assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 214 | assertEquals('function', mirror.property('c').getter().type()); |
| 215 | assertEquals('function', mirror.property('c').setter().type()); |
| 216 | assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source()); |
| 217 | assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 218 | |
| 219 | // Test objects with native accessors. |
| 220 | mirror = debug.MakeMirror(new String('abc')); |
| 221 | assertTrue(mirror instanceof debug.ObjectMirror); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 222 | assertFalse(mirror.property('length').hasGetter()); |
| 223 | assertFalse(mirror.property('length').hasSetter()); |
| 224 | assertTrue(mirror.property('length').isNative()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 225 | assertEquals('a', mirror.property(0).value().value()); |
| 226 | assertEquals('b', mirror.property(1).value().value()); |
| 227 | assertEquals('c', mirror.property(2).value().value()); |