1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2008 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Flags: --expose-debug-as debug
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test the mirror object for objects
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction MirrorRefCache(json_refs) {
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var tmp = eval('(' + json_refs + ')');
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  this.refs_ = [];
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (var i = 0; i < tmp.length; i++) {
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    this.refs_[tmp[i].handle] = tmp[i];
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockMirrorRefCache.prototype.lookup = function(handle) {
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return this.refs_[handle];
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testArrayMirror(a, names) {
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Create mirror and JSON representation.
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var mirror = debug.MakeMirror(a);
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var serializer = debug.MakeMirrorSerializer();
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var json = JSON.stringify(serializer.serializeValue(mirror));
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var refs = new MirrorRefCache(
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      JSON.stringify(serializer.serializeReferencedObjects()));
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Check the mirror hierachy.
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy');
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierachy');
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror instanceof debug.ArrayMirror, 'Unexpected mirror hierachy');
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Check the mirror properties.
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror.isArray(), 'Unexpected mirror');
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('object', mirror.type(), 'Unexpected mirror type');
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror');
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('Array', mirror.className(), 'Unexpected mirror class name');
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('Array', mirror.constructorFunction().name(), 'Unexpected constructor function name');
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(mirror.length(), a.length, "Length mismatch");
67589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var indexedProperties = mirror.indexedPropertiesFromRange();
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(indexedProperties.length, a.length);
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (var i = 0; i < indexedProperties.length; i++) {
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertTrue(indexedProperties[i] instanceof debug.Mirror, 'Unexpected mirror hierachy');
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertTrue(indexedProperties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy');
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Parse JSON representation and check.
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var fromJSON = eval('(' + json + ')');
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON');
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('Array', fromJSON.className, 'Unexpected mirror class name in JSON');
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.ref, 'Unexpected constructor function handle in JSON');
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, 'Unexpected constructor function type in JSON');
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('Array', refs.lookup(fromJSON.constructorFunction.ref).name, 'Unexpected constructor function name in JSON');
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON');
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expected in JSON');
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Check that the serialization contains all indexed properties and the length property.
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var length_found = false;
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (var i = 0; i < fromJSON.properties.length; i++) {
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if (fromJSON.properties[i].name == 'length') {
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      length_found = true;
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      assertEquals('number', refs.lookup(fromJSON.properties[i].ref).type, "Unexpected type of the length property");
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      assertEquals(a.length, refs.lookup(fromJSON.properties[i].ref).value, "Length mismatch in parsed JSON");
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    } else {
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      var index = parseInt(fromJSON.properties[i].name);
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        print(index);
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      if (!isNaN(index)) {
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        print(index);
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        // This test assumes that the order of the indexeed properties is in the
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        // same order in the serialization as returned from
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        // indexedPropertiesFromRange()
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        assertEquals(indexedProperties[index].name(), index);
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        assertEquals(indexedProperties[index].value().type(), refs.lookup(fromJSON.properties[i].ref).type, 'Unexpected serialized type');
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      }
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    }
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(length_found, 'Property length not found');
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Check that the serialization contains all names properties.
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (names) {
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    for (var i = 0; i < names.length; i++) {
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      var found = false;
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      for (var j = 0; j < fromJSON.properties.length; j++) {
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        if (names[i] == fromJSON.properties[j].name) {
113589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch          found = true;
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        }
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      }
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      assertTrue(found, names[i])
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    }
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test a number of different arrays.
123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror([]);
124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror([1]);
125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror([1,2]);
126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror(["a", function(){}, [1,2], 2, /[ab]/]);
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka=[1];
129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka[100]=7;
130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror(a);
131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka=[1,2,3];
133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka.x=2.2;
134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka.y=function(){return null;}
135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror(a, ['x','y']);
136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar a = []; a.push(a);
138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestArrayMirror(a);
139