-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objs): adds native_pointer_wrapper obj
- Loading branch information
Showing
7 changed files
with
105 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
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
40 changes: 40 additions & 0 deletions
40
compiler+runtime/include/cpp/jank/runtime/obj/native_pointer_wrapper.hpp
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,40 @@ | ||
#pragma once | ||
|
||
#include <jank/runtime/object.hpp> | ||
|
||
namespace jank::runtime | ||
{ | ||
template <> | ||
struct static_object<object_type::native_pointer_wrapper> : gc | ||
{ | ||
static constexpr native_bool pointer_free{ false }; | ||
|
||
static_object() = default; | ||
static_object(static_object &&) = default; | ||
static_object(static_object const &) = default; | ||
static_object(void *); | ||
|
||
/* 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; | ||
|
||
template <typename T> | ||
T *as() const | ||
{ | ||
return reinterpret_cast<T *>(data); | ||
} | ||
|
||
object base{ object_type::native_pointer_wrapper }; | ||
|
||
void *data{}; | ||
}; | ||
|
||
namespace obj | ||
{ | ||
using native_pointer_wrapper = static_object<object_type::native_pointer_wrapper>; | ||
using native_pointer_wrapper_ptr = native_box<native_pointer_wrapper>; | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
compiler+runtime/src/cpp/jank/runtime/obj/native_pointer_wrapper.cpp
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,45 @@ | ||
#include <jank/runtime/obj/native_pointer_wrapper.hpp> | ||
|
||
namespace jank::runtime | ||
{ | ||
obj::native_pointer_wrapper::static_object(void *d) | ||
: data{ d } | ||
{ | ||
} | ||
|
||
native_bool obj::native_pointer_wrapper::equal(object const &o) const | ||
{ | ||
if(o.type != object_type::native_pointer_wrapper) | ||
{ | ||
return false; | ||
} | ||
|
||
auto const c(expect_object<obj::native_pointer_wrapper>(&o)); | ||
return data == c->data; | ||
} | ||
|
||
void obj::native_pointer_wrapper::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::native_pointer_wrapper::to_string() const | ||
{ | ||
fmt::memory_buffer buff; | ||
to_string(buff); | ||
return native_persistent_string{ buff.data(), buff.size() }; | ||
} | ||
|
||
native_persistent_string obj::native_pointer_wrapper::to_code_string() const | ||
{ | ||
return to_string(); | ||
} | ||
|
||
native_hash obj::native_pointer_wrapper::to_hash() const | ||
{ | ||
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(data)); | ||
} | ||
} |