Skip to content

Commit

Permalink
Add jit_closure object
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Oct 20, 2024
1 parent 966e912 commit 1aadea4
Show file tree
Hide file tree
Showing 12 changed files with 2,131 additions and 40 deletions.
1 change: 1 addition & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ add_library(
src/cpp/jank/runtime/obj/number.cpp
src/cpp/jank/runtime/obj/native_function_wrapper.cpp
src/cpp/jank/runtime/obj/jit_function.cpp
src/cpp/jank/runtime/obj/jit_closure.cpp
src/cpp/jank/runtime/obj/multi_function.cpp
src/cpp/jank/runtime/obj/symbol.cpp
src/cpp/jank/runtime/obj/keyword.cpp
Expand Down
21 changes: 14 additions & 7 deletions compiler+runtime/include/cpp/jank/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,23 @@ extern "C"
jank_native_bool is_variadic,
jank_native_bool is_variadic_ambiguous);
jank_object_ptr jank_function_create(jank_arity_flags arity_flags);
jank_object_ptr jank_function_create_closure(jank_arity_flags arity_flags, void *context);
jank_object_ptr jank_function_set_arity0(jank_object_ptr fn, jank_object_ptr (*f)());
jank_object_ptr
jank_function_set_arity1(jank_object_ptr fn, jank_object_ptr (*f)(jank_object_ptr));
jank_object_ptr jank_function_set_arity2(jank_object_ptr fn,
jank_object_ptr (*f)(jank_object_ptr, jank_object_ptr));
jank_object_ptr
void jank_function_set_arity0(jank_object_ptr fn, jank_object_ptr (*f)());
void jank_function_set_arity1(jank_object_ptr fn, jank_object_ptr (*f)(jank_object_ptr));
void jank_function_set_arity2(jank_object_ptr fn,
jank_object_ptr (*f)(jank_object_ptr, jank_object_ptr));
void
jank_function_set_arity3(jank_object_ptr fn,
jank_object_ptr (*f)(jank_object_ptr, jank_object_ptr, jank_object_ptr));

jank_object_ptr jank_closure_create(jank_arity_flags arity_flags, void *context);
void jank_closure_set_arity0(jank_object_ptr fn, jank_object_ptr (*f)());
void jank_closure_set_arity1(jank_object_ptr fn, jank_object_ptr (*f)(jank_object_ptr));
void jank_closure_set_arity2(jank_object_ptr fn,
jank_object_ptr (*f)(jank_object_ptr, jank_object_ptr));
void
jank_closure_set_arity3(jank_object_ptr fn,
jank_object_ptr (*f)(jank_object_ptr, jank_object_ptr, jank_object_ptr));

jank_native_bool jank_truthy(jank_object_ptr o);
jank_native_bool jank_equal(jank_object_ptr l, jank_object_ptr r);
jank_native_hash jank_to_hash(jank_object_ptr o);
Expand Down
6 changes: 6 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/erasure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <jank/runtime/obj/chunked_cons.hpp>
#include <jank/runtime/obj/range.hpp>
#include <jank/runtime/obj/jit_function.hpp>
#include <jank/runtime/obj/jit_closure.hpp>
#include <jank/runtime/obj/multi_function.hpp>
#include <jank/runtime/obj/native_function_wrapper.hpp>
#include <jank/runtime/obj/persistent_vector_sequence.hpp>
Expand Down Expand Up @@ -363,6 +364,11 @@ namespace jank::runtime
return fn(expect_object<obj::jit_function>(erased), std::forward<Args>(args)...);
}
break;
case object_type::jit_closure:
{
return fn(expect_object<obj::jit_closure>(erased), std::forward<Args>(args)...);
}
break;
case object_type::multi_function:
{
return fn(expect_object<obj::multi_function>(erased), std::forward<Args>(args)...);
Expand Down
125 changes: 125 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/jit_closure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#pragma once

#include <jank/runtime/object.hpp>
#include <jank/runtime/behavior/callable.hpp>

namespace jank::runtime
{
template <>
struct static_object<object_type::jit_closure>
: gc
, behavior::callable
{
static constexpr native_bool pointer_free{ false };

static_object() = default;
static_object(static_object &&) = default;
static_object(static_object const &) = default;
static_object(arity_flag_t arity_flags);
static_object(arity_flag_t arity_flags, void *context);
static_object(object_ptr meta);

/* behavior::object_like */
native_bool equal(object const &) const;
native_persistent_string to_string();
void to_string(fmt::memory_buffer &buff);
native_persistent_string to_code_string();
native_hash to_hash() const;

/* behavior::metadatable */
native_box<static_object> with_meta(object_ptr m);

/* behavior::callable */
object_ptr call() final;
object_ptr call(object_ptr) final;
object_ptr call(object_ptr, object_ptr) final;
object_ptr call(object_ptr, object_ptr, object_ptr) final;
object_ptr call(object_ptr, object_ptr, object_ptr, object_ptr) final;
object_ptr call(object_ptr, object_ptr, object_ptr, object_ptr, object_ptr) final;
object_ptr call(object_ptr, object_ptr, object_ptr, object_ptr, object_ptr, object_ptr) final;
object_ptr
call(object_ptr, object_ptr, object_ptr, object_ptr, object_ptr, object_ptr, object_ptr)
final;
object_ptr call(object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr) final;
object_ptr call(object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr) final;
object_ptr call(object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr) final;

arity_flag_t get_arity_flags() const final;

object_ptr this_object_ptr() final;

object base{ object_type::jit_closure };
void *context{};
object *(*arity_0)(void *){};
object *(*arity_1)(void *, object *){};
object *(*arity_2)(void *, object *, object *){};
object *(*arity_3)(void *, object *, object *, object *){};
object *(*arity_4)(void *, object *, object *, object *, object *){};
object *(*arity_5)(void *, object *, object *, object *, object *, object *){};
object *(*arity_6)(void *, object *, object *, object *, object *, object *, object *){};
object *(
*arity_7)(void *, object *, object *, object *, object *, object *, object *, object *){};
object *(*arity_8)(void *,
object *,
object *,
object *,
object *,
object *,
object *,
object *,
object *){};
object *(*arity_9)(void *,
object *,
object *,
object *,
object *,
object *,
object *,
object *,
object *,
object *){};
object *(*arity_10)(void *,
object *,
object *,
object *,
object *,
object *,
object *,
object *,
object *,
object *,
object *){};
option<object_ptr> meta;
arity_flag_t arity_flags{};
};

namespace obj
{
using jit_closure = static_object<object_type::jit_closure>;
using jit_closure_ptr = native_box<jit_closure>;
}
}
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace jank::runtime

native_function_wrapper,
jit_function,
jit_closure,
multi_function,

atom,
Expand Down
Loading

0 comments on commit 1aadea4

Please sign in to comment.