Use snapshots of the stack to restore stack to its previous state
[fur] / templates / function_definition.c
index c483bda..9af7632 100644 (file)
@@ -3,22 +3,19 @@ Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environmen
 {
   environment = Environment_construct(environmentPool, environment);
 
-  Stack stackMemory;
-  Stack* stack = &stackMemory;
-  Stack_initialize(stack);
+  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);
+
     longjmp(parentJump, 1);
   }
 
@@ -34,5 +31,7 @@ Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environmen
 
   // TODO Set the environment back to the parent environment
   Environment_setLive(environment, false);
+
+  Stack_destruct(stack);
   return result;
 }