blob: 1873d1eb6e34d99990d3538436d24c3d531ebd12 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +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
ager@chromium.org32912102009-01-16 10:38:43 +000031function 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];
41}
42
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000043function testArrayMirror(a, names) {
44 // Create mirror and JSON representation.
45 var mirror = debug.MakeMirror(a);
ager@chromium.org32912102009-01-16 10:38:43 +000046 var serializer = debug.MakeMirrorSerializer();
47 var json = serializer.serializeValue(mirror);
48 var refs = new MirrorRefCache(serializer.serializeReferencedObjects());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000049
50 // Check the mirror hierachy.
ager@chromium.org32912102009-01-16 10:38:43 +000051 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');
54 assertTrue(mirror instanceof debug.ArrayMirror, 'Unexpected mirror hierachy');
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055
56 // Check the mirror properties.
ager@chromium.org32912102009-01-16 10:38:43 +000057 assertTrue(mirror.isArray(), 'Unexpected mirror');
58 assertEquals('object', mirror.type(), 'Unexpected mirror type');
59 assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror');
60 assertEquals('Array', mirror.className(), 'Unexpected mirror class name');
61 assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
62 assertEquals('Array', 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');
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +000065 assertEquals(mirror.length(), a.length, "Length mismatch");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066
ager@chromium.org32912102009-01-16 10:38:43 +000067 var indexedProperties = mirror.indexedPropertiesFromRange();
68 assertEquals(indexedProperties.length, a.length);
69 for (var i = 0; i < indexedProperties.length; i++) {
70 assertTrue(indexedProperties[i] instanceof debug.Mirror, 'Unexpected mirror hierachy');
71 assertTrue(indexedProperties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy');
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000072 }
73
74 // Parse JSON representation and check.
75 var fromJSON = eval('(' + json + ')');
ager@chromium.org32912102009-01-16 10:38:43 +000076 assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON');
77 assertEquals('Array', fromJSON.className, 'Unexpected mirror class name in JSON');
78 assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.ref, 'Unexpected constructor function handle in JSON');
79 assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, 'Unexpected constructor function type in JSON');
80 assertEquals('Array', refs.lookup(fromJSON.constructorFunction.ref).name, 'Unexpected constructor function name in JSON');
81 assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON');
82 assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expected in JSON');
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000083
ager@chromium.org32912102009-01-16 10:38:43 +000084 // Check that the serialization contains all indexed properties and the length property.
85 var length_found = false;
86 for (var i = 0; i < fromJSON.properties.length; i++) {
87 if (fromJSON.properties[i].name == 'length') {
88 length_found = true;
89 assertEquals('number', refs.lookup(fromJSON.properties[i].ref).type, "Unexpected type of the length property");
90 assertEquals(a.length, refs.lookup(fromJSON.properties[i].ref).value, "Length mismatch in parsed JSON");
91 } else {
92 var index = parseInt(fromJSON.properties[i].name);
93 print(index);
94 if (!isNaN(index)) {
95 print(index);
96 // This test assumes that the order of the indexeed properties is in the
97 // same order in the serialization as returned from
98 // indexedPropertiesFromRange()
99 assertEquals(indexedProperties[index].name(), index);
100 assertEquals(indexedProperties[index].value().type(), refs.lookup(fromJSON.properties[i].ref).type, 'Unexpected serialized type');
101 }
102 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000103 }
ager@chromium.org32912102009-01-16 10:38:43 +0000104 assertTrue(length_found, 'Property length not found');
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000105
106 // Check that the serialization contains all names properties.
107 if (names) {
108 for (var i = 0; i < names.length; i++) {
109 var found = false;
110 for (var j = 0; j < fromJSON.properties.length; j++) {
111 if (names[i] == fromJSON.properties[j].name) {
112 found = true;
113 }
114 }
115 assertTrue(found, names[i])
116 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000117 }
118}
119
120
121// Test a number of different arrays.
122testArrayMirror([]);
123testArrayMirror([1]);
124testArrayMirror([1,2]);
125testArrayMirror(["a", function(){}, [1,2], 2, /[ab]/]);
126
127a=[1];
128a[100]=7;
129testArrayMirror(a);
130
131a=[1,2,3];
132a.x=2.2;
133a.y=function(){return null;}
134testArrayMirror(a, ['x','y']);
135
136var a = []; a.push(a);
137testArrayMirror(a);