builder.defineImportedFunction("i", trampImports[i][0], trampImports[i][1], true, trampImports[i][2]);
}
+ // Assign import indices so they get emitted in the import section
+ for (let i = 0; i < trampImports.length; i++)
+ builder.markImportAsUsed(trampImports[i][0]);
+
builder._generateImportSection();
// Function section
// Emit function imports
for (let i = 0; i < trampImports.length; i++)
- builder.defineImportedFunction("i", trampImports[i][0], trampImports[i][1], true, trampImports[i][2]);
+ builder.defineImportedFunction("i", trampImports[i][0], trampImports[i][1], false, trampImports[i][2]);
+
+ // Assign import indices so they get emitted in the import section
+ for (let i = 0; i < trampImports.length; i++)
+ builder.markImportAsUsed(trampImports[i][0]);
+
builder._generateImportSection();
// Function section
// console.log(`referenced ${importsToEmit.length} import(s)`);
for (let i = 0; i < importsToEmit.length; i++) {
const ifi = importsToEmit[i];
- // console.log(` #${ifi.index} ${ifi.module}.${ifi.name} = ${ifi.func}`);
+ // console.log(` #${ifi.index} ${ifi.module}.${ifi.name} = ${ifi.func}. typeIndex=${ifi.typeIndex}`);
this.appendName(ifi.module);
this.appendName(this.getCompressedName(ifi));
this.appendU8(0x0); // function
return result;
}
+ markImportAsUsed(name: string) {
+ const func = this.importedFunctions[name];
+ if (!func)
+ throw new Error("No imported function named " + name);
+ if (typeof (func.index) !== "number")
+ func.index = this.importedFunctionCount++;
+ }
+
defineFunction(
options: {
type: string,
const func = this.importedFunctions[name];
if (!func)
throw new Error("No imported function named " + name);
- if (typeof (func.index) !== "number")
+ if (typeof (func.index) !== "number") {
+ if (this.lockImports)
+ throw new Error("Import section was emitted before assigning an index to import named " + name);
func.index = this.importedFunctionCount++;
+ }
this.appendU8(WasmOpcode.call);
this.appendULeb(func.index);
}