diff --git a/include/ink.h b/include/ink.h index 665a28a..ede8912 100644 --- a/include/ink.h +++ b/include/ink.h @@ -338,6 +338,12 @@ struct elem ink_make_transparent(struct context* ctx, int type_id, void* ptr); */ void ink_gc(struct context* ctx); +/** + * Cleans up coroutine stacks before reuse + * @param ctx + */ +void ink_clean_routines(struct context* ctx); + /** * Obtains the type id from the declared name of the type * @param ctx The context where we want to detect the type diff --git a/lib.c b/lib.c index e433c2b..7008b69 100644 --- a/lib.c +++ b/lib.c @@ -1153,6 +1153,22 @@ struct elem ink_make_native(struct context* ctx, int type, void* ptr) { return ink_make_native_unsafe(ctx, type, ptr, 0); } +void ink_clean_routines(struct context* ctx) { + int i, j; + struct elem null; + + null.value = 0; + null.type = INK_INTEGER; + for(i = 0; i < ctx->routines_top; ++i) { + if(ctx->routines[i].panic == INK_ROUTINE_CAN_REUSE) { + for (j = 0; j < ctx->routines[i].top; ++j) { + ctx->routines[i].stack[j] = null; + } + ctx->routines[i].top = 0; + } + } +} + void ink_gc(struct context* ctx) { int i, j, k; int marked;