@@ -187,7 +187,7 @@ static VALUE Factory_registered_types_internal(VALUE self)
187187 VALUE uk_mapping = rb_hash_new ();
188188 if (fc -> ukrg ) {
189189 for (int i = 0 ; i < 256 ; i ++ ) {
190- if (fc -> ukrg -> array [i ] != Qnil ) {
190+ if (! NIL_P ( fc -> ukrg -> array [i ]) ) {
191191 rb_hash_aset (uk_mapping , INT2FIX (i - 128 ), fc -> ukrg -> array [i ]);
192192 }
193193 }
@@ -200,72 +200,35 @@ static VALUE Factory_registered_types_internal(VALUE self)
200200 );
201201}
202202
203- static VALUE Factory_register_type ( int argc , VALUE * argv , VALUE self )
203+ static VALUE Factory_register_type_internal ( VALUE self , VALUE rb_ext_type , VALUE ext_module , VALUE options )
204204{
205205 msgpack_factory_t * fc = Factory_get (self );
206206
207- int ext_type ;
208- int flags = 0 ;
209- VALUE ext_module ;
210- VALUE options = Qnil ;
211- VALUE packer_arg , unpacker_arg ;
212- VALUE packer_proc , unpacker_proc ;
207+ Check_Type (rb_ext_type , T_FIXNUM );
213208
214- if ( OBJ_FROZEN ( self ) ) {
215- rb_raise (rb_eFrozenError , "can't modify frozen MessagePack::Factory" );
209+ if ( rb_type ( ext_module ) != T_MODULE && rb_type ( ext_module ) != T_CLASS ) {
210+ rb_raise (rb_eArgError , "expected Module/Class but found %s." , rb_obj_classname ( ext_module ) );
216211 }
217212
218- switch (argc ) {
219- case 2 :
220- /* register_type(0x7f, Time) */
221- packer_arg = ID2SYM (rb_intern ("to_msgpack_ext" ));
222- unpacker_arg = ID2SYM (rb_intern ("from_msgpack_ext" ));
223- break ;
224- case 3 :
225- /* register_type(0x7f, Time, packer: proc-like, unpacker: proc-like) */
226- options = argv [2 ];
227- if (rb_type (options ) != T_HASH ) {
228- rb_raise (rb_eArgError , "expected Hash but found %s." , rb_obj_classname (options ));
229- }
213+ int flags = 0 ;
230214
231- packer_arg = rb_hash_aref (options , ID2SYM (rb_intern ("packer" )));
232- unpacker_arg = rb_hash_aref (options , ID2SYM (rb_intern ("unpacker" )));
233- break ;
234- default :
235- rb_raise (rb_eArgError , "wrong number of arguments (%d for 2..3)" , argc );
215+ VALUE packer_proc = Qnil ;
216+ VALUE unpacker_proc = Qnil ;
217+ if (!NIL_P (options )) {
218+ Check_Type (options , T_HASH );
219+ packer_proc = rb_hash_aref (options , ID2SYM (rb_intern ("packer" )));
220+ unpacker_proc = rb_hash_aref (options , ID2SYM (rb_intern ("unpacker" )));
236221 }
237222
238- if (options != Qnil ) {
239- Check_Type ( options , T_HASH );
223+ if (OBJ_FROZEN ( self ) ) {
224+ rb_raise ( rb_eFrozenError , "can't modify frozen MessagePack::Factory" );
240225 }
241226
242- ext_type = NUM2INT (argv [ 0 ] );
227+ int ext_type = NUM2INT (rb_ext_type );
243228 if (ext_type < -128 || ext_type > 127 ) {
244229 rb_raise (rb_eRangeError , "integer %d too big to convert to `signed char'" , ext_type );
245230 }
246231
247- ext_module = argv [1 ];
248- if (rb_type (ext_module ) != T_MODULE && rb_type (ext_module ) != T_CLASS ) {
249- rb_raise (rb_eArgError , "expected Module/Class but found %s." , rb_obj_classname (ext_module ));
250- }
251-
252- packer_proc = Qnil ;
253- unpacker_proc = Qnil ;
254-
255- if (packer_arg != Qnil ) {
256- packer_proc = rb_funcall (packer_arg , rb_intern ("to_proc" ), 0 );
257- }
258-
259- if (unpacker_arg != Qnil ) {
260- if (rb_type (unpacker_arg ) == T_SYMBOL || rb_type (unpacker_arg ) == T_STRING ) {
261- unpacker_proc = rb_obj_method (ext_module , unpacker_arg );
262- } else if (rb_respond_to (unpacker_arg , rb_intern ("call" ))) {
263- unpacker_proc = unpacker_arg ;
264- } else {
265- unpacker_proc = rb_funcall (unpacker_arg , rb_intern ("method" ), 1 , ID2SYM (rb_intern ("call" )));
266- }
267- }
268-
269232 if (ext_module == rb_cSymbol ) {
270233 if (NIL_P (options ) || RTEST (rb_hash_aref (options , ID2SYM (rb_intern ("packer" ))))) {
271234 fc -> has_symbol_ext_type = true;
@@ -289,8 +252,8 @@ static VALUE Factory_register_type(int argc, VALUE* argv, VALUE self)
289252 }
290253 }
291254
292- msgpack_packer_ext_registry_put (self , & fc -> pkrg , ext_module , ext_type , flags , packer_proc , packer_arg );
293- msgpack_unpacker_ext_registry_put (self , & fc -> ukrg , ext_module , ext_type , flags , unpacker_proc , unpacker_arg );
255+ msgpack_packer_ext_registry_put (self , & fc -> pkrg , ext_module , ext_type , flags , packer_proc );
256+ msgpack_unpacker_ext_registry_put (self , & fc -> ukrg , ext_module , ext_type , flags , unpacker_proc );
294257
295258 return Qnil ;
296259}
@@ -309,5 +272,5 @@ void MessagePack_Factory_module_init(VALUE mMessagePack)
309272 rb_define_method (cMessagePack_Factory , "unpacker" , MessagePack_Factory_unpacker , -1 );
310273
311274 rb_define_private_method (cMessagePack_Factory , "registered_types_internal" , Factory_registered_types_internal , 0 );
312- rb_define_method (cMessagePack_Factory , "register_type " , Factory_register_type , -1 );
275+ rb_define_private_method (cMessagePack_Factory , "register_type_internal " , Factory_register_type_internal , 3 );
313276}
0 commit comments