Skip to content

Commit e92eee2

Browse files
authored
Merge pull request ohai#30 from smalruby/smalruby/ruby-3.4-support
feat: Ruby 3.4 support — migrate from Data_Wrap_Struct to TypedData
2 parents 6ebccec + 9e08034 commit e92eee2

File tree

8 files changed

+121
-68
lines changed

8 files changed

+121
-68
lines changed

event.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ static VALUE event_type_to_class[SDL_LASTEVENT];
8686
* timestamp of the event
8787
* @return [Integer]
8888
*/
89+
DEFINE_DATA_TYPE(SDL_Event, free);
90+
8991
static VALUE Event_new(SDL_Event* ev)
9092
{
91-
SDL_Event* e = ALLOC(SDL_Event);
93+
SDL_Event* e;
94+
VALUE obj = TypedData_Make_Struct(event_type_to_class[ev->type], SDL_Event, &SDL_Event_data_type, e);
9295
*e = *ev;
93-
return Data_Wrap_Struct(event_type_to_class[ev->type], 0, free, e);
96+
return obj;
9497
}
9598

9699
static VALUE Event_s_allocate(VALUE klass)
@@ -99,11 +102,12 @@ static VALUE Event_s_allocate(VALUE klass)
99102
VALUE event_type = rb_iv_get(klass, "event_type");
100103
if (event_type == Qnil)
101104
rb_raise(rb_eArgError, "Cannot allocate %s", rb_class2name(klass));
102-
103-
e = ALLOC(SDL_Event);
104-
memset(e, 0, sizeof(SDL_Event));
105-
e->common.type = NUM2INT(event_type);
106-
return Data_Wrap_Struct(klass, 0, free, e);
105+
106+
{
107+
VALUE obj = TypedData_Make_Struct(klass, SDL_Event, &SDL_Event_data_type, e);
108+
e->common.type = NUM2INT(event_type);
109+
return obj;
110+
}
107111
}
108112

