From d9074056e0776a595399efd73256b99b67cba551 Mon Sep 17 00:00:00 2001 From: loading <49134864+load1n9@users.noreply.github.com> Date: Sun, 27 Mar 2022 12:47:46 -0400 Subject: [PATCH] fix: examples with temp typescript system --- examples/add.ts | 36 +++++++++++++++++++++++++++++++----- examples/helloworld.ts | 41 +++++++++++++++++++++++++++++++---------- examples/utils.ts | 20 ++++++++++++++++++++ 3 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 examples/utils.ts diff --git a/examples/add.ts b/examples/add.ts index 708df0d..183e98c 100644 --- a/examples/add.ts +++ b/examples/add.ts @@ -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)); \ No newline at end of file +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); diff --git a/examples/helloworld.ts b/examples/helloworld.ts index 0df0285..e2bc4e3 100644 --- a/examples/helloworld.ts +++ b/examples/helloworld.ts @@ -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); \ No newline at end of file +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(); diff --git a/examples/utils.ts b/examples/utils.ts new file mode 100644 index 0000000..acf5e47 --- /dev/null +++ b/examples/utils.ts @@ -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!)); + } + } \ No newline at end of file