Skip to content

Commit

Permalink
Updated implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
jianlingzhong committed Nov 5, 2024
1 parent bd502b7 commit 4131370
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
2 changes: 2 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ namespace jank::runtime
native_persistent_string namespace_(object_ptr o);

native_bool is_callable(object_ptr o);

object_ptr force(object_ptr o);
}
11 changes: 4 additions & 7 deletions compiler+runtime/include/cpp/jank/runtime/obj/delay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ namespace jank::runtime
native_hash to_hash() const;

/* behavior::derefable */
object_ptr deref() const;

static object_ptr force(object_ptr const &d);
object_ptr deref();

object base{ object_type::delay };
mutable object_ptr val{};
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);
object_ptr error{};
std::mutex mutex;
};

namespace obj
Expand Down
7 changes: 7 additions & 0 deletions compiler+runtime/src/cpp/jank/runtime/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,11 @@ namespace jank::runtime
},
o);
}

object_ptr force(object_ptr const o) {
if (o->type == object_type::delay) {
return expect_object<obj::delay>(o)->deref();
}
return o;
}
}
34 changes: 8 additions & 26 deletions compiler+runtime/src/cpp/jank/runtime/obj/delay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,9 @@ namespace jank::runtime
{
fmt::memory_buffer buff;
to_string(buff);
return native_persistent_string{ reinterpret_cast<native_persistent_string::size_type>(
buff.data()),
static_cast<char>(buff.size()) };
return native_persistent_string{buff.data(), buff.size()};
}

object_ptr obj::delay::force(object_ptr const &d)
{
return visit_object(
[d](auto const typed_d) -> object_ptr {
using T = typename decltype(typed_d)::value_type;

if constexpr(behavior::derefable<T>)
{
return typed_d->deref();
}
else
{
return d;
}
},
d);
}

void obj::delay::to_string(fmt::memory_buffer &buff) const
{
Expand All @@ -57,27 +38,28 @@ namespace jank::runtime
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(this));
}

object_ptr obj::delay::deref() const
object_ptr obj::delay::deref()
{
std::lock_guard<std::mutex> const lock{mutex};
if(val != nullptr)
{
return val;
}

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

try {
val = dynamic_call(fn);
}
catch (std::exception const &e)
{
error.store(make_box(e.what()));
error = make_box(e.what());
throw;
}
catch (object_ptr const &e) {
error.store(e);
catch (object_ptr const e) {
error = e;
throw;
}
return val;
Expand Down
11 changes: 8 additions & 3 deletions compiler+runtime/src/jank/clojure/core.jank
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,14 @@
[x]
(native/raw "__value = make_box(~{ x }->type == object_type::volatile_);"))

; Delay and force
(defn delay [exp] (native/raw "__value = make_box<obj::delay>(~{ exp });"))
(defn force [exp] (native/raw "__value = try_object<obj::delay>(~{ exp })->deref();"))
(defn delay* [f]
(native/raw "__value = make_box<obj::delay>(~{ f });"))

(defmacro delay [& body]
(list 'delay* (cons 'fn (cons '[] body))))

(defn force [o]
(native/raw "__value = runtime::force(~{ o });"))

; Primitives.
;; Arithmetic.
Expand Down

0 comments on commit 4131370

Please sign in to comment.