Depth check

This commit is contained in:
2022-09-07 19:00:24 +01:00
parent 9a55ff221a
commit 28b1b8e2cc
4 changed files with 17 additions and 22 deletions

View File

@@ -33,18 +33,18 @@ DONE: Assertion-based test scripts
DONE: Import/export keywords
DONE: A way to check the type of a variable (typeOf keyword)
DONE: slice and dot notation around the builtin _index and _dot functions
DONE: maximum recursion/function depth
TODO: nested compound assignment
TODO: better sugar for _push and _pop
TODO: ternary operator
TODO: Nullish types?
TODO: hooks on the external libraries, triggered on import
TODO: standard library
TODO: external script runner library
TODO: document how it all works - book?
TODO: maximum recursion/function depth
TODO: better API
TODO: better sugar for _push and _pop
TODO: nested compound assignment
TODO: packaging for release?
NOPE: a = b = c = 1;

View File

@@ -1,25 +1,10 @@
fn loop(count) {
print count++;
fn makeCounter() {
var total: int = 0;
fn counter(): int {
return ++total;
}
return counter;
loop(count);
}
var tally = makeCounter();
print tally(); //1
print tally(); //2
print tally(); //3
export tally;
//heck yeah!
import tally as tally2;
print tally2(); //4
loop(0);

View File

@@ -991,6 +991,13 @@ static void execInterpreter(Interpreter*);
static void readInterpreterSections(Interpreter* interpreter);
static bool execFnCall(Interpreter* interpreter) {
//BUGFIX: depth check - don't drown!
if (interpreter->depth >= 1000) {
interpreter->errorOutput("Depth check failed\n");
interpreter->panic = true;
return false;
}
LiteralArray arguments;
initLiteralArray(&arguments);
@@ -1057,6 +1064,7 @@ static bool execFnCall(Interpreter* interpreter) {
inner.length = func.as.function.length;
inner.count = 0;
inner.codeStart = -1;
inner.depth = interpreter->depth + 1;
inner.panic = false;
initLiteralArray(&inner.stack);
inner.exports = interpreter->exports;
@@ -2368,6 +2376,7 @@ void runInterpreter(Interpreter* interpreter, unsigned char* bytecode, int lengt
initLiteralArray(&interpreter->stack);
interpreter->depth = 0;
interpreter->panic = false;
//prep the bytecode

View File

@@ -27,6 +27,7 @@ typedef struct Interpreter {
PrintFn assertOutput;
PrintFn errorOutput;
int depth; //don't overflow
bool panic;
} Interpreter;