Commit acfc50ac authored by yurys@chromium.org's avatar yurys@chromium.org

MirrorSerializer now converts mirrors to plain JS objects. This objects are...

MirrorSerializer now converts mirrors to plain JS objects. This objects are serialized to json string using JSON.stringify.
Review URL: http://codereview.chromium.org/113399

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1957 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 77b9c601
...@@ -833,7 +833,7 @@ BreakEvent.prototype.toJSONProtocol = function() { ...@@ -833,7 +833,7 @@ BreakEvent.prototype.toJSONProtocol = function() {
event: "break", event: "break",
body: { invocationText: this.exec_state_.frame(0).invocationText(), body: { invocationText: this.exec_state_.frame(0).invocationText(),
} }
} };
// Add script related information to the event if available. // Add script related information to the event if available.
var script = this.func().script(); var script = this.func().script();
...@@ -861,8 +861,7 @@ BreakEvent.prototype.toJSONProtocol = function() { ...@@ -861,8 +861,7 @@ BreakEvent.prototype.toJSONProtocol = function() {
o.body.breakpoints.push(number); o.body.breakpoints.push(number);
} }
} }
return JSON.stringify(ObjectToProtocolObject_(o));
return SimpleObjectToJSON_(o);
}; };
...@@ -923,7 +922,7 @@ ExceptionEvent.prototype.toJSONProtocol = function() { ...@@ -923,7 +922,7 @@ ExceptionEvent.prototype.toJSONProtocol = function() {
o.event = "exception"; o.event = "exception";
o.body = { uncaught: this.uncaught_, o.body = { uncaught: this.uncaught_,
exception: MakeMirror(this.exception_) exception: MakeMirror(this.exception_)
} };
// Exceptions might happen whithout any JavaScript frames. // Exceptions might happen whithout any JavaScript frames.
if (this.exec_state_.frameCount() > 0) { if (this.exec_state_.frameCount() > 0) {
...@@ -1079,56 +1078,53 @@ ProtocolMessage.prototype.failed = function(message) { ...@@ -1079,56 +1078,53 @@ ProtocolMessage.prototype.failed = function(message) {
ProtocolMessage.prototype.toJSONProtocol = function() { ProtocolMessage.prototype.toJSONProtocol = function() {
// Encode the protocol header. // Encode the protocol header.
var json = '{'; var json = {};
json += '"seq":' + this.seq; json.seq= this.seq;
if (this.request_seq) { if (this.request_seq) {
json += ',"request_seq":' + this.request_seq; json.request_seq = this.request_seq;
} }
json += ',"type":"' + this.type + '"'; json.type = this.type;
if (this.event) { if (this.event) {
json += ',"event":' + StringToJSON_(this.event); json.event = this.event;
} }
if (this.command) { if (this.command) {
json += ',"command":' + StringToJSON_(this.command); json.command = this.command;
} }
if (this.success) { if (this.success) {
json += ',"success":' + this.success; json.success = this.success;
} else { } else {
json += ',"success":false'; json.success = false;
} }
if (this.body) { if (this.body) {
json += ',"body":';
// Encode the body part. // Encode the body part.
var bodyJson;
var serializer = MakeMirrorSerializer(true, this.options_); var serializer = MakeMirrorSerializer(true, this.options_);
if (this.body instanceof Mirror) { if (this.body instanceof Mirror) {
json += serializer.serializeValue(this.body); bodyJson = serializer.serializeValue(this.body);
} else if (this.body instanceof Array) { } else if (this.body instanceof Array) {
json += '['; bodyJson = [];
for (var i = 0; i < this.body.length; i++) { for (var i = 0; i < this.body.length; i++) {
if (i != 0) json += ',';
if (this.body[i] instanceof Mirror) { if (this.body[i] instanceof Mirror) {
json += serializer.serializeValue(this.body[i]); bodyJson.push(serializer.serializeValue(this.body[i]));
} else { } else {
json += SimpleObjectToJSON_(this.body[i], serializer); bodyJson.push(ObjectToProtocolObject_(this.body[i], serializer));
} }
} }
json += ']';
} else { } else {
json += SimpleObjectToJSON_(this.body, serializer); bodyJson = ObjectToProtocolObject_(this.body, serializer);
} }
json += ',"refs":'; json.body = bodyJson;
json += serializer.serializeReferencedObjects(); json.refs = serializer.serializeReferencedObjects();
} }
if (this.message) { if (this.message) {
json += ',"message":' + StringToJSON_(this.message) ; json.message = this.message;
} }
if (this.running) { if (this.running) {
json += ',"running":true'; json.running = true;
} else { } else {
json += ',"running":false'; json.running = false;
} }
json += '}'; return JSON.stringify(json);
return json;
} }
...@@ -1799,97 +1795,82 @@ DebugCommandProcessor.prototype.formatCFrame = function(cframe_value) { ...@@ -1799,97 +1795,82 @@ DebugCommandProcessor.prototype.formatCFrame = function(cframe_value) {
/** /**
* Convert an Object to its JSON representation (see http://www.json.org/). * Convert an Object to its debugger protocol representation. The representation
* This implementation simply runs through all string property names and adds * may be serilized to a JSON object using JSON.stringify().
* each property to the JSON representation for some predefined types. For type * This implementation simply runs through all string property names, converts
* "object" the function calls itself recursively unless the object has the * each property value to a protocol value and adds the property to the result
* function property "toJSONProtocol" in which case that is used. This is not * object. For type "object" the function will be called recursively. Note that
* a general implementation but sufficient for the debugger. Note that circular * circular structures will cause infinite recursion.
* structures will cause infinite recursion. * @param {Object} object The object to format as protocol object.
* @param {Object} object The object to format as JSON
* @param {MirrorSerializer} mirror_serializer The serializer to use if any * @param {MirrorSerializer} mirror_serializer The serializer to use if any
* mirror objects are encountered. * mirror objects are encountered.
* @return {string} JSON formatted object value * @return {Object} Protocol object value.
*/ */
function SimpleObjectToJSON_(object, mirror_serializer) { function ObjectToProtocolObject_(object, mirror_serializer) {
var content = []; var content = {};
for (var key in object) { for (var key in object) {
// Only consider string keys. // Only consider string keys.
if (typeof key == 'string') { if (typeof key == 'string') {
var property_value = object[key];
// Format the value based on its type. // Format the value based on its type.
var property_value_json; var property_value_json = ValueToProtocolValue_(object[key],
switch (typeof property_value) { mirror_serializer);
case 'object':
if (property_value instanceof Mirror) {
property_value_json = mirror_serializer.serializeValue(property_value);
} else if (typeof property_value.toJSONProtocol == 'function') {
property_value_json = property_value.toJSONProtocol(true)
} else if (IS_ARRAY(property_value)){
property_value_json = SimpleArrayToJSON_(property_value, mirror_serializer);
} else {
property_value_json = SimpleObjectToJSON_(property_value, mirror_serializer);
}
break;
case 'boolean':
property_value_json = BooleanToJSON_(property_value);
break;
case 'number':
property_value_json = NumberToJSON_(property_value);
break;
case 'string':
property_value_json = StringToJSON_(property_value);
break;
default:
property_value_json = null;
}
// Add the property if relevant. // Add the property if relevant.
if (property_value_json) { if (!IS_UNDEFINED(property_value_json)) {
content.push(StringToJSON_(key) + ':' + property_value_json); content[key] = property_value_json;
} }
} }
} }
// Make JSON object representation. return content;
return '{' + content.join(',') + '}';
} }
/** /**
* Convert an array to its JSON representation. This is a VERY simple * Convert an array to its debugger protocol representation. It will convert
* implementation just to support what is needed for the debugger. * each array element to a protocol value.
* @param {Array} array The array to format as JSON * @param {Array} array The array to format as protocol array.
* @param {MirrorSerializer} mirror_serializer The serializer to use if any * @param {MirrorSerializer} mirror_serializer The serializer to use if any
* mirror objects are encountered. * mirror objects are encountered.
* @return {string} JSON formatted array value * @return {Array} Protocol array value.
*/ */
function SimpleArrayToJSON_(array, mirror_serializer) { function ArrayToProtocolArray_(array, mirror_serializer) {
// Make JSON array representation. var json = [];
var json = '[';
for (var i = 0; i < array.length; i++) { for (var i = 0; i < array.length; i++) {
if (i != 0) { json.push(ValueToProtocolValue_(array[i], mirror_serializer));
json += ','; }
} return json;
var elem = array[i]; }
if (elem instanceof Mirror) {
json += mirror_serializer.serializeValue(elem);
} else if (IS_OBJECT(elem)) { /**
json += SimpleObjectToJSON_(elem); * Convert a value to its debugger protocol representation.
} else if (IS_BOOLEAN(elem)) { * @param {*} value The value to format as protocol value.
json += BooleanToJSON_(elem); * @param {MirrorSerializer} mirror_serializer The serializer to use if any
} else if (IS_NUMBER(elem)) { * mirror objects are encountered.
json += NumberToJSON_(elem); * @return {*} Protocol value.
} else if (IS_STRING(elem)) { */
json += StringToJSON_(elem); function ValueToProtocolValue_(value, mirror_serializer) {
} else { // Format the value based on its type.
json += elem; var json;
} switch (typeof value) {
case 'object':
if (value instanceof Mirror) {
json = mirror_serializer.serializeValue(value);
} else if (IS_ARRAY(value)){
json = ArrayToProtocolArray_(value, mirror_serializer);
} else {
json = ObjectToProtocolObject_(value, mirror_serializer);
}
break;
case 'boolean':
case 'string':
case 'number':
json = value;
break
default:
json = null;
} }
json += ']';
return json; return json;
} }
This diff is collapsed.
...@@ -44,8 +44,9 @@ function testArrayMirror(a, names) { ...@@ -44,8 +44,9 @@ function testArrayMirror(a, names) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(a); var mirror = debug.MakeMirror(a);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); var refs = new MirrorRefCache(
JSON.stringify(serializer.serializeReferencedObjects()));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy'); assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy');
......
...@@ -32,7 +32,7 @@ function testBooleanMirror(b) { ...@@ -32,7 +32,7 @@ function testBooleanMirror(b) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(b); var mirror = debug.MakeMirror(b);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -32,7 +32,7 @@ function testDateMirror(d, iso8601) { ...@@ -32,7 +32,7 @@ function testDateMirror(d, iso8601) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(d); var mirror = debug.MakeMirror(d);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
...@@ -55,9 +55,9 @@ function testDateMirror(d, iso8601) { ...@@ -55,9 +55,9 @@ function testDateMirror(d, iso8601) {
assertEquals(iso8601, fromJSON.value); assertEquals(iso8601, fromJSON.value);
} }
// Test Date values. // Test Date values.
testDateMirror(new Date(Date.parse("Dec 25, 1995 1:30 UTC")), "1995-12-25T01:30:00.000Z"); testDateMirror(new Date(Date.parse("Dec 25, 1995 1:30 UTC")),
"1995-12-25T01:30:00Z");
d = new Date(); d = new Date();
d.setUTCFullYear(1967); d.setUTCFullYear(1967);
d.setUTCMonth(0); // January. d.setUTCMonth(0); // January.
...@@ -66,10 +66,10 @@ d.setUTCHours(9); ...@@ -66,10 +66,10 @@ d.setUTCHours(9);
d.setUTCMinutes(22); d.setUTCMinutes(22);
d.setUTCSeconds(59); d.setUTCSeconds(59);
d.setUTCMilliseconds(0); d.setUTCMilliseconds(0);
testDateMirror(d, "1967-01-17T09:22:59.000Z"); testDateMirror(d, "1967-01-17T09:22:59Z");
d.setUTCMilliseconds(1); d.setUTCMilliseconds(1);
testDateMirror(d, "1967-01-17T09:22:59.001Z"); testDateMirror(d, "1967-01-17T09:22:59Z");
d.setUTCMilliseconds(12); d.setUTCSeconds(12);
testDateMirror(d, "1967-01-17T09:22:59.012Z"); testDateMirror(d, "1967-01-17T09:22:12Z");
d.setUTCMilliseconds(123); d.setUTCSeconds(36);
testDateMirror(d, "1967-01-17T09:22:59.123Z"); testDateMirror(d, "1967-01-17T09:22:36Z");
...@@ -44,8 +44,9 @@ function testErrorMirror(e) { ...@@ -44,8 +44,9 @@ function testErrorMirror(e) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(e); var mirror = debug.MakeMirror(e);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); var refs = new MirrorRefCache(
JSON.stringify(serializer.serializeReferencedObjects()));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -44,8 +44,9 @@ function testFunctionMirror(f) { ...@@ -44,8 +44,9 @@ function testFunctionMirror(f) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(f); var mirror = debug.MakeMirror(f);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); var refs = new MirrorRefCache(
JSON.stringify(serializer.serializeReferencedObjects()));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(null); var mirror = debug.MakeMirror(null);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -32,7 +32,7 @@ function testNumberMirror(n) { ...@@ -32,7 +32,7 @@ function testNumberMirror(n) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(n); var mirror = debug.MakeMirror(n);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -44,8 +44,9 @@ function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) { ...@@ -44,8 +44,9 @@ function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(obj); var mirror = debug.MakeMirror(obj);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); var refs = new MirrorRefCache(
JSON.stringify(serializer.serializeReferencedObjects()));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy'); assertTrue(mirror instanceof debug.Mirror, 'Unexpected mirror hierachy');
...@@ -105,7 +106,7 @@ function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) { ...@@ -105,7 +106,7 @@ function testObjectMirror(obj, cls_name, ctor_name, hasSpecialProperties) {
assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON'); assertEquals(names.length, fromJSON.properties.length, 'Some properties missing in JSON');
for (var i = 0; i < fromJSON.properties.length; i++) { for (var i = 0; i < fromJSON.properties.length; i++) {
var name = fromJSON.properties[i].name; var name = fromJSON.properties[i].name;
if (!name) name = fromJSON.properties[i].index; if (typeof name == 'undefined') name = fromJSON.properties[i].index;
var found = false; var found = false;
for (var j = 0; j < names.length; j++) { for (var j = 0; j < names.length; j++) {
if (names[j] == name) { if (names[j] == name) {
...@@ -157,7 +158,6 @@ function Point(x,y) { ...@@ -157,7 +158,6 @@ function Point(x,y) {
this.y_ = y; this.y_ = y;
} }
// Test a number of different objects. // Test a number of different objects.
testObjectMirror({}, 'Object', 'Object'); testObjectMirror({}, 'Object', 'Object');
testObjectMirror({'a':1,'b':2}, 'Object', 'Object'); testObjectMirror({'a':1,'b':2}, 'Object', 'Object');
......
...@@ -55,8 +55,9 @@ function testRegExpMirror(r) { ...@@ -55,8 +55,9 @@ function testRegExpMirror(r) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(r); var mirror = debug.MakeMirror(r);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); var refs = new MirrorRefCache(
JSON.stringify(serializer.serializeReferencedObjects()));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -32,7 +32,7 @@ function testScriptMirror(f, file_name, file_lines, script_type, script_source) ...@@ -32,7 +32,7 @@ function testScriptMirror(f, file_name, file_lines, script_type, script_source)
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(f).script(); var mirror = debug.MakeMirror(f).script();
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -34,7 +34,7 @@ function testStringMirror(s) { ...@@ -34,7 +34,7 @@ function testStringMirror(s) {
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(s); var mirror = debug.MakeMirror(s);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
// Create mirror and JSON representation. // Create mirror and JSON representation.
var mirror = debug.MakeMirror(void 0); var mirror = debug.MakeMirror(void 0);
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
// Check the mirror hierachy. // Check the mirror hierachy.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
...@@ -42,8 +42,9 @@ MirrorRefCache.prototype.lookup = function(handle) { ...@@ -42,8 +42,9 @@ MirrorRefCache.prototype.lookup = function(handle) {
var mirror = new debug.UnresolvedFunctionMirror("f"); var mirror = new debug.UnresolvedFunctionMirror("f");
var serializer = debug.MakeMirrorSerializer(); var serializer = debug.MakeMirrorSerializer();
var json = serializer.serializeValue(mirror); var json = JSON.stringify(serializer.serializeValue(mirror));
var refs = new MirrorRefCache(serializer.serializeReferencedObjects()); var refs = new MirrorRefCache(
JSON.stringify(serializer.serializeReferencedObjects()));
// Check the mirror hierachy for unresolved functions. // Check the mirror hierachy for unresolved functions.
assertTrue(mirror instanceof debug.Mirror); assertTrue(mirror instanceof debug.Mirror);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment