Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 1 | // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Flags: --expose-debug-as debug --harmony-async-await --allow-natives-syntax |
| 6 | // Test the mirror object for promises. |
| 7 | |
| 8 | var AsyncFunction = (async function() {}).constructor; |
| 9 | |
| 10 | function MirrorRefCache(json_refs) { |
| 11 | var tmp = eval('(' + json_refs + ')'); |
| 12 | this.refs_ = []; |
| 13 | for (var i = 0; i < tmp.length; i++) { |
| 14 | this.refs_[tmp[i].handle] = tmp[i]; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | MirrorRefCache.prototype.lookup = function(handle) { |
| 19 | return this.refs_[handle]; |
| 20 | } |
| 21 | |
| 22 | function testPromiseMirror(promise, status, value) { |
| 23 | // Create mirror and JSON representation. |
| 24 | var mirror = debug.MakeMirror(promise); |
| 25 | var serializer = debug.MakeMirrorSerializer(); |
| 26 | var json = JSON.stringify(serializer.serializeValue(mirror)); |
| 27 | var refs = new MirrorRefCache( |
| 28 | JSON.stringify(serializer.serializeReferencedObjects())); |
| 29 | |
| 30 | // Check the mirror hierachy. |
| 31 | assertTrue(mirror instanceof debug.Mirror); |
| 32 | assertTrue(mirror instanceof debug.ValueMirror); |
| 33 | assertTrue(mirror instanceof debug.ObjectMirror); |
| 34 | assertTrue(mirror instanceof debug.PromiseMirror); |
| 35 | |
| 36 | // Check the mirror properties. |
| 37 | assertEquals(status, mirror.status()); |
| 38 | assertTrue(mirror.isPromise()); |
| 39 | assertEquals('promise', mirror.type()); |
| 40 | assertFalse(mirror.isPrimitive()); |
| 41 | assertEquals("Object", mirror.className()); |
| 42 | assertEquals("#<Promise>", mirror.toText()); |
| 43 | assertSame(promise, mirror.value()); |
| 44 | assertTrue(mirror.promiseValue() instanceof debug.Mirror); |
| 45 | assertEquals(value, mirror.promiseValue().value()); |
| 46 | |
| 47 | // Parse JSON representation and check. |
| 48 | var fromJSON = eval('(' + json + ')'); |
| 49 | assertEquals('promise', fromJSON.type); |
| 50 | assertEquals('Object', fromJSON.className); |
| 51 | assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type); |
| 52 | assertEquals('Promise', refs.lookup(fromJSON.constructorFunction.ref).name); |
| 53 | assertEquals(status, fromJSON.status); |
| 54 | assertEquals(value, refs.lookup(fromJSON.promiseValue.ref).value); |
| 55 | } |
| 56 | |
| 57 | // Test a number of different promises. |
| 58 | var resolved = (async function() {})(); |
| 59 | var rejected = (async function() { throw undefined; })(); |
| 60 | var pending = (async function() { await 1; })(); |
| 61 | |
| 62 | testPromiseMirror(resolved, "resolved", undefined); |
| 63 | testPromiseMirror(rejected, "rejected", undefined); |
| 64 | testPromiseMirror(pending, "pending", undefined); |
| 65 | |
| 66 | var resolvedv = (async function() { return "resolve"; })(); |
| 67 | var rejectedv = (async function() { return Promise.reject("reject"); })(); |
| 68 | var thrownv = (async function() { throw "throw"; })(); |
| 69 | |
| 70 | testPromiseMirror(resolvedv, "resolved", 'resolve'); |
| 71 | testPromiseMirror(rejectedv, "rejected", 'reject'); |
| 72 | testPromiseMirror(thrownv, "rejected", 'throw'); |
| 73 | |
| 74 | // Test internal properties of different promises. |
| 75 | var m1 = debug.MakeMirror((async function() { return 1; })()); |
| 76 | var ip = m1.internalProperties(); |
| 77 | assertEquals(2, ip.length); |
| 78 | assertEquals("[[PromiseStatus]]", ip[0].name()); |
| 79 | assertEquals("[[PromiseValue]]", ip[1].name()); |
| 80 | assertEquals("resolved", ip[0].value().value()); |
| 81 | assertEquals(1, ip[1].value().value()); |
| 82 | |
| 83 | var m2 = debug.MakeMirror((async function() { throw 2; })()); |
| 84 | ip = m2.internalProperties(); |
| 85 | assertEquals("rejected", ip[0].value().value()); |
| 86 | assertEquals(2, ip[1].value().value()); |
| 87 | |
| 88 | var m3 = debug.MakeMirror((async function() { await 1; })()); |
| 89 | ip = m3.internalProperties(); |
| 90 | assertEquals("pending", ip[0].value().value()); |
| 91 | assertEquals("undefined", typeof(ip[1].value().value())); |
| 92 | |
| 93 | %RunMicrotasks(); |