-
Notifications
You must be signed in to change notification settings - Fork 1
/
mrubyclass.hpp
267 lines (216 loc) · 7.87 KB
/
mrubyclass.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#ifndef __MRUBYCLASS_HPP__
#define __MRUBYCLASS_HPP__
template<class TClass>
class Class : public Module
{
template <typename ... TConstructorArgs>
static mrb_value constructor(mrb_state* mrb, mrb_value self)
{
START_HANDLE_ERROR
{
RClass* cls = TypeBinder<RClass*>::from_mrb_value(mrb, self);
mrb_sym nsym = mrb_intern_lit(mrb, "__classname__");
mrb_value nval = mrb_obj_iv_get(mrb, (struct RObject*)cls, nsym);
std::string str = TypeBinder<std::string>::from_mrb_value(mrb, nval);
std::function<std::shared_ptr<TClass>(TConstructorArgs...)> func =
[=](TConstructorArgs... params) -> std::shared_ptr<TClass>
{
return std::make_shared<TClass>(params...);
};
mrb_value* args;
size_t argc = 0;
mrb_get_args(mrb, "*", &args, &argc);
if (argc != sizeof...(TConstructorArgs))
{
HANDLE_ERROR_AND_EXIT error_argument_count(mrb, str, "initialize", argc, sizeof...(TConstructorArgs));
}
auto curried = curry(func);
std::shared_ptr<TClass> instance = nullptr;
try
{
instance = func_caller<0, std::shared_ptr<TClass>, TConstructorArgs...>(mrb, curried, args);
}
catch (const RubyException &e)
{
HANDLE_ERROR_AND_EXIT mrb_exc_new(mrb, E_RUNTIME_ERROR, e.what(), strlen(e.what()));
}
NativeObject<TClass>* ptr = new NativeObject<TClass>(str, instance);
DATA_TYPE(self) = ptr->get_type_ptr();
DATA_PTR(self) = ptr;
return self;
}
END_HANDLE_ERROR
}
static mrb_value default_constructor(mrb_state* mrb, mrb_value self)
{
START_HANDLE_ERROR
{
RClass* cls = TypeBinder<RClass*>::from_mrb_value(mrb, self);
mrb_sym nsym = mrb_intern_lit(mrb, "__classname__");
mrb_value nval = mrb_obj_iv_get(mrb, (struct RObject*)cls, nsym);
std::string str = TypeBinder<std::string>::from_mrb_value(mrb, nval);
mrb_value* args;
size_t argc = 0;
mrb_get_args(mrb, "*", &args, &argc);
if (argc != 0)
{
HANDLE_ERROR_AND_EXIT error_argument_count(mrb, str, "initialize", argc, 0);
}
std::shared_ptr<TClass> instance = nullptr;
try
{
instance = std::make_shared<TClass>();
}
catch (const RubyException &e)
{
HANDLE_ERROR_AND_EXIT mrb_exc_new(mrb, E_RUNTIME_ERROR, e.what(), strlen(e.what()));
}
NativeObject<TClass>* ptr = new NativeObject<TClass>(str, instance);
DATA_TYPE(self) = ptr->get_type_ptr();
DATA_PTR(self) = ptr;
return self;
}
END_HANDLE_ERROR
}
static mrb_value error_constructor_closed(mrb_state *mrb, mrb_value class_name) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "'%S' cannot be created with new",
class_name);
return mrb_nil_value();
}
static mrb_value closed_constructor(mrb_state* mrb, mrb_value self)
{
return error_constructor_closed(mrb, self);
}
public:
template <typename ... TConstructorArgs>
Class(std::shared_ptr<mrb_state> mrb, const std::string& name, RClass* cls, void(*)(TConstructorArgs...)) :
Module(mrb, name, cls)
{
MRB_SET_INSTANCE_TT(cls, MRB_TT_DATA);
mrb_define_method(mrb.get(), cls, "initialize", constructor<TConstructorArgs...>, MRB_ARGS_ARG(sizeof...(TConstructorArgs),0));
}
Class(std::shared_ptr<mrb_state> mrb, const std::string& name, RClass* cls, void(*)(void)) :
Module(mrb, name, cls)
{
MRB_SET_INSTANCE_TT(cls, MRB_TT_DATA);
mrb_define_method(mrb.get(), cls, "initialize", default_constructor, MRB_ARGS_ARG(0, 0));
}
Class(std::shared_ptr<mrb_state> mrb, const std::string& name, RClass* cls) :
Module(mrb, name, cls)
{
MRB_SET_INSTANCE_TT(cls, MRB_TT_DATA);
mrb_define_method(mrb.get(), cls, "initialize", closed_constructor, MRB_ARGS_ARG(0, 0));
}
~Class()
{
}
template<typename TVal>
static mrb_value mruby_member_setter(mrb_state* mrb, mrb_value self)
{
try
{
typedef TVal TClass::* memptr_t;
RClass* cls = TypeBinder<RClass*>::from_mrb_value(mrb, self);
mrb_value* args;
size_t argc = 0;
mrb_get_args(mrb, "*", &args, &argc);
mrb_value kernel_val = TypeBinder<RClass*>::to_mrb_value(mrb, mrb->kernel_module);
mrb_value nval = mrb_funcall(mrb, kernel_val, "__method__", 0);
std::string name = TypeBinder<std::string>::from_mrb_value(mrb, nval);
std::string ptr_name = "__allocated_memptr__" + name.substr(0, name.length()-1);
mrb_sym mem_ptr_sym = mrb_intern_cstr(mrb, ptr_name.c_str());
mrb_value mem_ptr_holder = mrb_mod_cv_get(mrb, cls, mem_ptr_sym);
if (argc != 1)
{
std::string class_name = TypeBinder<std::string>::from_mrb_value(mrb, self);
throw error_argument_count(mrb, class_name, name, argc, 1);
}
NativeObject<memptr_t> obj = TypeBinder< NativeObject<memptr_t> >::from_mrb_value(mrb, mem_ptr_holder);
memptr_t* ptr = obj.get_instance();
NativeObject<TClass>* thisptr = (NativeObject<TClass>*)DATA_PTR(self);
thisptr->get_instance()->**ptr = TypeBinder<TVal>::from_mrb_value(mrb, *args);
}
catch(mrb_value exc)
{
mrb_exc_raise(mrb, exc);
}
return mrb_nil_value();
}
template<typename TVal>
static mrb_value mruby_member_getter(mrb_state* mrb, mrb_value self)
{
typedef TVal TClass::* memptr_t;
RClass* cls = TypeBinder<RClass*>::from_mrb_value(mrb, self);
mrb_value kernel_val = TypeBinder<RClass*>::to_mrb_value(mrb, mrb->kernel_module);
mrb_value nval = mrb_funcall(mrb, kernel_val, "__method__", 0);
std::string name = TypeBinder<std::string>::from_mrb_value(mrb, nval);
std::string ptr_name = "__allocated_memptr__" + name;
mrb_sym mem_ptr_sym = mrb_intern_cstr(mrb, ptr_name.c_str());
mrb_value mem_ptr_holder = mrb_mod_cv_get(mrb, cls, mem_ptr_sym);
NativeObject<memptr_t> obj = TypeBinder< NativeObject<memptr_t> >::from_mrb_value(mrb, mem_ptr_holder);
memptr_t* ptr = obj.get_instance();
NativeObject<TClass>* thisptr = (NativeObject<TClass>*)DATA_PTR(self);
return TypeBinder<TVal>::to_mrb_value(mrb, thisptr->get_instance()->**ptr);
}
template<typename TVal>
void create_accessor(const std::string& name, TVal TClass::* var, RClass* module_class)
{
typedef TVal TClass::* memptr_t;
std::string ptr_name = "__allocated_memptr__" + name;
mrb_sym mem_ptr_sym = mrb_intern_cstr(mrb.get(), ptr_name.c_str());
function_definer_t define_function_method = mrb_define_method;
memptr_t* ptr = new memptr_t;
*ptr = var;
NativeObject<memptr_t> obj("Object", std::shared_ptr<memptr_t>(ptr));
mrb_mod_cv_set(
mrb.get(),
module_class,
mem_ptr_sym,
TypeBinder< NativeObject<memptr_t> >::to_mrb_value(mrb.get(), obj));
define_function_method(
mrb.get(),
module_class,
name.c_str(),
mruby_member_getter<TVal>,
MRB_ARGS_REQ(0));
define_function_method(
mrb.get(),
module_class,
(name + "=").c_str(),
mruby_member_setter<TVal>,
MRB_ARGS_REQ(1));
}
template<typename TVar>
void bind_instance_variable(const std::string& name, TVar TClass::* var)
{
create_accessor(name, var, cls);
}
#ifndef _MSC_VER
template<typename TC2, typename TVar>
void bind_instance_variable(const std::string& name, TVar TC2::* var)
{
static_assert(std::is_base_of<TC2, TClass>::value, "You must provide a member variable that is somewhere up the inheritance chain of this class");
create_accessor(name, (TVar TClass::*)(var), cls);
}
#endif
template<typename TRet, typename ... TArgs>
void bind_instance_method(const std::string& name, TRet(TClass::*func)(TArgs...))
{
create_function(name, func, cls, mrb_define_method);
}
template<typename TRet, typename ... TArgs>
void bind_instance_method(const std::string& name, TRet(TClass::*func)(TArgs...) const)
{
create_function(name, func, cls, mrb_define_method);
}
#ifndef _MSC_VER
template<typename TC2, typename TRet, typename ... TArgs>
void bind_instance_method(const std::string& name, TRet(TC2::*func)(TArgs...))
{
static_assert(std::is_base_of<TC2, TClass>::value, "You must provide a member function that is somewhere in the inheritance chain of this class");
typedef TRet(TClass::*TTarget)(TArgs...);
create_function(name, (TTarget)func, cls, mrb_define_method);
}
#endif
};
#endif // __MRUBYCLASS_HPP__