Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: test/mjsunit/mirror-object.js

Issue 18842: Experimental: periodic merge of the bleeding_edge branch to the... (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/toiger/
Patch Set: Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/mjsunit/mirror-number.js ('k') | test/mjsunit/mirror-regexp.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 10 matching lines...) Expand all
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --expose-debug-as debug 28 // Flags: --expose-debug-as debug
29 // Test the mirror object for objects 29 // Test the mirror object for objects
30 30
31 function testObjectMirror(o, cls_name, ctor_name, hasSpecialProperties) { 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
43 function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) {
32 // Create mirror and JSON representation. 44 // Create mirror and JSON representation.
33 var mirror = debug.MakeMirror(o); 45 var mirror = debug.MakeMirror(obj);
34 var json = mirror.toJSONProtocol(true); 46 var serializer = debug.MakeMirrorSerializer();
47 var json = serializer.serializeValue(mirror);
48 var refs = new MirrorRefCache(serializer.serializeReferencedObjects());
35 49
36 // Check the mirror hierachy. 50 // Check the mirror hierachy.
37 assertTrue(mirror instanceof debug.Mirror); 51 assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy');
38 assertTrue(mirror instanceof debug.ValueMirror); 52 assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierachy');
39 assertTrue(mirror instanceof debug.ObjectMirror); 53 assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierachy') ;
40 54
41 // Check the mirror properties. 55 // Check the mirror properties.
42 assertTrue(mirror.isObject()); 56 assertTrue(mirror.isObject(), 'Unexpected mirror');
43 assertEquals('object', mirror.type()); 57 assertEquals('object', mirror.type(), 'Unexpected mirror type');
44 assertFalse(mirror.isPrimitive()); 58 assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror');
45 assertEquals(cls_name, mirror.className()); 59 assertEquals(cls_name, mirror.className(), 'Unexpected mirror class name');
46 assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror); 60 assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpe cted mirror hierachy');
47 assertTrue(mirror.protoObject() instanceof debug.Mirror); 61 assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected const ructor function name');
48 assertTrue(mirror.prototypeObject() instanceof debug.Mirror); 62 assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hi erachy');
49 assertFalse(mirror.hasNamedInterceptor(), "hasNamedInterceptor()"); 63 assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirro r hierachy');
50 assertFalse(mirror.hasIndexedInterceptor(), "hasIndexedInterceptor()"); 64 assertFalse(mirror.hasNamedInterceptor(), 'No named interceptor expected');
65 assertFalse(mirror.hasIndexedInterceptor(), 'No indexed interceptor expected') ;
51 66
52 var names = mirror.propertyNames(); 67 var names = mirror.propertyNames();
53 var properties = mirror.properties() 68 var properties = mirror.properties()
54 assertEquals(names.length, properties.length); 69 assertEquals(names.length, properties.length);
55 for (var i = 0; i < properties.length; i++) { 70 for (var i = 0; i < properties.length; i++) {
56 assertTrue(properties[i] instanceof debug.Mirror); 71 assertTrue(properties[i] instanceof debug.Mirror, 'Unexpected mirror hierach y');
57 assertTrue(properties[i] instanceof debug.PropertyMirror); 72 assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy');
58 assertEquals('property', properties[i].type()); 73 assertEquals('property', properties[i].type(), 'Unexpected mirror type');
59 assertEquals(names[i], properties[i].name()); 74 assertEquals(names[i], properties[i].name(), 'Unexpected property name');
60 } 75 }
61 76
62 for (var p in o) { 77 for (var p in obj) {
63 var property_mirror = mirror.property(p); 78 var property_mirror = mirror.property(p);
64 assertTrue(property_mirror instanceof debug.PropertyMirror); 79 assertTrue(property_mirror instanceof debug.PropertyMirror);
65 assertEquals(p, property_mirror.name()); 80 assertEquals(p, property_mirror.name());
66 // If the object has some special properties don't test for these. 81 // If the object has some special properties don't test for these.
67 if (!hasSpecialProperties) { 82 if (!hasSpecialProperties) {
68 assertEquals(0, property_mirror.attributes(), property_mirror.name()); 83 assertEquals(0, property_mirror.attributes(), property_mirror.name());
69 assertFalse(property_mirror.isReadOnly()); 84 assertFalse(property_mirror.isReadOnly());
70 assertTrue(property_mirror.isEnum()); 85 assertTrue(property_mirror.isEnum());
71 assertTrue(property_mirror.canDelete()); 86 assertTrue(property_mirror.canDelete());
72 } 87 }
73 } 88 }
74 89
75 // Parse JSON representation and check. 90 // Parse JSON representation and check.
76 var fromJSON = eval('(' + json + ')'); 91 var fromJSON = eval('(' + json + ')');
77 assertEquals('object', fromJSON.type); 92 assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON');
78 assertEquals(cls_name, fromJSON.className); 93 assertEquals(cls_name, fromJSON.className, 'Unexpected mirror class name in JS ON');
79 assertEquals('function', fromJSON.constructorFunction.type); 94 assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFuncti on.ref, 'Unexpected constructor function handle in JSON');
80 if (ctor_name !== undefined) 95 assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, ' Unexpected constructor function type in JSON');
81 assertEquals(ctor_name, fromJSON.constructorFunction.name); 96 assertEquals(ctor_name, refs.lookup(fromJSON.constructorFunction.ref).name, 'U nexpected constructor function name in JSON');
82 assertEquals(void 0, fromJSON.namedInterceptor); 97 assertEquals(mirror.protoObject().handle(), fromJSON.protoObject.ref, 'Unexpec ted proto object handle in JSON');
83 assertEquals(void 0, fromJSON.indexedInterceptor); 98 assertEquals(mirror.protoObject().type(), refs.lookup(fromJSON.protoObject.ref ).type, 'Unexpected proto object type in JSON');
84 99 assertEquals(mirror.prototypeObject().handle(), fromJSON.prototypeObject.ref, 'Unexpected prototype object handle in JSON');
85 // For array the index properties are seperate from named properties. 100 assertEquals(mirror.prototypeObject().type(), refs.lookup(fromJSON.prototypeOb ject.ref).type, 'Unexpected prototype object type in JSON');
86 if (!cls_name == 'Array') { 101 assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON');
87 assertEquals(names.length, fromJSON.properties.length, 'Some properties miss ing in JSON'); 102 assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expe cted in JSON');
88 }
89 103
90 // Check that the serialization contains all properties. 104 // Check that the serialization contains all properties.
105 assertEquals(names.length, fromJSON.properties.length, 'Some properties missin g in JSON');
91 for (var i = 0; i < fromJSON.properties.length; i++) { 106 for (var i = 0; i < fromJSON.properties.length; i++) {
92 var name = fromJSON.properties[i].name; 107 var name = fromJSON.properties[i].name;
93 if (!name) name = fromJSON.properties[i].index; 108 if (!name) name = fromJSON.properties[i].index;
94 var found = false; 109 var found = false;
95 for (var j = 0; j < names.length; j++) { 110 for (var j = 0; j < names.length; j++) {
96 if (names[j] == name) { 111 if (names[j] == name) {
97 assertEquals(properties[i].value().type(), fromJSON.properties[i].value. type); 112 // Check that serialized handle is correct.
98 // If property type is normal nothing is serialized. 113 assertEquals(properties[i].value().handle(), fromJSON.properties[i].ref, 'Unexpected serialized handle');
114
115 // Check that serialized name is correct.
116 assertEquals(properties[i].name(), fromJSON.properties[i].name, 'Unexpec ted serialized name');
117
118 // If property type is normal property type is not serialized.
99 if (properties[i].propertyType() != debug.PropertyType.Normal) { 119 if (properties[i].propertyType() != debug.PropertyType.Normal) {
100 assertEquals(properties[i].propertyType(), fromJSON.properties[i].prop ertyType); 120 assertEquals(properties[i].propertyType(), fromJSON.properties[i].prop ertyType, 'Unexpected serialized property type');
101 } else { 121 } else {
102 assertTrue(typeof(fromJSON.properties[i].propertyType) === 'undefined' ); 122 assertTrue(typeof(fromJSON.properties[i].propertyType) === 'undefined' , 'Unexpected serialized property type');
103 } 123 }
104 // If there are no attributes nothing is serialized. 124
125 // If there are no attributes attributes are not serialized.
105 if (properties[i].attributes() != debug.PropertyAttribute.None) { 126 if (properties[i].attributes() != debug.PropertyAttribute.None) {
106 assertEquals(properties[i].attributes(), fromJSON.properties[i].attrib utes); 127 assertEquals(properties[i].attributes(), fromJSON.properties[i].attrib utes, 'Unexpected serialized attributes');
107 } else { 128 } else {
108 assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined'); 129 assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined', 'Unexpected serialized attributes');
109 } 130 }
110 if (!properties[i].value().isPrimitive()) { 131
111 // NaN is not equal to NaN. 132 // Lookup the serialized object from the handle reference.
112 if (isNaN(properties[i].value().value())) { 133 var o = refs.lookup(fromJSON.properties[i].ref);
113 assertTrue(isNaN(fromJSON.properties[i].value.value)); 134 assertTrue(o != void 0, 'Referenced object is not serialized');
135
136 assertEquals(properties[i].value().type(), o.type, 'Unexpected serialize d property type for ' + name);
137 if (properties[i].value().isPrimitive()) {
138 // Special check for NaN as NaN == NaN is false.
139 if (properties[i].value().isNumber() && isNaN(properties[i].value().va lue())) {
140 assertEquals('NaN', o.value, 'Unexpected serialized property value f or ' + name);
114 } else { 141 } else {
115 assertEquals(properties[i].value().value(), fromJSON.properties[i].v alue.value); 142 assertEquals(properties[i].value().value(), o.value, 'Unexpected ser ialized property value for ' + name);
116 } 143 }
144 } else if (properties[i].value().isFunction()) {
145 assertEquals(properties[i].value().source(), o.source, 'Unexpected ser ialized property value for ' + name);
117 } 146 }
118 found = true; 147 found = true;
119 } 148 }
120 } 149 }
121 assertTrue(found, '"' + name + '" not found (' + json + ')'); 150 assertTrue(found, '"' + name + '" not found (' + json + ')');
122 } 151 }
123 } 152 }
124 153
125 154
126 function Point(x,y) { 155 function Point(x,y) {
127 this.x_ = x; 156 this.x_ = x;
128 this.y_ = y; 157 this.y_ = y;
129 } 158 }
130 159
131 160
132 // Test a number of different objects. 161 // Test a number of different objects.
133 testObjectMirror({}, 'Object', 'Object'); 162 testObjectMirror({}, 'Object', 'Object');
134 testObjectMirror({'a':1,'b':2}, 'Object', 'Object'); 163 testObjectMirror({'a':1,'b':2}, 'Object', 'Object');
135 testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y) ;}}, 'Object', 'Object'); 164 testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y) ;}}, 'Object', 'Object');
136 testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point'); 165 testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point');
137 testObjectMirror(this, 'global', undefined, true); // Global object has special properties 166 testObjectMirror(this, 'global', '', true); // Global object has special proper ties
167 testObjectMirror(this.__proto__, 'Object', '');
138 testObjectMirror([], 'Array', 'Array'); 168 testObjectMirror([], 'Array', 'Array');
139 testObjectMirror([1,2], 'Array', 'Array'); 169 testObjectMirror([1,2], 'Array', 'Array');
140 170
141 // Test circular references. 171 // Test circular references.
142 o = {}; 172 o = {};
143 o.o = o; 173 o.o = o;
144 testObjectMirror(o, 'Object', 'Object'); 174 testObjectMirror(o, 'Object', 'Object');
145 175
146 // Test that non enumerable properties are part of the mirror 176 // Test that non enumerable properties are part of the mirror
147 global_mirror = debug.MakeMirror(this); 177 global_mirror = debug.MakeMirror(this);
(...skipping 14 matching lines...) Expand all
162 o.__defineSetter__('c', function(){throw 'c';}); 192 o.__defineSetter__('c', function(){throw 'c';});
163 testObjectMirror(o, 'Object', 'Object'); 193 testObjectMirror(o, 'Object', 'Object');
164 mirror = debug.MakeMirror(o); 194 mirror = debug.MakeMirror(o);
165 // a has getter but no setter. 195 // a has getter but no setter.
166 assertTrue(mirror.property('a').hasGetter()); 196 assertTrue(mirror.property('a').hasGetter());
167 assertFalse(mirror.property('a').hasSetter()); 197 assertFalse(mirror.property('a').hasSetter());
168 assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType()); 198 assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType());
169 assertEquals('function', mirror.property('a').getter().type()); 199 assertEquals('function', mirror.property('a').getter().type());
170 assertEquals('undefined', mirror.property('a').setter().type()); 200 assertEquals('undefined', mirror.property('a').setter().type());
171 assertEquals('function (){return \'a\';}', mirror.property('a').getter().source( )); 201 assertEquals('function (){return \'a\';}', mirror.property('a').getter().source( ));
172 assertEquals('a', mirror.property('a').value().value());
173 assertFalse(mirror.property('a').isException());
174 // b has setter but no getter. 202 // b has setter but no getter.
175 assertFalse(mirror.property('b').hasGetter()); 203 assertFalse(mirror.property('b').hasGetter());
176 assertTrue(mirror.property('b').hasSetter()); 204 assertTrue(mirror.property('b').hasSetter());
177 assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType()); 205 assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType());
178 assertEquals('undefined', mirror.property('b').getter().type()); 206 assertEquals('undefined', mirror.property('b').getter().type());
179 assertEquals('function', mirror.property('b').setter().type()); 207 assertEquals('function', mirror.property('b').setter().type());
180 assertEquals('function (){}', mirror.property('b').setter().source()); 208 assertEquals('function (){}', mirror.property('b').setter().source());
181 assertFalse(mirror.property('b').isException()); 209 assertFalse(mirror.property('b').isException());
182 // c has both getter and setter. The getter throws an exception. 210 // c has both getter and setter. The getter throws an exception.
183 assertTrue(mirror.property('c').hasGetter()); 211 assertTrue(mirror.property('c').hasGetter());
184 assertTrue(mirror.property('c').hasSetter()); 212 assertTrue(mirror.property('c').hasSetter());
185 assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType()); 213 assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType());
186 assertEquals('function', mirror.property('c').getter().type()); 214 assertEquals('function', mirror.property('c').getter().type());
187 assertEquals('function', mirror.property('c').setter().type()); 215 assertEquals('function', mirror.property('c').setter().type());
188 assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source() ); 216 assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source() );
189 assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source() ); 217 assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source() );
190 assertEquals('c', mirror.property('c').value().value());
191 assertTrue(mirror.property('c').isException());
192 218
193 // Test objects with native accessors. 219 // Test objects with native accessors.
194 mirror = debug.MakeMirror(new String('abc')); 220 mirror = debug.MakeMirror(new String('abc'));
195 assertTrue(mirror instanceof debug.ObjectMirror); 221 assertTrue(mirror instanceof debug.ObjectMirror);
196 assertFalse(mirror.property('length').hasGetter()); 222 assertFalse(mirror.property('length').hasGetter());
197 assertFalse(mirror.property('length').hasSetter()); 223 assertFalse(mirror.property('length').hasSetter());
198 assertTrue(mirror.property('length').isNative()); 224 assertTrue(mirror.property('length').isNative());
199 assertEquals('a', mirror.property(0).value().value()); 225 assertEquals('a', mirror.property(0).value().value());
200 assertEquals('b', mirror.property(1).value().value()); 226 assertEquals('b', mirror.property(1).value().value());
201 assertEquals('c', mirror.property(2).value().value()); 227 assertEquals('c', mirror.property(2).value().value());
OLDNEW
« no previous file with comments | « test/mjsunit/mirror-number.js ('k') | test/mjsunit/mirror-regexp.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698