109113
/*
@@ -176,15 +180,15 @@ static void set_string(char* field, VALUE str, int maxlength)
176180
static VALUE Ev##classname##_##name(VALUE self) \
177181
{ \
178182
SDL_Event* ev; \
179-
Data_Get_Struct(self, SDL_Event, ev); \
183+
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev); \
180184
return c2ruby(ev->field); \
181185
} \
182186

183187
#define EVENT_WRITER(classname, name, field, ruby2c) \
184188
static VALUE Ev##classname##_set_##name(VALUE self, VALUE val) \
185189
{ \
186190
SDL_Event* ev; \
187-
Data_Get_Struct(self, SDL_Event, ev); \
191+
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev); \
188192
ev->field = ruby2c(val); \
189193
return Qnil; \
190194
}
@@ -214,7 +218,7 @@ EVENT_ACCESSOR_UINT(Event, timestamp, common.timestamp);
214218
/* @return [String] inspection string */
215219
static VALUE Event_inspect(VALUE self)
216220
{
217-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
221+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
218222
return rb_sprintf("<%s: type=%u timestamp=%u>",
219223
rb_obj_classname(self), ev->common.type, ev->common.timestamp);
220224
}
@@ -303,7 +307,7 @@ EVENT_ACCESSOR_INT(Window, data2, window.data2);
303307
/* @return [String] inspection string */
304308
static VALUE EvWindow_inspect(VALUE self)
305309
{
306-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
310+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
307311
return rb_sprintf("<%s: type=%u timestamp=%u window_id=%u event=%u data1=%d data2=%d>",
308312
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
309313
ev->window.windowID, ev->window.event,
@@ -357,7 +361,7 @@ EVENT_ACCESSOR_UINT(Keyboard, mod, key.keysym.mod);
357361
/* @return [String] inspection string */
358362
static VALUE EvKeyboard_inspect(VALUE self)
359363
{
360-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
364+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
361365
return rb_sprintf("<%s: type=%u timestamp=%u"
362366
" window_id=%u state=%u repeat=%u"
363367
" scancode=%u sym=%u mod=%u>",
@@ -407,15 +411,15 @@ EVENT_READER(TextEditing, text, edit.text, utf8str_new_cstr);
407411
static VALUE EvTextEditing_set_text(VALUE self, VALUE str)
408412
{
409413
SDL_Event* ev;
410-
Data_Get_Struct(self, SDL_Event, ev);
414+
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
411415
set_string(ev->edit.text, str, 30);
412416
return str;
413417
}
414418

415419
/* @return [String] inspection string */
416420
static VALUE EvTextEditing_inspect(VALUE self)
417421
{
418-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
422+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
419423
return rb_sprintf("<%s: type=%u timestamp=%u"
420424
" window_id=%u text=%s start=%d length=%d>",
421425
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -441,15 +445,15 @@ EVENT_READER(TextInput, text, text.text, utf8str_new_cstr);
441445
static VALUE EvTextInput_set_text(VALUE self, VALUE str)
442446
{
443447
SDL_Event* ev;
444-
Data_Get_Struct(self, SDL_Event, ev);
448+
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
445449
set_string(ev->text.text, str, 30);
446450
return str;
447451
}
448452

449453
/* @return [String] inspection string */
450454
static VALUE EvTextInput_inspect(VALUE self)
451455
{
452-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
456+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
453457
return rb_sprintf("<%s: type=%u timestamp=%u window_id=%u text=%s>",
454458
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
455459
ev->text.windowID, ev->text.text);
@@ -507,7 +511,7 @@ EVENT_ACCESSOR_INT(MouseButton, y, button.y);
507511
/* @return [String] inspection string */
508512
static VALUE EvMouseButton_inspect(VALUE self)
509513
{
510-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
514+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
511515
return rb_sprintf("<%s: type=%u timestamp=%u"
512516
" window_id=%u which=%u button=%hhu pressed=%s"
513517
#if SDL_VERSION_ATLEAST(2,0,2)
@@ -576,7 +580,7 @@ EVENT_ACCESSOR_INT(MouseMotion, yrel, motion.yrel);
576580
/* @return [String] inspection string */
577581
static VALUE EvMouseMotion_inspect(VALUE self)
578582
{
579-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
583+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
580584
return rb_sprintf("<%s: type=%u timestamp=%u"
581585
" window_id=%u which=%u state=%u"
582586
" x=%d y=%d xrel=%d yrel=%d>",
@@ -615,7 +619,7 @@ EVENT_ACCESSOR_INT(MouseWheel, y, wheel.y);
615619
/* @return [String] inspection string */
616620
static VALUE EvMouseWheel_inspect(VALUE self)
617621
{
618-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
622+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
619623
return rb_sprintf("<%s: type=%u timestamp=%u"
620624
" window_id=%u which=%u x=%d y=%d>",
621625
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -651,7 +655,7 @@ EVENT_ACCESSOR_BOOL(JoyButton, pressed, jbutton.state);
651655
/* @return [String] inspection string */
652656
static VALUE EvJoyButton_inspect(VALUE self)
653657
{
654-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
658+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
655659
return rb_sprintf("<%s: type=%u timestamp=%u"
656660
" which=%d button=%u pressed=%s>",
657661
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -693,7 +697,7 @@ EVENT_ACCESSOR_INT(JoyAxisMotion, value, jaxis.value);
693697
/* @return [String] inspection string */
694698
static VALUE EvJoyAxisMotion_inspect(VALUE self)
695699
{
696-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
700+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
697701
return rb_sprintf("<%s: type=%u timestamp=%u"
698702
" which=%d axis=%u value=%d>",
699703
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -728,7 +732,7 @@ EVENT_ACCESSOR_INT(JoyBallMotion, yrel, jball.yrel);
728732
/* @return [String] inspection string */
729733
static VALUE EvJoyBallMotion_inspect(VALUE self)
730734
{
731-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
735+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
732736
return rb_sprintf("<%s: type=%u timestamp=%u"
733737
" which=%d ball=%u xrel=%d yrel=%d>",
734738
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -758,7 +762,7 @@ EVENT_ACCESSOR_UINT8(JoyHatMotion, value, jhat.value);
758762
/* @return [String] inspection string */
759763
static VALUE EvJoyHatMotion_inspect(VALUE self)
760764
{
761-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
765+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
762766
return rb_sprintf("<%s: type=%u timestamp=%u which=%d hat=%u value=%u>",
763767
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
764768
ev->jhat.which, ev->jhat.hat, ev->jhat.value);
@@ -775,7 +779,7 @@ EVENT_ACCESSOR_INT(JoyDevice, which, jdevice.which);
775779
/* @return [String] inspection string */
776780
static VALUE EvJoyDevice_inspect(VALUE self)
777781
{
778-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
782+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
779783
return rb_sprintf("<%s: type=%u timestamp=%u which=%d>",
780784
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
781785
ev->jdevice.which);
@@ -816,7 +820,7 @@ EVENT_ACCESSOR_INT(ControllerAxis, value, caxis.value);
816820
/* @return [String] inspection string */
817821
static VALUE ControllerAxis_inspect(VALUE self)
818822
{
819-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
823+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
820824
return rb_sprintf("<%s: type=%u timestamp=%u"
821825
" which=%d axis=%s value=%d>",
822826
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -853,7 +857,7 @@ EVENT_ACCESSOR_BOOL(ControllerButton, pressed, cbutton.state);
853857
/* @return [String] inspection string */
854858
static VALUE ControllerButton_inspect(VALUE self)
855859
{
856-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
860+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
857861
return rb_sprintf("<%s: type=%u timestamp=%u"
858862
" which=%d button=%s state=%s>",
859863
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
@@ -893,7 +897,7 @@ EVENT_ACCESSOR_INT(ControllerDevice, which, cdevice.which);
893897
/* @return [String] inspection string */
894898
static VALUE ControllerDevice_inspect(VALUE self)
895899
{
896-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
900+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
897901
return rb_sprintf("<%s: type=%u timestamp=%u which=%d>",
898902
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
899903
ev->cdevice.which);
@@ -953,7 +957,7 @@ EVENT_ACCESSOR_DBL(TouchFinger, pressure, tfinger.pressure);
953957
/* @return [String] inspection string */
954958
static VALUE EvTouchFinger_inspect(VALUE self)
955959
{
956-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
960+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
957961
return rb_sprintf("<%s: type=%u timestamp=%u"
958962
" touch_id=%d finger_id=%d"
959963
" x=%f y=%f pressure=%f>",
@@ -990,7 +994,7 @@ EVENT_ACCESSOR_DBL(FingerMotion, dy, tfinger.dy);
990994
/* @return [String] inspection string */
991995
static VALUE EvFingerMotion_inspect(VALUE self)
992996
{
993-
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
997+
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
994998
return rb_sprintf("<%s: type=%u timestamp=%u"
995999
" touch_id=%d finger_id=%d"
9961000
" x=%f y=%f pressure=%f"

gamecontroller.c.m4

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static void GameController_free(GameController* g)
1717
free(g);
1818
}
1919

20+
DEFINE_DATA_TYPE(GameController, GameController_free);
21+
2022
/*
2123
* Document-class: SDL2::GameController
2224
*
@@ -60,9 +62,10 @@ static void GameController_free(GameController* g)
6062

6163
static VALUE GameController_new(SDL_GameController* controller)
6264
{
63-
GameController* g = ALLOC(GameController);
65+
GameController* g;
66+
VALUE obj = TypedData_Make_Struct(cGameController, GameController, &GameController_data_type, g);
6467
g->controller = controller;
65-
return Data_Wrap_Struct(cGameController, 0, GameController_free, g);
68+
return obj;
6669
}
6770

6871
DEFINE_WRAPPER(SDL_GameController, GameController, controller, cGameController,

gl.c.m4

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ typedef struct GLContext {
1212
SDL_GLContext context;
1313
} GLContext;
1414

15-
DEFINE_WRAPPER(SDL_GLContext, GLContext, context, cGLContext, "SDL2::GL::Context");
16-
1715
static void GLContext_free(GLContext* c)
1816
{
1917
if (c->context)
2018
SDL_GL_DeleteContext(c->context);
2119
free(c);
2220
}
2321

22+
DEFINE_DATA_TYPE(GLContext, GLContext_free);
23+
24+
DEFINE_WRAPPER(SDL_GLContext, GLContext, context, cGLContext, "SDL2::GL::Context");
25+
2426
static VALUE GLContext_new(SDL_GLContext context)
2527
{
26-
GLContext* c = ALLOC(GLContext);
28+
GLContext* c;
29+
VALUE obj = TypedData_Make_Struct(cGLContext, GLContext, &GLContext_data_type, c);
2730
c->context = context;
28-
return Data_Wrap_Struct(cGLContext, 0, GLContext_free, c);
31+
return obj;
2932
}
3033

3134
/*

joystick.c.m4

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ static void Joystick_free(Joystick* j)
1818
free(j);
1919
}
2020

21+
DEFINE_DATA_TYPE(Joystick, Joystick_free);
22+
2123
static VALUE Joystick_new(SDL_Joystick* joystick)
2224
{
23-
Joystick* j = ALLOC(Joystick);
25+
Joystick* j;
26+
VALUE obj = TypedData_Make_Struct(cJoystick, Joystick, &Joystick_data_type, j);
2427
j->joystick = joystick;
25-
return Data_Wrap_Struct(cJoystick, 0, Joystick_free, j);
28+
return obj;
2629
}
2730

2831
DEFINE_WRAPPER(SDL_Joystick, Joystick, joystick, cJoystick, "SDL2::Joystick");

mixer.c.m4

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,40 @@ typedef struct Music {
2727

2828
static void Chunk_free(Chunk* c)
2929
{
30-
if (rubysdl2_is_active() && c->chunk)
30+
if (rubysdl2_is_active() && c->chunk)
3131
Mix_FreeChunk(c->chunk);
3232
free(c);
3333
}
3434

35+
DEFINE_DATA_TYPE(Chunk, Chunk_free);
36+
3537
static VALUE Chunk_new(Mix_Chunk* chunk)
3638
{
37-
Chunk* c = ALLOC(Chunk);
39+
Chunk* c;
40+
VALUE obj = TypedData_Make_Struct(cChunk, Chunk, &Chunk_data_type, c);
3841
c->chunk = chunk;
39-
return Data_Wrap_Struct(cChunk, 0, Chunk_free, c);
42+
return obj;
4043
}
4144

4245
DEFINE_WRAPPER(Mix_Chunk, Chunk, chunk, cChunk, "SDL2::Mixer::Chunk");
4346

4447
static void Music_free(Music* m)
4548
{
46-
if (rubysdl2_is_active() && m->music)
49+
if (rubysdl2_is_active() && m->music)
4750
Mix_FreeMusic(m->music);
4851
free(m);
4952
}
5053

54+
DEFINE_DATA_TYPE(Music, Music_free);
55+
5156
static VALUE Music_new(Mix_Music* music)
5257
{
53-
Music* c = ALLOC(Music);
58+
Music* c;
59+
VALUE obj = TypedData_Make_Struct(cMusic, Music, &Music_data_type, c);
5460
c->music = music;
55-
return Data_Wrap_Struct(cMusic, 0, Music_free, c);
61+
return obj;
5662
}
57-
63+
5864
DEFINE_WRAPPER(Mix_Music, Music, music, cMusic, "SDL2::Mixer::Music");
5965

6066
/*

rubysdl2_internal.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,25 @@ void rubysdl2_init_gamecontorller(void);
7171
#define UCHAR2NUM UINT2NUM
7272
#define rb_str_export_to_utf8(str) rb_str_export_to_enc((str), rb_utf8_encoding())
7373

74+
/* Helper macro to define a rb_data_type_t for TypedData.
75+
* Usage: DEFINE_DATA_TYPE(struct_name, free_func)
76+
* Defines: static const rb_data_type_t struct_name##_data_type
77+
*/
78+
#define DEFINE_DATA_TYPE(struct_name, free_func) \
79+
static const rb_data_type_t struct_name##_data_type = { \
80+
"ruby-sdl2/" #struct_name, \
81+
{ NULL, (void (*)(void*))(free_func), NULL }, \
82+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY \
83+
};
84+
7485
#define DEFINE_GETTER(scope, ctype, var_class, classname) \
7586
scope ctype* Get_##ctype(VALUE obj) \
7687
{ \
7788
ctype* s; \
7889
if (!rb_obj_is_kind_of(obj, var_class)) \
7990
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", \
8091
rb_obj_classname(obj), classname); \
81-
Data_Get_Struct(obj, ctype, s); \
92+
TypedData_Get_Struct(obj, ctype, &ctype##_data_type, s); \
8293
\
8394
return s; \
8495
}

ttf.c.m4

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ static void TTF_free(TTF* f)
4343
free(f);
4444
}
4545

46+
DEFINE_DATA_TYPE(TTF, TTF_free);
47+
4648
static VALUE TTF_new(TTF_Font* font)
4749
{
48-
TTF* f = ALLOC(TTF);
50+
TTF* f;
51+
VALUE obj = TypedData_Make_Struct(cTTF, TTF, &TTF_data_type, f);
4952
f->font = font;
50-
return Data_Wrap_Struct(cTTF, 0, TTF_free, f);
53+
return obj;
5154
}
5255

5356
DEFINE_WRAPPER(TTF_Font, TTF, font, cTTF, "SDL2::TTF");

0 commit comments

Comments
 (0)