2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2008 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// Flags: --expose-debug-as debug
|
|
|
|
// Test the mirror object for objects
|
|
|
|
|
2009-01-16 09:54:46 +00:00
|
|
|
function MirrorRefCache(json_refs) {
|
|
|
|
var tmp = eval('(' + json_refs + ')');
|
|
|
|
this.refs_ = [];
|
|
|
|
for (var i = 0; i < tmp.length; i++) {
|
|
|
|
this.refs_[tmp[i].handle] = tmp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MirrorRefCache.prototype.lookup = function(handle) {
|
|
|
|
return this.refs_[handle];
|
|
|
|
}
|
|
|
|
|
|
|
|
function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Create mirror and JSON representation.
|
2009-01-16 09:54:46 +00:00
|
|
|
var mirror = debug.MakeMirror(obj);
|
|
|
|
var serializer = debug.MakeMirrorSerializer();
|
2009-05-15 07:35:11 +00:00
|
|
|
var json = JSON.stringify(serializer.serializeValue(mirror));
|
|
|
|
var refs = new MirrorRefCache(
|
|
|
|
JSON.stringify(serializer.serializeReferencedObjects()));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check the mirror hierachy.
|
2009-01-16 09:54:46 +00:00
|
|
|
assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy');
|
|
|
|
assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierachy');
|
|
|
|
assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check the mirror properties.
|
2009-01-16 09:54:46 +00:00
|
|
|
assertTrue(mirror.isObject(), 'Unexpected mirror');
|
|
|
|
assertEquals('object', mirror.type(), 'Unexpected mirror type');
|
|
|
|
assertFalse(mirror.isPrimitive(), 'Unexpected primitive mirror');
|
|
|
|
assertEquals(cls_name, mirror.className(), 'Unexpected mirror class name');
|
|
|
|
assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierachy');
|
|
|
|
assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected constructor function name');
|
|
|
|
assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
|
|
|
|
assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierachy');
|
|
|
|
assertFalse(mirror.hasNamedInterceptor(), 'No named interceptor expected');
|
|
|
|
assertFalse(mirror.hasIndexedInterceptor(), 'No indexed interceptor expected');
|
2008-08-27 10:11:39 +00:00
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
var names = mirror.propertyNames();
|
|
|
|
var properties = mirror.properties()
|
|
|
|
assertEquals(names.length, properties.length);
|
|
|
|
for (var i = 0; i < properties.length; i++) {
|
2009-01-16 09:54:46 +00:00
|
|
|
assertTrue(properties[i] instanceof debug.Mirror, 'Unexpected mirror hierachy');
|
|
|
|
assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierachy');
|
|
|
|
assertEquals('property', properties[i].type(), 'Unexpected mirror type');
|
|
|
|
assertEquals(names[i], properties[i].name(), 'Unexpected property name');
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2009-01-16 09:54:46 +00:00
|
|
|
for (var p in obj) {
|
2008-08-22 13:33:59 +00:00
|
|
|
var property_mirror = mirror.property(p);
|
|
|
|
assertTrue(property_mirror instanceof debug.PropertyMirror);
|
|
|
|
assertEquals(p, property_mirror.name());
|
|
|
|
// If the object has some special properties don't test for these.
|
|
|
|
if (!hasSpecialProperties) {
|
|
|
|
assertEquals(0, property_mirror.attributes(), property_mirror.name());
|
|
|
|
assertFalse(property_mirror.isReadOnly());
|
|
|
|
assertTrue(property_mirror.isEnum());
|
|
|
|
assertTrue(property_mirror.canDelete());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse JSON representation and check.
|
|
|
|
var fromJSON = eval('(' + json + ')');
|
2009-01-16 09:54:46 +00:00
|
|
|
assertEquals('object', fromJSON.type, 'Unexpected mirror type in JSON');
|
|
|
|
assertEquals(cls_name, fromJSON.className, 'Unexpected mirror class name in JSON');
|
|
|
|
assertEquals(mirror.constructorFunction().handle(), fromJSON.constructorFunction.ref, 'Unexpected constructor function handle in JSON');
|
|
|
|
assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type, 'Unexpected constructor function type in JSON');
|
|
|
|
assertEquals(ctor_name, refs.lookup(fromJSON.constructorFunction.ref).name, 'Unexpected constructor function name in JSON');
|
|
|
|
assertEquals(mirror.protoObject().handle(), fromJSON.protoObject.ref, 'Unexpected proto object handle in JSON');
|
|
|
|
assertEquals(mirror.protoObject().type(), refs.lookup(fromJSON.protoObject.ref).type, 'Unexpected proto object type in JSON');
|
|
|
|
assertEquals(mirror.prototypeObject().handle(), fromJSON.prototypeObject.ref, 'Unexpected prototype object handle in JSON');
|
|
|
|
assertEquals(mirror.prototypeObject().type(), refs.lookup(fromJSON.prototypeObject.ref).type, 'Unexpected prototype object type in JSON');
|
|
|
|
assertEquals(void 0, fromJSON.namedInterceptor, 'No named interceptor expected in JSON');
|
|
|
|
assertEquals(void 0, fromJSON.indexedInterceptor, 'No indexed interceptor expected in JSON');
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check that the serialization contains all properties.
|
2009-01-16 09:54:46 +00:00
|
|
|
assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON');
|
2008-08-22 13:33:59 +00:00
|
|
|
for (var i = 0; i < fromJSON.properties.length; i++) {
|
|
|
|
var name = fromJSON.properties[i].name;
|
2009-05-15 07:35:11 +00:00
|
|
|
if (typeof name == 'undefined') name = fromJSON.properties[i].index;
|
2008-08-22 13:33:59 +00:00
|
|
|
var found = false;
|
|
|
|
for (var j = 0; j < names.length; j++) {
|
|
|
|
if (names[j] == name) {
|
2009-01-16 09:54:46 +00:00
|
|
|
// Check that serialized handle is correct.
|
|
|
|
assertEquals(properties[i].value().handle(), fromJSON.properties[i].ref, 'Unexpected serialized handle');
|
|
|
|
|
|
|
|
// Check that serialized name is correct.
|
|
|
|
assertEquals(properties[i].name(), fromJSON.properties[i].name, 'Unexpected serialized name');
|
|
|
|
|
|
|
|
// If property type is normal property type is not serialized.
|
2008-08-22 13:33:59 +00:00
|
|
|
if (properties[i].propertyType() != debug.PropertyType.Normal) {
|
2009-01-16 09:54:46 +00:00
|
|
|
assertEquals(properties[i].propertyType(), fromJSON.properties[i].propertyType, 'Unexpected serialized property type');
|
2008-08-22 13:33:59 +00:00
|
|
|
} else {
|
2009-01-16 09:54:46 +00:00
|
|
|
assertTrue(typeof(fromJSON.properties[i].propertyType) === 'undefined', 'Unexpected serialized property type');
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2009-01-16 09:54:46 +00:00
|
|
|
|
|
|
|
// If there are no attributes attributes are not serialized.
|
2008-08-22 13:33:59 +00:00
|
|
|
if (properties[i].attributes() != debug.PropertyAttribute.None) {
|
2009-01-16 09:54:46 +00:00
|
|
|
assertEquals(properties[i].attributes(), fromJSON.properties[i].attributes, 'Unexpected serialized attributes');
|
2008-08-22 13:33:59 +00:00
|
|
|
} else {
|
2009-01-16 09:54:46 +00:00
|
|
|
assertTrue(typeof(fromJSON.properties[i].attributes) === 'undefined', 'Unexpected serialized attributes');
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2009-01-16 09:54:46 +00:00
|
|
|
|
|
|
|
// Lookup the serialized object from the handle reference.
|
|
|
|
var o = refs.lookup(fromJSON.properties[i].ref);
|
|
|
|
assertTrue(o != void 0, 'Referenced object is not serialized');
|
|
|
|
|
|
|
|
assertEquals(properties[i].value().type(), o.type, 'Unexpected serialized property type for ' + name);
|
|
|
|
if (properties[i].value().isPrimitive()) {
|
2009-01-21 09:32:07 +00:00
|
|
|
// Special check for NaN as NaN == NaN is false.
|
|
|
|
if (properties[i].value().isNumber() && isNaN(properties[i].value().value())) {
|
|
|
|
assertEquals('NaN', o.value, 'Unexpected serialized property value for ' + name);
|
|
|
|
} else {
|
|
|
|
assertEquals(properties[i].value().value(), o.value, 'Unexpected serialized property value for ' + name);
|
|
|
|
}
|
2009-01-16 09:54:46 +00:00
|
|
|
} else if (properties[i].value().isFunction()) {
|
|
|
|
assertEquals(properties[i].value().source(), o.source, 'Unexpected serialized property value for ' + name);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2009-01-07 09:27:30 +00:00
|
|
|
assertTrue(found, '"' + name + '" not found (' + json + ')');
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Point(x,y) {
|
|
|
|
this.x_ = x;
|
|
|
|
this.y_ = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a number of different objects.
|
|
|
|
testObjectMirror({}, 'Object', 'Object');
|
|
|
|
testObjectMirror({'a':1,'b':2}, 'Object', 'Object');
|
|
|
|
testObjectMirror({'1':void 0,'2':null,'f':function pow(x,y){return Math.pow(x,y);}}, 'Object', 'Object');
|
|
|
|
testObjectMirror(new Point(-1.2,2.003), 'Object', 'Point');
|
2009-01-16 09:54:46 +00:00
|
|
|
testObjectMirror(this, 'global', '', true); // Global object has special properties
|
2009-01-21 09:32:07 +00:00
|
|
|
testObjectMirror(this.__proto__, 'Object', '');
|
2008-08-22 13:33:59 +00:00
|
|
|
testObjectMirror([], 'Array', 'Array');
|
|
|
|
testObjectMirror([1,2], 'Array', 'Array');
|
|
|
|
|
2009-01-07 09:27:30 +00:00
|
|
|
// Test circular references.
|
|
|
|
o = {};
|
|
|
|
o.o = o;
|
|
|
|
testObjectMirror(o, 'Object', 'Object');
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
// Test that non enumerable properties are part of the mirror
|
|
|
|
global_mirror = debug.MakeMirror(this);
|
|
|
|
assertEquals('property', global_mirror.property("Math").type());
|
|
|
|
assertFalse(global_mirror.property("Math").isEnum(), "Math is enumerable" + global_mirror.property("Math").attributes());
|
|
|
|
|
|
|
|
math_mirror = global_mirror.property("Math").value();
|
|
|
|
assertEquals('property', math_mirror.property("E").type());
|
|
|
|
assertFalse(math_mirror.property("E").isEnum(), "Math.E is enumerable");
|
|
|
|
assertTrue(math_mirror.property("E").isReadOnly());
|
|
|
|
assertFalse(math_mirror.property("E").canDelete());
|
|
|
|
|
|
|
|
// Test objects with JavaScript accessors.
|
|
|
|
o = {}
|
2009-01-13 14:38:44 +00:00
|
|
|
o.__defineGetter__('a', function(){return 'a';});
|
|
|
|
o.__defineSetter__('b', function(){});
|
|
|
|
o.__defineGetter__('c', function(){throw 'c';});
|
|
|
|
o.__defineSetter__('c', function(){throw 'c';});
|
2008-08-22 13:33:59 +00:00
|
|
|
testObjectMirror(o, 'Object', 'Object');
|
|
|
|
mirror = debug.MakeMirror(o);
|
|
|
|
// a has getter but no setter.
|
2009-01-13 14:38:44 +00:00
|
|
|
assertTrue(mirror.property('a').hasGetter());
|
|
|
|
assertFalse(mirror.property('a').hasSetter());
|
2008-08-22 13:33:59 +00:00
|
|
|
assertEquals(debug.PropertyType.Callbacks, mirror.property('a').propertyType());
|
2009-01-13 14:38:44 +00:00
|
|
|
assertEquals('function', mirror.property('a').getter().type());
|
|
|
|
assertEquals('undefined', mirror.property('a').setter().type());
|
|
|
|
assertEquals('function (){return \'a\';}', mirror.property('a').getter().source());
|
2008-08-22 13:33:59 +00:00
|
|
|
// b has setter but no getter.
|
2009-01-13 14:38:44 +00:00
|
|
|
assertFalse(mirror.property('b').hasGetter());
|
|
|
|
assertTrue(mirror.property('b').hasSetter());
|
2008-08-22 13:33:59 +00:00
|
|
|
assertEquals(debug.PropertyType.Callbacks, mirror.property('b').propertyType());
|
2009-01-13 14:38:44 +00:00
|
|
|
assertEquals('undefined', mirror.property('b').getter().type());
|
|
|
|
assertEquals('function', mirror.property('b').setter().type());
|
|
|
|
assertEquals('function (){}', mirror.property('b').setter().source());
|
|
|
|
assertFalse(mirror.property('b').isException());
|
|
|
|
// c has both getter and setter. The getter throws an exception.
|
|
|
|
assertTrue(mirror.property('c').hasGetter());
|
|
|
|
assertTrue(mirror.property('c').hasSetter());
|
2008-08-22 13:33:59 +00:00
|
|
|
assertEquals(debug.PropertyType.Callbacks, mirror.property('c').propertyType());
|
2009-01-13 14:38:44 +00:00
|
|
|
assertEquals('function', mirror.property('c').getter().type());
|
|
|
|
assertEquals('function', mirror.property('c').setter().type());
|
|
|
|
assertEquals('function (){throw \'c\';}', mirror.property('c').getter().source());
|
|
|
|
assertEquals('function (){throw \'c\';}', mirror.property('c').setter().source());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Test objects with native accessors.
|
|
|
|
mirror = debug.MakeMirror(new String('abc'));
|
|
|
|
assertTrue(mirror instanceof debug.ObjectMirror);
|
2009-01-13 14:38:44 +00:00
|
|
|
assertFalse(mirror.property('length').hasGetter());
|
|
|
|
assertFalse(mirror.property('length').hasSetter());
|
|
|
|
assertTrue(mirror.property('length').isNative());
|
2008-08-22 13:33:59 +00:00
|
|
|
assertEquals('a', mirror.property(0).value().value());
|
|
|
|
assertEquals('b', mirror.property(1).value().value());
|
|
|
|
assertEquals('c', mirror.property(2).value().value());
|