Skip to content

Commit 720bd1c

Browse files
committed
Completely change how type IDs are generated and exposed
1 parent a1f7e52 commit 720bd1c

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

lib/asbind-instance/asbind-instance.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ async function compileStreaming(source) {
3939

4040
function extractTypeDescriptor(module) {
4141
const sections = WebAssembly.Module.customSections(module, SECTION_NAME);
42-
console.assert(
43-
sections.length === 1,
44-
`Need exactly one custom section ${JSON.stringify(SECTION_NAME)}`
45-
);
4642
const str = new TextDecoder("utf8").decode(new Uint8Array(sections[0]));
4743
try {
4844
return JSON.parse(str);
@@ -67,12 +63,10 @@ export default class AsbindInstance {
6763
);
6864
}
6965
if (
70-
!WebAssembly.Module.exports(this.module).find(
71-
exp => exp.name === "__asbind_String_ID"
72-
)
66+
WebAssembly.Module.customSections(this.module, SECTION_NAME).length !== 1
7367
) {
7468
throw new Error(
75-
"The AssemblyScript wasm module was not built with the as-bind entryfile. Please see the as-bind documentation (Quick Start), and rebuild your AssemblyScript wasm module."
69+
"The AssemblyScript wasm module was not built with the as-bind transform."
7670
);
7771
}
7872
}

transform.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ function containingModule(func) {
2323
return container;
2424
}
2525

26-
function functionTypeDescriptor(func) {
26+
function getFunctionTypeDescriptor(func) {
27+
// TODO: Generics?
28+
func = func.instances.get("");
2729
return {
2830
returnType: typeName(func.declaration.signature.returnType),
2931
parameters: func.declaration.signature.parameters.map(parameter =>
@@ -32,19 +34,29 @@ function functionTypeDescriptor(func) {
3234
};
3335
}
3436

35-
const AS_BIND_SRC = "lib/assembly/as-bind.ts";
37+
function extractTypeMap(func) {
38+
// TODO: Generics?
39+
func = func.instances.get("");
40+
const result = {
41+
[typeName(
42+
func.declaration.signature.returnType
43+
)]: func.signature.returnType?.getClass?.()?.id
44+
};
45+
func.declaration.signature.parameters.forEach((parameter, i) => {
46+
result[typeName(parameter.type)] = func.signature.parameterTypes[
47+
i
48+
].getClass?.()?.id;
49+
});
50+
return result;
51+
}
52+
3653
const SECTION_NAME = "as-bind_bindings";
3754

3855
class AsBindTransform extends Transform {
39-
afterParse(parser) {
40-
const bindSrc = fs.readFileSync(
41-
require.resolve("./" + AS_BIND_SRC),
42-
"utf8"
43-
);
44-
parser.parseFile(bindSrc, "~as-bind/" + AS_BIND_SRC, true);
45-
}
46-
afterInitialize(program) {
47-
const flatExportedFunctions = [...program.elementsByDeclaration.values()]
56+
afterCompile(module) {
57+
const flatExportedFunctions = [
58+
...this.program.elementsByDeclaration.values()
59+
]
4860
.filter(el =>
4961
elementHasFlag(el, assemblyscript.CommonFlags.MODULE_EXPORT)
5062
)
@@ -53,13 +65,18 @@ class AsBindTransform extends Transform {
5365
el =>
5466
el.declaration.kind === assemblyscript.NodeKind.FUNCTIONDECLARATION
5567
);
56-
const flatImportedFunctions = [...program.elementsByDeclaration.values()]
68+
const flatImportedFunctions = [
69+
...this.program.elementsByDeclaration.values()
70+
]
5771
.filter(el => elementHasFlag(el, assemblyscript.CommonFlags.DECLARE))
5872
.filter(el => !isInternalElement(el))
5973
.filter(
6074
v => v.declaration.kind === assemblyscript.NodeKind.FUNCTIONDECLARATION
6175
);
76+
77+
const typeIds = {};
6278
const importedFunctions = {};
79+
debugger;
6380
for (const importedFunction of flatImportedFunctions) {
6481
// To know under what module name an imported function will be expected,
6582
// we have to find the containing module of the given function, take the
@@ -74,17 +91,22 @@ class AsBindTransform extends Transform {
7491
}
7592
importedFunctions[moduleName][
7693
importedFunction.name
77-
] = functionTypeDescriptor(importedFunction);
94+
] = getFunctionTypeDescriptor(importedFunction);
95+
Object.assign(typeIds, extractTypeMap(importedFunction));
7896
}
7997
const exportedFunctions = {};
8098
for (const exportedFunction of flatExportedFunctions) {
81-
exportedFunctions[exportedFunction.name] = functionTypeDescriptor(
99+
exportedFunctions[exportedFunction.name] = getFunctionTypeDescriptor(
82100
exportedFunction
83101
);
102+
Object.assign(typeIds, extractTypeMap(exportedFunction));
84103
}
85-
this.typeData = JSON.stringify({ importedFunctions, exportedFunctions });
86-
}
87-
afterCompile(module) {
104+
this.typeData = JSON.stringify({
105+
typeIds,
106+
importedFunctions,
107+
exportedFunctions
108+
});
109+
88110
module.addCustomSection(
89111
SECTION_NAME,
90112
new TextEncoder("utf8").encode(this.typeData)

0 commit comments

Comments
 (0)