35 Companion zmachine.js (14/28)
// Hand over a line of input. Returns what run() returns.
sendLine(line) {
const w = this.waiting;
if (!w) throw new ZError('the game is not waiting for input');
if (w.kind === 'key') return this.sendKey(line.length ? line : '\n');
this.waiting = null;
const v = this.version;
const codes = [];
for (const ch of line.toLowerCase()) {
const z = this.charToZscii(ch);
if (z !== 13) codes.push(z);
}
const text = w.text;
if (v >= 5) {
const max = this.mem[text];
const already = Math.min(this.mem[text + 1], max);
const room = max - already;
const take = codes.slice(0, room);
take.forEach((z, i) => (this.mem[text + 2 + already + i] = z));
this.mem[text + 1] = already + take.length;
} else {
const max = this.mem[text] - (v <= 4 ? 1 : 0);
const take = codes.slice(0, Math.max(0, max));
take.forEach((z, i) => (this.mem[text + 1 + i] = z));
this.mem[text + 1 + take.length] = 0;
}
if (w.parse) this.tokenise(text, w.parse);
if (v >= 5) this.writeVar(w.store, 13);
return this.run();
}