Skip to content

Commit

Permalink
Accept non-Uint8Array buffer sources in parseImports
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Jun 13, 2024
1 parent db768f1 commit 1f6ca50
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
* const { instance } = await WebAssembly.instantiate(moduleBytes, importObject);
*/
export function parseImports(moduleBytes) {
if (moduleBytes instanceof Uint8Array) {
// no cast needed
} else if (moduleBytes instanceof ArrayBuffer) {
// cast ArrayBuffer to Uint8Array
moduleBytes = new Uint8Array(moduleBytes);
} else if (moduleBytes.buffer instanceof ArrayBuffer) {
// cast TypedArray or DataView to Uint8Array
moduleBytes = new Uint8Array(moduleBytes.buffer);
} else {
throw new Error("Argument must be a buffer source, like Uint8Array or ArrayBuffer");
}
const parseState = new ParseState(moduleBytes);
parseMagicNumber(parseState);
parseVersion(parseState);
Expand Down
55 changes: 41 additions & 14 deletions test/check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ function isStructurallyEqual(a, b) {
return true;
}

const minimalModuleBytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, // magic number
0x01, 0x00, 0x00, 0x00, // version
// import section with one import
0x02, // section code
0x06, // section length
0x01, // number of imports
0x00, // module name length
0x00, // field name length
0x02, // import kind: memory
0x00, // limits flags
0x01, // initial pages: 1
]);

async function check(wasmFilePath) {
const bytes = fs.readFileSync(wasmFilePath);
let module;
Expand Down Expand Up @@ -78,21 +92,29 @@ async function check(wasmFilePath) {
return true;
}

async function checkApiSurface() {
const bytes = minimalModuleBytes;
for (const source of [
bytes,
bytes.buffer,
new DataView(bytes.buffer),
]) {
try {
parseImports(source);
} catch (e) {
process.stdout.write("\x1b[31mF\x1b[0m\n");
console.error(`Failed to parse imports from ${source}`);
console.error(e);
return false;
}
process.stdout.write("\x1b[32m.\x1b[0m");
}
return true;
}

async function checkPolyfill() {
const bytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, // magic number
0x01, 0x00, 0x00, 0x00, // version
// import section with one import
0x02, // section code
0x06, // section length
0x01, // number of imports
0x00, // module name length
0x00, // field name length
0x02, // import kind: memory
0x00, // limits flags
0x01, // initial pages: 1
]);
const polyfilledWebAssembly = await polyfill(WebAssembly);
const bytes = minimalModuleBytes;
const polyfilledWebAssembly = polyfill(WebAssembly);
for (const getModule of [
{
name: "new WebAssembly.Module",
Expand Down Expand Up @@ -181,6 +203,11 @@ async function main() {

process.stdout.write("\n");

console.log("Checking API surface");
checkApiSurface();

process.stdout.write("\n");

console.log("Checking polyfill");

const ok = await checkPolyfill();
Expand Down

0 comments on commit 1f6ca50

Please sign in to comment.