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
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];
41}
42
43function 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()));
50
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');
56
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");
67
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');
73  }
74
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');
84
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') {
89      length_found = true;
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");
92    } else {
93      var index = parseInt(fromJSON.properties[i].name);
94        print(index);
95      if (!isNaN(index)) {
96        print(index);
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');
102      }
103    }
104  }
105  assertTrue(length_found, 'Property length not found');
106
107  // Check that the serialization contains all names properties.
108  if (names) {
109    for (var i = 0; i < names.length; i++) {
110      var found = false;
111      for (var j = 0; j < fromJSON.properties.length; j++) {
112        if (names[i] == fromJSON.properties[j].name) {
113          found = true;
114        }
115      }
116      assertTrue(found, names[i])
117    }
118  }
119}
120
121
122// Test a number of different arrays.
123testArrayMirror([]);
124testArrayMirror([1]);
125testArrayMirror([1,2]);
126testArrayMirror(["a", function(){}, [1,2], 2, /[ab]/]);
127
128a=[1];
129a[100]=7;
130testArrayMirror(a);
131
132a=[1,2,3];
133a.x=2.2;
134a.y=function(){return null;}
135testArrayMirror(a, ['x','y']);
136
137var a = []; a.push(a);
138testArrayMirror(a);
139