From 5773010948486038faa6799f841e284a26f46082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sat, 14 May 2022 08:51:25 +0200 Subject: [PATCH] web: Call async callbacks with WAForth object --- src/web/waforth.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/web/waforth.ts b/src/web/waforth.ts index b8d37f6..8dd80b4 100644 --- a/src/web/waforth.ts +++ b/src/web/waforth.ts @@ -47,7 +47,7 @@ function saveString(s: string, memory: WebAssembly.Memory, addr: number) { class WAForth { core?: WebAssembly.Instance; #buffer?: number[]; - #fns: Record void>; + #fns: Record void>; /** * Callback that is called when a character needs to be emitted. @@ -238,22 +238,22 @@ class WAForth { * When an SCALL is done with `name` on the top of the stack, `fn` will be called (with the name popped off the stack). * Use `stack` to pop parameters off the stack, and push results back on the stack. */ - bind(name: string, fn: (stack: Stack) => void) { + bind(name: string, fn: (f: WAForth) => void) { this.#fns[name] = fn; } /** - * Bind `name` to SCALL in Forth. + * Bind async `name` to SCALL in Forth. * * When an SCALL is done with `name` on the top of the stack, `fn` will be called (with the name popped off the stack). * Expects an execution token on the top of the stack, which will be called when the async callback is finished. * The execution parameter will be called with the success flag set. */ - bindAsync(name: string, fn: () => Promise) { + bindAsync(name: string, fn: (f: WAForth) => Promise) { this.#fns[name] = async () => { const cbxt = this.pop(); try { - await fn(); + await fn(this); this.push(-1); } catch (e) { console.error(e); @@ -266,10 +266,4 @@ class WAForth { } } -export interface Stack { - push(n: number): void; - pop(): number; - popString(): string; -} - export default WAForth;