diff options
author | Wayne Meissner <wmeissner@gmail.com> | 2009-10-09 13:06:10 +1000 |
---|---|---|
committer | Wayne Meissner <wmeissner@gmail.com> | 2009-10-09 13:06:10 +1000 |
commit | b86312f1d80443ae8e24ab1bfa64f045ea00f1e6 (patch) | |
tree | 37d2c373511d74d52d05a51332e9c3d28151a453 | |
parent | 0ab5f11400024c4f9804e84a33840ac549a87772 (diff) | |
download | ffi-b86312f1d80443ae8e24ab1bfa64f045ea00f1e6.tar.gz |
Add spec for varargs functions returning struct-by-value. From Scott Brooks <scott@scottbrooks.ca>
-rw-r--r-- | libtest/StructTest.c | 24 | ||||
-rw-r--r-- | spec/ffi/struct_spec.rb | 13 |
2 files changed, 37 insertions, 0 deletions
diff --git a/libtest/StructTest.c b/libtest/StructTest.c index ca55638..675dbd6 100644 --- a/libtest/StructTest.c +++ b/libtest/StructTest.c @@ -30,6 +30,7 @@ #include <stdbool.h> #include <stdint.h> #include <string.h> +#include <stdarg.h> typedef char s8; typedef short s16; @@ -215,3 +216,26 @@ struct_s8s32_s64_ret_s64(struct s8s32 s, long long s64) { return s64; } + +// Pass a char *, copy into buffer length struct +struct struct_string { + char *bytes; + int len; +}; + +struct struct_string +struct_varargs_ret_struct_string(int len, ...) +{ + struct struct_string ss; + va_list vl; + + va_start(vl, len); + + ss.len = len; + ss.bytes = va_arg(vl, char *); + + va_end(vl); + + return ss; +} + diff --git a/spec/ffi/struct_spec.rb b/spec/ffi/struct_spec.rb index d5f1cd8..6deafad 100644 --- a/spec/ffi/struct_spec.rb +++ b/spec/ffi/struct_spec.rb @@ -387,12 +387,18 @@ describe FFI::Struct, ' by value' do class S8S32 < FFI::Struct layout :s8, :char, :s32, :int end + + class StructString < FFI::Struct + layout :bytes, :string, :len, :int + end + attach_function :struct_return_s8s32, [ ], S8S32.by_value attach_function :struct_s8s32_set, [ :char, :int ], S8S32.by_value attach_function :struct_s8s32_get_s8, [ S8S32.by_value ], :char attach_function :struct_s8s32_get_s32, [ S8S32.by_value ], :int attach_function :struct_s8s32_s32_ret_s32, [ S8S32.by_value, :int ], :int attach_function :struct_s8s32_s64_ret_s64, [ S8S32.by_value, :long_long ], :long_long + attach_function :struct_varargs_ret_struct_string, [ :int, :varargs ], StructString.by_value end it 'return using pre-set values' do @@ -430,6 +436,13 @@ describe FFI::Struct, ' by value' do LibTest.struct_s8s32_s64_ret_s64(s, 0xdeadcafebabe).should == 0xdeadcafebabe end + + it 'varargs returning a struct' do + string = "test" + s = LibTest.struct_varargs_ret_struct_string(4, :string, string) + s[:len].should == string.length + s[:bytes].should == string + end end describe FFI::Struct, ' with an array field' do |