diff --git a/binding-mri/binding-util.cpp b/binding-mri/binding-util.cpp index 4dc6fc9..d70a039 100644 --- a/binding-mri/binding-util.cpp +++ b/binding-mri/binding-util.cpp @@ -29,19 +29,6 @@ #include #include -void initType(rb_data_type_t &type, - const char *name, - void (*freeInst)(void *)) -{ - type.wrap_struct_name = name; - type.function.dmark = 0; - type.function.dsize = 0; - type.function.dfree = freeInst; - type.function.reserved[0] = - type.function.reserved[1] = 0; - type.parent = 0; -} - RbData *getRbData() { return static_cast(shState->bindingData()); diff --git a/binding-mri/binding-util.h b/binding-mri/binding-util.h index 93fe006..703d525 100644 --- a/binding-mri/binding-util.h +++ b/binding-mri/binding-util.h @@ -64,12 +64,27 @@ raiseRbExc(const Exception &exc); #define DECL_TYPE(Klass) \ extern rb_data_type_t Klass##Type -#define DEF_TYPE(Klass) \ - rb_data_type_t Klass##Type +/* 2.1 has added a new field (flags) to rb_data_type_t */ +#include +#if RUBY_API_VERSION_MINOR > 0 +/* TODO: can mkxp use RUBY_TYPED_FREE_IMMEDIATELY here? */ +#define DEF_TYPE_FLAGS 0 +#else +#define DEF_TYPE_FLAGS +#endif -void initType(rb_data_type_t &type, - const char *name, - void (*freeInst)(void*)); +#define DEF_TYPE_CUSTOMNAME_AND_FREE(Klass, Name, Free) \ + rb_data_type_t Klass##Type = { \ + Name, { 0, Free, 0, { 0, 0 } }, 0, 0, DEF_TYPE_FLAGS \ + } + +#define DEF_TYPE_CUSTOMFREE(Klass, Free) \ + DEF_TYPE_CUSTOMNAME_AND_FREE(Klass, #Klass, Free) + +#define DEF_TYPE_CUSTOMNAME(Klass, Name) \ + DEF_TYPE_CUSTOMNAME_AND_FREE(Klass, Name, freeInstance) + +#define DEF_TYPE(Klass) DEF_TYPE_CUSTOMNAME(Klass, #Klass) template static VALUE classAllocate(VALUE klass) @@ -83,8 +98,6 @@ static void freeInstance(void *inst) delete static_cast(inst); } -#define INIT_TYPE(Klass) initType(Klass##Type, #Klass, freeInstance) - void raiseDisposedAccess(VALUE self); diff --git a/binding-mri/bitmap-binding.cpp b/binding-mri/bitmap-binding.cpp index a5652f8..e7ef5d4 100644 --- a/binding-mri/bitmap-binding.cpp +++ b/binding-mri/bitmap-binding.cpp @@ -427,8 +427,6 @@ INITCOPY_FUN(Bitmap) void bitmapBindingInit() { - INIT_TYPE(Bitmap); - VALUE klass = rb_define_class("Bitmap", rb_cObject); rb_define_alloc_func(klass, classAllocate<&BitmapType>); diff --git a/binding-mri/etc-binding.cpp b/binding-mri/etc-binding.cpp index 91c250f..32ad494 100644 --- a/binding-mri/etc-binding.cpp +++ b/binding-mri/etc-binding.cpp @@ -169,7 +169,6 @@ INITCOPY_FUN(Rect) #define INIT_BIND(Klass) \ { \ - INIT_TYPE(Klass); \ klass = rb_define_class(#Klass, rb_cObject); \ rb_define_alloc_func(klass, classAllocate<&Klass##Type>); \ rb_define_class_method(klass, "_load", Klass##Load); \ diff --git a/binding-mri/filesystem-binding.cpp b/binding-mri/filesystem-binding.cpp index b8100d2..42bcec3 100644 --- a/binding-mri/filesystem-binding.cpp +++ b/binding-mri/filesystem-binding.cpp @@ -28,7 +28,16 @@ #include "ruby/encoding.h" #include "ruby/intern.h" -DEF_TYPE(FileInt); +static void +fileIntFreeInstance(void *inst) +{ + SDL_RWops *ops = static_cast(inst); + + SDL_RWclose(ops); + SDL_FreeRW(ops); +} + +DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance); static VALUE fileIntForPath(const char *path) @@ -100,15 +109,6 @@ RB_METHOD(fileIntBinmode) return Qnil; } -static void -fileIntFreeInstance(void *inst) -{ - SDL_RWops *ops = static_cast(inst); - - SDL_RWclose(ops); - SDL_FreeRW(ops); -} - VALUE kernelLoadDataInt(const char *filename) { @@ -196,8 +196,6 @@ RB_METHOD(_marshalLoad) void fileIntBindingInit() { - initType(FileIntType, "FileInt", fileIntFreeInstance); - VALUE klass = rb_define_class("FileInt", rb_cIO); rb_define_alloc_func(klass, classAllocate<&FileIntType>); diff --git a/binding-mri/font-binding.cpp b/binding-mri/font-binding.cpp index abf0581..c33e138 100644 --- a/binding-mri/font-binding.cpp +++ b/binding-mri/font-binding.cpp @@ -260,8 +260,6 @@ RB_METHOD(FontSetDefaultColor) void fontBindingInit() { - INIT_TYPE(Font); - VALUE klass = rb_define_class("Font", rb_cObject); rb_define_alloc_func(klass, classAllocate<&FontType>); diff --git a/binding-mri/plane-binding.cpp b/binding-mri/plane-binding.cpp index d1426a2..0cbb48f 100644 --- a/binding-mri/plane-binding.cpp +++ b/binding-mri/plane-binding.cpp @@ -59,8 +59,6 @@ DEF_PROP_F(Plane, ZoomY) void planeBindingInit() { - INIT_TYPE(Plane); - VALUE klass = rb_define_class("Plane", rb_cObject); rb_define_alloc_func(klass, classAllocate<&PlaneType>); diff --git a/binding-mri/sprite-binding.cpp b/binding-mri/sprite-binding.cpp index 6dea6f5..830ef87 100644 --- a/binding-mri/sprite-binding.cpp +++ b/binding-mri/sprite-binding.cpp @@ -99,8 +99,6 @@ RB_METHOD(spriteHeight) void spriteBindingInit() { - INIT_TYPE(Sprite); - VALUE klass = rb_define_class("Sprite", rb_cObject); rb_define_alloc_func(klass, classAllocate<&SpriteType>); diff --git a/binding-mri/table-binding.cpp b/binding-mri/table-binding.cpp index 9a29079..97831f2 100644 --- a/binding-mri/table-binding.cpp +++ b/binding-mri/table-binding.cpp @@ -147,8 +147,6 @@ INITCOPY_FUN(Table) void tableBindingInit() { - INIT_TYPE(Table); - VALUE klass = rb_define_class("Table", rb_cObject); rb_define_alloc_func(klass, classAllocate<&TableType>); diff --git a/binding-mri/tilemap-binding.cpp b/binding-mri/tilemap-binding.cpp index a62cca2..58674b9 100644 --- a/binding-mri/tilemap-binding.cpp +++ b/binding-mri/tilemap-binding.cpp @@ -28,7 +28,7 @@ #include "binding-util.h" #include "binding-types.h" -rb_data_type_t TilemapAutotilesType; +DEF_TYPE_CUSTOMFREE(TilemapAutotiles, RUBY_TYPED_NEVER_FREE); RB_METHOD(tilemapAutotilesSet) { @@ -137,16 +137,12 @@ DEF_PROP_I(Tilemap, OY) void tilemapBindingInit() { - initType(TilemapAutotilesType, "TilemapAutotiles", 0); - VALUE klass = rb_define_class("TilemapAutotiles", rb_cObject); rb_define_alloc_func(klass, classAllocate<&TilemapAutotilesType>); _rb_define_method(klass, "[]=", tilemapAutotilesSet); _rb_define_method(klass, "[]", tilemapAutotilesGet); - INIT_TYPE(Tilemap); - klass = rb_define_class("Tilemap", rb_cObject); rb_define_alloc_func(klass, classAllocate<&TilemapType>); diff --git a/binding-mri/tilemapvx-binding.cpp b/binding-mri/tilemapvx-binding.cpp index d87d16b..7e51d2f 100644 --- a/binding-mri/tilemapvx-binding.cpp +++ b/binding-mri/tilemapvx-binding.cpp @@ -29,8 +29,9 @@ #include "binding-util.h" #include "binding-types.h" -DEF_TYPE(TilemapVX); -rb_data_type_t BitmapArrayType; +DEF_TYPE_CUSTOMNAME(TilemapVX, "Tilemap"); + +DEF_TYPE_CUSTOMFREE(BitmapArray, RUBY_TYPED_NEVER_FREE); RB_METHOD(tilemapVXInitialize) { @@ -130,8 +131,6 @@ RB_METHOD(tilemapVXBitmapsGet) void tilemapVXBindingInit() { - initType(TilemapVXType, "Tilemap", freeInstance); - VALUE klass = rb_define_class("Tilemap", rb_cObject); rb_define_alloc_func(klass, classAllocate<&TilemapVXType>); @@ -157,8 +156,6 @@ tilemapVXBindingInit() INIT_PROP_BIND( TilemapVX, Flags, "passages" ); } - initType(BitmapArrayType, "BitmapArray", 0); - klass = rb_define_class_under(klass, "BitmapArray", rb_cObject); rb_define_alloc_func(klass, classAllocate<&BitmapArrayType>); diff --git a/binding-mri/viewport-binding.cpp b/binding-mri/viewport-binding.cpp index 2cd6fd4..612ac16 100644 --- a/binding-mri/viewport-binding.cpp +++ b/binding-mri/viewport-binding.cpp @@ -89,8 +89,6 @@ DEF_PROP_I(Viewport, OY) void viewportBindingInit() { - INIT_TYPE(Viewport); - VALUE klass = rb_define_class("Viewport", rb_cObject); rb_define_alloc_func(klass, classAllocate<&ViewportType>); diff --git a/binding-mri/window-binding.cpp b/binding-mri/window-binding.cpp index 0fddf6c..5df0e7b 100644 --- a/binding-mri/window-binding.cpp +++ b/binding-mri/window-binding.cpp @@ -73,8 +73,6 @@ DEF_PROP_I(Window, ContentsOpacity) void windowBindingInit() { - INIT_TYPE(Window); - VALUE klass = rb_define_class("Window", rb_cObject); rb_define_alloc_func(klass, classAllocate<&WindowType>); diff --git a/binding-mri/windowvx-binding.cpp b/binding-mri/windowvx-binding.cpp index b698a58..d70f02e 100644 --- a/binding-mri/windowvx-binding.cpp +++ b/binding-mri/windowvx-binding.cpp @@ -26,7 +26,7 @@ #include "bitmap.h" -DEF_TYPE(WindowVX); +DEF_TYPE_CUSTOMNAME(WindowVX, "Window"); void bitmapInitProps(Bitmap *b, VALUE self); @@ -135,9 +135,6 @@ DEF_PROP_B(WindowVX, Pause) void windowVXBindingInit() { - // FIXME: data type name will end up as "WindowVX" - INIT_TYPE(WindowVX); - VALUE klass = rb_define_class("Window", rb_cObject); rb_define_alloc_func(klass, classAllocate<&WindowVXType>);