ruby-changes:26265
From: ngoto <ko1@a...>
Date: Tue, 11 Dec 2012 19:41:22 +0900 (JST)
Subject: [ruby-changes:26265] ngoto:r38322 (trunk): * ext/fiddle/function.c (Fiddle::Function.new): new keyword argument :name to set the name attribute.
ngoto 2012-12-11 19:41:00 +0900 (Tue, 11 Dec 2012) New Revision: 38322 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38322 Log: * ext/fiddle/function.c (Fiddle::Function.new): new keyword argument :name to set the name attribute. * ext/fiddle/lib/fiddle/import.rb (import_function, bind_function): set function name by using the :name keyword argument. Re-fixes r38243. [ruby-core:50566] * test/fiddle/test_function.rb (test_name): test for the :name keyword argument and Fiddle::Function#name. Modified files: trunk/ChangeLog trunk/ext/fiddle/function.c trunk/ext/fiddle/lib/fiddle/import.rb trunk/test/fiddle/test_function.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38321) +++ ChangeLog (revision 38322) @@ -1,3 +1,13 @@ +Tue Dec 11 19:38:37 2012 Naohisa Goto <ngotogenome@g...> + + * ext/fiddle/function.c (Fiddle::Function.new): new keyword argument + :name to set the name attribute. + * ext/fiddle/lib/fiddle/import.rb (import_function, bind_function): + set function name by using the :name keyword argument. + Re-fixes r38243. [ruby-core:50566] + * test/fiddle/test_function.rb (test_name): test for the :name keyword + argument and Fiddle::Function#name. + Tue Dec 11 16:57:33 2012 Eric Hodel <drbrain@s...> * common.mk: Added --pages-dir to rdoc creation. Now doc/ items show Index: ext/fiddle/function.c =================================================================== --- ext/fiddle/function.c (revision 38321) +++ ext/fiddle/function.c (revision 38322) @@ -50,16 +50,27 @@ return rb_class_new_instance(3, argv, cFiddleFunction); } +static int +parse_keyword_arg_i(VALUE key, VALUE value, VALUE self) +{ + if (key == ID2SYM(rb_intern("name"))) { + rb_iv_set(self, "@name", value); + } else { + rb_raise(rb_eArgError, "unknown keyword: %"PRIsVALUE, key); + } + return ST_CONTINUE; +} + static VALUE initialize(int argc, VALUE argv[], VALUE self) { ffi_cif * cif; ffi_type **arg_types; ffi_status result; - VALUE ptr, args, ret_type, abi; + VALUE ptr, args, ret_type, abi, kwds; int i; - rb_scan_args(argc, argv, "31", &ptr, &args, &ret_type, &abi); + rb_scan_args(argc, argv, "31:", &ptr, &args, &ret_type, &abi, &kwds); if(NIL_P(abi)) abi = INT2NUM(FFI_DEFAULT_ABI); Check_Type(args, T_ARRAY); @@ -69,6 +80,8 @@ rb_iv_set(self, "@return_type", ret_type); rb_iv_set(self, "@abi", abi); + if (!NIL_P(kwds)) rb_hash_foreach(kwds, parse_keyword_arg_i, self); + TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif); arg_types = xcalloc(RARRAY_LEN(args) + 1, sizeof(ffi_type *)); Index: ext/fiddle/lib/fiddle/import.rb =================================================================== --- ext/fiddle/lib/fiddle/import.rb (revision 38321) +++ ext/fiddle/lib/fiddle/import.rb (revision 38322) @@ -290,9 +290,8 @@ if( !addr ) raise(DLError, "cannot find the function: #{name}()") end - f = Function.new(addr, argtype, ctype, CALL_TYPE_TO_ABI[call_type]) - f.instance_eval { @name = name } - f + Function.new(addr, argtype, ctype, CALL_TYPE_TO_ABI[call_type], + name: name) end # Returns a new closure wrapper for the +name+ function. @@ -309,9 +308,7 @@ define_method(:call, block) }.new(ctype, argtype, abi) - f = Function.new(closure, argtype, ctype, abi) - f.instance_eval { @name = name } - f + Function.new(closure, argtype, ctype, abi, name: name) end end end Index: test/fiddle/test_function.rb =================================================================== --- test/fiddle/test_function.rb (revision 38321) +++ test/fiddle/test_function.rb (revision 38322) @@ -15,6 +15,11 @@ assert_equal Function::DEFAULT, func.abi end + def test_name + func = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE, name: 'sin') + assert_equal 'sin', func.name + end + def test_argument_errors assert_raises(TypeError) do Function.new(@libm['sin'], TYPE_DOUBLE, TYPE_DOUBLE) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/