Skip to content

Commit c8b8f7c

Browse files
committed
[Heavy] Add cleanup for Environment on error
1 parent ec02af6 commit c8b8f7c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

heavy/lib/Context.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,13 +1333,21 @@ class LibraryEnv {
13331333

13341334
// Since we don't own PrevEnv do not capture it in scheme land.
13351335
Value After = Context.CreateLambda(
1336-
[PrevOpGen, PrevEnv](heavy::Context& C, ValueRefs) {
1336+
[PrevOpGen, PrevEnv](heavy::Context& C, ValueRefs Args) {
13371337
C.setEnvironment(PrevEnv);
13381338
C.OpGen = PrevOpGen;
1339+
if (Args.size() == 1 && isa<Error>(Args[0]))
1340+
return C.Cont(Args[0]);
13391341
C.Cont();
13401342
}, {});
13411343

1342-
Context.DynamicWind(std::move(Ptr), Before, Thunk, After);
1344+
Value SafeThunk = Context.CreateLambda([](heavy::Context& C, ValueRefs) {
1345+
heavy::Value Thunk = C.getCapture(0);
1346+
heavy::Value After = C.getCapture(1);
1347+
// Clean up with the After-thunk.
1348+
C.WithExceptionHandler(After, Thunk);
1349+
}, CaptureList{Thunk, After});
1350+
Context.DynamicWind(std::move(Ptr), Before, SafeThunk, After);
13431351
}
13441352
};
13451353
} // namespace

0 commit comments

Comments
 (0)