diff options
Diffstat (limited to 'src/js/tinyvm.ts')
| -rw-r--r-- | src/js/tinyvm.ts | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/js/tinyvm.ts b/src/js/tinyvm.ts index ab1e4da..b6146fb 100644 --- a/src/js/tinyvm.ts +++ b/src/js/tinyvm.ts @@ -171,6 +171,9 @@ export class TinyVM { private inputBuffer: Uint8Array = new Uint8Array(0); private inputPos: number = 0; + /** true while a streaming run is suspended by a poll (syscall 42) rather than by a read: the host + should resume it after its event loop has run, whether or not input came. */ + public polling: boolean = false; private outputBuffer: number[] = []; private onOutput?: (byte: number) => void; private onMessage?: (msg: string) => void; @@ -429,6 +432,14 @@ export class TinyVM { } return -1n; } + case 42: // poll: input is waiting + return this.inputPos < this.inputBuffer.length ? 1n : 0n; + case 41: { // shortest float text into a word buffer + const text = formatFloat(a, 0n, 17n); + if (b < 0n || c <= BigInt(text.length) || b > BigInt(this.mw) || c > BigInt(this.mw) - b) return -1n; + for (let i = 0; i < text.length; i++) this.mem[Number(b) + i] = BigInt(text.charCodeAt(i)); + this.mem[Number(b) + text.length] = 0n; return BigInt(text.length); + } case 18: { // fill heap words without running one vm store per word if (a < BigInt(this.L.HEAP_LO) || c <= 0n || a > BigInt(this.mw) || c > BigInt(this.mw) - a) return -1n; this.mem.fill(b, Number(a), Number(a + c)); return c; @@ -721,6 +732,14 @@ export class TinyVM { break; case 2: // st if (Number(a) === this.L.SYSGATE) { + if (b === 42n && this.streaming) { + // poll: suspend once so the host can deliver what has arrived, then say if input waits + if (!this.polling) { this.polling = true; this.rp = p - 1; this.rs = s; this.rr = r; this.blocked = true; return BLOCKED; } + this.polling = false; + mem[Number(a)] = this.inputPos < this.inputBuffer.length ? 1n : 0n; + s += 2; + break; + } this.rs = s + 2; this.rr = r; const result = this.syscall(b, mem[Number(a) + 1], mem[Number(a) + 2], mem[Number(a) + 3]); mem = this.mem; bytes = this.bytes; view = this.view; mw = this.mw; // a grow may have moved them @@ -898,6 +917,20 @@ export class TinyVM { // command line runner: node src/js/tinyvm.js <file.zc>, or the source on stdin if (typeof require === 'function' && typeof module !== 'undefined' && require.main === module) { + const option = process.argv[2]; + if (option === '--help' || option === '-h') { + process.stdout.write('usage: node src/js/tinyvm.js [file.zc]\n' + + 'compiles and runs the file, or reads source from stdin when no file is supplied.\n' + + 'input after a backtick is program input.\n' + + 'options:\n --help, -h show this help and exit\n' + + 'environment:\n TINYVM_MEMORY_WORDS initial vm memory in words\n' + + ' TINYVM_MAX_MEMORY_WORDS maximum vm memory in words\n'); + process.exit(0); + } + if (option && option.startsWith('-')) { + process.stderr.write('tinyvm.js: unknown option ' + option + '; use --help for usage\n'); + process.exit(2); + } const genomePath = path.join(__dirname, 'genome.json'); if (!fs.existsSync(genomePath)) { console.error('Error: genome.json not found. Run extract_genome.js first.'); |
