OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 load('test/mjsunit/wasm/wasm-constants.js'); | |
6 load('test/mjsunit/wasm/wasm-module-builder.js'); | |
7 | |
8 var builder = new WasmModuleBuilder(); | |
9 | |
10 var func_a_idx = | |
11 builder.addFunction('wasm_A', kSig_v_v).addBody([kExprNop, kExprNop]).index; | |
12 | |
13 // wasm_B calls wasm_A <param0> times. | |
14 builder.addFunction('wasm_B', kSig_v_i) | |
15 .addBody([ | |
16 // clang-format off | |
17 kExprLoop, kWasmStmt, // while | |
18 kExprGetLocal, 0, // - | |
19 kExprIf, kWasmStmt, // if <param0> != 0 | |
20 kExprGetLocal, 0, // - | |
21 kExprI32Const, 1, // - | |
22 kExprI32Sub, // - | |
23 kExprSetLocal, 0, // decrease <param0> | |
24 kExprCallFunction, func_a_idx, // - | |
25 kExprBr, 1, // continue | |
26 kExprEnd, // - | |
27 kExprEnd, // break | |
28 // clang-format on | |
29 ]) | |
30 .exportAs('main'); | |
31 | |
32 var module_bytes = builder.toArray(); | |
33 | |
34 function instantiate(bytes) { | |
35 var buffer = new ArrayBuffer(bytes.length); | |
36 var view = new Uint8Array(buffer); | |
37 for (var i = 0; i < bytes.length; ++i) { | |
38 view[i] = bytes[i] | 0; | |
39 } | |
40 | |
41 var module = new WebAssembly.Module(buffer); | |
42 // Set global variable. | |
43 instance = new WebAssembly.Instance(module); | |
44 } | |
45 | |
46 var addSourceUrl = (code, url) => '//# sourceURL=' + url + '\n' + code; | |
kozy
2017/01/25 18:04:59
Usually sourceURL comment is last line of source.
Clemens Hammacher
2017/01/25 21:06:11
Done.
| |
47 | |
48 function evalThen(expr, url, next) { | |
49 var promise = Protocol.Runtime.evaluate( | |
50 {'expression': addSourceUrl(expr, 'v8://test/' + url)}); | |
51 if (next) promise = promise.then(next); | |
52 return promise; | |
53 } | |
54 | |
55 Protocol.Debugger.onScriptParsed(handleScriptParsed); | |
56 Protocol.Debugger.onPaused(handlePaused); | |
57 var wasm_B_scriptId; | |
58 var step_actions = [ | |
59 'stepInto', // == stepOver, to call instruction | |
60 'stepInto', // into call to wasm_A | |
61 'stepOver', // over first nop | |
62 'stepOut', // out of wasm_A | |
63 'stepOut', // out of wasm_B, stop on breakpoint again | |
64 'stepOver', // to call | |
65 'stepOver', // over call | |
66 'resume', // to next breakpoint (third iteration) | |
67 'stepInto', // to call | |
68 'stepInto', // into wasm_A | |
69 'stepOut', // out to wasm_B | |
70 // now step 9 times, until we are in wasm_A again. | |
71 'stepInto', 'stepInto', 'stepInto', 'stepInto', 'stepInto', 'stepInto', | |
72 'stepInto', 'stepInto', 'stepInto', | |
73 // 3 more times, back to wasm_B. | |
74 'stepInto', 'stepInto', 'stepInto', | |
75 // then just resume. | |
76 'resume' | |
77 ]; | |
78 var sources = {}; | |
79 var urls = {}; | |
80 var afterTwoSourcesCallback; | |
81 | |
82 InspectorTest.runTestSuite([ | |
kozy
2017/01/25 18:04:59
We usually use it when test contains set of small
Clemens Hammacher
2017/01/25 21:06:11
Changed it.
| |
83 function enableDebugger(next) { | |
84 Protocol.Debugger.enable().then(next); | |
85 }, | |
86 | |
87 function defineInstance(next) { | |
88 evalThen('var instance;', 'defineInstance', next); | |
89 }, | |
90 | |
91 function defineInstantiateFunction(next) { | |
92 evalThen(instantiate.toString(), 'defineInstantiateFunction', next); | |
93 }, | |
94 | |
95 function callInstantiate(next) { | |
96 afterTwoSourcesCallback = next; | |
97 evalThen( | |
98 'instantiate(' + JSON.stringify(module_bytes) + ')', 'callInstantiate'); | |
99 }, | |
100 | |
101 function setBreakpoint(next) { | |
102 if (!wasm_B_scriptId) { | |
103 InspectorTest.log('Missing wasm_B script id'); | |
104 } | |
105 InspectorTest.log( | |
106 'Setting breakpoint on line 7 (on the setlocal before the call)'); | |
107 Protocol.Debugger | |
108 .setBreakpoint( | |
109 {'location': {'scriptId': wasm_B_scriptId, 'lineNumber': 7}}) | |
110 .then(msg => InspectorTest.logMessage(msg)) | |
kozy
2017/01/25 18:04:59
.then(InspectorTest.logMessage)
Clemens Hammacher
2017/01/25 21:06:11
Done.
| |
111 .then(next); | |
112 }, | |
113 | |
114 function runTestFunction(next) { | |
115 evalThen('instance.exports.main(4)', 'runWasm') | |
116 .then(() => InspectorTest.log('exports.main returned!')) | |
117 .then(next); | |
118 }, | |
119 | |
120 function finished(next) { | |
121 InspectorTest.log('Finished!'); | |
122 next(); | |
123 } | |
124 ]); | |
125 | |
126 function getSourceFromMessage(message) { | |
127 if (!message.result || !message.result.scriptSource) { | |
128 InspectorTest.logMessage(message); | |
129 return ''; | |
130 } | |
131 return message.result.scriptSource; | |
132 } | |
133 | |
134 function handleScriptParsed(msg) { | |
135 var url = msg.params.url; | |
136 if (!url.startsWith('wasm://')) { | |
137 InspectorTest.log('Ignoring script with url ' + url); | |
138 return; | |
139 } | |
140 var scriptId = msg.params.scriptId; | |
141 urls[scriptId] = url; | |
142 InspectorTest.log('Got wasm script: ' + url); | |
143 if (url.substr(-2) == '-1') wasm_B_scriptId = scriptId; | |
144 InspectorTest.log('Requesting source...'); | |
145 function printAndStoreSource(src) { | |
146 InspectorTest.log(src); | |
147 sources[scriptId] = src; | |
148 if (Object.keys(sources).length == 2) afterTwoSourcesCallback(); | |
kozy
2017/01/25 18:04:59
You could right this logic in following way:
Proto
Clemens Hammacher
2017/01/25 21:06:11
Done.
I hope I will get better in writing these te
| |
149 } | |
150 | |
151 Protocol.Debugger.getScriptSource({scriptId: scriptId}) | |
152 .then(msg => printAndStoreSource(getSourceFromMessage(msg))); | |
153 } | |
154 | |
155 function printPauseLocation(scriptId, lineNr, columnNr) { | |
156 var lines = sources[scriptId].split('\n'); | |
157 var line = '<illegal line number>'; | |
158 if (lineNr < lines.length) { | |
159 line = lines[lineNr]; | |
160 if (columnNr < line.length) { | |
161 line = line.substr(0, columnNr) + '>' + line.substr(columnNr); | |
162 } | |
163 } | |
164 InspectorTest.log( | |
165 'Paused at ' + urls[scriptId] + ':' + lineNr + ':' + columnNr + ': ' + | |
166 line); | |
167 } | |
168 | |
169 function handlePaused(msg) { | |
170 var loc = msg.params.callFrames[0].location; | |
171 printPauseLocation(loc.scriptId, loc.lineNumber, loc.columnNumber); | |
172 var action = step_actions.shift(); | |
173 InspectorTest.log('Step action: ' + action); | |
kozy
2017/01/25 18:04:59
move this line to else block of following if, othe
Clemens Hammacher
2017/01/25 21:06:11
As this is not supposed to happen, I just removed
| |
174 if (!action) { | |
175 InspectorTest.log('Undefined step action! Assuming resume.'); | |
176 action = 'resume'; | |
177 } | |
178 Protocol.Debugger[action](); | |
179 } | |
OLD | NEW |