-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from jianlingzhong/delay
Add support for delay
- Loading branch information
Showing
9 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,6 @@ CMakeUserPresets.json | |
|
||
# Rust | ||
rust/jank | ||
|
||
# secrets | ||
.github_access_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
namespace jank::runtime | ||
{ | ||
template <> | ||
struct static_object<object_type::delay> : gc | ||
{ | ||
static constexpr native_bool pointer_free{ false }; | ||
|
||
static_object() = default; | ||
static_object(object_ptr fn); | ||
|
||
/* behavior::object_like */ | ||
native_bool equal(object const &) const; | ||
native_persistent_string to_string() const; | ||
void to_string(fmt::memory_buffer &buff) const; | ||
native_persistent_string to_code_string() const; | ||
native_hash to_hash() const; | ||
|
||
/* behavior::derefable */ | ||
object_ptr deref(); | ||
|
||
object base{ object_type::delay }; | ||
object_ptr val{}; | ||
object_ptr fn{}; | ||
object_ptr error{}; | ||
std::mutex mutex; | ||
}; | ||
|
||
namespace obj | ||
{ | ||
using delay = static_object<object_type::delay>; | ||
using delay_ptr = native_box<delay>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ namespace jank::runtime | |
atom, | ||
volatile_, | ||
reduced, | ||
delay, | ||
ns, | ||
|
||
var, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <jank/runtime/obj/delay.hpp> | ||
|
||
namespace jank::runtime | ||
{ | ||
obj::delay::static_object(object_ptr const fn) | ||
: fn{ fn } | ||
{ | ||
} | ||
|
||
native_bool obj::delay::equal(object const &o) const | ||
{ | ||
return &o == &base; | ||
} | ||
|
||
native_persistent_string obj::delay::to_string() const | ||
{ | ||
fmt::memory_buffer buff; | ||
to_string(buff); | ||
return native_persistent_string{ buff.data(), buff.size() }; | ||
} | ||
|
||
void obj::delay::to_string(fmt::memory_buffer &buff) const | ||
{ | ||
fmt::format_to(std::back_inserter(buff), | ||
"{}@{}", | ||
magic_enum::enum_name(base.type), | ||
fmt::ptr(&base)); | ||
} | ||
|
||
native_persistent_string obj::delay::to_code_string() const | ||
{ | ||
return to_string(); | ||
} | ||
|
||
native_hash obj::delay::to_hash() const | ||
{ | ||
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(this)); | ||
} | ||
|
||
object_ptr obj::delay::deref() | ||
{ | ||
std::lock_guard<std::mutex> const lock{ mutex }; | ||
if(val != nullptr) | ||
{ | ||
return val; | ||
} | ||
|
||
if(error != nullptr) | ||
{ | ||
throw error; | ||
} | ||
|
||
try | ||
{ | ||
val = dynamic_call(fn); | ||
} | ||
catch(std::exception const &e) | ||
{ | ||
error = make_box(e.what()); | ||
throw; | ||
} | ||
catch(object_ptr const e) | ||
{ | ||
error = e; | ||
throw; | ||
} | ||
return val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters