Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jianlingzhong committed Nov 5, 2024
1 parent 20284b2 commit bd502b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
3 changes: 2 additions & 1 deletion compiler+runtime/include/cpp/jank/runtime/obj/delay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace jank::runtime
static object_ptr force(object_ptr const &d);

object base{ object_type::delay };
object_ptr val{};
mutable object_ptr val{};
object_ptr fn{};
mutable std::atomic<object_ptr> error{};
std::unique_ptr<std::exception_ptr> delay_exception_ptr
= std::make_unique<std::exception_ptr>(nullptr);
};
Expand Down
48 changes: 15 additions & 33 deletions compiler+runtime/src/cpp/jank/runtime/obj/delay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,22 @@ namespace jank::runtime
return val;
}

if(delay_exception_ptr != nullptr)
{
throw *delay_exception_ptr;
if (error.load() != nullptr) {
throw error.load();
}

return visit_object(
[&]<typename T>(T const typed_s) -> object_ptr {
if constexpr(behavior::function_like<T> && !std::same_as<T, obj::nil>)
{
try
{
val = typed_s->call();
return val;
}
catch(...)
{
*delay_exception_ptr = std::current_exception();
throw *delay_exception_ptr;
}
}
else
{
try
{
throw std::runtime_error{ fmt::format("Invalid expression provided: {}",
typed_s->to_string()) };
}
catch(...)
{
*delay_exception_ptr = std::current_exception();
std::rethrow_exception(*delay_exception_ptr);
}
}
},
fn);
try {
val = dynamic_call(fn);
}
catch (std::exception const &e)
{
error.store(make_box(e.what()));
throw;
}
catch (object_ptr const &e) {
error.store(e);
throw;
}
return val;
}
}

0 comments on commit bd502b7

Please sign in to comment.