1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-26 15:16:51 +00:00

Invert recursion guard to count down instead of up.

This commit is contained in:
bakpakin 2017-07-09 13:31:30 -04:00
parent b19c834cf4
commit 891c550980
2 changed files with 8 additions and 8 deletions

View File

@ -1263,7 +1263,7 @@ static Slot compile_form(GstCompiler *c, FormOptions opts, const GstValue *form)
static Slot compile_value(GstCompiler *c, FormOptions opts, GstValue x) { static Slot compile_value(GstCompiler *c, FormOptions opts, GstValue x) {
Slot ret; Slot ret;
/* Check if recursion is too deep */ /* Check if recursion is too deep */
if (c->recursionGuard++ > GST_RECURSION_GUARD) { if (--c->recursionGuard == 0) {
c_error(c, "recursed too deeply"); c_error(c, "recursed too deeply");
} }
switch (x.type) { switch (x.type) {
@ -1289,7 +1289,7 @@ static Slot compile_value(GstCompiler *c, FormOptions opts, GstValue x) {
ret = compile_literal(c, opts, x); ret = compile_literal(c, opts, x);
break; break;
} }
c->recursionGuard--; c->recursionGuard++;
return ret; return ret;
} }
@ -1300,7 +1300,7 @@ void gst_compiler(GstCompiler *c, Gst *vm) {
c->tail = NULL; c->tail = NULL;
c->error.type = GST_NIL; c->error.type = GST_NIL;
c->env = vm->env; c->env = vm->env;
c->recursionGuard = 0; c->recursionGuard = GST_RECURSION_GUARD;
compiler_push_scope(c, 0); compiler_push_scope(c, 0);
} }
@ -1308,7 +1308,7 @@ void gst_compiler(GstCompiler *c, Gst *vm) {
* given AST. Returns NULL if there was an error during compilation. */ * given AST. Returns NULL if there was an error during compilation. */
GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form) { GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form) {
FormOptions opts = form_options_default(); FormOptions opts = form_options_default();
c->recursionGuard = 0; c->recursionGuard = GST_RECURSION_GUARD;
GstFuncDef *def; GstFuncDef *def;
if (setjmp(c->onError)) { if (setjmp(c->onError)) {
/* Clear all but root scope */ /* Clear all but root scope */

View File

@ -131,7 +131,7 @@ static const char *gst_deserialize_impl(
#define read_i64(out) do{deser_datacheck(8); (out)=bytes2int(data); data += 8; }while(0) #define read_i64(out) do{deser_datacheck(8); (out)=bytes2int(data); data += 8; }while(0)
/* Check if we have recursed too deeply */ /* Check if we have recursed too deeply */
if (depth++ > GST_RECURSION_GUARD) { if (--depth == 0) {
return "deserialize recursed too deeply"; return "deserialize recursed too deeply";
} }
@ -467,7 +467,7 @@ const char *gst_deserialize(
GstValue ret; GstValue ret;
const char *err; const char *err;
GstArray *visited = gst_array(vm, 10); GstArray *visited = gst_array(vm, 10);
err = gst_deserialize_impl(vm, data, data + len, nextData, visited, &ret, 0); err = gst_deserialize_impl(vm, data, data + len, nextData, visited, &ret, GST_RECURSION_GUARD);
if (err != NULL) return err; if (err != NULL) return err;
*out = ret; *out = ret;
return NULL; return NULL;
@ -501,7 +501,7 @@ static const char *gst_serialize_impl(
/*printf("Type: %d\n", x.type);*/ /*printf("Type: %d\n", x.type);*/
/* Check if we have gone too deep */ /* Check if we have gone too deep */
if (depth++ > GST_RECURSION_GUARD) { if (--depth == 0) {
return "serialize recursed too deeply"; return "serialize recursed too deeply";
} }
@ -759,7 +759,7 @@ const char *gst_serialize(Gst *vm, GstBuffer *buffer, GstValue x) {
uint32_t oldCount = buffer->count; uint32_t oldCount = buffer->count;
const char *err; const char *err;
GstTable *visited = gst_table(vm, 10); GstTable *visited = gst_table(vm, 10);
err = gst_serialize_impl(vm, buffer, visited, &nextId, x, 0); err = gst_serialize_impl(vm, buffer, visited, &nextId, x, GST_RECURSION_GUARD);
if (err != NULL) { if (err != NULL) {
buffer->count = oldCount; buffer->count = oldCount;
} }