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
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.
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.
28 // Flags: --expose-debug-as debug
29 // Test the mirror object for objects
31 function MirrorRefCache(json_refs) {
32 var tmp = eval('(' + json_refs + ')');
34 for (var i = 0; i < tmp.length; i++) {
35 this.refs_[tmp[i].handle] = tmp[i];
39 MirrorRefCache.prototype.lookup = function(handle) {
40 return this.refs_[handle];
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()));
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');
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 hierarchy');
62 assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected constructor function name');
63 assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierarchy');
64 assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierarchy');
65 assertFalse(mirror.hasNamedInterceptor(), 'No named interceptor expected');
66 assertFalse(mirror.hasIndexedInterceptor(), 'No indexed interceptor expected');
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 hierarchy');
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');
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, 'Unexpected mirror hierarchy');
82 assertEquals('internalProperty', internalProperties[i].type(), 'Unexpected mirror type');
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());
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 JSON');
102 assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.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, 'Unexpected constructor function name in JSON');
105 assertEquals(mirror.protoObject().handle(), fromJSON.protoObject.ref, 'Unexpected 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.prototypeObject.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 expected in JSON');
112 // Check that the serialization contains all properties.
113 assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON');
114 for (var j = 0; j < names.length; 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;
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');
125 // Check that serialized name is correct.
126 assertEquals(properties[i].name(), fromJSON.properties[i].name, 'Unexpected serialized name');
128 // If property type is normal property type is not serialized.
129 if (properties[i].propertyType() != debug.PropertyType.Normal) {
130 assertEquals(properties[i].propertyType(), fromJSON.properties[i].propertyType, 'Unexpected serialized property type');
132 assertTrue(typeof(fromJSON.properties[i].propertyType) === 'undefined', 'Unexpected serialized property type');
135 // If there are no attributes attributes are not serialized.
136 if (properties[i].attributes() != debug.PropertyAttribute.None) {
137 assertEquals(properties[i].attributes(), fromJSON.properties[i].attributes, 'Unexpected serialized attributes');
139 assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined', 'Unexpected serialized attributes');
142 // Lookup the serialized object from the handle reference.
143 var o = refs.lookup(fromJSON.properties[i].ref);
144 assertTrue(o != void 0, 'Referenced object is not serialized');
146 assertEquals(properties[i].value().type(), o.type, 'Unexpected serialized property type for ' + name);
147 if (properties[i].value().isPrimitive()) {
148 if (properties[i].value().type() == "null" ||
149 properties[i].value().type() == "undefined") {
150 // Null and undefined has no value property.
151 assertFalse("value" in o, 'Unexpected value property for ' + name);
152 } else if (properties[i].value().type() == "number" &&
153 !isFinite(properties[i].value().value())) {
154 assertEquals(String(properties[i].value().value()), o.value,
155 'Unexpected serialized property value for ' + name);
157 assertEquals(properties[i].value().value(), o.value, 'Unexpected serialized property value for ' + name);
159 } else if (properties[i].value().isFunction()) {
160 assertEquals(properties[i].value().source(), o.source, 'Unexpected serialized property value for ' + name);
165 assertTrue(found, '"' + name + '" not found (' + json + ')');
170 function Point(x,y) {
175 var object_with_symbol = {};
176 object_with_symbol[Symbol.iterator] = 42;
178 // Test a number of different objects.
179 testObjectMirror({}, 'Object', 'Object');
180 testObjectMirror({'a':1,'b':2}, 'Object', 'Object');
181 testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y);}}, 'Object', 'Object');
182 testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point');
183 testObjectMirror(this, 'global', '', true); // Global object has special properties
184 testObjectMirror(this.__proto__, 'Object', '');
185 testObjectMirror([], 'Array', 'Array');
186 testObjectMirror([1,2], 'Array', 'Array');
187 testObjectMirror(Object(17), 'Number', 'Number');
188 testObjectMirror(object_with_symbol, 'Object', 'Object');
190 // Test circular references.
193 testObjectMirror(o, 'Object', 'Object');
195 // Test that non enumerable properties are part of the mirror
196 global_mirror = debug.MakeMirror(this);
197 assertEquals('property', global_mirror.property("Math").type());
198 assertFalse(global_mirror.property("Math").isEnum(), "Math is enumerable" + global_mirror.property("Math").attributes());
200 math_mirror = global_mirror.property("Math").value();
201 assertEquals('property', math_mirror.property("E").type());
202 assertFalse(math_mirror.property("E").isEnum(), "Math.E is enumerable");
203 assertTrue(math_mirror.property("E").isReadOnly());
204 assertFalse(math_mirror.property("E").canDelete());
206 // Test objects with JavaScript accessors.
208 o.__defineGetter__('a', function(){return 'a';});
209 o.__defineSetter__('b', function(){});
210 o.__defineGetter__('c', function(){throw 'c';});
211 o.__defineSetter__('c', function(){throw 'c';});
212 testObjectMirror(o, 'Object', 'Object');
213 mirror = debug.MakeMirror(o);
214 // a has getter but no setter.
215 assertTrue(mirror.property('a').hasGetter());
216 assertFalse(mirror.property('a').hasSetter());
217 assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType());
218 assertEquals('function', mirror.property('a').getter().type());
219 assertEquals('undefined', mirror.property('a').setter().type());
220 assertEquals('function (){return \'a\';}', mirror.property('a').getter().source());
221 // b has setter but no getter.
222 assertFalse(mirror.property('b').hasGetter());
223 assertTrue(mirror.property('b').hasSetter());
224 assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType());
225 assertEquals('undefined', mirror.property('b').getter().type());
226 assertEquals('function', mirror.property('b').setter().type());
227 assertEquals('function (){}', mirror.property('b').setter().source());
228 assertFalse(mirror.property('b').isException());
229 // c has both getter and setter. The getter throws an exception.
230 assertTrue(mirror.property('c').hasGetter());
231 assertTrue(mirror.property('c').hasSetter());
232 assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType());
233 assertEquals('function', mirror.property('c').getter().type());
234 assertEquals('function', mirror.property('c').setter().type());
235 assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source());
236 assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source());
238 // Test objects with native accessors.
239 mirror = debug.MakeMirror(new String('abc'));
240 assertTrue(mirror instanceof debug.ObjectMirror);
241 assertFalse(mirror.property('length').hasGetter());
242 assertFalse(mirror.property('length').hasSetter());
243 assertTrue(mirror.property('length').isNative());
244 assertEquals('a', mirror.property(0).value().value());
245 assertEquals('b', mirror.property(1).value().value());
246 assertEquals('c', mirror.property(2).value().value());
248 // Test value wrapper internal properties.
249 mirror = debug.MakeMirror(Object("Capybara"));
250 var ip = mirror.internalProperties();
251 assertEquals(1, ip.length);
252 assertEquals("[[PrimitiveValue]]", ip[0].name());
253 assertEquals("string", ip[0].value().type());
254 assertEquals("Capybara", ip[0].value().value());
256 // Test bound function internal properties.
257 mirror = debug.MakeMirror(Number.bind(Array, 2));
258 ip = mirror.internalProperties();
259 assertEquals(3, ip.length);
260 var property_map = {};
261 for (var i = 0; i < ip.length; i++) {
262 property_map[ip[i].name()] = ip[i];
264 assertTrue("[[BoundThis]]" in property_map);
265 assertEquals("function", property_map["[[BoundThis]]"].value().type());
266 assertEquals(Array, property_map["[[BoundThis]]"].value().value());
267 assertTrue("[[TargetFunction]]" in property_map);
268 assertEquals("function", property_map["[[TargetFunction]]"].value().type());
269 assertEquals(Number, property_map["[[TargetFunction]]"].value().value());
270 assertTrue("[[BoundArgs]]" in property_map);
271 assertEquals("object", property_map["[[BoundArgs]]"].value().type());
272 assertEquals(1, property_map["[[BoundArgs]]"].value().value().length);