8181# define RUBY19_OR_LATER 1
8282#endif
8383
84+ #if (defined(RUBY_VERSION ) && RUBY_VERSION >= 20 ) \
85+ || (defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 20 )
86+ # define RUBY20_OR_LATER 1
87+ #endif
88+
8489#if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 19
8590/* Ruby 1.9 defines a number of static functions which use rb_num2long and
8691 * rb_int2big */
103108#endif
104109#if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 22
105110# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
106- # define rb_check_type rb_check_type_stub
107111#endif
108112
109113#ifdef FEAT_GUI_MACVIM
130134# define __OPENTRANSPORTPROVIDERS__
131135#endif
132136
137+ /*
138+ * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
139+ * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
140+ * The old Data_XXX macro family was deprecated on Ruby 2.2.
141+ * Use TypedData_XXX if available.
142+ */
143+ #if defined(TypedData_Wrap_Struct ) && defined(RUBY20_OR_LATER )
144+ # define USE_TYPEDDATA 1
145+ #endif
146+
133147/*
134148 * Backward compatibility for Ruby 1.8 and earlier.
135149 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
@@ -192,11 +206,20 @@ static void ruby_vim_init(void);
192206 */
193207# define rb_assoc_new dll_rb_assoc_new
194208# define rb_cObject (*dll_rb_cObject)
195- # if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER < 22
196- # define rb_check_type dll_rb_check_type
209+ # define rb_check_type dll_rb_check_type
210+ # ifdef USE_TYPEDDATA
211+ # define rb_check_typeddata dll_rb_check_typeddata
197212# endif
198213# define rb_class_path dll_rb_class_path
199- # define rb_data_object_alloc dll_rb_data_object_alloc
214+ # ifdef USE_TYPEDDATA
215+ # if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 23
216+ # define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
217+ # else
218+ # define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
219+ # endif
220+ # else
221+ # define rb_data_object_alloc dll_rb_data_object_alloc
222+ # endif
200223# define rb_define_class_under dll_rb_define_class_under
201224# define rb_define_const dll_rb_define_const
202225# define rb_define_global_function dll_rb_define_global_function
@@ -305,8 +328,19 @@ static VALUE *dll_rb_cObject;
305328VALUE * dll_rb_cSymbol ;
306329VALUE * dll_rb_cTrueClass ;
307330static void (* dll_rb_check_type ) (VALUE ,int );
331+ # ifdef USE_TYPEDDATA
332+ static void * (* dll_rb_check_typeddata ) (VALUE ,const rb_data_type_t * );
333+ # endif
308334static VALUE (* dll_rb_class_path ) (VALUE );
335+ # ifdef USE_TYPEDDATA
336+ # if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 23
337+ static VALUE (* dll_rb_data_typed_object_wrap ) (VALUE , void * , const rb_data_type_t * );
338+ # else
339+ static VALUE (* dll_rb_data_typed_object_alloc ) (VALUE , void * , const rb_data_type_t * );
340+ # endif
341+ # else
309342static VALUE (* dll_rb_data_object_alloc ) (VALUE , void * , RUBY_DATA_FUNC , RUBY_DATA_FUNC );
343+ # endif
310344static VALUE (* dll_rb_define_class_under ) (VALUE , const char * , VALUE );
311345static void (* dll_rb_define_const ) (VALUE ,const char * ,VALUE );
312346static void (* dll_rb_define_global_function ) (const char * ,VALUE (* )(),int );
@@ -459,13 +493,6 @@ void rb_gc_writebarrier_unprotect_stub(VALUE obj)
459493# endif
460494# endif
461495
462- # if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 22
463- void rb_check_type_stub (VALUE v , int i )
464- {
465- dll_rb_check_type (v , i );
466- }
467- # endif
468-
469496static HINSTANCE hinstRuby = NULL ; /* Instance of ruby.dll */
470497
471498/*
@@ -488,8 +515,19 @@ static struct
488515 {"rb_cSymbol" , (RUBY_PROC * )& dll_rb_cSymbol },
489516 {"rb_cTrueClass" , (RUBY_PROC * )& dll_rb_cTrueClass },
490517 {"rb_check_type" , (RUBY_PROC * )& dll_rb_check_type },
518+ # ifdef USE_TYPEDDATA
519+ {"rb_check_typeddata" , (RUBY_PROC * )& dll_rb_check_typeddata },
520+ # endif
491521 {"rb_class_path" , (RUBY_PROC * )& dll_rb_class_path },
522+ # ifdef USE_TYPEDDATA
523+ # if defined(DYNAMIC_RUBY_VER ) && DYNAMIC_RUBY_VER >= 23
524+ {"rb_data_typed_object_wrap" , (RUBY_PROC * )& dll_rb_data_typed_object_wrap },
525+ # else
526+ {"rb_data_typed_object_alloc" , (RUBY_PROC * )& dll_rb_data_typed_object_alloc },
527+ # endif
528+ # else
492529 {"rb_data_object_alloc" , (RUBY_PROC * )& dll_rb_data_object_alloc },
530+ # endif
493531 {"rb_define_class_under" , (RUBY_PROC * )& dll_rb_define_class_under },
494532 {"rb_define_const" , (RUBY_PROC * )& dll_rb_define_const },
495533 {"rb_define_global_function" , (RUBY_PROC * )& dll_rb_define_global_function },
@@ -1034,6 +1072,24 @@ static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
10341072#endif
10351073}
10361074
1075+ #ifdef USE_TYPEDDATA
1076+ static size_t buffer_dsize (const void * buf );
1077+
1078+ static const rb_data_type_t buffer_type = {
1079+ "vim_buffer" ,
1080+ {0 , 0 , buffer_dsize , {0 , 0 }},
1081+ 0 , 0 ,
1082+ # ifdef RUBY_TYPED_FREE_IMMEDIATELY
1083+ 0 ,
1084+ # endif
1085+ };
1086+
1087+ static size_t buffer_dsize (const void * buf UNUSED )
1088+ {
1089+ return sizeof (buf_T );
1090+ }
1091+ #endif
1092+
10371093static VALUE buffer_new (buf_T * buf )
10381094{
10391095 if (buf -> b_ruby_ref )
@@ -1042,7 +1098,11 @@ static VALUE buffer_new(buf_T *buf)
10421098 }
10431099 else
10441100 {
1101+ #ifdef USE_TYPEDDATA
1102+ VALUE obj = TypedData_Wrap_Struct (cBuffer , & buffer_type , buf );
1103+ #else
10451104 VALUE obj = Data_Wrap_Struct (cBuffer , 0 , 0 , buf );
1105+ #endif
10461106 buf -> b_ruby_ref = (void * ) obj ;
10471107 rb_hash_aset (objtbl , rb_obj_id (obj ), obj );
10481108 return obj ;
@@ -1053,7 +1113,11 @@ static buf_T *get_buf(VALUE obj)
10531113{
10541114 buf_T * buf ;
10551115
1116+ #ifdef USE_TYPEDDATA
1117+ TypedData_Get_Struct (obj , buf_T , & buffer_type , buf );
1118+ #else
10561119 Data_Get_Struct (obj , buf_T , buf );
1120+ #endif
10571121 if (buf == NULL )
10581122 rb_raise (eDeletedBufferError , "attempt to refer to deleted buffer" );
10591123 return buf ;
@@ -1250,6 +1314,24 @@ static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
12501314 return str ;
12511315}
12521316
1317+ #ifdef USE_TYPEDDATA
1318+ static size_t window_dsize (const void * buf );
1319+
1320+ static const rb_data_type_t window_type = {
1321+ "vim_window" ,
1322+ {0 , 0 , window_dsize , {0 , 0 }},
1323+ 0 , 0 ,
1324+ # ifdef RUBY_TYPED_FREE_IMMEDIATELY
1325+ 0 ,
1326+ # endif
1327+ };
1328+
1329+ static size_t window_dsize (const void * win UNUSED )
1330+ {
1331+ return sizeof (win_T );
1332+ }
1333+ #endif
1334+
12531335static VALUE window_new (win_T * win )
12541336{
12551337 if (win -> w_ruby_ref )
@@ -1258,7 +1340,11 @@ static VALUE window_new(win_T *win)
12581340 }
12591341 else
12601342 {
1343+ #ifdef USE_TYPEDDATA
1344+ VALUE obj = TypedData_Wrap_Struct (cVimWindow , & window_type , win );
1345+ #else
12611346 VALUE obj = Data_Wrap_Struct (cVimWindow , 0 , 0 , win );
1347+ #endif
12621348 win -> w_ruby_ref = (void * ) obj ;
12631349 rb_hash_aset (objtbl , rb_obj_id (obj ), obj );
12641350 return obj ;
@@ -1269,7 +1355,11 @@ static win_T *get_win(VALUE obj)
12691355{
12701356 win_T * win ;
12711357
1358+ #ifdef USE_TYPEDDATA
1359+ TypedData_Get_Struct (obj , win_T , & window_type , win );
1360+ #else
12721361 Data_Get_Struct (obj , win_T , win );
1362+ #endif
12731363 if (win == NULL )
12741364 rb_raise (eDeletedWindowError , "attempt to refer to deleted window" );
12751365 return win ;
0 commit comments