Nobuyoshi Nakada 2020-06-26 15:27:18 +09:00
parent 40b40523dc
commit 4949df498a
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6
2 changed files with 24 additions and 4 deletions

View File

@ -93,7 +93,7 @@ initialize(int argc, VALUE argv[], VALUE self)
ffi_cif * cif;
ffi_type **arg_types, *rtype;
ffi_status result;
VALUE ptr, args, ret_type, abi, kwds, ary;
VALUE ptr, args, ret_type, abi, kwds;
int i, len;
int nabi;
void *cfunc;
@ -113,14 +113,15 @@ initialize(int argc, VALUE argv[], VALUE self)
Check_Type(args, T_ARRAY);
len = RARRAY_LENINT(args);
Check_Max_Args("args", len);
ary = rb_ary_subseq(args, 0, len);
/* freeze to prevent inconsistency at calling #to_int later */
args = rb_ary_subseq(args, 0, len);
for (i = 0; i < RARRAY_LEN(args); i++) {
VALUE a = RARRAY_AREF(args, i);
int type = NUM2INT(a);
(void)INT2FFI_TYPE(type); /* raise */
if (INT2FIX(type) != a) rb_ary_store(ary, i, INT2FIX(type));
if (INT2FIX(type) != a) rb_ary_store(args, i, INT2FIX(type));
}
OBJ_FREEZE(ary);
OBJ_FREEZE(args);
rb_iv_set(self, "@ptr", ptr);
rb_iv_set(self, "@args", args);

View File

@ -35,6 +35,25 @@ module Fiddle
end
end
def test_argument_type_conversion
type = Struct.new(:int, :call_count) do
def initialize(int)
super(int, 0)
end
def to_int
raise "exhausted" if (self.call_count += 1) > 1
self.int
end
end
type_arg = type.new(TYPE_DOUBLE)
type_result = type.new(TYPE_DOUBLE)
assert_nothing_raised(RuntimeError) do
Function.new(@libm['sin'], [type_arg], type_result)
end
assert_equal(1, type_arg.call_count)
assert_equal(1, type_result.call_count)
end
def test_call
func = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE)
assert_in_delta 1.0, func.call(90 * Math::PI / 180), 0.0001