diff --git a/src/waforth.wat b/src/waforth.wat index 39b1760..57e1f8d 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -2767,7 +2767,7 @@ (func (export "tos") (result i32) (global.get $tos)) - (func (export "here" (result i32)) + (func (export "here") (result i32) (global.get $here)) (func (export "interpret") (result i32) diff --git a/src/web/examples/fetch/fetch.ts b/src/web/examples/fetch/fetch.ts index 1de4ac6..ea2bae1 100644 --- a/src/web/examples/fetch/fetch.ts +++ b/src/web/examples/fetch/fetch.ts @@ -13,34 +13,34 @@ import WAForth from "waforth"; await forth.load(); // Bind "age" call to a function that fetches the age of the given person, and calls the continuation callback - forth.bind("age", async (stack) => { - const name = stack.popString(); - const result = await ( - await fetch("https://api.agify.io/?name=" + encodeURIComponent(name)) - ).json(); - - // After this point, use the `forth` object directly, since we're no longer in the callback. - forth.push(parseInt(result.age)); - forth.interpret("AGE-CB"); + forth.bind("?ip", async () => { + const cbxt = forth.pop(); + try { + const result = await ( + await fetch("https://api.ipify.org?format=json") + ).json(); + forth.pushString(result.ip); + forth.push(cbxt); + forth.interpret("EXECUTE"); + } catch (e) { + console.error(e); + } }); // Load Forth code to bind the "age" call, and define the continuation callback forth.interpret(` -: AGE ( c-addr u -- ) - S" age" SCALL +: ?IP-CB ( c-addr n -- ) + ." Your IP address is " TYPE CR ; -: AGE-CB ( d -- ) - ." Your age is " . -; - -: GUESS-AGE ( -- ) - S" Remko" AGE +: ?IP ( -- ) + ['] ?IP-CB + S" ?ip" SCALL ; `); // Ask for a number (via Forth) when the user clicks the button btn.addEventListener("click", () => { - forth.interpret("GUESS-AGE"); + forth.interpret("?IP"); }); })(); diff --git a/src/web/examples/prompt/prompt.ts b/src/web/examples/prompt/prompt.ts index 7f88f4b..aeb0407 100644 --- a/src/web/examples/prompt/prompt.ts +++ b/src/web/examples/prompt/prompt.ts @@ -34,7 +34,7 @@ import WAForth from "waforth"; ( Prompt the user for a number, and write it to output ) : ASK-NUMBER ( -- ) S" Please enter a number" PROMPT - ." The number was" SPACE . + ." The number was " . CR ; `);