Skip to content

Commit

Permalink
Error on EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Jan 3, 2024
1 parent bce3e22 commit 3f8eb7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,21 @@ function run_scratch_file(filename) {
}

vm.runtime.on('QUESTION', function (question) {
if (question !== null) {
if (question === 'read_token') {
vm.runtime.emit('ANSWER', Kattio.nextToken());
} else {
vm.runtime.emit('ANSWER', Kattio.nextLine());
try {
if (question !== null) {
if (question === 'read_token') {
vm.runtime.emit('ANSWER', Kattio.nextToken());
} else {
vm.runtime.emit('ANSWER', Kattio.nextLine());
}
}
} catch (e) {
writeStderrSync(
'scratch-vm encountered an error: Could not read input: ' +
e.message +
'\n'
);
process.exit(1);
}
});

Expand Down
9 changes: 7 additions & 2 deletions src/kattio.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const Kattio = {

_readUntil: function (stop) {
this._ensure();
if (this._bufPos === this._bufLen) throw new Error('eof');
if (this._bufPos === this._bufLen) {
throw new Error('End of file reached');
}

var start = this._bufPos;
var before = null;
for (;;) {
Expand All @@ -51,7 +54,9 @@ const Kattio = {
if (this._bufPos === this._bufLen || stop(this._buf[this._bufPos])) break;
this._bufPos++;
}
if (!before) return this._buf.toString('utf8', start, this._bufPos);
if (!before) {
return this._buf.toString('utf8', start, this._bufPos);
}
var after = this._buf.slice(start, this._bufPos);
var res = Buffer.alloc(before.length + after.length);
before.copy(res);
Expand Down

0 comments on commit 3f8eb7c

Please sign in to comment.