mirror of
https://github.com/remko/waforth
synced 2024-12-27 09:59:29 +01:00
getc->read
This commit is contained in:
parent
92c97db7f9
commit
fd6a03cc8d
2 changed files with 19 additions and 14 deletions
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
;; I/O
|
;; I/O
|
||||||
(import "shell" "emit" (func $shell_emit (param i32)))
|
(import "shell" "emit" (func $shell_emit (param i32)))
|
||||||
(import "shell" "getc" (func $shell_getc (result i32)))
|
(import "shell" "read" (func $shell_read (param i32 i32) (result i32)))
|
||||||
(import "shell" "key" (func $shell_key (result i32)))
|
(import "shell" "key" (func $shell_key (result i32)))
|
||||||
|
|
||||||
;; Load a webassembly module.
|
;; Load a webassembly module.
|
||||||
|
@ -77,6 +77,7 @@
|
||||||
;;
|
;;
|
||||||
;; Memory layout:
|
;; Memory layout:
|
||||||
;; INPUT_BUFFER_BASE := 0x300
|
;; INPUT_BUFFER_BASE := 0x300
|
||||||
|
;; INPUT_BUFFER_SIZE := 0x700
|
||||||
;; (Compiled modules are limited to 4096 bytes until Chrome refuses to load them synchronously)
|
;; (Compiled modules are limited to 4096 bytes until Chrome refuses to load them synchronously)
|
||||||
;; MODULE_HEADER_BASE := 0x1000
|
;; MODULE_HEADER_BASE := 0x1000
|
||||||
;; RETURN_STACK_BASE := 0x2000
|
;; RETURN_STACK_BASE := 0x2000
|
||||||
|
@ -1730,13 +1731,10 @@
|
||||||
(then
|
(then
|
||||||
(call $push (i32.const -1))
|
(call $push (i32.const -1))
|
||||||
(return)))
|
(return)))
|
||||||
(block $endLoop (param i32) (result i32)
|
(global.set $inputBufferSize
|
||||||
(loop $loop (param i32) (result i32)
|
(call $shell_read
|
||||||
(br_if $endLoop (i32.eq (local.tee $char (call $shell_getc)) (i32.const -1)))
|
(i32.const 0x300 (; = INPUT_BUFFER_BASE ;))
|
||||||
(i32.store8 (i32.add (i32.const 0x300 (; = INPUT_BUFFER_BASE ;)) (global.get $inputBufferSize))
|
(i32.const 0x700 (; = INPUT_BUFFER_SIZE ;))))
|
||||||
(local.get $char))
|
|
||||||
(global.set $inputBufferSize (i32.add (global.get $inputBufferSize) (i32.const 1)))
|
|
||||||
(br $loop)))
|
|
||||||
(if (param i32) (result i32) (i32.eqz (global.get $inputBufferSize))
|
(if (param i32) (result i32) (i32.eqz (global.get $inputBufferSize))
|
||||||
(then (call $push (i32.const 0)))
|
(then (call $push (i32.const 0)))
|
||||||
(else
|
(else
|
||||||
|
|
|
@ -96,7 +96,7 @@ class WAForth {
|
||||||
async load() {
|
async load() {
|
||||||
let table: WebAssembly.Table;
|
let table: WebAssembly.Table;
|
||||||
let memory: WebAssembly.Memory;
|
let memory: WebAssembly.Memory;
|
||||||
const buffer = (this.#buffer = []);
|
this.#buffer = [];
|
||||||
|
|
||||||
const instance = await WebAssembly.instantiate(wasmModule, {
|
const instance = await WebAssembly.instantiate(wasmModule, {
|
||||||
shell: {
|
shell: {
|
||||||
|
@ -110,11 +110,18 @@ class WAForth {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getc: () => {
|
read: (addr: number, length: number): number => {
|
||||||
if (buffer.length === 0) {
|
let data = new Uint8Array(
|
||||||
return -1;
|
(this.core!.exports.memory as WebAssembly.Memory).buffer,
|
||||||
|
addr,
|
||||||
|
length
|
||||||
|
);
|
||||||
|
let n = 0;
|
||||||
|
while (this.#buffer!.length > 0 && n < length) {
|
||||||
|
data[n] = this.#buffer!.shift()!;
|
||||||
|
n += 1;
|
||||||
}
|
}
|
||||||
return buffer.pop();
|
return n;
|
||||||
},
|
},
|
||||||
|
|
||||||
key: () => {
|
key: () => {
|
||||||
|
@ -212,7 +219,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 = 0, len = data.length; i < len; ++i) {
|
||||||
this.#buffer!.push(data[i]);
|
this.#buffer!.push(data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue