diff options
-rw-r--r-- | spec/ffi/fixtures/FunctionTest.c | 32 | ||||
-rw-r--r-- | spec/ffi/variadic_spec.rb | 18 |
2 files changed, 50 insertions, 0 deletions
diff --git a/spec/ffi/fixtures/FunctionTest.c b/spec/ffi/fixtures/FunctionTest.c index a37373a..e44e21f 100644 --- a/spec/ffi/fixtures/FunctionTest.c +++ b/spec/ffi/fixtures/FunctionTest.c @@ -11,6 +11,7 @@ #ifndef _WIN32 #include <unistd.h> #include <pthread.h> +#include <stdarg.h> #include <stdlib.h> #endif @@ -61,6 +62,37 @@ void testBlockingClose(struct testBlockingData *self) { free(self); } +char testBlockingWRva(struct testBlockingData *self, char c, ...) { + /* Process argument list but ignore values */ + va_list args; + va_start(args, c); + char arg; + while ((arg = va_arg(args, char)) != 0) { + continue; + } + va_end(args); + + if( pipeHelperWriteChar(self->pipe1[1], c) != 1) + return 0; + return pipeHelperReadChar(self->pipe2[0], 10); +} + +char testBlockingRWva(struct testBlockingData *self, char c, ...) { + /* Process argument list but ignore values */ + va_list args; + va_start(args, c); + char arg; + while ((arg = va_arg(args, char)) != 0) { + continue; + } + va_end(args); + + char d = pipeHelperReadChar(self->pipe1[0], 10); + if( pipeHelperWriteChar(self->pipe2[1], c) != 1) + return 0; + return d; +} + struct async_data { void (*fn)(int); int value; diff --git a/spec/ffi/variadic_spec.rb b/spec/ffi/variadic_spec.rb index 4138280..0b19d16 100644 --- a/spec/ffi/variadic_spec.rb +++ b/spec/ffi/variadic_spec.rb @@ -13,6 +13,11 @@ describe "Function with variadic arguments" do enum :enum_type2, [:c3, 42, :c4] attach_function :pack_varargs, [ :buffer_out, :string, :varargs ], :void attach_function :pack_varargs2, [ :buffer_out, :enum_type1, :string, :varargs ], :enum_type1 + + attach_function :testBlockingOpen, [ ], :pointer + attach_function :testBlockingRWva, [ :pointer, :char, :varargs ], :char, :blocking => true + attach_function :testBlockingWRva, [ :pointer, :char, :varargs ], :char, :blocking => true + attach_function :testBlockingClose, [ :pointer ], :void end it "takes enum arguments" do @@ -27,6 +32,19 @@ describe "Function with variadic arguments" do expect(LibTest.pack_varargs2(buf, :c1, "ii", :int, :c3, :int, :c4)).to eq(:c2) end + it 'can wrap a blocking function with varargs' do + handle = LibTest.testBlockingOpen + expect(handle).not_to be_null + begin + thWR = Thread.new { LibTest.testBlockingWRva(handle, 63, :char, 0) } + thRW = Thread.new { LibTest.testBlockingRWva(handle, 64, :char, 0) } + expect(thWR.value).to eq(64) + expect(thRW.value).to eq(63) + ensure + LibTest.testBlockingClose(handle) + end + end + [ 0, 127, -128, -1 ].each do |i| it "call variadic with (:char (#{i})) argument" do buf = FFI::Buffer.new :long_long |