Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: examples with temp typescript system #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions examples/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import { Whistle, load } from "../mod.ts";
import { compile, lex, load, parse } from "../mod.ts";
import { System } from "./utils.ts";

const bits = await new Whistle("export fn add(a: i32, b: i32): i32 { return a + b }").compile();
const code = `
export fn add(a: i32, b: i32): i32 {
return a + b
}
`;
// Load the wasm module
await load();
// Lex the code
const tokens = lex(code);
console.log(tokens);
// Parse the code
const ast = parse(code);
console.log(ast);
// Compile the code
const wasm = compile(code);
const module = await WebAssembly.compile(wasm);

const { add } = await load(bits);

console.log(add(40, 2));
const imports = {
sys: {
printInt: System.printInt,
printString: System.printString,
},
};
// Instantiate the module
const instance = await WebAssembly.instantiate(module, imports);
// Get the exported function
const { add } = instance.exports as { add: (a: number, b: number) => number };
// Call the exported function
const result = add(40, 2);
console.log(result);
41 changes: 31 additions & 10 deletions examples/helloworld.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { Whistle, load } from "../mod.ts";

const bits = await new Whistle(`
// optional main function runs by default
export fn main(): i32 {
// prints Hello World to the console
printString("Hello World!")
import { compile, lex, load, parse } from "../mod.ts";
import { System } from "./utils.ts";
const code = `
export fn helloWorld(): i32 {
printString("Hello, world!")
return 0
}
`).compile();
}`;

// Load the wasm module
await load();
// Lex the code
const tokens = lex(code);
console.log(tokens);
// Parse the code
const ast = parse(code);
console.log(ast);
// Compile the code
const wasm = compile(code);
const module = await WebAssembly.compile(wasm);

await load(bits);
const imports = {
sys: {
printInt: System.printInt,
printString: System.printString,
},
};
// Instantiate the module
const instance = await WebAssembly.instantiate(module, imports);
System.memory = (instance.exports.memory as WebAssembly.Memory) || undefined;
// Get the exported function
const { helloWorld } = instance.exports as { helloWorld: () => number };
// Call the exported function which prints "Hello, world!"
helloWorld();
20 changes: 20 additions & 0 deletions examples/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const readString = (
ptr: number,
memory?: WebAssembly.Memory,
): string | undefined => {
if (memory === undefined) return;
const view = new Uint8Array(memory.buffer);
let end = ptr;
while (view[end]) ++end;
return (new TextDecoder()).decode(new Uint8Array(view.subarray(ptr, end)));
};

export class System {
static memory?: WebAssembly.Memory;
static printInt(arg: number) {
console.log(arg);
}
static printString(arg: number) {
console.log(readString(arg, System.memory!));
}
}