Fix major incorrect usage of mrb class definitions causing segfaults

This commit is contained in:
2026-05-05 21:09:52 +01:00
parent d362bbc114
commit 0da950caf9
9 changed files with 393 additions and 152 deletions
+23 -10
View File
@@ -12,8 +12,12 @@
#define HASH_FLOAT(h, key, out) \
do { \
mrb_value v = HASH_GET(h, key); \
if (mrb_fixnum_p(v)) \
if (mrb_float_p(v)) \
out = mrb_float(v); \
else if (mrb_fixnum_p(v)) \
out = (float)mrb_fixnum(v); \
else \
out = 0; \
} while (0)
#define HASH_BOOL(h, key, out) \
@@ -40,11 +44,19 @@ struct Wrapper {
}; \
static mrb_value NAME##_init(mrb_state *mrb, mrb_value self) { \
UNUSED(mrb); \
Wrapper *old = (Wrapper *)DATA_PTR(self); \
if (old) \
mrb_free(mrb, old); \
mrb_data_init(self, nullptr, &NAME##_type); \
Wrapper *s = new Wrapper(); \
FN_INIT; \
DATA_PTR(self) = s; \
DATA_TYPE(self) = &NAME##_type; \
mrb_data_init(self, s, &NAME##_type); \
return self; \
} \
inline int get_id_##NAME(mrb_state *mrb, mrb_value self) { \
Wrapper *wrapper; \
Data_Get_Struct(mrb, self, &NAME##_type, wrapper); \
return wrapper->id; \
}
#define ADD_METHOD(NAME, METHOD, FUNC, ARGS) \
@@ -52,6 +64,7 @@ struct Wrapper {
#define DEF_CLASS(NAME, ARGS) \
RClass *NAME##_class = mrb_define_class(Ruby::mrb, #NAME, Ruby::mrb->object_class); \
MRB_SET_INSTANCE_TT(NAME##_class, MRB_TT_CDATA); \
ADD_METHOD(NAME, "initialize", NAME##_init, ARGS);
#define DEF_MODULE(NAME) \
@@ -60,10 +73,6 @@ struct Wrapper {
#define ADD_MODULE_METHOD(MODULE, METHOD, FUNC, ARGS) \
mrb_define_class_method(Ruby::mrb, MODULE##_module, METHOD, FUNC, ARGS);
inline int get_id(mrb_value self) {
return ((Wrapper *)DATA_PTR(self))->id;
}
namespace Ruby {
inline mrb_state *mrb = mrb_open();
@@ -71,7 +80,7 @@ struct Block {
mrb_value proc;
Block(mrb_value proc) : proc(proc) {
mrb_gc_register(mrb, proc);
mrb_gc_protect(mrb, proc);
}
Block() : proc(mrb_nil_value()) {}
@@ -81,7 +90,7 @@ struct Block {
mrb_gc_unregister(mrb, proc);
proc = new_proc;
if (!mrb_nil_p(proc))
mrb_gc_register(mrb, proc);
mrb_gc_protect(mrb, proc);
}
Block(const Block &) = delete;
@@ -90,7 +99,8 @@ struct Block {
mrb_value call(int argc = 0, mrb_value *argv = nullptr) const {
if (mrb_nil_p(proc))
return mrb_nil_value();
return mrb_funcall(mrb, proc, "call", argc, argv);
mrb_value result = mrb_funcall_argv(mrb, proc, mrb_intern_cstr(mrb, "call"), argc, argv);
return result;
}
~Block() {
@@ -110,7 +120,10 @@ DEF_SYM(bold)
DEF_SYM(italic)
DEF_SYM(underline)
DEF_SYM(position)
DEF_SYM(alpha)
DEF_SYM(fps)
DEF_SYM(click_through)
DEF_SYM(call)
DEF_SYM(x)
DEF_SYM(y)
DEF_SYM(w)