-
-
Notifications
You must be signed in to change notification settings - Fork 91
feat(transient-vector): adds initial support for transient vector #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
#pragma once | ||
|
||
#include <jank/runtime/object.hpp> | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace jank::runtime | ||
{ | ||
template <> | ||
struct static_object<object_type::transient_vector> : gc | ||
{ | ||
static constexpr bool pointer_free{ false }; | ||
|
||
using value_type = detail::native_transient_vector; | ||
using persistent_type = static_object<object_type::persistent_vector>; | ||
|
||
static_object() = default; | ||
static_object(static_object &&) = default; | ||
static_object(static_object const &) = default; | ||
static_object(detail::native_persistent_vector const &d); | ||
static_object(detail::native_persistent_vector &&d); | ||
static_object(value_type &&d); | ||
|
||
static native_box<static_object> empty() | ||
{ | ||
static auto const ret(make_box<static_object>()); | ||
return ret; | ||
} | ||
|
||
/* behavior::objectable */ | ||
native_bool equal(object const &) const; | ||
native_persistent_string to_string() const; | ||
void to_string(fmt::memory_buffer &buff) const; | ||
native_hash to_hash() const; | ||
|
||
/* behavior::countable */ | ||
size_t count() const; | ||
|
||
/* behavior::consable_in_place */ | ||
native_box<static_object> cons_in_place(object_ptr head); | ||
|
||
/* behavior::persistentable */ | ||
native_box<persistent_type> to_persistent(); | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* behavior::callable */ | ||
object_ptr call(object_ptr) const; | ||
object_ptr call(object_ptr, object_ptr) const; | ||
|
||
void assert_active() const; | ||
|
||
object base{ object_type::transient_vector }; | ||
value_type data; | ||
mutable native_hash hash{}; | ||
native_bool active{ true }; | ||
}; | ||
|
||
namespace obj | ||
{ | ||
using transient_vector = static_object<object_type::transient_vector>; | ||
using transient_vector_ptr = native_box<transient_vector>; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,118 @@ | ||
#include "jank/runtime/detail/object_util.hpp" | ||
#include "jank/runtime/erasure.hpp" | ||
#include "jank/type.hpp" | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <jank/runtime/util.hpp> | ||
#include <jank/runtime/obj/native_function_wrapper.hpp> | ||
#include <jank/runtime/obj/transient_vector.hpp> | ||
#include <stdexcept> | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace jank::runtime | ||
{ | ||
obj::transient_vector::static_object(runtime::detail::native_persistent_vector &&d) | ||
: data{ d.transient() } | ||
{ | ||
} | ||
|
||
obj::transient_vector::static_object(runtime::detail::native_persistent_vector const &d) | ||
: data{ d.transient() } | ||
{ | ||
} | ||
|
||
obj::transient_vector::static_object(runtime::detail::native_transient_vector &&d) | ||
: data{ d } | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
native_bool obj::transient_vector::equal(object const &o) const | ||
{ | ||
/* Transient equality, in Clojure, is based solely on identity. */ | ||
return &base == &o; | ||
} | ||
|
||
native_persistent_string obj::transient_vector::to_string() const | ||
{ | ||
fmt::memory_buffer buff; | ||
to_string(buff); | ||
return native_persistent_string{ buff.data(), buff.size() }; | ||
} | ||
|
||
void obj::transient_vector::to_string(fmt::memory_buffer &buff) const | ||
{ | ||
auto inserter(std::back_inserter(buff)); | ||
fmt::format_to(inserter, "{}@{}", magic_enum::enum_name(base.type), fmt::ptr(&base)); | ||
} | ||
|
||
native_hash obj::transient_vector::to_hash() const | ||
{ | ||
/* Hash is also based only on identity. Clojure uses default hashCode, which does the same. */ | ||
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(this)); | ||
} | ||
|
||
size_t obj::transient_vector::count() const | ||
{ | ||
assert_active(); | ||
return data.size(); | ||
} | ||
|
||
obj::transient_vector_ptr obj::transient_vector::cons_in_place(object_ptr const head) | ||
{ | ||
assert_active(); | ||
data.push_back(head); | ||
return this; | ||
} | ||
|
||
native_box<obj::transient_vector::persistent_type> obj::transient_vector::to_persistent() | ||
{ | ||
assert_active(); | ||
active = false; | ||
return make_box<obj::persistent_vector>(std::move(data)); | ||
} | ||
|
||
object_ptr obj::transient_vector::call(object_ptr const o) const | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
assert_active(); | ||
if(o->type == object_type::integer) | ||
{ | ||
auto const i(expect_object<obj::integer>(o)->data); | ||
if(i < 0 || data.size() <= static_cast<size_t>(i)) | ||
{ | ||
return obj::nil::nil_const(); | ||
} | ||
|
||
return data.at(i); | ||
Samy-33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else | ||
{ | ||
throw std::runtime_error{ fmt::format("key must be an integer; found {}", | ||
runtime::detail::to_string(o)) }; | ||
} | ||
} | ||
|
||
object_ptr obj::transient_vector::call(object_ptr const o, object_ptr const fallback) const | ||
{ | ||
assert_active(); | ||
if(o->type == object_type::integer) | ||
{ | ||
auto const i(expect_object<obj::integer>(o)->data); | ||
if(i < 0 || data.size() <= static_cast<size_t>(i)) | ||
{ | ||
return fallback; | ||
} | ||
|
||
return data.at(i); | ||
} | ||
else | ||
{ | ||
throw std::runtime_error{ fmt::format("key must be an integer; found {}", | ||
runtime::detail::to_string(o)) }; | ||
} | ||
} | ||
|
||
void obj::transient_vector::assert_active() const | ||
{ | ||
if(!active) | ||
{ | ||
throw std::runtime_error{ "transient used after it's been made persistent" }; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.