ruby-changes:13879
From: tenderlove <ko1@a...>
Date: Sat, 7 Nov 2009 07:50:48 +0900 (JST)
Subject: [ruby-changes:13879] Ruby:r25679 (trunk): * ext/dl/cfunc.c (rb_dlcfunc_initialize): cleaning up C macros
tenderlove 2009-11-07 07:50:40 +0900 (Sat, 07 Nov 2009) New Revision: 25679 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25679 Log: * ext/dl/cfunc.c (rb_dlcfunc_initialize): cleaning up C macros * ext/dl/cfunc.c (**): adding documentation * test/dl/test_cfunc.rb (test_set_calltype) testing calltype Modified files: trunk/ext/dl/cfunc.c trunk/test/dl/test_cfunc.rb Index: ext/dl/cfunc.c =================================================================== --- ext/dl/cfunc.c (revision 25678) +++ ext/dl/cfunc.c (revision 25679) @@ -133,7 +133,14 @@ return rb_typeddata_is_kind_of(func, &dlcfunc_data_type); } -VALUE +/* + * call-seq: + * DL::CFunc.new(address, type=DL::TYPE_VOID, name=nil, calltype=:cdecl) + * + * Create a new function that points to +address+ with an optional return type + * of +type+, a name of +name+ and a calltype of +calltype+. + */ +static VALUE rb_dlcfunc_initialize(int argc, VALUE argv[], VALUE self) { VALUE addr, name, type, calltype; @@ -150,13 +157,19 @@ if( data->name ) xfree(data->name); data->ptr = saddr; data->name = sname ? strdup(sname) : 0; - data->type = (type == Qnil) ? DLTYPE_VOID : NUM2INT(type); - data->calltype = (calltype == Qnil) ? CFUNC_CDECL : SYM2ID(calltype); + data->type = NIL_P(type) ? DLTYPE_VOID : NUM2INT(type); + data->calltype = NIL_P(calltype) ? CFUNC_CDECL : SYM2ID(calltype); return Qnil; } -VALUE +/* + * call-seq: + * name => str + * + * Get the name of this function + */ +static VALUE rb_dlcfunc_name(VALUE self) { struct cfunc_data *cfunc; @@ -165,7 +178,14 @@ return cfunc->name ? rb_tainted_str_new2(cfunc->name) : Qnil; } -VALUE +/* + * call-seq: + * cfunc.ctype => num + * + * Get the C function return value type. See DL for a list of constants + * corresponding to this method's return value. + */ +static VALUE rb_dlcfunc_ctype(VALUE self) { struct cfunc_data *cfunc; @@ -174,7 +194,13 @@ return INT2NUM(cfunc->type); } -VALUE +/* + * call-seq: + * cfunc.ctype = type + * + * Set the C function return value type to +type+. + */ +static VALUE rb_dlcfunc_set_ctype(VALUE self, VALUE ctype) { struct cfunc_data *cfunc; @@ -184,7 +210,13 @@ return ctype; } -VALUE +/* + * call-seq: + * cfunc.calltype => symbol + * + * Get the call type of this function. + */ +static VALUE rb_dlcfunc_calltype(VALUE self) { struct cfunc_data *cfunc; @@ -193,7 +225,13 @@ return ID2SYM(cfunc->calltype); } -VALUE +/* + * call-seq: + * cfunc.calltype = symbol + * + * Set the call type for this function. + */ +static VALUE rb_dlcfunc_set_calltype(VALUE self, VALUE sym) { struct cfunc_data *cfunc; @@ -264,7 +302,7 @@ } -VALUE +static VALUE rb_dlcfunc_call(VALUE self, VALUE ary) { struct cfunc_data *cfunc; Index: test/dl/test_cfunc.rb =================================================================== --- test/dl/test_cfunc.rb (revision 25678) +++ test/dl/test_cfunc.rb (revision 25679) @@ -9,6 +9,11 @@ @cf = CFunc.new(@libc[@name], TYPE_VOIDP, @name) end + def test_set_calltype + @cf.calltype = :foo + assert_equal :foo, @cf.calltype + end + def test_new_ptr_type_name assert_equal @name, @cf.name assert @cf.name.tainted?, 'name should be tainted' @@ -47,6 +52,7 @@ def test_to_i assert_equal @cf.to_i, @cf.ptr + assert_equal @libc[@name], @cf.to_i end def test_last_error -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/