summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2020-06-26 15:27:18 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-06-26 17:05:27 +0900
commit4949df498a4ca67234c42f00b8e2b513a6a2b6c6 (patch)
tree9b44f70614695d41d1b20dcf9f4681a0fc0df67c
parent40b40523dcf957fa9459c1c1037ad79d73867f83 (diff)
[ruby/fiddle] Fixed typos
https://github.com/ruby/fiddle/commit/a09e66adf4 https://github.com/ruby/fiddle/commit/6cab9b45d6 https://github.com/ruby/fiddle/commit/ab72b19bed
-rw-r--r--ext/fiddle/function.c9
-rw-r--r--test/fiddle/test_function.rb19
2 files changed, 24 insertions, 4 deletions
diff --git a/ext/fiddle/function.c b/ext/fiddle/function.c
index 994402e26b..b624255f27 100644
--- a/ext/fiddle/function.c
+++ b/ext/fiddle/function.c
@@ -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);
diff --git a/test/fiddle/test_function.rb b/test/fiddle/test_function.rb
index 4a6bc6c3d4..60168eb04b 100644
--- a/test/fiddle/test_function.rb
+++ b/test/fiddle/test_function.rb
@@ -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