-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
native_function_wrapper.hpp
157 lines (138 loc) · 4.32 KB
/
native_function_wrapper.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once
#include <iostream>
#include <any>
#include <jank/runtime/behavior/callable.hpp>
#include <jank/runtime/behavior/metadatable.hpp>
namespace jank::runtime
{
namespace obj::detail
{
struct function_type
{
template <typename T>
using value_type = std::function<T>;
function_type() = default;
template <typename R, typename... Args>
function_type(R (* const f)(Args...))
: function_type{ value_type<R(Args...)>{ f } }
{
}
template <typename R, typename... Args>
function_type(value_type<R(Args...)> &&f)
: value{ std::move(f) }
{
}
template <typename R, typename... Args>
function_type(value_type<R(Args...)> const &f)
: value{ f }
{
}
template <typename F>
F const *get() const
{
return std::any_cast<F>(&value);
}
std::any value;
};
}
template <size_t Arity>
struct invalid_arity : std::runtime_error
{
invalid_arity()
: std::runtime_error{ fmt::format("invalid call to fn with {} args provided", Arity) }
{
}
};
template <>
struct static_object<object_type::native_function_wrapper>
: gc
, behavior::callable
{
static constexpr native_bool pointer_free{ true };
static_object() = default;
static_object(static_object &&) = default;
static_object(static_object const &) = default;
static_object(obj::detail::function_type &&d);
static_object(obj::detail::function_type const &d);
/* 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::callable */
object_ptr call() const final;
object_ptr call(object_ptr) const final;
object_ptr call(object_ptr, object_ptr) const final;
object_ptr call(object_ptr, object_ptr, object_ptr) const final;
object_ptr call(object_ptr, object_ptr, object_ptr, object_ptr) const final;
object_ptr call(object_ptr, object_ptr, object_ptr, object_ptr, object_ptr) const final;
object_ptr
call(object_ptr, object_ptr, object_ptr, object_ptr, object_ptr, object_ptr) const final;
object_ptr
call(object_ptr, object_ptr, object_ptr, object_ptr, object_ptr, object_ptr, object_ptr)
const final;
object_ptr call(object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr) const final;
object_ptr call(object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr) const final;
object_ptr call(object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr,
object_ptr) const final;
/* behavior::metadatable */
object_ptr with_meta(object_ptr m) const;
object base{ object_type::native_function_wrapper };
obj::detail::function_type data{};
option<object_ptr> meta;
};
namespace obj
{
using native_function_wrapper = static_object<object_type::native_function_wrapper>;
using native_function_wrapper_ptr = native_box<native_function_wrapper>;
}
namespace detail
{
/* TODO: Is this needed, given dynamic_call? */
template <typename F, typename... Args>
object_ptr invoke(F const &f, Args &&...args)
{
if constexpr(std::is_function_v<std::remove_pointer_t<std::decay_t<decltype(f)>>>)
{
return f(std::forward<Args>(args)...);
}
else
{
auto const * const c((*f)->as_callable());
if(c)
{
return c->call(std::forward<Args>(args)...);
}
else
{
/* TODO: Better error. */
std::cout << "(invoke) object is not callable: " << **f << std::endl;
throw std::runtime_error{ "object is not callable" };
}
}
}
}
}