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 functions. |
| 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 testFunctionMirror(f) { |
| 23 | // Create mirror and JSON representation. |
| 24 | var mirror = debug.MakeMirror(f); |
| 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.FunctionMirror); |
| 35 | |
| 36 | // Check the mirror properties. |
| 37 | assertTrue(mirror.isFunction()); |
| 38 | assertEquals('function', mirror.type()); |
| 39 | assertFalse(mirror.isPrimitive()); |
| 40 | assertEquals("Function", mirror.className()); |
| 41 | assertEquals(f.name, mirror.name()); |
| 42 | assertTrue(mirror.resolved()); |
| 43 | assertEquals(f.toString(), mirror.source()); |
| 44 | assertTrue(mirror.constructorFunction() instanceof debug.ObjectMirror); |
| 45 | assertTrue(mirror.protoObject() instanceof debug.Mirror); |
| 46 | assertTrue(mirror.prototypeObject() instanceof debug.Mirror); |
| 47 | |
| 48 | // Test text representation |
| 49 | assertEquals(f.toString(), mirror.toText()); |
| 50 | |
| 51 | // Parse JSON representation and check. |
| 52 | var fromJSON = eval('(' + json + ')'); |
| 53 | assertEquals('function', fromJSON.type); |
| 54 | assertEquals('Function', fromJSON.className); |
| 55 | assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type); |
| 56 | assertEquals('AsyncFunction', |
| 57 | refs.lookup(fromJSON.constructorFunction.ref).name); |
| 58 | assertTrue(fromJSON.resolved); |
| 59 | assertEquals(f.name, fromJSON.name); |
| 60 | assertEquals(f.toString(), fromJSON.source); |
| 61 | |
| 62 | // Check the formatted text (regress 1142074). |
| 63 | assertEquals(f.toString(), fromJSON.text); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | // Test a number of different functions. |
| 68 | testFunctionMirror(async function(){}); |
| 69 | testFunctionMirror(AsyncFunction()); |
| 70 | testFunctionMirror(new AsyncFunction()); |
| 71 | testFunctionMirror(async() => {}); |
| 72 | testFunctionMirror(async function a(){return 1;}); |
| 73 | testFunctionMirror(({ async foo() {} }).foo); |
| 74 | testFunctionMirror((async function(){}).bind({}), "Object"); |
| 75 | |
| 76 | %RunMicrotasks(); |