Skip to content

Commit

Permalink
change bytecode to accept uint8array
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 12, 2024
1 parent 9acb88b commit 6ef8df8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
1 change: 0 additions & 1 deletion packages/object/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GSet, gset_create, gset_add, gset_merge } from "@topology-foundation/cr
class Chat {
// store messages as strings in the format (timestamp, message, peerId)
messages: GSet<string>;

constructor() {
this.messages = gset_create<string>();
}
Expand Down
11 changes: 8 additions & 3 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { compileWasm } from "./wasm/compiler.js";

export * from "./proto/object_pb.js";


export async function compileCRO(path: string): Promise<Uint8Array> {
return compileWasm(path);
}

/* Creates a new TopologyObject */
export function newTopologyObject(peerId: string, id?: string, abi?: string, bytecode?: string): TopologyObject {
export function newTopologyObject(peerId: string, id?: string, abi?: string, bytecode?: Uint8Array): TopologyObject {
return {
id: id ?? crypto
.createHash("sha256")
Expand All @@ -14,13 +19,13 @@ export function newTopologyObject(peerId: string, id?: string, abi?: string, byt
.update(Math.floor(Math.random() * Number.MAX_VALUE).toString())
.digest("hex"),
abi: abi ?? "",
bytecode: bytecode ?? ""
bytecode: bytecode ?? new Uint8Array(),
}
}

async function run() {
// TODO: just for testing wasm compilation with tsx, should be deleted
let bytecode = await compileWasm();
let bytecode = await compileCRO("/Users/droak/code/topology/ts-topology/packages/object/src/chat.ts");
let obj = newTopologyObject("peerId", undefined, undefined, bytecode);
console.log(obj);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/object/src/proto/object.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package topology.object;
message TopologyObject {
string id = 1;
string abi = 2;
string bytecode = 3;
bytes bytecode = 3;
}
43 changes: 34 additions & 9 deletions packages/object/src/proto/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions packages/object/src/wasm/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
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",
export async function compileWasm(path: string) {
const { error } = await asc.main([
path,
"--config=../../asconfig.json",
"--outFile=dist/tmp.wasm",
"--target=release"
]);

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

0 comments on commit 6ef8df8

Please sign in to comment.