ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 31 | function 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 | |
| 39 | MirrorRefCache.prototype.lookup = function(handle) { |
| 40 | return this.refs_[handle]; |
| 41 | } |
| 42 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 43 | function testArrayMirror(a, names) { |
| 44 | // Create mirror and JSON representation. |
| 45 | var mirror = debug.MakeMirror(a); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 46 | var serializer = debug.MakeMirrorSerializer(); |
| 47 | var json = serializer.serializeValue(mirror); |
| 48 | var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 49 | |
| 50 | // Check the mirror hierachy. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 51 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 55 | |
| 56 | // Check the mirror properties. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 57 | 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.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 65 | assertEquals(mirror.length(), a.length, "Length mismatch"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 66 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 67 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Parse JSON representation and check. |
| 75 | var fromJSON = eval('(' + json + ')'); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 76 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 83 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 84 | // 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 103 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 104 | assertTrue(length_found, 'Property length not found'); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 105 | |
| 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // Test a number of different arrays. |
| 122 | testArrayMirror([]); |
| 123 | testArrayMirror([1]); |
| 124 | testArrayMirror([1,2]); |
| 125 | testArrayMirror(["a", function(){}, [1,2], 2, /[ab]/]); |
| 126 | |
| 127 | a=[1]; |
| 128 | a[100]=7; |
| 129 | testArrayMirror(a); |
| 130 | |
| 131 | a=[1,2,3]; |
| 132 | a.x=2.2; |
| 133 | a.y=function(){return null;} |
| 134 | testArrayMirror(a, ['x','y']); |
| 135 | |
| 136 | var a = []; a.push(a); |
| 137 | testArrayMirror(a); |