forked from skandhas/mrubybind
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmrubybind.cc
349 lines (307 loc) · 10.3 KB
/
mrubybind.cc
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Do not modify this file directly, this is generated
#include "mrubybind.h"
#include "mruby/compile.h"
#include "mruby/dump.h"
#include "mruby/proc.h"
#include "mruby/string.h"
#include "mruby/variable.h"
#include <iostream>
namespace mrubybind {
//#include "mrubybind.dat"
const char Type<int>::TYPE_NAME[] = "Fixnum";
const char Type<unsigned int>::TYPE_NAME[] = "Fixnum";
const char Type<float>::TYPE_NAME[] = "Float";
const char Type<double>::TYPE_NAME[] = "Float";
const char Type<const char*>::TYPE_NAME[] = "String";
const char Type<std::string>::TYPE_NAME[] = "String";
const char Type<const std::string>::TYPE_NAME[] = "String";
const char Type<const std::string&>::TYPE_NAME[] = "String";
const char Type<bool>::TYPE_NAME[] = "Bool";
const char Type<void*>::TYPE_NAME[] = "Voidp";
const char Type<MrubyRef>::TYPE_NAME[] = "MrubyRef";
const char TypeFuncBase::TYPE_NAME[] = "Func";
const char TypeClassBase::TYPE_NAME[] = "CppClass";
const char* untouchable_table = "__ untouchable table __";
const char* untouchable_object = "__ untouchable object __";
const char* untouchable_last_exception = "__ untouchable last exception __";
mrb_value raise(mrb_state *mrb, int parameter_index,
const char* required_type_name, mrb_value value) {
const char * argument_class_name = mrb_obj_classname(mrb, value);
mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into %S, argument %S(%S)",
mrb_str_new_cstr(mrb, argument_class_name),
mrb_str_new_cstr(mrb, required_type_name),
mrb_fixnum_value(parameter_index + 1), value);
return mrb_nil_value();
}
mrb_value raisenarg(mrb_state *mrb, mrb_value func_name, int narg, int nparam) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "'%S': wrong number of arguments (%S for %S)",
func_name,
mrb_fixnum_value(narg),
mrb_fixnum_value(nparam));
return mrb_nil_value();
}
void
MrubyBind::mrb_define_class_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, struct RProc *p)
{
mrb_define_class_method(mrb, c, mrb_sym2name(mrb, mid), NULL, MRB_ARGS_ANY()); // Dummy registration.
mrb_define_method_raw(mrb, ((RObject*)c)->c, mid, p);
}
MrubyBind::MrubyBind(mrb_state* mrb) : mrb_(mrb), mod_(mrb_->kernel_module) {
Initialize();
}
MrubyBind::MrubyBind(mrb_state* mrb, RClass* mod) : mrb_(mrb), mod_(mod) {
Initialize();
}
MrubyBind::~MrubyBind() {
mrb_gc_arena_restore(mrb_, arena_index_);
}
void MrubyBind::Initialize() {
arena_index_ = mrb_gc_arena_save(mrb_);
mrb_sym sym_mrubybind = mrb_intern_cstr(mrb_, "MrubyBind");
RClass* mrubybind = mrb_define_module(mrb_, "MrubyBind");
if (mrb_const_defined(mrb_, mrb_obj_value(mrb_->kernel_module),
sym_mrubybind)) {
avoid_gc_table_ = mrb_obj_iv_get(mrb_, (RObject*)mrubybind,
mrb_intern_cstr(mrb_, untouchable_table));
} else {
mrb_const_set(mrb_, mrb_obj_value(mrb_->kernel_module), sym_mrubybind, mrb_obj_value(mrubybind));
{
avoid_gc_table_ = mrb_ary_new(mrb_);
mrb_obj_iv_set(mrb_, (RObject*)mrubybind,
mrb_intern_cstr(mrb_, untouchable_table), avoid_gc_table_);
std::shared_ptr<MrubyBindStatus> mrbs = std::make_shared<MrubyBindStatus>(mrb_, avoid_gc_table_);
mrubybind::MrubyBind b(mrb_);
b.bind_class<std::shared_ptr<MrubyBindStatus> >("MrubyBind", "MrubyBindStatusPtr");
mrb_value msbpv = Type<std::shared_ptr<MrubyBindStatus> >::ret(mrb_, mrbs);
mrb_obj_iv_set(mrb_, (RObject*)mrubybind,
mrb_intern_cstr(mrb_, untouchable_object), msbpv);
}
}
}
std::vector<std::string> MrubyBind::SplitModule(const char* module_name)
{
std::vector<std::string> splited;
if(!module_name){
return splited;
}
std::string str(module_name);
std::string d("::");
size_t c = 0, f, dsz = d.size();
while((f = str.find(d, c)) != std::string::npos){
splited.push_back(str.substr(c, f - c));
c = f + dsz;
}
splited.push_back(str.substr(c, str.size() - c));
return splited;
}
struct RClass* MrubyBind::DefineModule(const char* module_name)
{
std::vector<std::string> modules = SplitModule(module_name);
struct RClass* last_module = nullptr;
for(size_t i = 0 ; i < modules.size() ; i++){
auto m = modules[i];
if(i == 0){
if(mrb_const_defined_at(mrb_, mrb_obj_value(mrb_->object_class), mrb_intern_cstr(mrb_, m.c_str()))){
mrb_value mdl = mrb_const_get(mrb_, mrb_obj_value(mrb_->object_class), mrb_intern_cstr(mrb_, m.c_str()));
last_module = mrb_class_ptr(mdl);
}
else {
last_module = mrb_define_module(mrb_, m.c_str());
}
}
else{
if(mrb_const_defined_at(mrb_, mrb_obj_value(mrb_->object_class), mrb_intern_cstr(mrb_, m.c_str()))){
mrb_value mdl = mrb_const_get(mrb_, mrb_obj_value(last_module), mrb_intern_cstr(mrb_, m.c_str()));
last_module = mrb_class_ptr(mdl);
}
else {
last_module = mrb_define_module_under(mrb_, last_module, m.c_str());
}
}
}
return last_module;
}
struct RClass* MrubyBind::DefineClass(const char* module_name, const char* class_name)
{
struct RClass * tc;
mrb_value mod = mrb_obj_value(mod_);
std::string name;
if(module_name){
name = module_name;
name += "::";
name += class_name;
tc = mrb_define_class(mrb_, name.c_str(), mrb_->object_class);
struct RClass * mdp = DefineModule(module_name);
mod = mrb_obj_value(mdp);
mrb_define_const(mrb_, mdp, class_name, mrb_obj_value(tc));
}
else
{
name = class_name;
tc = mrb_define_class(mrb_, class_name, mrb_->object_class);
}
return tc;
}
struct RClass* MrubyBind::GetClass(const char* class_name) {
mrb_value mod = mrb_obj_value(mod_);
mrb_value klass_v = mrb_const_get(mrb_, mod, mrb_intern_cstr(mrb_, class_name));
return mrb_class_ptr(klass_v);
}
struct RClass* MrubyBind::GetClass(const char* module_name, const char* class_name) {
mrb_value mod = mrb_obj_value(mod_);
if(module_name){
mod = mrb_obj_value(DefineModule(module_name));
}
mrb_value klass_v = mrb_const_get(mrb_, mod, mrb_intern_cstr(mrb_, class_name));
return mrb_class_ptr(klass_v);
}
void MrubyBind::BindInstanceMethod(
const char* class_name, const char* method_name,
mrb_value original_func_v,
mrb_value (*binder_func)(mrb_state*, mrb_value)) {
BindInstanceMethod(NULL,
class_name, method_name,
original_func_v,
binder_func);
}
void MrubyBind::BindInstanceMethod(const char* module_name,
const char* class_name, const char* method_name,
mrb_value original_func_v,
mrb_value (*binder_func)(mrb_state*, mrb_value))
{
mrb_sym method_name_s = mrb_intern_cstr(mrb_, method_name);
mrb_value env[] = {
original_func_v, // 0: c function pointer
mrb_symbol_value(method_name_s), // 1: method name
};
struct RProc* proc = mrb_proc_new_cfunc_with_env(mrb_, binder_func, 2, env);
struct RClass* klass = GetClass(module_name, class_name);
mrb_define_method_raw(mrb_, klass, method_name_s, proc);
}
MrubyRef load_string(mrb_state* mrb, std::string code)
{
mrubybind::MrubyArenaStore mas(mrb);
RClass* mrubybind = mrb_define_module(mrb, "MrubyBind");
mrb->exc = NULL;
mrb_value r = mrb_load_string(mrb,
code.c_str());
if(mrb->exc){
mrb_obj_iv_set(mrb, (RObject*)mrubybind,
mrb_intern_cstr(mrb, untouchable_last_exception), mrb_obj_value(mrb->exc));
r = mrb_nil_value();
}
else{
mrb_obj_iv_set(mrb, (RObject*)mrubybind,
mrb_intern_cstr(mrb, untouchable_last_exception), mrb_nil_value());
}
return MrubyRef(mrb, r);
}
MrubyRef::MrubyRef(){
}
MrubyRef::MrubyRef(mrb_state* mrb, const mrb_value& v){
this->mrb = mrb;
mrb_value* p = new mrb_value();
*p = v;
this->v = std::shared_ptr<mrb_value>(p, set_avoid_gc<mrb_value>(mrb, v));
}
MrubyRef::~MrubyRef(){
}
bool MrubyRef::is_living() const{
return MrubyBindStatus::is_living(mrb);
}
mrb_state* MrubyRef::get_mrb() const{
return this->mrb;
}
mrb_value MrubyRef::get_v() const{
if(v.get()){
return *(this->v.get());
}
return mrb_nil_value();
}
bool MrubyRef::empty() const{
if(!v.get()){
return true;
}
return false;
}
bool MrubyRef::test() const{
if(v.get()){
mrb_value v = *(this->v.get());
return mrb_test(v);
}
return false;
}
bool MrubyRef::obj_equal(const MrubyRef& r) const{
if(!empty() && !r.empty()){
return mrb_obj_equal(mrb, get_v(), r.get_v());
}
else{
return empty() == r.empty();
}
}
std::string MrubyRef::to_s() const{
if(v.get()){
MrubyArenaStore mas(mrb);
mrb_value v = *(this->v.get());
if(mrb_string_p(v)){
return std::string(RSTRING_PTR(v), RSTRING_LEN(v));
}
else{
v = mrb_funcall(mrb, v, "to_s", 0);
return std::string(RSTRING_PTR(v), RSTRING_LEN(v));
}
}
else{
return "";
}
}
int MrubyRef::to_i() const{
if(v.get()){
mrb_value v = *(this->v.get());
if(mrb_fixnum_p(v)){
return mrb_fixnum(v);
}
else{
v = mrb_funcall(mrb, v, "to_i", 0);
return mrb_fixnum(v);
}
}
else{
return 0;
}
}
float MrubyRef::to_float() const{
if(v.get()){
mrb_value v = *(this->v.get());
if(mrb_float_p(v)){
return mrb_float(v);
}
else{
v = mrb_funcall(mrb, v, "to_f", 0);
return mrb_float(v);
}
}
else{
return 0.0f;
}
}
double MrubyRef::to_double() const{
if(v.get()){
mrb_value v = *(this->v.get());
if(mrb_float_p(v)){
return mrb_float(v);
}
else{
v = mrb_funcall(mrb, v, "to_f", 0);
return (double)mrb_float(v);
}
}
else{
return 0.0;
}
}
MrubyRef MrubyRef::call(std::string name){
MrubyArenaStore mas(mrb);
return MrubyRef(mrb, mrb_funcall(mrb, *(this->v.get()), name.c_str(), 0));
}
} // namespace mrubybind