| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#ifndef __MRUBYMODULE_HPP__ |
| 2 |
|
|
#define __MRUBYMODULE_HPP__ |
| 3 |
|
|
|
| 4 |
|
|
#define START_HANDLE_ERROR \ |
| 5 |
|
|
mrb_value exc = mrb_nil_value(); \ |
| 6 |
|
|
try // Try cover to cleanup before raising into mruby |
| 7 |
|
|
|
| 8 |
|
|
#define HANDLE_ERROR_AND_EXIT throw |
| 9 |
|
|
|
| 10 |
|
|
#define END_HANDLE_ERROR \ |
| 11 |
|
|
catch(mrb_value the_exception) \ |
| 12 |
|
|
{ \ |
| 13 |
|
|
exc = the_exception; \ |
| 14 |
|
|
} \ |
| 15 |
|
|
mrb_exc_raise(mrb, exc); \ |
| 16 |
|
|
return mrb_nil_value(); // This should never run because raise jumps away |
| 17 |
|
|
|
| 18 |
|
|
// additional prototypes for some internalized functions |
| 19 |
|
|
// strangely enough their corresponding set functions are still external? |
| 20 |
|
|
// can remove when we know what the replacements should be |
| 21 |
|
|
extern "C" { |
| 22 |
|
|
MRB_API mrb_value mrb_mod_cv_get(mrb_state *mrb, struct RClass *c, mrb_sym sym); |
| 23 |
|
|
MRB_API mrb_value mrb_vm_const_get(mrb_state *mrb, mrb_sym sym); |
| 24 |
|
|
} |
| 25 |
|
|
|
| 26 |
|
|
template<class TClass> |
| 27 |
|
|
class Class; |
| 28 |
|
|
|
| 29 |
|
|
class Module |
| 30 |
|
|
{ |
| 31 |
|
|
protected: |
| 32 |
|
|
typedef void(*function_definer_t)(mrb_state*, RClass*, const char*, mrb_func_t, mrb_aspec); |
| 33 |
|
|
|
| 34 |
|
|
std::shared_ptr<mrb_state> mrb; |
| 35 |
|
|
RClass* cls; |
| 36 |
|
|
|
| 37 |
|
60 |
Module(std::shared_ptr<mrb_state> mrb) : |
| 38 |
2/4
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 60 times.
✗ Branch 6 not taken.
|
60 |
Module(mrb, "", nullptr) |
| 39 |
|
|
{ |
| 40 |
|
60 |
} |
| 41 |
|
|
|
| 42 |
|
2 |
static mrb_value error_argument_count(mrb_state *mrb, const std::string &class_name, const std::string &func_name, size_t given, size_t expected) { |
| 43 |
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 |
std::stringstream ss; |
| 44 |
9/18
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 2 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 2 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 2 times.
✗ Branch 26 not taken.
|
2 |
ss << "in '" << class_name << "': " << func_name << ": wrong number of arguments (" << given << " for " << expected << ")"; |
| 45 |
|
|
|
| 46 |
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
|
2 |
printf("%s\n", ss.str().c_str()); |
| 47 |
5/10
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 2 times.
✗ Branch 16 not taken.
|
4 |
return mrb_exc_new(mrb, E_ARGUMENT_ERROR, ss.str().c_str(), ss.str().length()); |
| 48 |
|
2 |
} |
| 49 |
|
|
|
| 50 |
|
|
template< typename TRet, typename ... TArgs > |
| 51 |
|
11 |
static mrb_value mruby_func_caller(mrb_state* mrb, mrb_value self) |
| 52 |
|
|
{ |
| 53 |
|
11 |
START_HANDLE_ERROR |
| 54 |
|
|
{ |
| 55 |
|
|
typedef TRet(func_t)(TArgs...); |
| 56 |
|
|
|
| 57 |
|
11 |
RClass* cls = TypeBinder<RClass*>::from_mrb_value(mrb, self); |
| 58 |
|
|
mrb_value* args; |
| 59 |
|
11 |
size_t argc = 0; |
| 60 |
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 |
mrb_get_args(mrb, "*", &args, &argc); |
| 61 |
|
|
|
| 62 |
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 |
mrb_value kernel_val = TypeBinder<RClass*>::to_mrb_value(mrb, mrb->kernel_module); |
| 63 |
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 |
mrb_value nval = mrb_funcall(mrb, kernel_val, "__method__", 0); |
| 64 |
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 |
std::string name = TypeBinder<std::string>::from_mrb_value(mrb, nval); |
| 65 |
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 |
std::string ptr_name = "__funcptr__" + name; |
| 66 |
|
|
|
| 67 |
1/2
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 |
mrb_sym func_ptr_sym = mrb_intern_cstr(mrb, ptr_name.c_str()); |
| 68 |
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 |
mrb_value func_ptr_holder = mrb_mod_cv_get(mrb, cls, func_ptr_sym); |
| 69 |
|
|
|
| 70 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 |
if (argc != sizeof...(TArgs)) |
| 71 |
|
|
{ |
| 72 |
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 |
std::string class_name = TypeBinder<std::string>::from_mrb_value(mrb, mrb_inspect(mrb, self)); |
| 73 |
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 |
HANDLE_ERROR_AND_EXIT error_argument_count(mrb, class_name, name, argc, sizeof...(TArgs)); |
| 74 |
|
1 |
} |
| 75 |
|
|
|
| 76 |
|
10 |
func_t* func = (func_t*)TypeBinder<size_t>::from_mrb_value(mrb, func_ptr_holder); |
| 77 |
|
10 |
std::function<func_t> func_obj(func); |
| 78 |
|
|
try |
| 79 |
|
|
{ |
| 80 |
3/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 5 times.
|
15 |
return mruby_func_called_returner<TRet, TArgs...>::call(mrb, func_obj, args); |
| 81 |
|
|
} |
| 82 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
10 |
catch (const RubyException &e) |
| 83 |
|
|
{ |
| 84 |
3/6
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
|
5 |
HANDLE_ERROR_AND_EXIT mrb_exc_new(mrb, E_RUNTIME_ERROR, e.what(), strlen(e.what())); |
| 85 |
|
|
} |
| 86 |
|
22 |
} |
| 87 |
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
6 |
END_HANDLE_ERROR |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
|
| 91 |
|
|
template< typename TRet, typename TClass, typename ... TArgs > |
| 92 |
|
16 |
static mrb_value mruby_member_func_caller(mrb_state* mrb, mrb_value self) |
| 93 |
|
|
{ |
| 94 |
|
16 |
START_HANDLE_ERROR |
| 95 |
|
|
{ |
| 96 |
|
|
typedef TRet(TClass::*memfuncptr_t)(TArgs...); |
| 97 |
|
16 |
RClass* cls = TypeBinder<RClass*>::from_mrb_value(mrb, self); |
| 98 |
|
|
|
| 99 |
|
|
mrb_value* args; |
| 100 |
|
16 |
size_t argc = 0; |
| 101 |
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
16 |
mrb_get_args(mrb, "*", &args, &argc); |
| 102 |
|
|
|
| 103 |
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
16 |
mrb_value kernel_val = TypeBinder<RClass*>::to_mrb_value(mrb, mrb->kernel_module); |
| 104 |
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
16 |
mrb_value nval = mrb_funcall(mrb, kernel_val, "__method__", 0); |
| 105 |
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
16 |
std::string name = TypeBinder<std::string>::from_mrb_value(mrb, nval); |
| 106 |
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
16 |
std::string ptr_name = "__allocated_funcptr__" + name; |
| 107 |
|
|
|
| 108 |
1/2
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
16 |
mrb_sym func_ptr_sym = mrb_intern_cstr(mrb, ptr_name.c_str()); |
| 109 |
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
16 |
mrb_value func_ptr_holder = mrb_mod_cv_get(mrb, cls, func_ptr_sym); |
| 110 |
|
|
|
| 111 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
|
16 |
if (argc != sizeof...(TArgs)) |
| 112 |
|
|
{ |
| 113 |
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 |
std::string class_name = TypeBinder<std::string>::from_mrb_value(mrb, mrb_inspect(mrb, self)); |
| 114 |
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 |
HANDLE_ERROR_AND_EXIT error_argument_count(mrb, class_name, name, argc, sizeof...(TArgs)); |
| 115 |
|
1 |
} |
| 116 |
|
|
|
| 117 |
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
15 |
NativeObject<memfuncptr_t> obj = TypeBinder< NativeObject<memfuncptr_t> >::from_mrb_value(mrb, func_ptr_holder); |
| 118 |
|
|
|
| 119 |
|
15 |
memfuncptr_t* ptr = obj.get_instance(); |
| 120 |
|
15 |
NativeObject<TClass>* thisptr = (NativeObject<TClass>*)DATA_PTR(self); |
| 121 |
|
|
|
| 122 |
|
|
try |
| 123 |
|
|
{ |
| 124 |
|
28 |
auto callable = [&](TArgs... params) -> TRet |
| 125 |
|
|
{ |
| 126 |
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
|
13 |
return (thisptr->get_instance()->**ptr)(params...); |
| 127 |
|
|
}; |
| 128 |
|
|
|
| 129 |
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
|
30 |
return mruby_func_called_returner<TRet, TArgs...>::call( |
| 130 |
|
|
mrb, |
| 131 |
|
18 |
[=](TArgs... params)->TRet |
| 132 |
|
|
{ |
| 133 |
|
13 |
return callable(params...); |
| 134 |
|
|
}, |
| 135 |
|
14 |
args); |
| 136 |
|
|
} |
| 137 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
2 |
catch (const RubyException &e) |
| 138 |
|
|
{ |
| 139 |
3/6
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
|
1 |
HANDLE_ERROR_AND_EXIT mrb_exc_new(mrb, E_RUNTIME_ERROR, e.what(), strlen(e.what())); |
| 140 |
|
|
} |
| 141 |
|
19 |
} |
| 142 |
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
2 |
END_HANDLE_ERROR |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
template<typename TRet, typename ... TArgs> |
| 146 |
|
15 |
void create_function(const std::string& name, TRet(*func)(TArgs...), RClass* module_class, function_definer_t define_function_method) |
| 147 |
|
|
{ |
| 148 |
|
15 |
const int argcount = sizeof...(TArgs); |
| 149 |
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
|
15 |
std::string ptr_name = "__funcptr__" + name; |
| 150 |
1/2
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
15 |
mrb_sym func_ptr_sym = mrb_intern_cstr(mrb.get(), ptr_name.c_str()); |
| 151 |
1/2
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
|
15 |
mrb_mod_cv_set( |
| 152 |
|
|
mrb.get(), |
| 153 |
|
|
module_class, |
| 154 |
|
|
func_ptr_sym, |
| 155 |
|
|
TypeBinder<size_t>::to_mrb_value(mrb.get(), (size_t)func)); |
| 156 |
|
|
|
| 157 |
1/2
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
15 |
define_function_method( |
| 158 |
|
|
mrb.get(), |
| 159 |
|
|
module_class, |
| 160 |
|
|
name.c_str(), |
| 161 |
|
|
mruby_func_caller<TRet, TArgs...>, |
| 162 |
|
|
MRB_ARGS_REQ(argcount)); |
| 163 |
|
15 |
} |
| 164 |
|
|
|
| 165 |
|
|
template<typename TRet, typename TClass, typename ... TArgs> |
| 166 |
|
22 |
void create_function(const std::string& name, TRet(TClass::*func)(TArgs...), RClass* module_class, function_definer_t define_function_method) |
| 167 |
|
|
{ |
| 168 |
|
|
typedef TRet(TClass::* memfuncptr_t)(TArgs...); |
| 169 |
|
22 |
const int argcount = sizeof...(TArgs); |
| 170 |
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
22 |
std::string ptr_name = "__allocated_funcptr__" + name; |
| 171 |
1/2
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
22 |
mrb_sym func_ptr_sym = mrb_intern_cstr(mrb.get(), ptr_name.c_str()); |
| 172 |
|
|
|
| 173 |
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
22 |
memfuncptr_t* ptr = new memfuncptr_t; |
| 174 |
|
22 |
*ptr = func; |
| 175 |
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
66 |
NativeObject<memfuncptr_t> obj("Object", std::shared_ptr<memfuncptr_t>(ptr)); |
| 176 |
|
|
|
| 177 |
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 20 times.
✗ Branch 10 not taken.
|
22 |
mrb_mod_cv_set( |
| 178 |
|
|
mrb.get(), |
| 179 |
|
|
module_class, |
| 180 |
|
|
func_ptr_sym, |
| 181 |
|
|
TypeBinder< NativeObject<memfuncptr_t> >::to_mrb_value(mrb.get(), obj)); |
| 182 |
|
|
|
| 183 |
1/2
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
22 |
define_function_method( |
| 184 |
|
|
mrb.get(), |
| 185 |
|
|
module_class, |
| 186 |
|
|
name.c_str(), |
| 187 |
|
|
mruby_member_func_caller<TRet, TClass, TArgs...>, |
| 188 |
|
|
MRB_ARGS_REQ(argcount)); |
| 189 |
|
|
|
| 190 |
|
22 |
} |
| 191 |
|
|
|
| 192 |
|
|
template<typename TRet, typename TClass, typename ... TArgs> |
| 193 |
|
|
void create_function(const std::string& name, TRet(TClass::*func)(TArgs...) const, RClass* module_class, function_definer_t define_function_method) |
| 194 |
|
|
{ |
| 195 |
|
|
typedef TRet(TClass::* memfuncptr_t)(TArgs...) const; |
| 196 |
|
|
const int argcount = sizeof...(TArgs); |
| 197 |
|
|
std::string ptr_name = "__allocated_funcptr__" + name; |
| 198 |
|
|
mrb_sym func_ptr_sym = mrb_intern_cstr(mrb.get(), ptr_name.c_str()); |
| 199 |
|
|
|
| 200 |
|
|
memfuncptr_t* ptr = new memfuncptr_t; |
| 201 |
|
|
*ptr = func; |
| 202 |
|
|
NativeObject<memfuncptr_t> obj("Object", std::shared_ptr<memfuncptr_t>(ptr)); |
| 203 |
|
|
|
| 204 |
|
|
mrb_mod_cv_set( |
| 205 |
|
|
mrb.get(), |
| 206 |
|
|
module_class, |
| 207 |
|
|
func_ptr_sym, |
| 208 |
|
|
TypeBinder< NativeObject<memfuncptr_t> >::to_mrb_value(mrb.get(), obj)); |
| 209 |
|
|
|
| 210 |
|
|
define_function_method( |
| 211 |
|
|
mrb.get(), |
| 212 |
|
|
module_class, |
| 213 |
|
|
name.c_str(), |
| 214 |
|
|
mruby_member_func_caller<TRet, TClass, TArgs...>, |
| 215 |
|
|
MRB_ARGS_REQ(argcount)); |
| 216 |
|
|
|
| 217 |
|
|
} |
| 218 |
|
|
private: |
| 219 |
|
|
std::string name; |
| 220 |
|
|
|
| 221 |
|
|
|
| 222 |
|
|
public: |
| 223 |
|
115 |
Module(std::shared_ptr<mrb_state> mrb, const std::string& name, RClass* cls) : |
| 224 |
|
115 |
mrb(mrb), |
| 225 |
|
115 |
cls(cls), |
| 226 |
1/2
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
|
115 |
name(name) |
| 227 |
|
|
{ |
| 228 |
|
|
|
| 229 |
|
115 |
} |
| 230 |
|
|
|
| 231 |
|
230 |
virtual ~Module() |
| 232 |
|
230 |
{ |
| 233 |
|
|
|
| 234 |
|
230 |
} |
| 235 |
|
|
|
| 236 |
|
|
mrb_sym symbol(const std::string& str) |
| 237 |
|
|
{ |
| 238 |
|
|
return mrb_intern_cstr(mrb.get(), str.c_str()); |
| 239 |
|
|
} |
| 240 |
|
|
|
| 241 |
|
5 |
bool thing_is_defined(const std::string& name, mrb_vtype type) |
| 242 |
|
|
{ |
| 243 |
1/2
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
|
5 |
mrb_sym name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 244 |
|
5 |
mrb_value module_object = mrb_obj_value(mrb->object_class); |
| 245 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 |
if (cls != nullptr) |
| 246 |
|
|
{ |
| 247 |
|
✗ |
module_object = mrb_obj_value(cls); |
| 248 |
|
|
} |
| 249 |
|
|
|
| 250 |
3/4
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3 times.
|
5 |
if(mrb_const_defined(mrb.get(), module_object, name_sym) == 0) |
| 251 |
|
|
{ |
| 252 |
|
2 |
return false; |
| 253 |
|
|
} |
| 254 |
|
|
|
| 255 |
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
mrb_value val = mrb_const_get(mrb.get(), module_object, name_sym); |
| 256 |
|
3 |
return mrb_type(val) == type; |
| 257 |
|
|
} |
| 258 |
|
|
|
| 259 |
|
3 |
std::shared_ptr<Module> get_class(const std::string& name) |
| 260 |
|
|
{ |
| 261 |
3/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
|
3 |
if (!thing_is_defined(name, MRB_TT_CLASS)) |
| 262 |
|
|
{ |
| 263 |
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
3 |
throw NameError("Class does not exist", name); |
| 264 |
|
|
} |
| 265 |
|
2 |
RClass *theclass = NULL; |
| 266 |
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 |
return std::make_shared<Module>(mrb, name, theclass); |
| 267 |
|
|
} |
| 268 |
|
|
|
| 269 |
|
2 |
std::shared_ptr<Module> get_module(const std::string& name) |
| 270 |
|
|
{ |
| 271 |
3/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 |
if (!thing_is_defined(name, MRB_TT_MODULE)) |
| 272 |
|
|
{ |
| 273 |
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
3 |
throw NameError("Module does not exist", name); |
| 274 |
|
|
} |
| 275 |
|
1 |
RClass *theclass = NULL; |
| 276 |
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 |
return std::make_shared<Module>(mrb, name, theclass); |
| 277 |
|
|
} |
| 278 |
|
|
|
| 279 |
|
10 |
std::shared_ptr<Module> create_module(const std::string& name) |
| 280 |
|
|
{ |
| 281 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 |
if (cls == nullptr) |
| 282 |
|
|
{ |
| 283 |
1/2
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
|
9 |
RClass* topmodule = mrb_define_module(mrb.get(), name.c_str()); |
| 284 |
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 |
return std::make_shared<Module>(mrb, name, topmodule); |
| 285 |
|
|
} |
| 286 |
|
|
|
| 287 |
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 |
RClass* submodule = mrb_define_module_under(mrb.get(), cls, name.c_str()); |
| 288 |
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 |
return std::make_shared<Module>(mrb, name, submodule); |
| 289 |
|
|
} |
| 290 |
|
|
|
| 291 |
|
|
static constexpr void* DUMMY_VALUE_TO_PASS_TYPE_TO_CONSTRUCTOR = nullptr; |
| 292 |
|
|
|
| 293 |
|
|
template<typename TClass, typename ... TConstructorArgs> |
| 294 |
|
41 |
std::shared_ptr< Class<TClass> > create_class(const std::string& name) |
| 295 |
|
|
{ |
| 296 |
|
|
typedef void(*typepasser_t)(TConstructorArgs...); |
| 297 |
|
|
|
| 298 |
|
41 |
RClass* rubyclass = nullptr; |
| 299 |
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 1 times.
|
41 |
if (cls == nullptr) |
| 300 |
|
|
{ |
| 301 |
1/2
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
|
40 |
rubyclass = mrb_define_class(mrb.get(), name.c_str(), mrb->object_class); |
| 302 |
|
|
} |
| 303 |
|
|
else |
| 304 |
|
|
{ |
| 305 |
1/2
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 |
rubyclass = mrb_define_class_under(mrb.get(), cls, name.c_str(), mrb->object_class); |
| 306 |
|
|
} |
| 307 |
|
|
|
| 308 |
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
|
82 |
return std::make_shared<Class<TClass>>(mrb, name, rubyclass, (typepasser_t)DUMMY_VALUE_TO_PASS_TYPE_TO_CONSTRUCTOR); |
| 309 |
|
|
} |
| 310 |
|
|
|
| 311 |
|
|
template<typename TClass> |
| 312 |
|
3 |
std::shared_ptr< Class<TClass> > create_closed_class(const std::string& name) |
| 313 |
|
|
{ |
| 314 |
|
3 |
RClass* rubyclass = nullptr; |
| 315 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
3 |
if (cls == nullptr) |
| 316 |
|
|
{ |
| 317 |
1/2
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
3 |
rubyclass = mrb_define_class(mrb.get(), name.c_str(), mrb->object_class); |
| 318 |
|
|
} |
| 319 |
|
|
else |
| 320 |
|
|
{ |
| 321 |
|
✗ |
rubyclass = mrb_define_class_under(mrb.get(), cls, name.c_str(), mrb->object_class); |
| 322 |
|
|
} |
| 323 |
|
|
|
| 324 |
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
6 |
return std::make_shared<Class<TClass>>(mrb, name, rubyclass); |
| 325 |
|
|
} |
| 326 |
|
|
|
| 327 |
|
|
template<typename T> |
| 328 |
|
1 |
void set_class_variable(const std::string& name, T value) |
| 329 |
|
|
{ |
| 330 |
|
1 |
mrb_sym var_name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 331 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (cls == nullptr) |
| 332 |
|
|
{ |
| 333 |
|
1 |
mrb_cv_set(mrb.get(), mrb_obj_value(mrb->object_class), var_name_sym, TypeBinder<T>::to_mrb_value(mrb.get(), value)); |
| 334 |
|
|
} |
| 335 |
|
|
else |
| 336 |
|
|
{ |
| 337 |
|
✗ |
mrb_cv_set(mrb.get(), mrb_obj_value(cls), var_name_sym, TypeBinder<T>::to_mrb_value(mrb.get(), value)); |
| 338 |
|
|
} |
| 339 |
|
1 |
} |
| 340 |
|
|
|
| 341 |
|
|
template<typename T> |
| 342 |
|
2 |
T get_class_variable(const std::string& name) |
| 343 |
|
|
{ |
| 344 |
|
2 |
mrb_sym var_name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 345 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 |
if (cls == nullptr) |
| 346 |
|
|
{ |
| 347 |
|
1 |
return TypeBinder<T>::from_mrb_value(mrb.get(), mrb_cv_get(mrb.get(), mrb_obj_value(mrb->object_class), var_name_sym)); |
| 348 |
|
|
} |
| 349 |
|
|
else |
| 350 |
|
|
{ |
| 351 |
|
1 |
return TypeBinder<T>::from_mrb_value(mrb.get(), mrb_cv_get(mrb.get(), mrb_obj_value(cls), var_name_sym)); |
| 352 |
|
|
} |
| 353 |
|
|
} |
| 354 |
|
|
|
| 355 |
|
|
template<typename T> |
| 356 |
|
1 |
void set_instance_variable(const std::string& name, T value) |
| 357 |
|
|
{ |
| 358 |
|
1 |
mrb_sym var_name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 359 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (cls == nullptr) |
| 360 |
|
|
{ |
| 361 |
|
1 |
mrb_iv_set(mrb.get(), mrb_obj_value(mrb->top_self), var_name_sym, TypeBinder<T>::to_mrb_value(mrb.get(), value)); |
| 362 |
|
|
} |
| 363 |
|
|
else |
| 364 |
|
|
{ |
| 365 |
|
✗ |
mrb_iv_set(mrb.get(), mrb_obj_value(cls), var_name_sym, TypeBinder<T>::to_mrb_value(mrb.get(), value)); |
| 366 |
|
|
} |
| 367 |
|
1 |
} |
| 368 |
|
|
|
| 369 |
|
|
template<typename T> |
| 370 |
|
1 |
T get_instance_variable(const std::string& name) |
| 371 |
|
|
{ |
| 372 |
|
1 |
mrb_sym var_name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 373 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (cls == nullptr) |
| 374 |
|
|
{ |
| 375 |
|
1 |
return TypeBinder<T>::from_mrb_value(mrb.get(), mrb_iv_get(mrb.get(), mrb_obj_value(mrb->top_self), var_name_sym)); |
| 376 |
|
|
} |
| 377 |
|
|
else |
| 378 |
|
|
{ |
| 379 |
|
✗ |
return TypeBinder<T>::from_mrb_value(mrb.get(), mrb_iv_get(mrb.get(), mrb_obj_value(cls), var_name_sym)); |
| 380 |
|
|
} |
| 381 |
|
|
} |
| 382 |
|
|
|
| 383 |
|
|
template<typename T> |
| 384 |
|
5 |
void set_global_variable(const std::string& name, T value) |
| 385 |
|
|
{ |
| 386 |
|
5 |
mrb_sym var_name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 387 |
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
|
5 |
mrb_gv_set(mrb.get(), var_name_sym, TypeBinder<T>::to_mrb_value(mrb.get(), value)); |
| 388 |
|
5 |
} |
| 389 |
|
|
|
| 390 |
|
|
template<typename T> |
| 391 |
|
32 |
T get_global_variable(const std::string& name) |
| 392 |
|
|
{ |
| 393 |
|
32 |
mrb_sym var_name_sym = mrb_intern_cstr(mrb.get(), name.c_str()); |
| 394 |
|
32 |
return TypeBinder<T>::from_mrb_value(mrb.get(), mrb_gv_get(mrb.get(), var_name_sym)); |
| 395 |
|
|
} |
| 396 |
|
|
|
| 397 |
|
|
template<typename TRet, typename ... TArgs> |
| 398 |
|
15 |
void bind_method(const std::string& name, TRet(*func)(TArgs...)) |
| 399 |
|
|
{ |
| 400 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
|
15 |
if (cls == nullptr) |
| 401 |
|
|
{ |
| 402 |
|
3 |
create_function(name, func, mrb->kernel_module, mrb_define_module_function); |
| 403 |
|
|
} |
| 404 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 |
else if (cls->tt == MRB_TT_MODULE) |
| 405 |
|
|
{ |
| 406 |
|
6 |
create_function(name, func, cls, mrb_define_module_function); |
| 407 |
|
|
} |
| 408 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
else if (cls->tt == MRB_TT_CLASS) |
| 409 |
|
|
{ |
| 410 |
|
6 |
create_function(name, func, cls, mrb_define_class_method); |
| 411 |
|
|
} |
| 412 |
|
15 |
} |
| 413 |
|
|
}; |
| 414 |
|
|
|
| 415 |
|
|
#endif // __MRUBYMODULE_HPP__ |
| 416 |
|
|
|