Skip to content

Commit b78521f

Browse files
author
Keita Obo
committed
Remove class variable
1 parent 5361d4f commit b78521f

File tree

3 files changed

+64
-55
lines changed

3 files changed

+64
-55
lines changed

examples/main.cc

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ int main() {
5151
mrb_state* mrb = mrb_open();
5252

5353
{
54-
using namespace mrubybind;
55-
initialize(mrb);
56-
bind(mrb, "hoge", hoge);
57-
bind(mrb, "fuga", fuga);
58-
bind(mrb, "piyo", piyo);
59-
bind(mrb, "square", square);
60-
bind(mrb, "add", add);
61-
bind(mrb, "test", test);
54+
mrubybind::MrubyBind b(mrb);
55+
b.bind("hoge", hoge);
56+
b.bind("fuga", fuga);
57+
b.bind("piyo", piyo);
58+
b.bind("square", square);
59+
b.bind("add", add);
60+
b.bind("test", test);
6261
}
6362
mrb_load_string(mrb,
6463
"hoge()\n"
@@ -73,9 +72,9 @@ int main() {
7372
}
7473

7574
{
76-
using namespace mrubybind;
77-
define_class(mrb, "Hoge", new_hoge);
78-
define_class_method(mrb, "Hoge", "hoge", &Hoge::hoge);
75+
mrubybind::MrubyBind b(mrb);
76+
b.define_class("Hoge", new_hoge);
77+
b.define_class_method("Hoge", "hoge", &Hoge::hoge);
7978
}
8079
mrb_load_string(mrb,
8180
"h = Hoge.new(111)\n"

mrubybind.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
#include <mruby/compile.h>
33
#include <mruby/dump.h>
44
#include <mruby/proc.h>
5+
#include <mruby/variable.h>
56

67
namespace mrubybind {
78

89
static
910
#include "mrubybind.dat"
1011

11-
RClass *mod_mrubybind;
12-
1312
static mrb_value call_cfunc(mrb_state *mrb, mrb_value self) {
1413
mrb_value binder;
1514
mrb_value p;
@@ -33,17 +32,23 @@ static mrb_value call_cmethod(mrb_state *mrb, mrb_value self) {
3332
return binderp(mrb, mrb_voidp(o), RSTRING_PTR(p), args, narg);
3433
}
3534

36-
int initialize(mrb_state* mrb) {
37-
mod_mrubybind = mrb_define_module(mrb, "MrubyBind");
38-
mrb_define_module_function(mrb, mod_mrubybind, "call_cfunc", call_cfunc,
39-
ARGS_REQ(2) | ARGS_REST());
40-
mrb_define_module_function(mrb, mod_mrubybind, "call_cmethod", call_cmethod,
41-
ARGS_REQ(3) | ARGS_REST());
42-
int n = mrb_read_irep(mrb, binder);
43-
if (n < 0)
44-
return 0;
45-
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
46-
return 1;
35+
MrubyBind::MrubyBind(mrb_state* mrb) {
36+
this->mrb = mrb;
37+
mrb_sym sym_mrubybind = mrb_intern(mrb, "MrubyBind");
38+
if (mrb_const_defined(mrb, mrb_obj_value(mrb->kernel_module), sym_mrubybind)) {
39+
this->mod_mrubybind = mrb_const_get(mrb, mrb_obj_value(mrb->kernel_module), sym_mrubybind);
40+
} else {
41+
RClass* mrubybind = mrb_define_module(mrb, "MrubyBind");
42+
this->mod_mrubybind = mrb_obj_value(mrubybind);
43+
mrb_define_module_function(mrb, mrubybind, "call_cfunc", call_cfunc,
44+
ARGS_REQ(2) | ARGS_REST());
45+
mrb_define_module_function(mrb, mrubybind, "call_cmethod", call_cmethod,
46+
ARGS_REQ(3) | ARGS_REST());
47+
int n = mrb_read_irep(mrb, binder);
48+
if (n >= 0) {
49+
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
50+
}
51+
}
4752
}
4853

4954
} // namespace mrubybind

mrubybind.h

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,37 +104,42 @@ struct Binder {
104104
//===========================================================================
105105
// Interface
106106

107-
extern RClass *mod_mrubybind;
108-
109-
int initialize(mrb_state* mrb);
110-
111-
// Bind function.
112-
template <class Func>
113-
void bind(mrb_state *mrb, const char* name, Func f) {
114-
mrb_value binder = mrb_voidp_value((void*)Binder<Func>::call);
115-
mrb_value fn = mrb_str_new_cstr(mrb, name);
116-
mrb_value fp = mrb_voidp_value((void*)f);
117-
mrb_funcall(mrb, mrb_obj_value(mod_mrubybind), "define_function", 3, binder, fn, fp);
118-
}
119-
120-
// Bind class.
121-
template <class Func>
122-
void define_class(mrb_state* mrb, const char* class_name, Func f) {
123-
mrb_value binder = mrb_voidp_value((void*)Binder<Func>::call);
124-
mrb_value cn = mrb_str_new_cstr(mrb, class_name);
125-
mrb_value fp = mrb_voidp_value((void*)f);
126-
mrb_funcall(mrb, mrb_obj_value(mod_mrubybind), "create_class", 3, binder, cn, fp);
127-
}
128-
129-
// Bind class method.
130-
template <class Method>
131-
void define_class_method(mrb_state* mrb, const char* class_name, const char* method_name, Method m) {
132-
mrb_value binder = mrb_voidp_value((void*)Binder<Method>::call);
133-
mrb_value cn = mrb_str_new_cstr(mrb, class_name);
134-
mrb_value mn = mrb_str_new_cstr(mrb, method_name);
135-
mrb_value mp = mrb_str_new(mrb, (char*)&m, sizeof(m));
136-
mrb_funcall(mrb, mrb_obj_value(mod_mrubybind), "define_class_method", 4, binder, cn, mn, mp);
137-
}
107+
class MrubyBind {
108+
public:
109+
MrubyBind(mrb_state* mrb);
110+
111+
// Bind function.
112+
template <class Func>
113+
void bind(const char* name, Func f) {
114+
mrb_value binder = mrb_voidp_value((void*)Binder<Func>::call);
115+
mrb_value fn = mrb_str_new_cstr(mrb, name);
116+
mrb_value fp = mrb_voidp_value((void*)f);
117+
mrb_funcall(mrb, mod_mrubybind, "define_function", 3, binder, fn, fp);
118+
}
119+
120+
// Bind class.
121+
template <class Func>
122+
void define_class(const char* class_name, Func f) {
123+
mrb_value binder = mrb_voidp_value((void*)Binder<Func>::call);
124+
mrb_value cn = mrb_str_new_cstr(mrb, class_name);
125+
mrb_value fp = mrb_voidp_value((void*)f);
126+
mrb_funcall(mrb, mod_mrubybind, "create_class", 3, binder, cn, fp);
127+
}
128+
129+
// Bind class method.
130+
template <class Method>
131+
void define_class_method(const char* class_name, const char* method_name, Method m) {
132+
mrb_value binder = mrb_voidp_value((void*)Binder<Method>::call);
133+
mrb_value cn = mrb_str_new_cstr(mrb, class_name);
134+
mrb_value mn = mrb_str_new_cstr(mrb, method_name);
135+
mrb_value mp = mrb_str_new(mrb, (char*)&m, sizeof(m));
136+
mrb_funcall(mrb, mod_mrubybind, "define_class_method", 4, binder, cn, mn, mp);
137+
}
138+
139+
private:
140+
mrb_state* mrb;
141+
mrb_value mod_mrubybind;
142+
};
138143

139144
} // namespace mrubybind
140145

0 commit comments

Comments
 (0)