2020-06-10 03:54:24 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-06-16 05:11:11 +00:00
|
|
|
utils.load('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
2020-06-10 03:54:24 +00:00
|
|
|
WasmInspectorTest = {}
|
2020-10-16 15:26:46 +00:00
|
|
|
InspectorTest.getWasmOpcodeName = getOpcodeName;
|
2020-06-10 03:54:24 +00:00
|
|
|
|
2021-04-09 08:58:46 +00:00
|
|
|
WasmInspectorTest.evalWithUrl = async function(expression, url) {
|
|
|
|
const sourceURL = `v8://test/${url}`;
|
|
|
|
const {result: {scriptId}} = await Protocol.Runtime.compileScript({
|
|
|
|
expression, sourceURL, persistScript: true}).then(printIfFailure);
|
2021-02-08 15:39:00 +00:00
|
|
|
return await Protocol.Runtime
|
2021-04-09 08:58:46 +00:00
|
|
|
.runScript({scriptId})
|
2021-02-08 15:39:00 +00:00
|
|
|
.then(printIfFailure);
|
|
|
|
};
|
2020-06-16 05:11:11 +00:00
|
|
|
|
2021-02-12 11:08:48 +00:00
|
|
|
WasmInspectorTest.compileFromBuffer = (function(bytes) {
|
2020-06-16 05:11:11 +00:00
|
|
|
var buffer = new ArrayBuffer(bytes.length);
|
|
|
|
var view = new Uint8Array(buffer);
|
|
|
|
for (var i = 0; i < bytes.length; ++i) {
|
|
|
|
view[i] = bytes[i] | 0;
|
|
|
|
}
|
2021-02-12 11:08:48 +00:00
|
|
|
return new WebAssembly.Module(buffer);
|
|
|
|
}).toString();
|
|
|
|
|
|
|
|
WasmInspectorTest.instantiateFromBuffer =
|
|
|
|
(function(bytes, imports) {
|
|
|
|
return new WebAssembly.Instance(compileFromBuffer(bytes), imports);
|
|
|
|
})
|
|
|
|
.toString()
|
|
|
|
.replace('compileFromBuffer', WasmInspectorTest.compileFromBuffer);
|
|
|
|
|
|
|
|
WasmInspectorTest.compile = async function(bytes, module_name = 'module') {
|
|
|
|
const compile_code = `var ${module_name} = (${
|
|
|
|
WasmInspectorTest.compileFromBuffer})(${JSON.stringify(bytes)});`;
|
|
|
|
await WasmInspectorTest.evalWithUrl(compile_code, 'compile_module');
|
2021-02-08 15:39:00 +00:00
|
|
|
};
|
2020-06-16 05:11:11 +00:00
|
|
|
|
2021-02-08 15:39:00 +00:00
|
|
|
WasmInspectorTest.instantiate =
|
2021-03-15 12:42:13 +00:00
|
|
|
async function(bytes, instance_name = 'instance', imports) {
|
2021-02-08 15:39:00 +00:00
|
|
|
const instantiate_code = `var ${instance_name} = (${
|
2021-03-15 12:42:13 +00:00
|
|
|
WasmInspectorTest.instantiateFromBuffer})(${JSON.stringify(bytes)},
|
|
|
|
${imports});`;
|
2020-06-16 05:11:11 +00:00
|
|
|
await WasmInspectorTest.evalWithUrl(instantiate_code, 'instantiate');
|
2021-02-08 15:39:00 +00:00
|
|
|
};
|
2020-06-16 05:11:11 +00:00
|
|
|
|
2020-06-10 03:54:24 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-02-08 15:39:00 +00:00
|
|
|
};
|
2020-06-10 03:54:24 +00:00
|
|
|
|
2021-02-08 15:39:00 +00:00
|
|
|
WasmInspectorTest.getWasmValue = async function(value) {
|
2021-03-04 17:37:44 +00:00
|
|
|
let msg = await Protocol.Runtime.getProperties({ objectId: value.objectId });
|
2021-02-08 15:39:00 +00:00
|
|
|
printIfFailure(msg);
|
|
|
|
const value_type = msg.result.result.find(({name}) => name === 'type');
|
|
|
|
const value_value = msg.result.result.find(({name}) => name === 'value');
|
|
|
|
return `${
|
|
|
|
value_value.value.unserializableValue ??
|
2021-03-04 17:37:44 +00:00
|
|
|
value_value.value.description ??
|
2021-02-08 15:39:00 +00:00
|
|
|
value_value.value.value} (${value_type.value.value})`;
|
|
|
|
};
|
2020-06-10 03:54:24 +00:00
|
|
|
|
|
|
|
function printIfFailure(message) {
|
|
|
|
if (!message.result) {
|
|
|
|
InspectorTest.logMessage(message);
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getScopeValues(name, value) {
|
2021-02-08 15:39:00 +00:00
|
|
|
async function printValue(value) {
|
|
|
|
if (value.type === 'object' && value.subtype === 'wasmvalue') {
|
|
|
|
return await WasmInspectorTest.getWasmValue(value);
|
|
|
|
} else if ('className' in value) {
|
|
|
|
return `(${value.className})`;
|
|
|
|
}
|
|
|
|
return `${value.unserializableValue ?? value.value} (${
|
|
|
|
value.subtype ?? value.type})`;
|
|
|
|
}
|
|
|
|
if (value.type === 'object' && value.subtype !== 'wasmvalue') {
|
|
|
|
if (value.subtype === 'typedarray' || value.subtype == 'webassemblymemory')
|
|
|
|
return value.description;
|
2020-12-29 11:59:03 +00:00
|
|
|
if (name === 'instance') return dumpInstanceProperties(value);
|
|
|
|
if (name === 'module') return value.description;
|
2021-05-20 08:30:48 +00:00
|
|
|
if (name === 'tables') return dumpTables(value);
|
2020-06-10 03:54:24 +00:00
|
|
|
|
|
|
|
let msg = await Protocol.Runtime.getProperties({objectId: value.objectId});
|
|
|
|
printIfFailure(msg);
|
2021-02-08 15:39:00 +00:00
|
|
|
async function printProperty({name, value}) {
|
|
|
|
return `"${name}": ${await printValue(value)}`;
|
2020-06-10 03:54:24 +00:00
|
|
|
}
|
2021-02-08 15:39:00 +00:00
|
|
|
return (await Promise.all(msg.result.result.map(printProperty))).join(', ');
|
2020-06-10 03:54:24 +00:00
|
|
|
}
|
2021-02-08 15:39:00 +00:00
|
|
|
return await printValue(value);
|
2020-06-10 03:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function recursiveGetPropertiesWrapper(value, depth) {
|
|
|
|
return recursiveGetProperties({result: {result: [value]}}, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function recursiveGetProperties(value, depth) {
|
|
|
|
if (depth > 0) {
|
2021-02-08 15:39:00 +00:00
|
|
|
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);
|
|
|
|
}));
|
2020-06-10 03:54:24 +00:00
|
|
|
return recursiveProperties.flat();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function dumpInstanceProperties(instanceObj) {
|
|
|
|
function invokeGetter(property) {
|
|
|
|
return this[JSON.parse(property)];
|
|
|
|
}
|
|
|
|
|
|
|
|
const exportsName = 'exports';
|
2021-02-08 15:39:00 +00:00
|
|
|
let exportsObj = await Protocol.Runtime.callFunctionOn({
|
|
|
|
objectId: instanceObj.objectId,
|
|
|
|
functionDeclaration: invokeGetter.toString(),
|
|
|
|
arguments: [{value: JSON.stringify(exportsName)}]
|
|
|
|
});
|
2020-06-10 03:54:24 +00:00
|
|
|
printIfFailure(exportsObj);
|
|
|
|
let exports = await Protocol.Runtime.getProperties(
|
|
|
|
{objectId: exportsObj.result.result.objectId});
|
|
|
|
printIfFailure(exports);
|
|
|
|
|
2021-02-08 15:39:00 +00:00
|
|
|
function printExports(value) {
|
2020-06-10 03:54:24 +00:00
|
|
|
return `"${value.name}" (${value.value.className})`;
|
|
|
|
}
|
|
|
|
const formattedExports = exports.result.result.map(printExports).join(', ');
|
|
|
|
return `${exportsName}: ${formattedExports}`
|
|
|
|
}
|
2021-05-20 08:30:48 +00:00
|
|
|
|
|
|
|
async function dumpTables(tablesObj) {
|
|
|
|
let msg =
|
|
|
|
await Protocol.Runtime.getProperties({objectId: tablesObj.objectId});
|
|
|
|
let tables_str = [];
|
|
|
|
for (let table of msg.result.result) {
|
|
|
|
let table_content =
|
|
|
|
await Protocol.Runtime.getProperties({objectId: table.value.objectId});
|
|
|
|
|
|
|
|
let entries_object = table_content.result.internalProperties.filter(
|
|
|
|
p => p.name === '[[Entries]]')[0];
|
|
|
|
entries = await Protocol.Runtime.getProperties(
|
|
|
|
{objectId: entries_object.value.objectId});
|
|
|
|
let functions = [];
|
|
|
|
for (let entry of entries.result.result) {
|
|
|
|
if (entry.name === 'length') continue;
|
|
|
|
functions.push(`${entry.name}: ${entry.value.description}`);
|
|
|
|
}
|
|
|
|
const functions_str = functions.join(', ');
|
|
|
|
tables_str.push(` ${table.name}: ${functions_str}`);
|
|
|
|
}
|
|
|
|
return '\n' + tables_str.join('\n');
|
|
|
|
}
|