From 75a7a3157f148ba23ca26fc38b8b7c67bbbace07 Mon Sep 17 00:00:00 2001 From: "yangguo@chromium.org" Date: Fri, 16 May 2014 13:06:20 +0000 Subject: [PATCH] Use %DebugGetProperty in debug mirror to check for Promise. R=aandrey@chromium.org, amikhaylova@google.com Review URL: https://codereview.chromium.org/283373003 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@21339 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/mirror-debugger.js | 33 ++++++++++++++++++++++++++------- src/promise.js | 9 --------- test/mjsunit/es6/mirror-promises.js | 25 +++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/mirror-debugger.js b/src/mirror-debugger.js index fde3f10..124347b 100644 --- a/src/mirror-debugger.js +++ b/src/mirror-debugger.js @@ -21,10 +21,11 @@ function ClearMirrorCache() { // Wrapper to check whether an object is a Promise. The call may not work // if promises are not enabled. -// TODO(yangguo): remove this wrapper once promises are enabled by default. +// TODO(yangguo): remove try-catch once promises are enabled by default. function ObjectIsPromise(value) { try { - return %IsPromise(value); + return IS_SPEC_OBJECT(value) && + !IS_UNDEFINED(%DebugGetProperty(value, builtins.promiseStatus)); } catch (e) { return false; } @@ -798,7 +799,8 @@ ObjectMirror.prototype.toText = function() { /** * Return the internal properties of the value, such as [[PrimitiveValue]] of - * scalar wrapper objects and properties of the bound function. + * scalar wrapper objects, properties of the bound function and properties of + * the promise. * This method is done static to be accessible from Debug API with the bare * values without mirrors. * @return {Array} array (possibly empty) of InternalProperty instances @@ -822,6 +824,13 @@ ObjectMirror.GetInternalProperties = function(value) { result.push(new InternalPropertyMirror("[[BoundArgs]]", boundArgs)); } return result; + } else if (ObjectIsPromise(value)) { + var result = []; + result.push(new InternalPropertyMirror("[[PromiseStatus]]", + PromiseGetStatus_(value))); + result.push(new InternalPropertyMirror("[[PromiseValue]]", + PromiseGetValue_(value))); + return result; } return []; } @@ -1185,16 +1194,26 @@ function PromiseMirror(value) { inherits(PromiseMirror, ObjectMirror); -PromiseMirror.prototype.status = function() { - var status = builtins.GetPromiseStatus(this.value_); +function PromiseGetStatus_(value) { + var status = %DebugGetProperty(value, builtins.promiseStatus); if (status == 0) return "pending"; if (status == 1) return "resolved"; return "rejected"; +} + + +function PromiseGetValue_(value) { + return %DebugGetProperty(value, builtins.promiseValue); +} + + +PromiseMirror.prototype.status = function() { + return PromiseGetStatus_(this.value_); }; PromiseMirror.prototype.promiseValue = function() { - return builtins.GetPromiseValue(this.value_); + return MakeMirror(PromiseGetValue_(this.value_)); }; @@ -2515,7 +2534,7 @@ JSONProtocolSerializer.prototype.serializeObject_ = function(mirror, content, if (mirror.isPromise()) { // Add promise specific properties. content.status = mirror.status(); - content.promiseValue = mirror.promiseValue(); + content.promiseValue = this.serializeReference(mirror.promiseValue()); } // Add actual properties - named properties followed by indexed properties. diff --git a/src/promise.js b/src/promise.js index f4c60d6..959be51 100644 --- a/src/promise.js +++ b/src/promise.js @@ -301,12 +301,3 @@ function SetUpPromise() { } SetUpPromise(); - -// Functions to expose promise details to the debugger. -function GetPromiseStatus(promise) { - return GET_PRIVATE(promise, promiseStatus); -} - -function GetPromiseValue(promise) { - return GET_PRIVATE(promise, promiseValue); -} diff --git a/test/mjsunit/es6/mirror-promises.js b/test/mjsunit/es6/mirror-promises.js index 5a21a6b..bce26f4 100644 --- a/test/mjsunit/es6/mirror-promises.js +++ b/test/mjsunit/es6/mirror-promises.js @@ -39,7 +39,8 @@ function testPromiseMirror(promise, status, value) { assertEquals("Object", mirror.className()); assertEquals("#", mirror.toText()); assertSame(promise, mirror.value()); - assertEquals(value, mirror.promiseValue()); + assertTrue(mirror.promiseValue() instanceof debug.Mirror); + assertEquals(value, mirror.promiseValue().value()); // Parse JSON representation and check. var fromJSON = eval('(' + json + ')'); @@ -48,7 +49,7 @@ function testPromiseMirror(promise, status, value) { assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type); assertEquals('Promise', refs.lookup(fromJSON.constructorFunction.ref).name); assertEquals(status, fromJSON.status); - assertEquals(value, fromJSON.promiseValue); + assertEquals(value, refs.lookup(fromJSON.promiseValue.ref).value); } // Test a number of different promises. @@ -67,3 +68,23 @@ var thrownv = new Promise(function(resolve, reject) { throw 'throw' }); testPromiseMirror(resolvedv, "resolved", 'resolve'); testPromiseMirror(rejectedv, "rejected", 'reject'); testPromiseMirror(thrownv, "rejected", 'throw'); + +// Test internal properties of different promises. +var m1 = debug.MakeMirror(new Promise( + function(resolve, reject) { resolve(1) })); +var ip = m1.internalProperties(); +assertEquals(2, ip.length); +assertEquals("[[PromiseStatus]]", ip[0].name()); +assertEquals("[[PromiseValue]]", ip[1].name()); +assertEquals("resolved", ip[0].value().value()); +assertEquals(1, ip[1].value().value()); + +var m2 = debug.MakeMirror(new Promise(function(resolve, reject) { reject(2) })); +ip = m2.internalProperties(); +assertEquals("rejected", ip[0].value().value()); +assertEquals(2, ip[1].value().value()); + +var m3 = debug.MakeMirror(new Promise(function(resolve, reject) { })); +ip = m3.internalProperties(); +assertEquals("pending", ip[0].value().value()); +assertEquals("undefined", typeof(ip[1].value().value())); -- 2.7.4