[wasm-test] WasmModuleBuilder.addCustomSection in the JS builder API.

R=rossberg@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2650053005
Cr-Commit-Position: refs/heads/master@{#42655}
This commit is contained in:
titzer 2017-01-25 03:53:09 -08:00 committed by Commit bot
parent 0ec3a264bc
commit f51a5f732c
2 changed files with 20 additions and 13 deletions

View File

@ -292,19 +292,10 @@ assertErrorMessage(
let customSectionModuleBinary2 = (() => {
let builder = new WasmModuleBuilder();
builder.addExplicitSection([kUnknownSectionCode, 3, 1, 'x'.charCodeAt(0), 2]);
builder.addExplicitSection([
kUnknownSectionCode, 6, 3, 'f'.charCodeAt(0), 'o'.charCodeAt(0),
'o'.charCodeAt(0), 66, 77
]);
builder.addExplicitSection([
kUnknownSectionCode, 7, 3, 'f'.charCodeAt(0), 'o'.charCodeAt(0),
'o'.charCodeAt(0), 91, 92, 93
]);
builder.addExplicitSection([
kUnknownSectionCode, 7, 3, 'f'.charCodeAt(0), 'o'.charCodeAt(0),
'x'.charCodeAt(0), 99, 99, 99
]);
builder.addCustomSection('x', [2]);
builder.addCustomSection('foo', [66, 77]);
builder.addCustomSection('foo', [91, 92, 93]);
builder.addCustomSection('fox', [99, 99, 99]);
return new Int8Array(builder.toBuffer());
})();
var arr = moduleCustomSections(new Module(customSectionModuleBinary2), 'x');

View File

@ -160,6 +160,22 @@ class WasmModuleBuilder {
return this;
}
stringToBytes(name) {
var result = new Binary();
result.emit_u32v(name.length);
for (var i = 0; i < name.length; i++) {
result.emit_u8(name.charCodeAt(i));
}
return result;
}
addCustomSection(name, bytes) {
name = this.stringToBytes(name);
var length = new Binary();
length.emit_u32v(name.length + bytes.length);
this.explicit.push([0, ...length, ...name, ...bytes]);
}
addType(type) {
// TODO: canonicalize types?
this.types.push(type);