diff options
author | Vincent Isambart <vincent.isambart@gmail.com> | 2021-02-25 10:30:57 +0900 |
---|---|---|
committer | Vincent Isambart <vincent.isambart@gmail.com> | 2021-02-25 10:30:57 +0900 |
commit | 0df6f5c7d8f08bdf6c413ae8e1e3ff9c111f35c5 (patch) | |
tree | 796b0b7897b960e18b64e67ca8a28f84f228bcb7 /spec | |
parent | 3f862ca78ec4e6c2a5eabca8a5f7780d1774c1a0 (diff) | |
download | ffi-0df6f5c7d8f08bdf6c413ae8e1e3ff9c111f35c5.tar.gz |
Allow to pass callbacks in varargs
Diffstat (limited to 'spec')
-rw-r--r-- | spec/ffi/callback_spec.rb | 8 | ||||
-rw-r--r-- | spec/ffi/fixtures/ClosureTest.c | 12 |
2 files changed, 19 insertions, 1 deletions
diff --git a/spec/ffi/callback_spec.rb b/spec/ffi/callback_spec.rb index 58e5c81..53ff1e4 100644 --- a/spec/ffi/callback_spec.rb +++ b/spec/ffi/callback_spec.rb @@ -49,6 +49,7 @@ module CallbackSpecs callback :cbVrU32, [ ], :uint callback :cbVrL, [ ], :long callback :cbVrUL, [ ], :ulong + callback :cbVrD, [ ], :double callback :cbVrS64, [ ], :long_long callback :cbVrU64, [ ], :ulong_long callback :cbVrP, [], :pointer @@ -86,7 +87,7 @@ module CallbackSpecs attach_variable :pVrS8, :gvar_pointer, :pointer attach_function :testGVarCallbackVrS8, :testClosureVrB, [ :pointer ], :char attach_function :testOptionalCallbackCrV, :testOptionalClosureBrV, [ :cbCrV, :char ], :void - + attach_function :testCallbackVrDva, :testClosureVrDva, [ :double, :varargs ], :double end it "returning :char (0)" do @@ -261,6 +262,11 @@ module CallbackSpecs expect(LibTest.testCallbackVrZ { true }).to be true end + it "returning double" do + pr = proc { 42.0 } + expect(LibTest.testCallbackVrDva(3.0, :cbVrD, pr)).to eq(45.0) + end + it "returning :pointer (nil)" do expect(LibTest.testCallbackVrP { nil }).to be_null end diff --git a/spec/ffi/fixtures/ClosureTest.c b/spec/ffi/fixtures/ClosureTest.c index c477be8..daabe0e 100644 --- a/spec/ffi/fixtures/ClosureTest.c +++ b/spec/ffi/fixtures/ClosureTest.c @@ -6,6 +6,7 @@ #include <stdlib.h> #include <stdbool.h> +#include <stdarg.h> #ifndef _WIN32 # include <pthread.h> #else @@ -13,6 +14,17 @@ # include <process.h> #endif +double testClosureVrDva(double d, ...) { + va_list args; + double (*closure)(void); + + va_start(args, d); + closure = va_arg(args, double (*)(void)); + va_end(args); + + return d + closure(); +} + #define R(T, rtype) rtype testClosureVr##T(rtype (*closure)(void)) { \ return closure != NULL ? (*closure)() : (rtype) 0; \ } |