Chromium Code Reviews| Index: test/inspector/debugger/wasm-stepping.js |
| diff --git a/test/inspector/debugger/wasm-stepping.js b/test/inspector/debugger/wasm-stepping.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..944e1c6031d5832be7bc1931b092851048c18d8f |
| --- /dev/null |
| +++ b/test/inspector/debugger/wasm-stepping.js |
| @@ -0,0 +1,179 @@ |
| +// Copyright 2017 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +load('test/mjsunit/wasm/wasm-constants.js'); |
| +load('test/mjsunit/wasm/wasm-module-builder.js'); |
| + |
| +var builder = new WasmModuleBuilder(); |
| + |
| +var func_a_idx = |
| + builder.addFunction('wasm_A', kSig_v_v).addBody([kExprNop, kExprNop]).index; |
| + |
| +// wasm_B calls wasm_A <param0> times. |
| +builder.addFunction('wasm_B', kSig_v_i) |
| + .addBody([ |
| + // clang-format off |
| + kExprLoop, kWasmStmt, // while |
| + kExprGetLocal, 0, // - |
| + kExprIf, kWasmStmt, // if <param0> != 0 |
| + kExprGetLocal, 0, // - |
| + kExprI32Const, 1, // - |
| + kExprI32Sub, // - |
| + kExprSetLocal, 0, // decrease <param0> |
| + kExprCallFunction, func_a_idx, // - |
| + kExprBr, 1, // continue |
| + kExprEnd, // - |
| + kExprEnd, // break |
| + // clang-format on |
| + ]) |
| + .exportAs('main'); |
| + |
| +var module_bytes = builder.toArray(); |
| + |
| +function instantiate(bytes) { |
| + var buffer = new ArrayBuffer(bytes.length); |
| + var view = new Uint8Array(buffer); |
| + for (var i = 0; i < bytes.length; ++i) { |
| + view[i] = bytes[i] | 0; |
| + } |
| + |
| + var module = new WebAssembly.Module(buffer); |
| + // Set global variable. |
| + instance = new WebAssembly.Instance(module); |
| +} |
| + |
| +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.
|
| + |
| +function evalThen(expr, url, next) { |
| + var promise = Protocol.Runtime.evaluate( |
| + {'expression': addSourceUrl(expr, 'v8://test/' + url)}); |
| + if (next) promise = promise.then(next); |
| + return promise; |
| +} |
| + |
| +Protocol.Debugger.onScriptParsed(handleScriptParsed); |
| +Protocol.Debugger.onPaused(handlePaused); |
| +var wasm_B_scriptId; |
| +var step_actions = [ |
| + 'stepInto', // == stepOver, to call instruction |
| + 'stepInto', // into call to wasm_A |
| + 'stepOver', // over first nop |
| + 'stepOut', // out of wasm_A |
| + 'stepOut', // out of wasm_B, stop on breakpoint again |
| + 'stepOver', // to call |
| + 'stepOver', // over call |
| + 'resume', // to next breakpoint (third iteration) |
| + 'stepInto', // to call |
| + 'stepInto', // into wasm_A |
| + 'stepOut', // out to wasm_B |
| + // now step 9 times, until we are in wasm_A again. |
| + 'stepInto', 'stepInto', 'stepInto', 'stepInto', 'stepInto', 'stepInto', |
| + 'stepInto', 'stepInto', 'stepInto', |
| + // 3 more times, back to wasm_B. |
| + 'stepInto', 'stepInto', 'stepInto', |
| + // then just resume. |
| + 'resume' |
| +]; |
| +var sources = {}; |
| +var urls = {}; |
| +var afterTwoSourcesCallback; |
| + |
| +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.
|
| + function enableDebugger(next) { |
| + Protocol.Debugger.enable().then(next); |
| + }, |
| + |
| + function defineInstance(next) { |
| + evalThen('var instance;', 'defineInstance', next); |
| + }, |
| + |
| + function defineInstantiateFunction(next) { |
| + evalThen(instantiate.toString(), 'defineInstantiateFunction', next); |
| + }, |
| + |
| + function callInstantiate(next) { |
| + afterTwoSourcesCallback = next; |
| + evalThen( |
| + 'instantiate(' + JSON.stringify(module_bytes) + ')', 'callInstantiate'); |
| + }, |
| + |
| + function setBreakpoint(next) { |
| + if (!wasm_B_scriptId) { |
| + InspectorTest.log('Missing wasm_B script id'); |
| + } |
| + InspectorTest.log( |
| + 'Setting breakpoint on line 7 (on the setlocal before the call)'); |
| + Protocol.Debugger |
| + .setBreakpoint( |
| + {'location': {'scriptId': wasm_B_scriptId, 'lineNumber': 7}}) |
| + .then(msg => InspectorTest.logMessage(msg)) |
|
kozy
2017/01/25 18:04:59
.then(InspectorTest.logMessage)
Clemens Hammacher
2017/01/25 21:06:11
Done.
|
| + .then(next); |
| + }, |
| + |
| + function runTestFunction(next) { |
| + evalThen('instance.exports.main(4)', 'runWasm') |
| + .then(() => InspectorTest.log('exports.main returned!')) |
| + .then(next); |
| + }, |
| + |
| + function finished(next) { |
| + InspectorTest.log('Finished!'); |
| + next(); |
| + } |
| +]); |
| + |
| +function getSourceFromMessage(message) { |
| + if (!message.result || !message.result.scriptSource) { |
| + InspectorTest.logMessage(message); |
| + return ''; |
| + } |
| + return message.result.scriptSource; |
| +} |
| + |
| +function handleScriptParsed(msg) { |
| + var url = msg.params.url; |
| + if (!url.startsWith('wasm://')) { |
| + InspectorTest.log('Ignoring script with url ' + url); |
| + return; |
| + } |
| + var scriptId = msg.params.scriptId; |
| + urls[scriptId] = url; |
| + InspectorTest.log('Got wasm script: ' + url); |
| + if (url.substr(-2) == '-1') wasm_B_scriptId = scriptId; |
| + InspectorTest.log('Requesting source...'); |
| + function printAndStoreSource(src) { |
| + InspectorTest.log(src); |
| + sources[scriptId] = src; |
| + 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
|
| + } |
| + |
| + Protocol.Debugger.getScriptSource({scriptId: scriptId}) |
| + .then(msg => printAndStoreSource(getSourceFromMessage(msg))); |
| +} |
| + |
| +function printPauseLocation(scriptId, lineNr, columnNr) { |
| + var lines = sources[scriptId].split('\n'); |
| + var line = '<illegal line number>'; |
| + if (lineNr < lines.length) { |
| + line = lines[lineNr]; |
| + if (columnNr < line.length) { |
| + line = line.substr(0, columnNr) + '>' + line.substr(columnNr); |
| + } |
| + } |
| + InspectorTest.log( |
| + 'Paused at ' + urls[scriptId] + ':' + lineNr + ':' + columnNr + ': ' + |
| + line); |
| +} |
| + |
| +function handlePaused(msg) { |
| + var loc = msg.params.callFrames[0].location; |
| + printPauseLocation(loc.scriptId, loc.lineNumber, loc.columnNumber); |
| + var action = step_actions.shift(); |
| + 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
|
| + if (!action) { |
| + InspectorTest.log('Undefined step action! Assuming resume.'); |
| + action = 'resume'; |
| + } |
| + Protocol.Debugger[action](); |
| +} |