mirror of
https://github.com/remko/waforth
synced 2024-12-28 10:00:06 +01:00
Documentation
This commit is contained in:
parent
77de4c4bac
commit
d7b80a49f0
3 changed files with 17 additions and 10 deletions
|
@ -36,9 +36,9 @@ function loadString(memory: WebAssembly.Memory, addr: number, len: number) {
|
||||||
* */
|
* */
|
||||||
class WAForth {
|
class WAForth {
|
||||||
core?: WebAssembly.Instance;
|
core?: WebAssembly.Instance;
|
||||||
buffer?: number[];
|
#buffer?: number[];
|
||||||
fns: Record<string, (v: Stack) => void>;
|
#fns: Record<string, (v: Stack) => void>;
|
||||||
stack?: Stack;
|
#stack?: Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback that is called when a character needs to be emitted.
|
* Callback that is called when a character needs to be emitted.
|
||||||
|
@ -48,13 +48,18 @@ class WAForth {
|
||||||
onEmit?: (c: string) => void;
|
onEmit?: (c: string) => void;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.fns = {};
|
this.#fns = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize WAForth.
|
||||||
|
*
|
||||||
|
* Needs to be called before interpret().
|
||||||
|
*/
|
||||||
async load() {
|
async load() {
|
||||||
let table: WebAssembly.Table;
|
let table: WebAssembly.Table;
|
||||||
let memory: WebAssembly.Memory;
|
let memory: WebAssembly.Memory;
|
||||||
const buffer = (this.buffer = []);
|
const buffer = (this.#buffer = []);
|
||||||
|
|
||||||
const instance = await WebAssembly.instantiate(wasmModule, {
|
const instance = await WebAssembly.instantiate(wasmModule, {
|
||||||
shell: {
|
shell: {
|
||||||
|
@ -139,11 +144,11 @@ class WAForth {
|
||||||
const len = pop();
|
const len = pop();
|
||||||
const addr = pop();
|
const addr = pop();
|
||||||
const fname = loadString(memory, addr, len);
|
const fname = loadString(memory, addr, len);
|
||||||
const fn = this.fns[fname];
|
const fn = this.#fns[fname];
|
||||||
if (!fn) {
|
if (!fn) {
|
||||||
console.error("Unbound SCALL: %s", fname);
|
console.error("Unbound SCALL: %s", fname);
|
||||||
} else {
|
} else {
|
||||||
fn(this.stack!);
|
fn(this.#stack!);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -164,7 +169,7 @@ class WAForth {
|
||||||
(this.core!.exports.push as any)(n);
|
(this.core!.exports.push as any)(n);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.stack = {
|
this.#stack = {
|
||||||
pop,
|
pop,
|
||||||
popString,
|
popString,
|
||||||
push,
|
push,
|
||||||
|
@ -176,7 +181,7 @@ class WAForth {
|
||||||
read(s: string) {
|
read(s: string) {
|
||||||
const data = new TextEncoder().encode(s);
|
const data = new TextEncoder().encode(s);
|
||||||
for (let i = data.length - 1; i >= 0; --i) {
|
for (let i = data.length - 1; i >= 0; --i) {
|
||||||
this.buffer!.push(data[i]);
|
this.#buffer!.push(data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,7 +205,7 @@ class WAForth {
|
||||||
* Use `stack` to pop parameters off the stack, and push results back on 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: (stack: Stack) => void) {
|
||||||
this.fns[name] = fn;
|
this.#fns[name] = fn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
"target": "es2015",
|
||||||
"typeRoots": ["./src/web/types"],
|
"typeRoots": ["./src/web/types"],
|
||||||
"types": ["node"]
|
"types": ["node"]
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
"target": "es2015",
|
||||||
"typeRoots": ["./src/web/types"],
|
"typeRoots": ["./src/web/types"],
|
||||||
"types": ["node"],
|
"types": ["node"],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
|
Loading…
Reference in a new issue