Skip to content

Commit

Permalink
Successful type descriptor embed
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Mar 9, 2021
1 parent 79158d2 commit 2b4fc71
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
17 changes: 17 additions & 0 deletions read_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require("fs/promises");

async function main() {
const b = await fs.readFile(process.argv[2]);
const { instance } = await WebAssembly.instantiate(b);
const ptr = instance.exports.__asbind_type_data.value;
const dv = new DataView(instance.exports.memory.buffer);
const strLen = dv.getUint32(ptr - 4, true);
const strView = new Uint16Array(
instance.exports.memory.buffer,
ptr,
strLen / Uint16Array.BYTES_PER_ELEMENT
);
const str = new TextDecoder("utf-16le").decode(strView);
console.log({ str });
}
main();
70 changes: 70 additions & 0 deletions transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { Transform } = require("assemblyscript/cli/transform");
const assemblyscript = require("assemblyscript");

function isInternalElement(element) {
return element.internalName.startsWith("~");
}

function elementHasFlag(el, flag) {
return (el.flags & flag) != 0;
}

function typeName(type) {
return type.name.text ?? type.name.identifier.text;
}

const marker = "__asbind_type_data";

class AsBindTransform extends Transform {
afterInitialize(program) {
const exportedFunctions = [...program.elementsByDeclaration.values()]
.filter(el =>
elementHasFlag(el, assemblyscript.CommonFlags.MODULE_EXPORT)
)
.filter(el => !isInternalElement(el))
.filter(
el =>
el.declaration.kind === assemblyscript.NodeKind.FUNCTIONDECLARATION
);
const importedFunctions = [...program.elementsByDeclaration.values()]
.filter(el => elementHasFlag(el, assemblyscript.CommonFlags.DECLARE))
.filter(el => !isInternalElement(el))
.filter(
v => v.declaration.kind === assemblyscript.NodeKind.FUNCTIONDECLARATION
);
const typeData = {
importedFunction: Object.fromEntries(
importedFunctions.map(func => [
func.name,
{
returnType: typeName(func.declaration.signature.returnType),
parameters: func.declaration.signature.parameters.map(parameter =>
typeName(parameter.type)
)
}
])
),
exportedFunctions: Object.fromEntries(
exportedFunctions.map(func => [
func.name,
{
returnType: typeName(func.declaration.signature.returnType),
parameters: func.declaration.signature.parameters.map(parameter =>
typeName(parameter.type)
)
}
])
)
};
const typeDataExport = [...program.elementsByDeclaration.values()].find(
v => v.name === marker
);
if (!typeDataExport) {
throw Error("Could not find type data export");
}
typeDataExport.declaration.initializer = new assemblyscript.StringLiteralExpression(
JSON.stringify(typeData)
);
}
}
module.exports = AsBindTransform;

0 comments on commit 2b4fc71

Please sign in to comment.