1 // Copyright 2008 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 testArrayMirror(a, names) {
44 // Create mirror and JSON representation.
45 var mirror = debug.MakeMirror(a);
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 hierachy');
53 assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierachy');
54 assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
55 assertTrue(mirror instanceof debug.ArrayMirror, 'Unexpected mirror hierachy');
57 // Check the mirror properties.
58 assertTrue(mirror.isArray(), 'Unexpected mirror');
59 assertEquals('object', mirror.type(), 'Unexpected mirror type');
60 assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror');
61 assertEquals('Array', mirror.className(), 'Unexpected mirror class name');
62 assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
63 assertEquals('Array', mirror.constructorFunction().name(), 'Unexpected constructor function name');
64 assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
65 assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
66 assertEquals(mirror.length(), a.length, "Length mismatch");
68 var indexedProperties = mirror.indexedPropertiesFromRange();
69 assertEquals(indexedProperties.length, a.length);
70 for (var i = 0; i < indexedProperties.length; i++) {
71 assertTrue(indexedProperties[i] instanceof debug.Mirror, 'Unexpected mirror hierachy');
72 assertTrue(indexedProperties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy');
75 // Parse JSON representation and check.
76 var fromJSON = eval('(' + json + ')');
77 assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON');
78 assertEquals('Array', fromJSON.className, 'Unexpected mirror class name in JSON');
79 assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.ref, 'Unexpected constructor function handle in JSON');
80 assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, 'Unexpected constructor function type in JSON');
81 assertEquals('Array', refs.lookup(fromJSON.constructorFunction.ref).name, 'Unexpected constructor function name in JSON');
82 assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON');
83 assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expected in JSON');
85 // Check that the serialization contains all indexed properties and the length property.
86 var length_found = false;
87 for (var i = 0; i < fromJSON.properties.length; i++) {
88 if (fromJSON.properties[i].name == 'length') {
90 assertEquals('number', refs.lookup(fromJSON.properties[i].ref).type, "Unexpected type of the length property");
91 assertEquals(a.length, refs.lookup(fromJSON.properties[i].ref).value, "Length mismatch in parsed JSON");
93 var index = parseInt(fromJSON.properties[i].name);
97 // This test assumes that the order of the indexeed properties is in the
98 // same order in the serialization as returned from
99 // indexedPropertiesFromRange()
100 assertEquals(indexedProperties[index].name(), index);
101 assertEquals(indexedProperties[index].value().type(), refs.lookup(fromJSON.properties[i].ref).type, 'Unexpected serialized type');
105 assertTrue(length_found, 'Property length not found');
107 // Check that the serialization contains all names properties.
109 for (var i = 0; i < names.length; i++) {
111 for (var j = 0; j < fromJSON.properties.length; j++) {
112 if (names[i] == fromJSON.properties[j].name) {
116 assertTrue(found, names[i])
122 // Test a number of different arrays.
124 testArrayMirror([1]);
125 testArrayMirror([1,2]);
126 testArrayMirror(["a", function(){}, [1,2], 2, /[ab]/]);
134 a.y=function(){return null;}
135 testArrayMirror(a, ['x','y']);
137 var a = []; a.push(a);