mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38322 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
16af9a107e
commit
3e6a624a83
4 changed files with 33 additions and 8 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
||||||
|
Tue Dec 11 19:38:37 2012 Naohisa Goto <ngotogenome@gmail.com>
|
||||||
|
|
||||||
|
* 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@segment7.net>
|
Tue Dec 11 16:57:33 2012 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
* common.mk: Added --pages-dir to rdoc creation. Now doc/ items show
|
* common.mk: Added --pages-dir to rdoc creation. Now doc/ items show
|
||||||
|
|
|
@ -50,16 +50,27 @@ rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type)
|
||||||
return rb_class_new_instance(3, argv, cFiddleFunction);
|
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
|
static VALUE
|
||||||
initialize(int argc, VALUE argv[], VALUE self)
|
initialize(int argc, VALUE argv[], VALUE self)
|
||||||
{
|
{
|
||||||
ffi_cif * cif;
|
ffi_cif * cif;
|
||||||
ffi_type **arg_types;
|
ffi_type **arg_types;
|
||||||
ffi_status result;
|
ffi_status result;
|
||||||
VALUE ptr, args, ret_type, abi;
|
VALUE ptr, args, ret_type, abi, kwds;
|
||||||
int i;
|
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);
|
if(NIL_P(abi)) abi = INT2NUM(FFI_DEFAULT_ABI);
|
||||||
|
|
||||||
Check_Type(args, T_ARRAY);
|
Check_Type(args, T_ARRAY);
|
||||||
|
@ -69,6 +80,8 @@ initialize(int argc, VALUE argv[], VALUE self)
|
||||||
rb_iv_set(self, "@return_type", ret_type);
|
rb_iv_set(self, "@return_type", ret_type);
|
||||||
rb_iv_set(self, "@abi", abi);
|
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);
|
TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif);
|
||||||
|
|
||||||
arg_types = xcalloc(RARRAY_LEN(args) + 1, sizeof(ffi_type *));
|
arg_types = xcalloc(RARRAY_LEN(args) + 1, sizeof(ffi_type *));
|
||||||
|
|
|
@ -290,9 +290,8 @@ module Fiddle
|
||||||
if( !addr )
|
if( !addr )
|
||||||
raise(DLError, "cannot find the function: #{name}()")
|
raise(DLError, "cannot find the function: #{name}()")
|
||||||
end
|
end
|
||||||
f = Function.new(addr, argtype, ctype, CALL_TYPE_TO_ABI[call_type])
|
Function.new(addr, argtype, ctype, CALL_TYPE_TO_ABI[call_type],
|
||||||
f.instance_eval { @name = name }
|
name: name)
|
||||||
f
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new closure wrapper for the +name+ function.
|
# Returns a new closure wrapper for the +name+ function.
|
||||||
|
@ -309,9 +308,7 @@ module Fiddle
|
||||||
define_method(:call, block)
|
define_method(:call, block)
|
||||||
}.new(ctype, argtype, abi)
|
}.new(ctype, argtype, abi)
|
||||||
|
|
||||||
f = Function.new(closure, argtype, ctype, abi)
|
Function.new(closure, argtype, ctype, abi, name: name)
|
||||||
f.instance_eval { @name = name }
|
|
||||||
f
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,11 @@ module Fiddle
|
||||||
assert_equal Function::DEFAULT, func.abi
|
assert_equal Function::DEFAULT, func.abi
|
||||||
end
|
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
|
def test_argument_errors
|
||||||
assert_raises(TypeError) do
|
assert_raises(TypeError) do
|
||||||
Function.new(@libm['sin'], TYPE_DOUBLE, TYPE_DOUBLE)
|
Function.new(@libm['sin'], TYPE_DOUBLE, TYPE_DOUBLE)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue