Merge pull request #58 from cremno/mri-use-rb_data_type_t

MRI: use rb_data_type_t and initialize in definition
This commit is contained in:
Jonas Kulla 2014-08-29 22:17:25 +02:00
commit f7d7b16eaf
14 changed files with 39 additions and 66 deletions

View File

@ -29,19 +29,6 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
void initType(rb_data_type_struct &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() RbData *getRbData()
{ {
return static_cast<RbData*>(shState->bindingData()); return static_cast<RbData*>(shState->bindingData());

View File

@ -62,14 +62,29 @@ void
raiseRbExc(const Exception &exc); raiseRbExc(const Exception &exc);
#define DECL_TYPE(Klass) \ #define DECL_TYPE(Klass) \
extern rb_data_type_struct Klass##Type extern rb_data_type_t Klass##Type
#define DEF_TYPE(Klass) \ /* 2.1 has added a new field (flags) to rb_data_type_t */
rb_data_type_struct Klass##Type #include <ruby/version.h>
#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_struct &type, #define DEF_TYPE_CUSTOMNAME_AND_FREE(Klass, Name, Free) \
const char *name, rb_data_type_t Klass##Type = { \
void (*freeInst)(void*)); 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<Klass>)
#define DEF_TYPE(Klass) DEF_TYPE_CUSTOMNAME(Klass, #Klass)
template<rb_data_type_t *rbType> template<rb_data_type_t *rbType>
static VALUE classAllocate(VALUE klass) static VALUE classAllocate(VALUE klass)
@ -83,8 +98,6 @@ static void freeInstance(void *inst)
delete static_cast<C*>(inst); delete static_cast<C*>(inst);
} }
#define INIT_TYPE(Klass) initType(Klass##Type, #Klass, freeInstance<Klass>)
void void
raiseDisposedAccess(VALUE self); raiseDisposedAccess(VALUE self);
@ -109,7 +122,7 @@ getPrivateData(VALUE self)
template<class C> template<class C>
static inline C * static inline C *
getPrivateDataCheck(VALUE self, const rb_data_type_struct &type) getPrivateDataCheck(VALUE self, const rb_data_type_t &type)
{ {
/* We don't check for disposed here because any disposable /* We don't check for disposed here because any disposable
* property is always also nullable */ * property is always also nullable */
@ -124,7 +137,7 @@ setPrivateData(VALUE self, void *p)
} }
inline VALUE inline VALUE
wrapObject(void *p, const rb_data_type_struct &type, wrapObject(void *p, const rb_data_type_t &type,
VALUE underKlass = rb_cObject) VALUE underKlass = rb_cObject)
{ {
VALUE klass = rb_const_get(underKlass, rb_intern(type.wrap_struct_name)); VALUE klass = rb_const_get(underKlass, rb_intern(type.wrap_struct_name));
@ -137,7 +150,7 @@ wrapObject(void *p, const rb_data_type_struct &type,
inline VALUE inline VALUE
wrapProperty(VALUE self, void *prop, const char *iv, wrapProperty(VALUE self, void *prop, const char *iv,
const rb_data_type_struct &type, const rb_data_type_t &type,
VALUE underKlass = rb_cObject) VALUE underKlass = rb_cObject)
{ {
VALUE propObj = wrapObject(prop, type, underKlass); VALUE propObj = wrapObject(prop, type, underKlass);

View File

@ -427,8 +427,6 @@ INITCOPY_FUN(Bitmap)
void void
bitmapBindingInit() bitmapBindingInit()
{ {
INIT_TYPE(Bitmap);
VALUE klass = rb_define_class("Bitmap", rb_cObject); VALUE klass = rb_define_class("Bitmap", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&BitmapType>); rb_define_alloc_func(klass, classAllocate<&BitmapType>);

View File

@ -169,7 +169,6 @@ INITCOPY_FUN(Rect)
#define INIT_BIND(Klass) \ #define INIT_BIND(Klass) \
{ \ { \
INIT_TYPE(Klass); \
klass = rb_define_class(#Klass, rb_cObject); \ klass = rb_define_class(#Klass, rb_cObject); \
rb_define_alloc_func(klass, classAllocate<&Klass##Type>); \ rb_define_alloc_func(klass, classAllocate<&Klass##Type>); \
rb_define_class_method(klass, "_load", Klass##Load); \ rb_define_class_method(klass, "_load", Klass##Load); \

View File

@ -28,7 +28,16 @@
#include "ruby/encoding.h" #include "ruby/encoding.h"
#include "ruby/intern.h" #include "ruby/intern.h"
DEF_TYPE(FileInt); static void
fileIntFreeInstance(void *inst)
{
SDL_RWops *ops = static_cast<SDL_RWops*>(inst);
SDL_RWclose(ops);
SDL_FreeRW(ops);
}
DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance);
static VALUE static VALUE
fileIntForPath(const char *path) fileIntForPath(const char *path)
@ -100,15 +109,6 @@ RB_METHOD(fileIntBinmode)
return Qnil; return Qnil;
} }
static void
fileIntFreeInstance(void *inst)
{
SDL_RWops *ops = static_cast<SDL_RWops*>(inst);
SDL_RWclose(ops);
SDL_FreeRW(ops);
}
VALUE VALUE
kernelLoadDataInt(const char *filename) kernelLoadDataInt(const char *filename)
{ {
@ -196,8 +196,6 @@ RB_METHOD(_marshalLoad)
void void
fileIntBindingInit() fileIntBindingInit()
{ {
initType(FileIntType, "FileInt", fileIntFreeInstance);
VALUE klass = rb_define_class("FileInt", rb_cIO); VALUE klass = rb_define_class("FileInt", rb_cIO);
rb_define_alloc_func(klass, classAllocate<&FileIntType>); rb_define_alloc_func(klass, classAllocate<&FileIntType>);

View File

@ -260,8 +260,6 @@ RB_METHOD(FontSetDefaultColor)
void void
fontBindingInit() fontBindingInit()
{ {
INIT_TYPE(Font);
VALUE klass = rb_define_class("Font", rb_cObject); VALUE klass = rb_define_class("Font", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&FontType>); rb_define_alloc_func(klass, classAllocate<&FontType>);

View File

@ -59,8 +59,6 @@ DEF_PROP_F(Plane, ZoomY)
void void
planeBindingInit() planeBindingInit()
{ {
INIT_TYPE(Plane);
VALUE klass = rb_define_class("Plane", rb_cObject); VALUE klass = rb_define_class("Plane", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&PlaneType>); rb_define_alloc_func(klass, classAllocate<&PlaneType>);

View File

@ -99,8 +99,6 @@ RB_METHOD(spriteHeight)
void void
spriteBindingInit() spriteBindingInit()
{ {
INIT_TYPE(Sprite);
VALUE klass = rb_define_class("Sprite", rb_cObject); VALUE klass = rb_define_class("Sprite", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&SpriteType>); rb_define_alloc_func(klass, classAllocate<&SpriteType>);

View File

@ -147,8 +147,6 @@ INITCOPY_FUN(Table)
void void
tableBindingInit() tableBindingInit()
{ {
INIT_TYPE(Table);
VALUE klass = rb_define_class("Table", rb_cObject); VALUE klass = rb_define_class("Table", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&TableType>); rb_define_alloc_func(klass, classAllocate<&TableType>);

View File

@ -28,7 +28,7 @@
#include "binding-util.h" #include "binding-util.h"
#include "binding-types.h" #include "binding-types.h"
rb_data_type_struct TilemapAutotilesType; DEF_TYPE_CUSTOMFREE(TilemapAutotiles, RUBY_TYPED_NEVER_FREE);
RB_METHOD(tilemapAutotilesSet) RB_METHOD(tilemapAutotilesSet)
{ {
@ -137,16 +137,12 @@ DEF_PROP_I(Tilemap, OY)
void void
tilemapBindingInit() tilemapBindingInit()
{ {
initType(TilemapAutotilesType, "TilemapAutotiles", 0);
VALUE klass = rb_define_class("TilemapAutotiles", rb_cObject); VALUE klass = rb_define_class("TilemapAutotiles", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&TilemapAutotilesType>); rb_define_alloc_func(klass, classAllocate<&TilemapAutotilesType>);
_rb_define_method(klass, "[]=", tilemapAutotilesSet); _rb_define_method(klass, "[]=", tilemapAutotilesSet);
_rb_define_method(klass, "[]", tilemapAutotilesGet); _rb_define_method(klass, "[]", tilemapAutotilesGet);
INIT_TYPE(Tilemap);
klass = rb_define_class("Tilemap", rb_cObject); klass = rb_define_class("Tilemap", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&TilemapType>); rb_define_alloc_func(klass, classAllocate<&TilemapType>);

View File

@ -29,8 +29,9 @@
#include "binding-util.h" #include "binding-util.h"
#include "binding-types.h" #include "binding-types.h"
DEF_TYPE(TilemapVX); DEF_TYPE_CUSTOMNAME(TilemapVX, "Tilemap");
rb_data_type_struct BitmapArrayType;
DEF_TYPE_CUSTOMFREE(BitmapArray, RUBY_TYPED_NEVER_FREE);
RB_METHOD(tilemapVXInitialize) RB_METHOD(tilemapVXInitialize)
{ {
@ -130,8 +131,6 @@ RB_METHOD(tilemapVXBitmapsGet)
void void
tilemapVXBindingInit() tilemapVXBindingInit()
{ {
initType(TilemapVXType, "Tilemap", freeInstance<TilemapVX>);
VALUE klass = rb_define_class("Tilemap", rb_cObject); VALUE klass = rb_define_class("Tilemap", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&TilemapVXType>); rb_define_alloc_func(klass, classAllocate<&TilemapVXType>);
@ -157,8 +156,6 @@ tilemapVXBindingInit()
INIT_PROP_BIND( TilemapVX, Flags, "passages" ); INIT_PROP_BIND( TilemapVX, Flags, "passages" );
} }
initType(BitmapArrayType, "BitmapArray", 0);
klass = rb_define_class_under(klass, "BitmapArray", rb_cObject); klass = rb_define_class_under(klass, "BitmapArray", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&BitmapArrayType>); rb_define_alloc_func(klass, classAllocate<&BitmapArrayType>);

View File

@ -89,8 +89,6 @@ DEF_PROP_I(Viewport, OY)
void void
viewportBindingInit() viewportBindingInit()
{ {
INIT_TYPE(Viewport);
VALUE klass = rb_define_class("Viewport", rb_cObject); VALUE klass = rb_define_class("Viewport", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&ViewportType>); rb_define_alloc_func(klass, classAllocate<&ViewportType>);

View File

@ -73,8 +73,6 @@ DEF_PROP_I(Window, ContentsOpacity)
void void
windowBindingInit() windowBindingInit()
{ {
INIT_TYPE(Window);
VALUE klass = rb_define_class("Window", rb_cObject); VALUE klass = rb_define_class("Window", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&WindowType>); rb_define_alloc_func(klass, classAllocate<&WindowType>);

View File

@ -26,7 +26,7 @@
#include "bitmap.h" #include "bitmap.h"
DEF_TYPE(WindowVX); DEF_TYPE_CUSTOMNAME(WindowVX, "Window");
void bitmapInitProps(Bitmap *b, VALUE self); void bitmapInitProps(Bitmap *b, VALUE self);
@ -135,9 +135,6 @@ DEF_PROP_B(WindowVX, Pause)
void void
windowVXBindingInit() windowVXBindingInit()
{ {
// FIXME: data type name will end up as "WindowVX"
INIT_TYPE(WindowVX);
VALUE klass = rb_define_class("Window", rb_cObject); VALUE klass = rb_define_class("Window", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&WindowVXType>); rb_define_alloc_func(klass, classAllocate<&WindowVXType>);