wasm-inspector-test.js 4.12 KB
Newer Older
1 2 3 4
// Copyright 2020 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.

5 6
utils.load('test/mjsunit/wasm/wasm-module-builder.js');

7 8
WasmInspectorTest = {}

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
WasmInspectorTest.evalWithUrl = (code, url) =>
    Protocol.Runtime
        .evaluate({'expression': code + '\n//# sourceURL=v8://test/' + url})
        .then(printIfFailure);

WasmInspectorTest.instantiateFromBuffer = function(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;
  }
  const module = new WebAssembly.Module(buffer);
  return new WebAssembly.Instance(module);
}

WasmInspectorTest.instantiate = async function(bytes, instance_name = 'instance') {
  const instantiate_code = `var ${instance_name} = (${WasmInspectorTest.instantiateFromBuffer})(${JSON.stringify(bytes)});`;
  await WasmInspectorTest.evalWithUrl(instantiate_code, 'instantiate');
}

29 30 31 32 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
WasmInspectorTest.dumpScopeProperties = async function(message) {
  printIfFailure(message);
  for (var value of message.result.result) {
    var value_str = await getScopeValues(value.name, value.value);
    InspectorTest.log('   ' + value.name + ': ' + value_str);
  }
}

WasmInspectorTest.getWasmValue = function(wasmValue) {
  return typeof (wasmValue.value) === 'undefined' ?
      wasmValue.unserializableValue :
      wasmValue.value;
}

function printIfFailure(message) {
  if (!message.result) {
    InspectorTest.logMessage(message);
  }
  return message;
}

async function getScopeValues(name, value) {
  if (value.type == 'object') {
    if (value.subtype == 'typedarray') return value.description;
    if (name == 'instance') return dumpInstanceProperties(value);
    if (name == 'function tables') return dumpTables(value);

    let msg = await Protocol.Runtime.getProperties({objectId: value.objectId});
    printIfFailure(msg);
    const printProperty = function(elem) {
      const wasmValue = WasmInspectorTest.getWasmValue(elem.value);
      return `"${elem.name}": ${wasmValue} (${elem.value.subtype})`;
    }
    return msg.result.result.map(printProperty).join(', ');
  }
  return WasmInspectorTest.getWasmValue(value) + ' (' + value.subtype + ')';
}

function recursiveGetPropertiesWrapper(value, depth) {
  return recursiveGetProperties({result: {result: [value]}}, depth);
}

async function recursiveGetProperties(value, depth) {
  if (depth > 0) {
    const properties = await Promise.all(value.result.result.map(
        x => {return Protocol.Runtime.getProperties({objectId: x.value.objectId});}));
    const recursiveProperties = await Promise.all(properties.map(
        x => {return recursiveGetProperties(x, depth - 1);}));
    return recursiveProperties.flat();
  }
  return value;
}

async function dumpTables(tablesObj) {
  let msg = await Protocol.Runtime.getProperties({objectId: tablesObj.objectId});
  var tables_str = [];
  for (var table of msg.result.result) {
    const func_entries = await recursiveGetPropertiesWrapper(table, 2);
    var functions = [];
    for (var func of func_entries) {
      for (var value of func.result.result) {
        functions.push(`${value.name}: ${value.value.description}`);
      }
    }
    const functions_str = functions.join(', ');
    tables_str.push(`      ${table.name}: ${functions_str}`);
  }
  return '\n' + tables_str.join('\n');
}

async function dumpInstanceProperties(instanceObj) {
  function invokeGetter(property) {
    return this[JSON.parse(property)];
  }

  const exportsName = 'exports';
  let exportsObj = await Protocol.Runtime.callFunctionOn(
      {objectId: instanceObj.objectId,
      functionDeclaration: invokeGetter.toString(),
      arguments: [{value: JSON.stringify(exportsName)}]
    });
  printIfFailure(exportsObj);
  let exports = await Protocol.Runtime.getProperties(
      {objectId: exportsObj.result.result.objectId});
  printIfFailure(exports);

  const printExports = function(value) {
    return `"${value.name}" (${value.value.className})`;
  }
  const formattedExports = exports.result.result.map(printExports).join(', ');
  return `${exportsName}: ${formattedExports}`
}