Skip to content

Commit 2ec1186

Browse files
committed
Use custom section
1 parent c04886d commit 2ec1186

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

lib/assembly/as-bind.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,3 @@ export const __asbind_I32ArrayArray_ID = idof<Array<Array<i32>>>();
3232
export const __asbind_I64ArrayArray_ID = idof<Array<Array<i64>>>();
3333
export const __asbind_StringArrayArray_ID = idof<Array<Array<string>>>();
3434
export const __asbind_BoolArrayArray_ID = idof<Array<Array<bool>>>();
35-
36-
// As-Bind type data
37-
export const __asbind_type_data: string = "";

read_type_data.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,9 @@ function justSatisfyAllImportsLol() {
2020

2121
async function main() {
2222
const b = await fs.readFile(process.argv[2]);
23-
const { instance } = await WebAssembly.instantiate(
24-
b,
25-
justSatisfyAllImportsLol()
26-
);
27-
const ptr = instance.exports.__asbind_type_data.value;
28-
const dv = new DataView(instance.exports.memory.buffer);
29-
const strLen = dv.getUint32(ptr - 4, true);
30-
const strView = new Uint16Array(
31-
instance.exports.memory.buffer,
32-
ptr,
33-
strLen / Uint16Array.BYTES_PER_ELEMENT
34-
);
35-
const str = new TextDecoder("utf-16le").decode(strView);
23+
const module = await WebAssembly.compile(b);
24+
const buffer = WebAssembly.Module.customSections(module, "bindings")[0];
25+
const str = new TextDecoder("utf8").decode(new Uint8Array(buffer));
3626
console.log({ str });
3727
const data = JSON.parse(str);
3828
console.log(JSON.stringify(data, null, 2));

transform.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,12 @@ class AsBindTransform extends Transform {
6565
])
6666
)
6767
};
68-
const typeDataExport = [...program.elementsByDeclaration.values()].find(
69-
v => v.name === MARKER
70-
);
71-
if (!typeDataExport) {
72-
throw Error("Could not find type data export");
73-
}
74-
typeDataExport.declaration.initializer = new assemblyscript.StringLiteralExpression(
75-
JSON.stringify(typeData)
68+
this.typeData = JSON.stringify(typeData);
69+
}
70+
afterCompile(module) {
71+
module.addCustomSection(
72+
"bindings",
73+
new TextEncoder("utf8").encode(this.typeData)
7674
);
7775
}
7876
}

0 commit comments

Comments
 (0)