2012-09-10 23:17:04 +00:00
|
|
|
// Copyright 2012 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];
|
2011-04-15 11:35:36 +00:00
|
|
|
};
|
2009-01-16 09:54:46 +00:00
|
|
|
|
|
|
|
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.
|
2012-09-10 23:17:04 +00:00
|
|
|
assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierarchy');
|
|
|
|
assertTrue(mirror instanceof debug.ValueMirror, 'Unexpected mirror hierarchy');
|
|
|
|
assertTrue(mirror instanceof debug.ObjectMirror, 'Unexpected mirror hierarchy');
|
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');
|
2012-09-10 23:17:04 +00:00
|
|
|
assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror, 'Unexpected mirror hierarchy');
|
2009-01-16 09:54:46 +00:00
|
|
|
assertEquals(ctor_name, mirror.constructorFunction().name(), 'Unexpected constructor function name');
|
2012-09-10 23:17:04 +00:00
|
|
|
assertTrue(mirror.protoObject() instanceof debug.Mirror, 'Unexpected mirror hierarchy');
|
|
|
|
assertTrue(mirror.prototypeObject() instanceof debug.Mirror, 'Unexpected mirror hierarchy');
|
2009-01-16 09:54:46 +00:00
|
|
|
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();
|
2011-04-15 11:35:36 +00:00
|
|
|
var properties = mirror.properties();
|
2008-08-22 13:33:59 +00:00
|
|
|
assertEquals(names.length, properties.length);
|
|
|
|
for (var i = 0; i < properties.length; i++) {
|
2012-09-10 23:17:04 +00:00
|
|
|
assertTrue(properties[i] instanceof debug.Mirror, 'Unexpected mirror hierarchy');
|
|
|
|
assertTrue(properties[i] instanceof debug.PropertyMirror, 'Unexpected mirror hierarchy');
|
2009-01-16 09:54:46 +00:00
|
|
|
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
|
|
|
|
2012-09-10 23:17:04 +00:00
|
|
|
var internalProperties = mirror.internalProperties();
|
|
|
|
for (var i = 0; i < internalProperties.length; i++) {
|
|
|
|
assertTrue(internalProperties[i] instanceof debug.Mirror, 'Unexpected mirror hierarchy');
|
|
|
|
assertTrue(internalProperties[i] instanceof debug.InternalPropertyMirror, 'Unexpected mirror hierarchy');
|
|
|
|
assertEquals('internalProperty', internalProperties[i].type(), 'Unexpected mirror type');
|
|
|
|
}
|
|
|
|
|
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');
|
2014-08-07 08:36:38 +00:00
|
|
|
for (var j = 0; j < names.length; j++) {
|
|
|
|
var name = names[j];
|
|
|
|
// Serialization of symbol-named properties to JSON doesn't really
|
|
|
|
// work currently, as they don't get a {name: ...} entry.
|
|
|
|
if (typeof name === 'symbol') continue;
|
2008-08-22 13:33:59 +00:00
|
|
|
var found = false;
|
2014-08-07 08:36:38 +00:00
|
|
|
for (var i = 0; i < fromJSON.properties.length; i++) {
|
|
|
|
if (fromJSON.properties[i].name == 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');
|
|
|
|
|
2014-11-19 11:45:17 +00:00
|
|
|
assertEquals(properties[i].propertyType(), fromJSON.properties[i].propertyType, 'Unexpected serialized property type');
|
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
|
|
|
|
2011-04-15 11:35:36 +00:00
|
|
|
// Lookup the serialized object from the handle reference.
|
2009-01-16 09:54:46 +00:00
|
|
|
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()) {
|
2011-04-15 11:35:36 +00:00
|
|
|
if (properties[i].value().type() == "null" ||
|
|
|
|
properties[i].value().type() == "undefined") {
|
|
|
|
// Null and undefined has no value property.
|
|
|
|
assertFalse("value" in o, 'Unexpected value property for ' + name);
|
|
|
|
} else if (properties[i].value().type() == "number" &&
|
|
|
|
!isFinite(properties[i].value().value())) {
|
|
|
|
assertEquals(String(properties[i].value().value()), o.value,
|
|
|
|
'Unexpected serialized property value for ' + name);
|
2009-01-21 09:32:07 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2014-08-07 08:36:38 +00:00
|
|
|
var object_with_symbol = {};
|
|
|
|
object_with_symbol[Symbol.iterator] = 42;
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
// 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');
|
2012-09-10 23:17:04 +00:00
|
|
|
testObjectMirror(Object(17), 'Number', 'Number');
|
2014-08-07 08:36:38 +00:00
|
|
|
testObjectMirror(object_with_symbol, 'Object', 'Object');
|
2008-08-22 13:33:59 +00:00
|
|
|
|
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());
|
2015-01-19 17:49:13 +00:00
|
|
|
assertEquals(debug.PropertyType.AccessorConstant, 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());
|
2015-01-19 17:49:13 +00:00
|
|
|
assertEquals(debug.PropertyType.AccessorConstant, 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());
|
2015-01-19 17:49:13 +00:00
|
|
|
assertEquals(debug.PropertyType.AccessorConstant, 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());
|
2012-09-10 23:17:04 +00:00
|
|
|
|
|
|
|
// Test value wrapper internal properties.
|
|
|
|
mirror = debug.MakeMirror(Object("Capybara"));
|
|
|
|
var ip = mirror.internalProperties();
|
|
|
|
assertEquals(1, ip.length);
|
|
|
|
assertEquals("[[PrimitiveValue]]", ip[0].name());
|
|
|
|
assertEquals("string", ip[0].value().type());
|
|
|
|
assertEquals("Capybara", ip[0].value().value());
|
|
|
|
|
|
|
|
// Test bound function internal properties.
|
|
|
|
mirror = debug.MakeMirror(Number.bind(Array, 2));
|
|
|
|
ip = mirror.internalProperties();
|
|
|
|
assertEquals(3, ip.length);
|
|
|
|
var property_map = {};
|
|
|
|
for (var i = 0; i < ip.length; i++) {
|
|
|
|
property_map[ip[i].name()] = ip[i];
|
|
|
|
}
|
|
|
|
assertTrue("[[BoundThis]]" in property_map);
|
|
|
|
assertEquals("function", property_map["[[BoundThis]]"].value().type());
|
|
|
|
assertEquals(Array, property_map["[[BoundThis]]"].value().value());
|
|
|
|
assertTrue("[[TargetFunction]]" in property_map);
|
|
|
|
assertEquals("function", property_map["[[TargetFunction]]"].value().type());
|
|
|
|
assertEquals(Number, property_map["[[TargetFunction]]"].value().value());
|
|
|
|
assertTrue("[[BoundArgs]]" in property_map);
|
|
|
|
assertEquals("object", property_map["[[BoundArgs]]"].value().type());
|
|
|
|
assertEquals(1, property_map["[[BoundArgs]]"].value().value().length);
|
2016-05-02 17:44:51 +00:00
|
|
|
|
|
|
|
// Test JSProxy internal properties.
|
|
|
|
var target = {};
|
|
|
|
var handler = {
|
|
|
|
get: function (target, name, receiver) {
|
|
|
|
return target[name];
|
|
|
|
},
|
|
|
|
set: function(target, name, value, receiver) {
|
|
|
|
target[name] = value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ip = debug.ObjectMirror.GetInternalProperties(new Proxy(target, handler));
|
|
|
|
assertEquals(3, ip.length);
|
|
|
|
var property_map = {};
|
|
|
|
for (var i = 0; i < ip.length; i++) {
|
|
|
|
property_map[ip[i].name()] = ip[i];
|
|
|
|
}
|
|
|
|
assertTrue("[[Target]]" in property_map);
|
|
|
|
assertEquals(target, property_map["[[Target]]"].value().value());
|
|
|
|
assertTrue("[[Handler]]" in property_map);
|
|
|
|
assertEquals(handler, property_map["[[Handler]]"].value().value());
|
|
|
|
assertTrue("[[IsRevoked]]" in property_map);
|
|
|
|
assertEquals(false, property_map["[[IsRevoked]]"].value().value());
|