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 <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()
{
return static_cast<RbData*>(shState->bindingData());

View File

@ -62,14 +62,29 @@ void
raiseRbExc(const Exception &exc);
#define DECL_TYPE(Klass) \
extern rb_data_type_struct Klass##Type
extern rb_data_type_t Klass##Type
#define DEF_TYPE(Klass) \
rb_data_type_struct Klass##Type
/* 2.1 has added a new field (flags) to rb_data_type_t */
#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,
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<Klass>)
#define DEF_TYPE(Klass) DEF_TYPE_CUSTOMNAME(Klass, #Klass)
template<rb_data_type_t *rbType>
static VALUE classAllocate(VALUE klass)
@ -83,8 +98,6 @@ static void freeInstance(void *inst)
delete static_cast<C*>(inst);
}
#define INIT_TYPE(Klass) initType(Klass##Type, #Klass, freeInstance<Klass>)
void
raiseDisposedAccess(VALUE self);
@ -109,7 +122,7 @@ getPrivateData(VALUE self)
template<class 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
* property is always also nullable */
@ -124,7 +137,7 @@ setPrivateData(VALUE self, void *p)
}
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 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
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 propObj = wrapObject(prop, type, underKlass);

View File

@ -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>);

View File

@ -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); \

View File

@ -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<SDL_RWops*>(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<SDL_RWops*>(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>);

View File

@ -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>);

View File

@ -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>);

View File

@ -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>);

View File

@ -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>);

View File

@ -28,7 +28,7 @@
#include "binding-util.h"
#include "binding-types.h"
rb_data_type_struct 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>);

View File

@ -29,8 +29,9 @@
#include "binding-util.h"
#include "binding-types.h"
DEF_TYPE(TilemapVX);
rb_data_type_struct 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<TilemapVX>);
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>);

View File

@ -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>);

View File

@ -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>);

View File

@ -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>);