1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-11 12:29:29 +00:00
8bitworkshop/src/common/basic/fuzz.ts

45 lines
1.4 KiB
TypeScript

import { BASICParser, DIALECTS, BASICOptions, CompileError } from "./compiler";
import { BASICRuntime } from "./runtime";
import { EmuHalt } from "../emu";
process.on('unhandledRejection', (reason, promise) => {
if (!(reason instanceof EmuHalt))
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
// Application specific logging, throwing an error, or other logic here
});
export function fuzz(buf) {
var parser = new BASICParser();
var str = buf.toString();
try {
var pgm = parser.parseFile(str, "test.bas");
var runtime = new BASICRuntime();
runtime.load(pgm);
runtime.reset();
runtime.print = (s) => {
if (s == null) throw new Error("PRINT null string");
}
runtime.input = function(prompt: string, nargs: number) : Promise<string[]> {
var p = new Promise<string[]>( (resolve, reject) => {
var arr = [];
for (var i=0; i<Math.random()*10; i++)
arr.push(i+"");
resolve(arr);
});
return p;
}
for (var i=0; i<50000; i++) {
if (!runtime.step()) break;
}
if (Math.random() < 0.001) runtime.load(pgm);
for (var i=0; i<50000; i++) {
if (!runtime.step()) break;
}
} catch (e) {
if (e instanceof EmuHalt) return;
if (e instanceof CompileError) return;
throw e;
}
}