Skip to content

Commit

Permalink
compiling bytecode to wasm (need to test in browser because of fs)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 12, 2024
1 parent d114a79 commit 9acb88b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 1 addition & 4 deletions packages/object/asconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
"debug": true
},
"release": {
"outFile": "dist/asc/release.wasm",
"textFile": "dist/asc/release.wat",
"sourceMap": true,
"sourceMap": false,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {
"bindings": "esm",
"lib": {
"@topology-foundation/crdt": ["../crdt/src/index.asc.ts"]
}
Expand Down
10 changes: 7 additions & 3 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function newTopologyObject(peerId: string, id?: string, abi?: string, byt
}
}

// just for testing wasm compilation with tsx, should be deleted
let obj = newTopologyObject("peerId");
console.log(obj.id);
async function run() {
// TODO: just for testing wasm compilation with tsx, should be deleted
let bytecode = await compileWasm();
let obj = newTopologyObject("peerId", undefined, undefined, bytecode);
console.log(obj);
}
run();
7 changes: 6 additions & 1 deletion packages/object/src/wasm/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
- Bytecode
- ABI
*/
import * as fs from "fs";
import asc from "assemblyscript/asc";

export async function compileWasm() {
const { error, stdout, stderr, stats } = await asc.main([
// Command line options
"/Users/droak/code/topology/ts-topology/packages/object/src/chat.ts",
"--config=/Users/droak/code/topology/ts-topology/packages/object/asconfig.json",
"--outFile=dist/tmp.wasm",
"--target=release"
]);

if (error) {
console.log("Compilation failed: " + error.message);
console.log(stderr.toString());
} else {
console.log(stdout.toString());
// read tmp file and delete it
const bytecode = fs.readFileSync('dist/tmp.wasm');
fs.unlinkSync('dist/tmp.wasm');
return bytecode;
}
}

0 comments on commit 9acb88b

Please sign in to comment.