| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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 hierarchy'); | |
| 53 assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierarchy')
; | |
| 54 assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierarchy'
); | |
| 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, 'Unexpe
cted mirror hierarchy'); | |
| 62 assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected const
ructor function name'); | |
| 63 assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hi
erarchy'); | |
| 64 assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirro
r hierarchy'); | |
| 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 hierarc
hy'); | |
| 73 assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror
hierarchy'); | |
| 74 assertEquals('property', properties[i].type(), 'Unexpected mirror type'); | |
| 75 assertEquals(names[i], properties[i].name(), 'Unexpected property name'); | |
| 76 } | |
| 77 | |
| 78 var internalProperties = mirror.internalProperties(); | |
| 79 for (var i = 0; i < internalProperties.length; i++) { | |
| 80 assertTrue(internalProperties[i] instanceof debug.Mirror, 'Unexpected mirror
hierarchy'); | |
| 81 assertTrue(internalProperties[i] instanceof debug.InternalPropertyMirror, 'U
nexpected mirror hierarchy'); | |
| 82 assertEquals('internalProperty', internalProperties[i].type(), 'Unexpected m
irror type'); | |
| 83 } | |
| 84 | |
| 85 for (var p in obj) { | |
| 86 var property_mirror = mirror.property(p); | |
| 87 assertTrue(property_mirror instanceof debug.PropertyMirror); | |
| 88 assertEquals(p, property_mirror.name()); | |
| 89 // If the object has some special properties don't test for these. | |
| 90 if (!hasSpecialProperties) { | |
| 91 assertEquals(0, property_mirror.attributes(), property_mirror.name()); | |
| 92 assertFalse(property_mirror.isReadOnly()); | |
| 93 assertTrue(property_mirror.isEnum()); | |
| 94 assertTrue(property_mirror.canDelete()); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 // Parse JSON representation and check. | |
| 99 var fromJSON = eval('(' + json + ')'); | |
| 100 assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON'); | |
| 101 assertEquals(cls_name, fromJSON.className, 'Unexpected mirror class name in JS
ON'); | |
| 102 assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFuncti
on.ref, 'Unexpected constructor function handle in JSON'); | |
| 103 assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, '
Unexpected constructor function type in JSON'); | |
| 104 assertEquals(ctor_name, refs.lookup(fromJSON.constructorFunction.ref).name, 'U
nexpected constructor function name in JSON'); | |
| 105 assertEquals(mirror.protoObject().handle(), fromJSON.protoObject.ref, 'Unexpec
ted proto object handle in JSON'); | |
| 106 assertEquals(mirror.protoObject().type(), refs.lookup(fromJSON.protoObject.ref
).type, 'Unexpected proto object type in JSON'); | |
| 107 assertEquals(mirror.prototypeObject().handle(), fromJSON.prototypeObject.ref,
'Unexpected prototype object handle in JSON'); | |
| 108 assertEquals(mirror.prototypeObject().type(), refs.lookup(fromJSON.prototypeOb
ject.ref).type, 'Unexpected prototype object type in JSON'); | |
| 109 assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected
in JSON'); | |
| 110 assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expe
cted in JSON'); | |
| 111 | |
| 112 // Check that the serialization contains all properties. | |
| 113 assertEquals(names.length, fromJSON.properties.length, 'Some properties missin
g in JSON'); | |
| 114 for (var j = 0; j < names.length; j++) { | |
| 115 var name = names[j]; | |
| 116 // Serialization of symbol-named properties to JSON doesn't really | |
| 117 // work currently, as they don't get a {name: ...} entry. | |
| 118 if (typeof name === 'symbol') continue; | |
| 119 var found = false; | |
| 120 for (var i = 0; i < fromJSON.properties.length; i++) { | |
| 121 if (fromJSON.properties[i].name == name) { | |
| 122 // Check that serialized handle is correct. | |
| 123 assertEquals(properties[i].value().handle(), fromJSON.properties[i].ref,
'Unexpected serialized handle'); | |
| 124 | |
| 125 // Check that serialized name is correct. | |
| 126 assertEquals(properties[i].name(), fromJSON.properties[i].name, 'Unexpec
ted serialized name'); | |
| 127 | |
| 128 assertEquals(properties[i].propertyType(), fromJSON.properties[i].proper
tyType, 'Unexpected serialized property type'); | |
| 129 | |
| 130 // If there are no attributes attributes are not serialized. | |
| 131 if (properties[i].attributes() != debug.PropertyAttribute.None) { | |
| 132 assertEquals(properties[i].attributes(), fromJSON.properties[i].attrib
utes, 'Unexpected serialized attributes'); | |
| 133 } else { | |
| 134 assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined',
'Unexpected serialized attributes'); | |
| 135 } | |
| 136 | |
| 137 // Lookup the serialized object from the handle reference. | |
| 138 var o = refs.lookup(fromJSON.properties[i].ref); | |
| 139 assertTrue(o != void 0, 'Referenced object is not serialized'); | |
| 140 | |
| 141 assertEquals(properties[i].value().type(), o.type, 'Unexpected serialize
d property type for ' + name); | |
| 142 if (properties[i].value().isPrimitive()) { | |
| 143 if (properties[i].value().type() == "null" || | |
| 144 properties[i].value().type() == "undefined") { | |
| 145 // Null and undefined has no value property. | |
| 146 assertFalse("value" in o, 'Unexpected value property for ' + name); | |
| 147 } else if (properties[i].value().type() == "number" && | |
| 148 !isFinite(properties[i].value().value())) { | |
| 149 assertEquals(String(properties[i].value().value()), o.value, | |
| 150 'Unexpected serialized property value for ' + name); | |
| 151 } else { | |
| 152 assertEquals(properties[i].value().value(), o.value, 'Unexpected ser
ialized property value for ' + name); | |
| 153 } | |
| 154 } else if (properties[i].value().isFunction()) { | |
| 155 assertEquals(properties[i].value().source(), o.source, 'Unexpected ser
ialized property value for ' + name); | |
| 156 } | |
| 157 found = true; | |
| 158 } | |
| 159 } | |
| 160 assertTrue(found, '"' + name + '" not found (' + json + ')'); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 | |
| 165 function Point(x,y) { | |
| 166 this.x_ = x; | |
| 167 this.y_ = y; | |
| 168 } | |
| 169 | |
| 170 var object_with_symbol = {}; | |
| 171 object_with_symbol[Symbol.iterator] = 42; | |
| 172 | |
| 173 // Test a number of different objects. | |
| 174 testObjectMirror({}, 'Object', 'Object'); | |
| 175 testObjectMirror({'a':1,'b':2}, 'Object', 'Object'); | |
| 176 testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y)
;}}, 'Object', 'Object'); | |
| 177 testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point'); | |
| 178 testObjectMirror(this, 'global', '', true); // Global object has special proper
ties | |
| 179 testObjectMirror(this.__proto__, 'Object', ''); | |
| 180 testObjectMirror([], 'Array', 'Array'); | |
| 181 testObjectMirror([1,2], 'Array', 'Array'); | |
| 182 testObjectMirror(Object(17), 'Number', 'Number'); | |
| 183 testObjectMirror(object_with_symbol, 'Object', 'Object'); | |
| 184 | |
| 185 // Test circular references. | |
| 186 o = {}; | |
| 187 o.o = o; | |
| 188 testObjectMirror(o, 'Object', 'Object'); | |
| 189 | |
| 190 // Test that non enumerable properties are part of the mirror | |
| 191 global_mirror = debug.MakeMirror(this); | |
| 192 assertEquals('property', global_mirror.property("Math").type()); | |
| 193 assertFalse(global_mirror.property("Math").isEnum(), "Math is enumerable" + glob
al_mirror.property("Math").attributes()); | |
| 194 | |
| 195 math_mirror = global_mirror.property("Math").value(); | |
| 196 assertEquals('property', math_mirror.property("E").type()); | |
| 197 assertFalse(math_mirror.property("E").isEnum(), "Math.E is enumerable"); | |
| 198 assertTrue(math_mirror.property("E").isReadOnly()); | |
| 199 assertFalse(math_mirror.property("E").canDelete()); | |
| 200 | |
| 201 // Test objects with JavaScript accessors. | |
| 202 o = {} | |
| 203 o.__defineGetter__('a', function (){return 'a';}); | |
| 204 o.__defineSetter__('b', function (){}); | |
| 205 o.__defineGetter__('c', function (){throw 'c';}); | |
| 206 o.__defineSetter__('c', function (){throw 'c';}); | |
| 207 testObjectMirror(o, 'Object', 'Object'); | |
| 208 mirror = debug.MakeMirror(o); | |
| 209 // a has getter but no setter. | |
| 210 assertTrue(mirror.property('a').hasGetter()); | |
| 211 assertFalse(mirror.property('a').hasSetter()); | |
| 212 assertEquals(debug.PropertyType.AccessorConstant, mirror.property('a').propertyT
ype()); | |
| 213 assertEquals('function', mirror.property('a').getter().type()); | |
| 214 assertEquals('undefined', mirror.property('a').setter().type()); | |
| 215 assertEquals('function (){return \'a\';}', mirror.property('a').getter().source(
)); | |
| 216 // b has setter but no getter. | |
| 217 assertFalse(mirror.property('b').hasGetter()); | |
| 218 assertTrue(mirror.property('b').hasSetter()); | |
| 219 assertEquals(debug.PropertyType.AccessorConstant, mirror.property('b').propertyT
ype()); | |
| 220 assertEquals('undefined', mirror.property('b').getter().type()); | |
| 221 assertEquals('function', mirror.property('b').setter().type()); | |
| 222 assertEquals('function (){}', mirror.property('b').setter().source()); | |
| 223 assertFalse(mirror.property('b').isException()); | |
| 224 // c has both getter and setter. The getter throws an exception. | |
| 225 assertTrue(mirror.property('c').hasGetter()); | |
| 226 assertTrue(mirror.property('c').hasSetter()); | |
| 227 assertEquals(debug.PropertyType.AccessorConstant, mirror.property('c').propertyT
ype()); | |
| 228 assertEquals('function', mirror.property('c').getter().type()); | |
| 229 assertEquals('function', mirror.property('c').setter().type()); | |
| 230 assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source()
); | |
| 231 assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source()
); | |
| 232 | |
| 233 // Test objects with native accessors. | |
| 234 mirror = debug.MakeMirror(new String('abc')); | |
| 235 assertTrue(mirror instanceof debug.ObjectMirror); | |
| 236 assertFalse(mirror.property('length').hasGetter()); | |
| 237 assertFalse(mirror.property('length').hasSetter()); | |
| 238 assertTrue(mirror.property('length').isNative()); | |
| 239 assertEquals('a', mirror.property(0).value().value()); | |
| 240 assertEquals('b', mirror.property(1).value().value()); | |
| 241 assertEquals('c', mirror.property(2).value().value()); | |
| 242 | |
| 243 // Test value wrapper internal properties. | |
| 244 mirror = debug.MakeMirror(Object("Capybara")); | |
| 245 var ip = mirror.internalProperties(); | |
| 246 assertEquals(1, ip.length); | |
| 247 assertEquals("[[PrimitiveValue]]", ip[0].name()); | |
| 248 assertEquals("string", ip[0].value().type()); | |
| 249 assertEquals("Capybara", ip[0].value().value()); | |
| 250 | |
| 251 // Test bound function internal properties. | |
| 252 mirror = debug.MakeMirror(Number.bind(Array, 2)); | |
| 253 ip = mirror.internalProperties(); | |
| 254 assertEquals(3, ip.length); | |
| 255 var property_map = {}; | |
| 256 for (var i = 0; i < ip.length; i++) { | |
| 257 property_map[ip[i].name()] = ip[i]; | |
| 258 } | |
| 259 assertTrue("[[BoundThis]]" in property_map); | |
| 260 assertEquals("function", property_map["[[BoundThis]]"].value().type()); | |
| 261 assertEquals(Array, property_map["[[BoundThis]]"].value().value()); | |
| 262 assertTrue("[[TargetFunction]]" in property_map); | |
| 263 assertEquals("function", property_map["[[TargetFunction]]"].value().type()); | |
| 264 assertEquals(Number, property_map["[[TargetFunction]]"].value().value()); | |
| 265 assertTrue("[[BoundArgs]]" in property_map); | |
| 266 assertEquals("object", property_map["[[BoundArgs]]"].value().type()); | |
| 267 assertEquals(1, property_map["[[BoundArgs]]"].value().value().length); | |
| 268 | |
| 269 // Test JSProxy internal properties. | |
| 270 var target = {}; | |
| 271 var handler = { | |
| 272 get: function (target, name, receiver) { | |
| 273 return target[name]; | |
| 274 }, | |
| 275 set: function(target, name, value, receiver) { | |
| 276 target[name] = value; | |
| 277 return value; | |
| 278 } | |
| 279 } | |
| 280 ip = debug.ObjectMirror.GetInternalProperties(new Proxy(target, handler)); | |
| 281 assertEquals(3, ip.length); | |
| 282 var property_map = {}; | |
| 283 for (var i = 0; i < ip.length; i++) { | |
| 284 property_map[ip[i].name()] = ip[i]; | |
| 285 } | |
| 286 assertTrue("[[Target]]" in property_map); | |
| 287 assertEquals(target, property_map["[[Target]]"].value().value()); | |
| 288 assertTrue("[[Handler]]" in property_map); | |
| 289 assertEquals(handler, property_map["[[Handler]]"].value().value()); | |
| 290 assertTrue("[[IsRevoked]]" in property_map); | |
| 291 assertEquals(false, property_map["[[IsRevoked]]"].value().value()); | |
| OLD | NEW |