Skip to content

Commit

Permalink
feat(objs): adds native_pointer_wrapper obj
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy-33 committed Nov 25, 2024
1 parent b9d90a4 commit 06ea3af
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
test.jank
jank-generated.hpp
a.out
.jank-native-repl-history
.jank-repl-history

# Vim files
/.ycm_extra_conf.py*
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ add_library(
src/cpp/jank/runtime/obj/native_function_wrapper.cpp
src/cpp/jank/runtime/obj/jit_function.cpp
src/cpp/jank/runtime/obj/multi_function.cpp
src/cpp/jank/runtime/obj/native_pointer_wrapper.cpp
src/cpp/jank/runtime/obj/symbol.cpp
src/cpp/jank/runtime/obj/keyword.cpp
src/cpp/jank/runtime/obj/character.cpp
Expand Down
8 changes: 8 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/core/make_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ namespace jank::runtime
return make_box<runtime::obj::persistent_list>(l);
}

template <typename T>
requires std::is_pointer_v<T>
[[gnu::always_inline, gnu::flatten, gnu::hot]]
inline auto make_box(T const d)
{
return make_box<obj::native_pointer_wrapper>(static_cast<void *>(d));
}

template <typename T>
requires std::is_floating_point_v<T>
[[gnu::always_inline, gnu::flatten, gnu::hot]]
Expand Down
7 changes: 7 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/erasure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <jank/runtime/obj/jit_function.hpp>
#include <jank/runtime/obj/multi_function.hpp>
#include <jank/runtime/obj/native_function_wrapper.hpp>
#include <jank/runtime/obj/native_pointer_wrapper.hpp>
#include <jank/runtime/obj/persistent_vector_sequence.hpp>
#include <jank/runtime/obj/persistent_string_sequence.hpp>
#include <jank/runtime/obj/persistent_list_sequence.hpp>
Expand Down Expand Up @@ -364,6 +365,12 @@ namespace jank::runtime
std::forward<Args>(args)...);
}
break;
case object_type::native_pointer_wrapper:
{
return fn(expect_object<obj::native_pointer_wrapper>(erased),
std::forward<Args>(args)...);
}
break;
case object_type::jit_function:
{
return fn(expect_object<obj::jit_function>(erased), std::forward<Args>(args)...);
Expand Down
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>;
}
}
2 changes: 2 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ namespace jank::runtime
jit_function,
multi_function,

native_pointer_wrapper,

atom,
volatile_,
reduced,
Expand Down
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));
}
}

0 comments on commit 06ea3af

Please sign in to comment.