blob: 966b0ce26743e4297d26d8e9e29c6f81732ef6a8 [file] [log] [blame]
Ben Murdoch61f157c2016-09-16 13:49:30 +01001// 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
8var AsyncFunction = (async function() {}).constructor;
9
10function 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
18MirrorRefCache.prototype.lookup = function(handle) {
19 return this.refs_[handle];
20}
21
22function 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.
58var resolved = (async function() {})();
59var rejected = (async function() { throw undefined; })();
60var pending = (async function() { await 1; })();
61
62testPromiseMirror(resolved, "resolved", undefined);
63testPromiseMirror(rejected, "rejected", undefined);
64testPromiseMirror(pending, "pending", undefined);
65
66var resolvedv = (async function() { return "resolve"; })();
67var rejectedv = (async function() { return Promise.reject("reject"); })();
68var thrownv = (async function() { throw "throw"; })();
69
70testPromiseMirror(resolvedv, "resolved", 'resolve');
71testPromiseMirror(rejectedv, "rejected", 'reject');
72testPromiseMirror(thrownv, "rejected", 'throw');
73
74// Test internal properties of different promises.
75var m1 = debug.MakeMirror((async function() { return 1; })());
76var ip = m1.internalProperties();
77assertEquals(2, ip.length);
78assertEquals("[[PromiseStatus]]", ip[0].name());
79assertEquals("[[PromiseValue]]", ip[1].name());
80assertEquals("resolved", ip[0].value().value());
81assertEquals(1, ip[1].value().value());
82
83var m2 = debug.MakeMirror((async function() { throw 2; })());
84ip = m2.internalProperties();
85assertEquals("rejected", ip[0].value().value());
86assertEquals(2, ip[1].value().value());
87
88var m3 = debug.MakeMirror((async function() { await 1; })());
89ip = m3.internalProperties();
90assertEquals("pending", ip[0].value().value());
91assertEquals("undefined", typeof(ip[1].value().value()));
92
93%RunMicrotasks();