From b7671eba40fb2c28eff40ce7758d9ddc841eae65 Mon Sep 17 00:00:00 2001 From: Ajani Bilby <11359344+AjaniBilby@users.noreply.github.com> Date: Sat, 23 Mar 2024 12:35:47 +1100 Subject: [PATCH] ~fixed bad import changes --- source/wasm/module.ts | 1 + source/wasm/section/import.ts | 12 +++++------- tests/e2e/wasm/hello-world.test.ts | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/source/wasm/module.ts b/source/wasm/module.ts index a921c4f..4cd165f 100644 --- a/source/wasm/module.ts +++ b/source/wasm/module.ts @@ -38,6 +38,7 @@ export default class Module { importFunction(mod: string, name: string, typeIdx: number) { const idx = this.importSect.registerFunction(mod, name, typeIdx); + if (idx === null) return null; const ref = new FuncRef(true); ref.resolve(idx); diff --git a/source/wasm/section/import.ts b/source/wasm/section/import.ts index 8f0b121..787abf0 100644 --- a/source/wasm/section/import.ts +++ b/source/wasm/section/import.ts @@ -24,17 +24,17 @@ class Register { } } -interface InnerObject { +interface ModuleMap { [key: string]: Register; } -interface OuterObject { - [key: string]: InnerObject; +interface NamespaceMap { + [key: string]: ModuleMap; } export default class ImportSection { - _entries: OuterObject; + _entries: NamespaceMap; _funcs: number; constructor() { @@ -50,9 +50,7 @@ export default class ImportSection { if (!mod[name]) { mod[name] = new Register(module, name, typeIdx, this._funcs++); - } else if (mod[name].type !== typeIdx) { - throw new Error(`Attempting to register import "${module}" "${name}" with new type`); - } + } else if (mod[name].type !== typeIdx) return null; return mod[name].idx; } diff --git a/tests/e2e/wasm/hello-world.test.ts b/tests/e2e/wasm/hello-world.test.ts index c15e448..8c5438b 100644 --- a/tests/e2e/wasm/hello-world.test.ts +++ b/tests/e2e/wasm/hello-world.test.ts @@ -12,10 +12,10 @@ Deno.test(`Wasm module test: should print "${goalText}"`, async () => { const mem = mod.addMemory(1); mod.exportMemory("memory", mem); - const type0 = mod.makeType([Type.Intrinsic.i32], [Type.Intrinsic.i32]); const type1 = mod.makeType([Type.Intrinsic.i32, Type.Intrinsic.i32, Type.Intrinsic.i32, Type.Intrinsic.i32], [Type.Intrinsic.i32]); const fd_write = mod.importFunction("wasi_snapshot_preview1", "fd_write", type1); + if (fd_write === null) fail(`Unable to import fd_write`); mod.setData(0, goalText); // The WASI iovec struct, which consists of a pointer to