wasm-scripts.js 2.19 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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.

// Flags: --expose-wasm

7 8
let {session, contextGroup, Protocol} = InspectorTest.start('Tests how wasm scripts are reported');

9
utils.load('test/mjsunit/wasm/wasm-module-builder.js');
10 11 12 13 14 15

// Add two empty functions. Both should be registered as individual scripts at
// module creation time.
var builder = new WasmModuleBuilder();
builder.addFunction('nopFunction', kSig_v_v).addBody([kExprNop]);
builder.addFunction('main', kSig_v_v)
16
    .addBody([kExprBlock, kWasmStmt, kExprI32Const, 2, kExprDrop, kExprEnd])
17 18 19 20 21 22 23 24 25 26 27 28 29 30
    .exportAs('main');
var module_bytes = builder.toArray();

function testFunction(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;
  }

  // Compilation triggers registration of wasm scripts.
  new WebAssembly.Module(buffer);
}

31 32
contextGroup.addScript(testFunction.toString(), 0, 0, 'v8://test/testFunction');
contextGroup.addScript('var module_bytes = ' + JSON.stringify(module_bytes));
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

Protocol.Debugger.enable();
Protocol.Debugger.onScriptParsed(handleScriptParsed);
InspectorTest.log(
    'Check that inspector gets two wasm scripts at module creation time.');
Protocol.Runtime
    .evaluate({
      'expression': '//# sourceURL=v8://test/runTestRunction\n' +
          'testFunction(module_bytes)'
    })
    .then(checkFinished);

var num_scripts = 0;
var missing_sources = 0;

function checkFinished() {
  if (missing_sources == 0)
    InspectorTest.completeTest();
}

function handleScriptParsed(messageObject)
{
  var url = messageObject.params.url;
  InspectorTest.log("Script #" + num_scripts + " parsed. URL: " + url);
  ++num_scripts;

  if (url.startsWith("wasm://")) {
    ++missing_sources;
    function dumpScriptSource(message) {
      InspectorTest.log("Source for " + url + ":");
      InspectorTest.log(message.result.scriptSource);
      --missing_sources;
    }

    Protocol.Debugger.getScriptSource({scriptId: messageObject.params.scriptId})
        .then(dumpScriptSource.bind(null))
        .then(checkFinished);
  }
}