From 76ffe5adf4f6497945684e700dec15c809897d77 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 12 Dec 2017 18:55:15 -0500 Subject: [PATCH] Use snapshots of the stack to restore stack to its previous state --- templates/function_definition.c | 7 ++----- templates/program.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/templates/function_definition.c b/templates/function_definition.c index d20eba6..9af7632 100644 --- a/templates/function_definition.c +++ b/templates/function_definition.c @@ -4,17 +4,14 @@ Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environmen environment = Environment_construct(environmentPool, environment); Stack* stack = Stack_construct(); + StackSnapshot stackSnapshot = Stack_takeSnapshot(stack); jmp_buf jump; if(setjmp(jump) != 0) { fprintf(stderr, "\tin {{name}}\n"); - while(Stack_any(stack)) - { - Object item = Stack_pop(stack); - Object_deinitialize(&item); - } + Stack_rewind(stack, stackSnapshot); Environment_setLive(environment, false); Stack_destruct(stack); diff --git a/templates/program.c b/templates/program.c index b25f7dc..25f9acb 100644 --- a/templates/program.c +++ b/templates/program.c @@ -333,6 +333,22 @@ void Object_deinitialize(Object* self) } } +typedef uint32_t StackSnapshot; + +StackSnapshot Stack_takeSnapshot(Stack* self) +{ + return (StackSnapshot) self->length; +} + +void Stack_rewind(Stack* self, StackSnapshot snapshot) +{ + while(self->length > snapshot) + { + Object item = Stack_pop(self); + Object_deinitialize(&item); + } +} + void Environment_deinitialize(Environment* self) { EnvironmentNode* next; -- 2.20.1