diff --git a/compiler+runtime/include/cpp/jank/runtime/obj/delay.hpp b/compiler+runtime/include/cpp/jank/runtime/obj/delay.hpp index fdcbed1d..41f83bd7 100644 --- a/compiler+runtime/include/cpp/jank/runtime/obj/delay.hpp +++ b/compiler+runtime/include/cpp/jank/runtime/obj/delay.hpp @@ -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 error{}; std::unique_ptr delay_exception_ptr = std::make_unique(nullptr); }; diff --git a/compiler+runtime/src/cpp/jank/runtime/obj/delay.cpp b/compiler+runtime/src/cpp/jank/runtime/obj/delay.cpp index 7c67eb4c..23ac1b8a 100644 --- a/compiler+runtime/src/cpp/jank/runtime/obj/delay.cpp +++ b/compiler+runtime/src/cpp/jank/runtime/obj/delay.cpp @@ -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( - [&](T const typed_s) -> object_ptr { - if constexpr(behavior::function_like && !std::same_as) - { - 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; } }