OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // TODO: add test API implementation. | 5 // If true, prints all messages sent and received by inspector. |
6 const printProtocolMessages = false; | |
Yang
2016/10/28 04:57:06
Let's put this script in strict mode.
jgruber
2016/10/28 06:55:01
Done.
| |
7 | |
8 // The active wrapper instance. | |
9 let activeWrapper = undefined; | |
10 | |
11 // Receiver function called by inspector, delegating to active wrapper. | |
12 function receive(message) { | |
13 activeWrapper.receiveMessage(message); | |
14 } | |
15 | |
16 // TODO(jgruber): Determine which of these are still required and possible. | |
17 // Debug events which can occur in the V8 JavaScript engine. | |
18 const DebugEvent = { Break: 1, | |
19 Exception: 2, | |
20 NewFunction: 3, | |
21 BeforeCompile: 4, | |
22 AfterCompile: 5, | |
23 CompileError: 6, | |
24 AsyncTaskEvent: 7 }; | |
25 | |
26 class DebugWrapper { | |
27 constructor() { | |
28 // Message dictionary storing {id, message} pairs. | |
29 this.receivedMessages = {}; | |
30 | |
31 // Each message dispatched by the Debug wrapper is assigned a unique number | |
32 // using nextMessageId. | |
33 this.nextMessageId = 0; | |
34 | |
35 // The listener method called on certain events. | |
36 this.listener = () => undefined; | |
37 | |
38 // Register as the active wrapper. | |
39 assertTrue(activeWrapper === undefined); | |
40 activeWrapper = this; | |
41 } | |
42 | |
43 enable() { | |
44 const {msgid, msg} = this.createMessage("Debugger.enable"); | |
45 this.sendMessage(msg); | |
46 assertTrue(this.receivedMessages[msgid] !== undefined); | |
47 } | |
48 | |
49 disable() { | |
50 const {msgid, msg} = this.createMessage("Debugger.disable"); | |
51 this.sendMessage(msg); | |
52 assertTrue(this.receivedMessages[msgid] !== undefined); | |
53 } | |
54 | |
55 setListener(listener) { | |
56 this.listener = listener; | |
57 } | |
58 | |
59 // --- Internal methods. ----------------------------------------------------- | |
60 | |
61 getNextMessageId() { | |
62 return this.nextMessageId++; | |
63 } | |
64 | |
65 createMessage(method, params) { | |
66 const id = this.getNextMessageId(); | |
67 const msg = JSON.stringify({ | |
68 id: id, | |
69 method: method, | |
70 params: params, | |
71 }); | |
72 return {msgid: id, msg: msg}; | |
73 } | |
74 | |
75 receiveMessage(message) { | |
76 if (printProtocolMessages) print(message); | |
77 | |
78 const parsedMessage = JSON.parse(message); | |
79 if (parsedMessage.id !== undefined) { | |
80 this.receivedMessages[parsedMessage.id] = parsedMessage; | |
81 } | |
82 | |
83 this.dispatchMessage(parsedMessage); | |
84 } | |
85 | |
86 sendMessage(message) { | |
87 if (printProtocolMessages) print(message); | |
88 send(message); | |
89 } | |
90 | |
91 // --- Message handlers. ----------------------------------------------------- | |
92 | |
93 dispatchMessage(message) { | |
94 const method = message.method; | |
95 if (method == "Debugger.scriptParsed") { | |
96 this.handleDebuggerScriptParsed(message); | |
97 } | |
98 } | |
99 | |
100 handleDebuggerScriptParsed(message) { | |
101 const params = message.params; | |
102 let eventData = { scriptId : params.scriptId | |
103 , eventType : DebugEvent.AfterCompile | |
104 } | |
105 | |
106 // TODO(jgruber): Arguments as needed. Still completely missing exec_state, | |
107 // and eventData used to contain the script mirror instead of its id. | |
108 this.listener(DebugEvent.AfterCompile, undefined, eventData, undefined); | |
109 } | |
110 } | |
OLD | NEW |