blob: 91d0f82fe49fad68d840a40e993786aaa1e69baa [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
31function 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
39MirrorRefCache.prototype.lookup = function(handle) {
40 return this.refs_[handle];
Ben Murdoch257744e2011-11-30 15:57:28 +000041};
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43function 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.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 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');
Steve Blocka7e24c12009-10-30 11:49:00 +000055
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');
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierarchy');
Steve Blocka7e24c12009-10-30 11:49:00 +000062 assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected constructor function name');
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierarchy');
64 assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierarchy');
Steve Blocka7e24c12009-10-30 11:49:00 +000065 assertFalse(mirror.hasNamedInterceptor(), 'No named interceptor expected');
66 assertFalse(mirror.hasIndexedInterceptor(), 'No indexed interceptor expected');
67
68 var names = mirror.propertyNames();
Ben Murdoch257744e2011-11-30 15:57:28 +000069 var properties = mirror.properties();
Steve Blocka7e24c12009-10-30 11:49:00 +000070 assertEquals(names.length, properties.length);
71 for (var i = 0; i < properties.length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 assertTrue(properties[i] instanceof debug.Mirror, 'Unexpected mirror hierarchy');
73 assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierarchy');
Steve Blocka7e24c12009-10-30 11:49:00 +000074 assertEquals('property', properties[i].type(), 'Unexpected mirror type');
75 assertEquals(names[i], properties[i].name(), 'Unexpected property name');
76 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010077
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 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');
83 }
84
Steve Blocka7e24c12009-10-30 11:49:00 +000085 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 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');
111
112 // Check that the serialization contains all properties.
113 assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 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;
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 var found = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 for (var i = 0; i < fromJSON.properties.length; i++) {
121 if (fromJSON.properties[i].name == name) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 // 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, 'Unexpected serialized name');
127
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400128 assertEquals(properties[i].propertyType(), fromJSON.properties[i].propertyType, 'Unexpected serialized property type');
Steve Blocka7e24c12009-10-30 11:49:00 +0000129
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].attributes, 'Unexpected serialized attributes');
133 } else {
134 assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined', 'Unexpected serialized attributes');
135 }
136
Ben Murdoch257744e2011-11-30 15:57:28 +0000137 // Lookup the serialized object from the handle reference.
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 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 serialized property type for ' + name);
142 if (properties[i].value().isPrimitive()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000143 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 } else {
152 assertEquals(properties[i].value().value(), o.value, 'Unexpected serialized property value for ' + name);
153 }
154 } else if (properties[i].value().isFunction()) {
155 assertEquals(properties[i].value().source(), o.source, 'Unexpected serialized property value for ' + name);
156 }
157 found = true;
158 }
159 }
160 assertTrue(found, '"' + name + '" not found (' + json + ')');
161 }
162}
163
164
165function Point(x,y) {
166 this.x_ = x;
167 this.y_ = y;
168}
169
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170var object_with_symbol = {};
171object_with_symbol[Symbol.iterator] = 42;
172
Steve Blocka7e24c12009-10-30 11:49:00 +0000173// Test a number of different objects.
174testObjectMirror({}, 'Object', 'Object');
175testObjectMirror({'a':1,'b':2}, 'Object', 'Object');
176testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y);}}, 'Object', 'Object');
177testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point');
178testObjectMirror(this, 'global', '', true); // Global object has special properties
179testObjectMirror(this.__proto__, 'Object', '');
180testObjectMirror([], 'Array', 'Array');
181testObjectMirror([1,2], 'Array', 'Array');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182testObjectMirror(Object(17), 'Number', 'Number');
183testObjectMirror(object_with_symbol, 'Object', 'Object');
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185// Test circular references.
186o = {};
187o.o = o;
188testObjectMirror(o, 'Object', 'Object');
189
190// Test that non enumerable properties are part of the mirror
191global_mirror = debug.MakeMirror(this);
192assertEquals('property', global_mirror.property("Math").type());
193assertFalse(global_mirror.property("Math").isEnum(), "Math is enumerable" + global_mirror.property("Math").attributes());
194
195math_mirror = global_mirror.property("Math").value();
196assertEquals('property', math_mirror.property("E").type());
197assertFalse(math_mirror.property("E").isEnum(), "Math.E is enumerable");
198assertTrue(math_mirror.property("E").isReadOnly());
199assertFalse(math_mirror.property("E").canDelete());
200
201// Test objects with JavaScript accessors.
202o = {}
203o.__defineGetter__('a', function(){return 'a';});
204o.__defineSetter__('b', function(){});
205o.__defineGetter__('c', function(){throw 'c';});
206o.__defineSetter__('c', function(){throw 'c';});
207testObjectMirror(o, 'Object', 'Object');
208mirror = debug.MakeMirror(o);
209// a has getter but no setter.
210assertTrue(mirror.property('a').hasGetter());
211assertFalse(mirror.property('a').hasSetter());
212assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType());
213assertEquals('function', mirror.property('a').getter().type());
214assertEquals('undefined', mirror.property('a').setter().type());
215assertEquals('function (){return \'a\';}', mirror.property('a').getter().source());
216// b has setter but no getter.
217assertFalse(mirror.property('b').hasGetter());
218assertTrue(mirror.property('b').hasSetter());
219assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType());
220assertEquals('undefined', mirror.property('b').getter().type());
221assertEquals('function', mirror.property('b').setter().type());
222assertEquals('function (){}', mirror.property('b').setter().source());
223assertFalse(mirror.property('b').isException());
224// c has both getter and setter. The getter throws an exception.
225assertTrue(mirror.property('c').hasGetter());
226assertTrue(mirror.property('c').hasSetter());
227assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType());
228assertEquals('function', mirror.property('c').getter().type());
229assertEquals('function', mirror.property('c').setter().type());
230assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source());
231assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source());
232
233// Test objects with native accessors.
234mirror = debug.MakeMirror(new String('abc'));
235assertTrue(mirror instanceof debug.ObjectMirror);
236assertFalse(mirror.property('length').hasGetter());
237assertFalse(mirror.property('length').hasSetter());
238assertTrue(mirror.property('length').isNative());
239assertEquals('a', mirror.property(0).value().value());
240assertEquals('b', mirror.property(1).value().value());
241assertEquals('c', mirror.property(2).value().value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242
243// Test value wrapper internal properties.
244mirror = debug.MakeMirror(Object("Capybara"));
245var ip = mirror.internalProperties();
246assertEquals(1, ip.length);
247assertEquals("[[PrimitiveValue]]", ip[0].name());
248assertEquals("string", ip[0].value().type());
249assertEquals("Capybara", ip[0].value().value());
250
251// Test bound function internal properties.
252mirror = debug.MakeMirror(Number.bind(Array, 2));
253ip = mirror.internalProperties();
254assertEquals(3, ip.length);
255var property_map = {};
256for (var i = 0; i < ip.length; i++) {
257 property_map[ip[i].name()] = ip[i];
258}
259assertTrue("[[BoundThis]]" in property_map);
260assertEquals("function", property_map["[[BoundThis]]"].value().type());
261assertEquals(Array, property_map["[[BoundThis]]"].value().value());
262assertTrue("[[TargetFunction]]" in property_map);
263assertEquals("function", property_map["[[TargetFunction]]"].value().type());
264assertEquals(Number, property_map["[[TargetFunction]]"].value().value());
265assertTrue("[[BoundArgs]]" in property_map);
266assertEquals("object", property_map["[[BoundArgs]]"].value().type());
267assertEquals(1, property_map["[[BoundArgs]]"].value().value().length);