diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | Gemfile.lock | 26 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | Rakefile | 16 | ||||
-rw-r--r-- | ext/ffi_c/Call.c | 105 | ||||
-rw-r--r-- | ext/ffi_c/libffi/src/x86/win32.S | 4 | ||||
-rw-r--r-- | ffi.gemspec | 2 | ||||
-rw-r--r-- | lib/ffi.rb | 10 | ||||
-rw-r--r-- | lib/ffi/enum.rb | 18 | ||||
-rw-r--r-- | lib/ffi/library.rb | 22 | ||||
-rw-r--r-- | lib/ffi/platform/x86_64-darwin/types.conf | 34 | ||||
-rw-r--r-- | lib/ffi/struct.rb | 6 | ||||
-rw-r--r-- | lib/ffi/version.rb | 2 | ||||
-rw-r--r-- | libtest/EnumTest.c | 17 | ||||
-rw-r--r-- | spec/ffi/enum_spec.rb | 196 | ||||
-rw-r--r-- | spec/ffi/fixtures/EnumTest.c | 17 | ||||
-rw-r--r-- | spec/ffi/function_spec.rb | 11 |
19 files changed, 413 insertions, 81 deletions
@@ -17,3 +17,4 @@ lib/ffi_c.bundle lib/ffi_c.so vendor .bundle +Gemfile.lock diff --git a/.travis.yml b/.travis.yml index 5c2ef43..42eaffe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,11 @@ rvm: - "1.8.7" - "2.0.0" - "2.1" + - "2.2" - "ruby-head" - - "rbx-2.1.1" - "rbx" matrix: allow_failures: - - rvm: "rbx-2.1.1" - rvm: "rbx" - rvm: "rbx-head" - rvm: "1.8.7" @@ -2,7 +2,7 @@ source 'https://rubygems.org' group :development do gem 'rake', '~> 10.1' - gem 'rake-compiler', '~> 0.9.2' + gem 'rake-compiler', '~> 0.9.5' gem 'rspec', '~> 2.14.1' gem 'rubygems-tasks', '~> 0.2.4', :require => 'rubygems/tasks' gem "rubysl", "~> 2.0", :platforms => 'rbx' diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 6d09b4a..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,26 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - diff-lcs (1.2.5) - rake (10.3.2) - rake-compiler (0.9.3) - rake - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.8) - rspec-expectations (2.14.5) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.6) - rubygems-tasks (0.2.4) - -PLATFORMS - java - -DEPENDENCIES - rake (~> 10.1) - rake-compiler (~> 0.9.2) - rspec (~> 2.14.1) - rubygems-tasks (~> 0.2.4) - rubysl (~> 2.0) @@ -63,7 +63,7 @@ as Rubyspec, see the LICENSE.SPECS file. ## Credits -The following people have submitted code, bug reports, or otherwide contributed to the success of this project: +The following people have submitted code, bug reports, or otherwise contributed to the success of this project: * Alban Peignier <alban.peignier@free.fr> * Aman Gupta <aman@tmm1.net> @@ -82,8 +82,8 @@ end TEST_DEPS = [ LIBTEST ] if RUBY_PLATFORM == "java" desc "Run all specs" - task :specs => TEST_DEPS do - sh %{#{Gem.ruby} -w -S rspec #{Dir["spec/ffi/*_spec.rb"].join(" ")} -fs --color} + task :specs, [:options] => TEST_DEPS do |t, args| + sh %{#{Gem.ruby} -w -S rspec #{args.options || Dir["spec/ffi/*_spec.rb"].join(" ")} -fs --color} end desc "Run rubinius specs" task :rbxspecs => TEST_DEPS do @@ -92,9 +92,9 @@ if RUBY_PLATFORM == "java" else TEST_DEPS.unshift :compile desc "Run all specs" - task :specs => TEST_DEPS do + task :specs, [:options] => TEST_DEPS do |t, args| ENV["MRI_FFI"] = "1" - sh %{#{Gem.ruby} -w -Ilib -I#{BUILD_EXT_DIR} -S rspec #{Dir["spec/ffi/*_spec.rb"].join(" ")} -fs --color} + sh %{#{Gem.ruby} -w -Ilib -I#{BUILD_EXT_DIR} -S rspec #{args.options || Dir["spec/ffi/*_spec.rb"].join(" ")} -fs --color} end desc "Run rubinius specs" task :rbxspecs => TEST_DEPS do @@ -188,20 +188,20 @@ if USE_RAKE_COMPILER task 'gem:win32' => ['gem:win32-x64', 'gem:win32-i386'] task 'gem:win32-i386' do - sh("rake cross native:i386-mingw32 gem RUBY_CC_VERSION='1.8.7:1.9.3:2.0.0:2.1.3'") || raise("win32-i386 build failed!") + sh("rake cross native:i386-mingw32 gem RUBY_CC_VERSION='1.8.7:1.9.3:2.0.0:2.1.5:2.2.1'") || raise("win32-i386 build failed!") end task 'gem:win32-x64' do - sh("rake cross native:x64-mingw32 gem RUBY_CC_VERSION='2.0.0:2.1.3'") || raise("win32-x64 build failed!") + sh("rake cross native:x64-mingw32 gem RUBY_CC_VERSION='2.0.0:2.1.5:2.2.1'") || raise("win32-x64 build failed!") end - (ENV['RUBY_CC_VERSION'] || '1.8.7:1.9.3:2.0.0:2.1.3' ).to_s.split(':').each do |ruby_version| + (ENV['RUBY_CC_VERSION'] || '1.8.7:1.9.3:2.0.0:2.1.5:2.2.1' ).to_s.split(':').each do |ruby_version| task "copy:ffi_c:i386-mingw32:#{ruby_version}" do |t| sh "i686-w64-mingw32-strip -S #{BUILD_DIR}/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" end end - (ENV['RUBY_CC_VERSION'] || '2.0.0:2.1.3' ).to_s.split(':').each do |ruby_version| + (ENV['RUBY_CC_VERSION'] || '2.0.0:2.1.5:2.2.1' ).to_s.split(':').each do |ruby_version| task "copy:ffi_c:x64-mingw32:#{ruby_version}" do |t| sh "x86_64-w64-mingw32-strip -S #{BUILD_DIR}/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" end diff --git a/ext/ffi_c/Call.c b/ext/ffi_c/Call.c index 679bfba..b028811 100644 --- a/ext/ffi_c/Call.c +++ b/ext/ffi_c/Call.c @@ -126,19 +126,30 @@ rbffi_SetupCallParams(int argc, VALUE* argv, int paramCount, Type** paramTypes, switch (paramType->nativeType) { case NATIVE_INT8: - param->s8 = NUM2INT(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->s8 = NUM2INT(value); + } else { + param->s8 = NUM2INT(argv[argidx]); + } + ++argidx; ADJ(param, INT8); break; - case NATIVE_INT16: - param->s16 = NUM2INT(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->s16 = NUM2INT(value); + + } else { + param->s16 = NUM2INT(argv[argidx]); + } + ++argidx; ADJ(param, INT16); break; - case NATIVE_INT32: if (unlikely(type == T_SYMBOL && enums != Qnil)) { VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); @@ -152,7 +163,6 @@ rbffi_SetupCallParams(int argc, VALUE* argv, int paramCount, Type** paramTypes, ADJ(param, INT32); break; - case NATIVE_BOOL: if (type != T_TRUE && type != T_FALSE) { rb_raise(rb_eTypeError, "wrong argument type (expected a boolean parameter)"); @@ -161,67 +171,122 @@ rbffi_SetupCallParams(int argc, VALUE* argv, int paramCount, Type** paramTypes, ADJ(param, INT8); break; - case NATIVE_UINT8: - param->u8 = NUM2UINT(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->u8 = NUM2UINT(value); + } else { + param->u8 = NUM2UINT(argv[argidx]); + } + ADJ(param, INT8); ++argidx; break; - case NATIVE_UINT16: - param->u16 = NUM2UINT(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->u16 = NUM2UINT(value); + } else { + param->u16 = NUM2UINT(argv[argidx]); + } + ADJ(param, INT16); ++argidx; break; - case NATIVE_UINT32: - param->u32 = NUM2UINT(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->u32 = NUM2UINT(value); + } else { + param->u32 = NUM2UINT(argv[argidx]); + } + ADJ(param, INT32); ++argidx; break; - case NATIVE_INT64: - param->i64 = NUM2LL(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->i64 = NUM2LL(value); + } else { + param->i64 = NUM2LL(argv[argidx]); + } + ADJ(param, INT64); ++argidx; break; - case NATIVE_UINT64: - param->u64 = NUM2ULL(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->u64 = NUM2ULL(value); + } else { + param->u64 = NUM2ULL(argv[argidx]); + } + ADJ(param, INT64); ++argidx; break; case NATIVE_LONG: - *(ffi_sarg *) param = NUM2LONG(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + *(ffi_sarg *) param = NUM2LONG(value); + } else { + *(ffi_sarg *) param = NUM2LONG(argv[argidx]); + } + ADJ(param, LONG); ++argidx; break; case NATIVE_ULONG: - *(ffi_arg *) param = NUM2ULONG(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + *(ffi_arg *) param = NUM2ULONG(value); + } else { + *(ffi_arg *) param = NUM2ULONG(argv[argidx]); + } + ADJ(param, LONG); ++argidx; break; case NATIVE_FLOAT32: - param->f32 = (float) NUM2DBL(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->f32 = (float) NUM2DBL(value); + } else { + param->f32 = (float) NUM2DBL(argv[argidx]); + } + ADJ(param, FLOAT32); ++argidx; break; case NATIVE_FLOAT64: - param->f64 = NUM2DBL(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->f64 = NUM2DBL(value); + } else { + param->f64 = NUM2DBL(argv[argidx]); + } + ADJ(param, FLOAT64); ++argidx; break; case NATIVE_LONGDOUBLE: - param->ld = rbffi_num2longdouble(argv[argidx]); + if (unlikely(type == T_SYMBOL && enums != Qnil)) { + VALUE value = rb_funcall(enums, id_map_symbol, 1, argv[argidx]); + param->ld = rbffi_num2longdouble(value); + } else { + param->ld = rbffi_num2longdouble(argv[argidx]); + } + ADJ(param, LONGDOUBLE); ++argidx; break; diff --git a/ext/ffi_c/libffi/src/x86/win32.S b/ext/ffi_c/libffi/src/x86/win32.S index 34ec0fd..06c893f 100644 --- a/ext/ffi_c/libffi/src/x86/win32.S +++ b/ext/ffi_c/libffi/src/x86/win32.S @@ -156,7 +156,7 @@ ca_epilogue: ret ffi_call_win32 ENDP -ffi_closure_SYSV PROC NEAR FORCEFRAME +ffi_closure_SYSV PROC NEAR <FORCEFRAME> ;; the ffi_closure ctx is passed in eax by the trampoline. sub esp, 40 @@ -314,7 +314,7 @@ ffi_closure_raw_SYSV ENDP #endif /* !FFI_NO_RAW_API */ -ffi_closure_STDCALL PROC NEAR FORCEFRAME +ffi_closure_STDCALL PROC NEAR <FORCEFRAME> ;; the ffi_closure ctx is passed in eax by the trampoline. sub esp, 40 diff --git a/ffi.gemspec b/ffi.gemspec index 99917c5..7b5b6e3 100644 --- a/ffi.gemspec +++ b/ffi.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |s| s.homepage = 'http://wiki.github.com/ffi/ffi' s.summary = 'Ruby FFI' s.description = 'Ruby FFI library' - s.files = %w(ffi.gemspec LICENSE COPYING README.md Rakefile) + Dir.glob("{ext,gen,lib,spec,libtest}/**/*").reject { |f| f =~ /(lib\/[12]\.[089]|\.so$|\.bundle|\.dylib$)/ } + s.files = %w(ffi.gemspec LICENSE COPYING README.md Rakefile) + Dir.glob("{ext,gen,lib,spec,libtest}/**/*").reject { |f| f =~ /(lib\/[12]\.[089]|\.s?[ao]$|\.bundle|\.dylib$)/ } s.extensions << 'ext/ffi_c/extconf.rb' s.has_rdoc = false s.rdoc_options = %w[--exclude=ext/ffi_c/.*\.o$ --exclude=ffi_c\.(bundle|so)$] @@ -1,14 +1,16 @@ if !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'rbx' Object.send(:remove_const, :FFI) if defined?(::FFI) begin - if RUBY_VERSION =~ /1.8/ + if RUBY_VERSION =~ /^1\.8/ require '1.8/ffi_c' - elsif RUBY_VERSION =~ /1.9/ + elsif RUBY_VERSION =~ /^1\.9/ require '1.9/ffi_c' - elsif RUBY_VERSION =~ /2.0/ + elsif RUBY_VERSION =~ /^2\.0/ require '2.0/ffi_c' - elsif RUBY_VERSION =~ /2.1/ + elsif RUBY_VERSION =~ /^2\.1/ require '2.1/ffi_c' + elsif RUBY_VERSION =~ /^2\.2/ + require '2.2/ffi_c' else require 'ffi_c' end diff --git a/lib/ffi/enum.rb b/lib/ffi/enum.rb index 9dcf4fa..1b0b898 100644 --- a/lib/ffi/enum.rb +++ b/lib/ffi/enum.rb @@ -86,10 +86,16 @@ module FFI attr_reader :tag - # @param [nil, Enumerable] info - # @param tag enum tag - def initialize(info, tag=nil) - @tag = tag + # @overload initialize(info, tag=nil) + # @param [nil, Enumerable] info + # @param [nil, Symbol] tag enum tag + # @overload initialize(native_type, info, tag=nil) + # @param [FFI::Type] native_type Native type for new Enum + # @param [nil, Enumerable] info symbols and values for new Enum + # @param [nil, Symbol] tag name of new Enum + def initialize(*args) + @native_type = args.shift if args.first.kind_of?(FFI::Type) + info, @tag = *args @kv_map = Hash.new unless info.nil? last_cst = nil @@ -144,9 +150,9 @@ module FFI alias to_hash symbol_map # Get native type of Enum - # @return [Type::INT] + # @return [Type] def native_type - Type::INT + @native_type || Type::INT end # @param [Symbol, Integer, #to_int] val diff --git a/lib/ffi/library.rb b/lib/ffi/library.rb index 47a0fa8..53eb87d 100644 --- a/lib/ffi/library.rb +++ b/lib/ffi/library.rb @@ -444,9 +444,29 @@ module FFI # @example # enum [:zero, :one, :two] # unnamed enum, equivalent to above example # @param [Array] values values for enum + # @overload enum(native_type, name, values) + # Create a named enum and specify the native type. + # @example + # enum FFI::Type::UINT64, :foo, [:zero, :one, :two] # named enum + # @param [FFI::Type] native_type native type for new enum + # @param [Symbol] name name for new enum + # @param [Array] values values for enum + # @overload enum(native_type, *args) + # Create an unnamed enum and specify the native type. + # @example + # enum FFI::Type::UINT64, :zero, :one, :two # unnamed enum + # @param [FFI::Type] native_type native type for new enum + # @param args values for enum + # @overload enum(native_type, values) + # Create an unnamed enum and specify the native type. + # @example + # enum Type::UINT64, [:zero, :one, :two] # unnamed enum, equivalent to above example + # @param [FFI::Type] native_type native type for new enum + # @param [Array] values values for enum # @return [FFI::Enum] # Create a new {FFI::Enum}. def enum(*args) + native_type = args.first.kind_of?(FFI::Type) ? args.shift : nil name, values = if args[0].kind_of?(Symbol) && args[1].kind_of?(Array) [ args[0], args[1] ] elsif args[0].kind_of?(Array) @@ -455,7 +475,7 @@ module FFI [ nil, args ] end @ffi_enums = FFI::Enums.new unless defined?(@ffi_enums) - @ffi_enums << (e = FFI::Enum.new(values, name)) + @ffi_enums << (e = native_type ? FFI::Enum.new(native_type, values, name) : FFI::Enum.new(values, name)) # If called as enum :foo, [ :zero, :one, :two ], add a typedef alias typedef(e, name) if name diff --git a/lib/ffi/platform/x86_64-darwin/types.conf b/lib/ffi/platform/x86_64-darwin/types.conf index 0cec3bf..51637ee 100644 --- a/lib/ffi/platform/x86_64-darwin/types.conf +++ b/lib/ffi/platform/x86_64-darwin/types.conf @@ -19,12 +19,12 @@ rbx.platform.typedef.__darwin_socklen_t = uint rbx.platform.typedef.__darwin_ssize_t = long rbx.platform.typedef.__darwin_time_t = long rbx.platform.typedef.int8_t = char -rbx.platform.typedef.u_int8_t = uchar rbx.platform.typedef.int16_t = short -rbx.platform.typedef.u_int16_t = ushort rbx.platform.typedef.int32_t = int -rbx.platform.typedef.u_int32_t = uint rbx.platform.typedef.int64_t = long_long +rbx.platform.typedef.u_int8_t = uchar +rbx.platform.typedef.u_int16_t = ushort +rbx.platform.typedef.u_int32_t = uint rbx.platform.typedef.u_int64_t = ulong_long rbx.platform.typedef.register_t = long_long rbx.platform.typedef.intptr_t = long @@ -35,6 +35,7 @@ rbx.platform.typedef.user_ssize_t = long_long rbx.platform.typedef.user_long_t = long_long rbx.platform.typedef.user_ulong_t = ulong_long rbx.platform.typedef.user_time_t = long_long +rbx.platform.typedef.user_off_t = long_long rbx.platform.typedef.syscall_arg_t = ulong_long rbx.platform.typedef.__darwin_blkcnt_t = long_long rbx.platform.typedef.__darwin_blksize_t = int @@ -50,12 +51,13 @@ rbx.platform.typedef.__darwin_mach_port_t = uint rbx.platform.typedef.__darwin_mode_t = ushort rbx.platform.typedef.__darwin_off_t = long_long rbx.platform.typedef.__darwin_pid_t = int -rbx.platform.typedef.__darwin_pthread_key_t = ulong rbx.platform.typedef.__darwin_sigset_t = uint rbx.platform.typedef.__darwin_suseconds_t = int rbx.platform.typedef.__darwin_uid_t = uint rbx.platform.typedef.__darwin_useconds_t = uint rbx.platform.typedef.__darwin_uuid_t[16] = uchar +rbx.platform.typedef.__darwin_uuid_string_t[37] = char +rbx.platform.typedef.__darwin_pthread_key_t = ulong rbx.platform.typedef.u_char = uchar rbx.platform.typedef.u_short = ushort rbx.platform.typedef.u_int = uint @@ -91,10 +93,34 @@ rbx.platform.typedef.ssize_t = long rbx.platform.typedef.time_t = long rbx.platform.typedef.useconds_t = uint rbx.platform.typedef.suseconds_t = int +rbx.platform.typedef.rsize_t = ulong +rbx.platform.typedef.errno_t = int rbx.platform.typedef.fd_mask = int rbx.platform.typedef.pthread_key_t = ulong rbx.platform.typedef.fsblkcnt_t = uint rbx.platform.typedef.fsfilcnt_t = uint rbx.platform.typedef.sa_family_t = uchar rbx.platform.typedef.socklen_t = uint +rbx.platform.typedef.uint8_t = uchar +rbx.platform.typedef.uint16_t = ushort +rbx.platform.typedef.uint32_t = uint +rbx.platform.typedef.uint64_t = ulong_long +rbx.platform.typedef.int_least8_t = char +rbx.platform.typedef.int_least16_t = short +rbx.platform.typedef.int_least32_t = int +rbx.platform.typedef.int_least64_t = long_long +rbx.platform.typedef.uint_least8_t = uchar +rbx.platform.typedef.uint_least16_t = ushort +rbx.platform.typedef.uint_least32_t = uint +rbx.platform.typedef.uint_least64_t = ulong_long +rbx.platform.typedef.int_fast8_t = char +rbx.platform.typedef.int_fast16_t = short +rbx.platform.typedef.int_fast32_t = int +rbx.platform.typedef.int_fast64_t = long_long +rbx.platform.typedef.uint_fast8_t = uchar +rbx.platform.typedef.uint_fast16_t = ushort +rbx.platform.typedef.uint_fast32_t = uint +rbx.platform.typedef.uint_fast64_t = ulong_long +rbx.platform.typedef.intmax_t = long +rbx.platform.typedef.uintmax_t = ulong rbx.platform.typedef.rlim_t = ulong_long diff --git a/lib/ffi/struct.rb b/lib/ffi/struct.rb index c699e0b..599ee9b 100644 --- a/lib/ffi/struct.rb +++ b/lib/ffi/struct.rb @@ -256,6 +256,12 @@ module FFI # :field2 => :pointer, # :field3 => :string # end + # @example Creating a layout with pointers to functions + # class MyFunctionTable < Struct + # layout :function1, callback([:int, :int], :int), + # :function2, callback([:pointer], :void), + # :field3, :string + # end # @note Creating a layout from a hash +spec+ is supported only for Ruby 1.9. def layout(*spec) #raise RuntimeError, "struct layout already defined for #{self.inspect}" if defined?(@layout) diff --git a/lib/ffi/version.rb b/lib/ffi/version.rb index d51b5ba..89a36ef 100644 --- a/lib/ffi/version.rb +++ b/lib/ffi/version.rb @@ -1,4 +1,4 @@ module FFI - VERSION = '1.9.6' + VERSION = '1.9.8' end diff --git a/libtest/EnumTest.c b/libtest/EnumTest.c index 4c9dda9..4bf8d23 100644 --- a/libtest/EnumTest.c +++ b/libtest/EnumTest.c @@ -3,6 +3,7 @@ * * For licensing, see LICENSE.SPECS */ +#include <stdint.h> int test_untagged_enum(int val) { return val; @@ -12,6 +13,22 @@ int test_untagged_typedef_enum(int val) { return val; } +uint8_t test_untagged_nonint_enum(uint8_t val) { + return val; +} + +uint16_t test_tagged_nonint_enum1(uint16_t val) { + return val; +} + +uint32_t test_tagged_nonint_enum2(uint32_t val) { + return val; +} + +uint64_t test_tagged_nonint_enum3(uint64_t val) { + return val; +} + typedef enum {c1, c2, c3, c4} enum_type1; enum_type1 test_tagged_typedef_enum1(enum_type1 val) { return val; diff --git a/spec/ffi/enum_spec.rb b/spec/ffi/enum_spec.rb index b65b1fb..55ff13a 100644 --- a/spec/ffi/enum_spec.rb +++ b/spec/ffi/enum_spec.rb @@ -36,6 +36,28 @@ module TestEnum3 attach_function :test_tagged_typedef_enum4, [:enum_type4], :enum_type4 end +module TestEnum4 + extend FFI::Library + ffi_lib TestLibrary::PATH + + enum [:c1, :c2, :c3, :c4] + enum :enum_type1, [:c5, 0x42, :c6, :c7, :c8] + enum :enum_type2, [:c9, 0x42, :c10, :c11, 0x4242, :c12] + enum :enum_type3, [:c13, 0x42, :c14, 0x4242, :c15, 0x42424242, :c16, 0x4242424242424242] + enum FFI::Type::UINT16, :enum_type4, [:c17, 0x42, :c18, :c19, :c20] + enum FFI::Type::UINT32, :enum_type5, [:c21, 0x42, :c22, :c23, 0x4242, :c24] + enum FFI::Type::UINT64, :enum_type6, [:c25, 0x42, :c26, 0x4242, :c27, 0x42424242, :c28, 0x4242424242424242] + enum FFI::Type::UINT64, [:c29, 0x4242424242424242, :c30, :c31, :c32] + + attach_function :test_untagged_nonint_enum, [:uint8], :uint8 + attach_function :test_tagged_nonint_enum1, [:uint16], :uint16 + attach_function :test_tagged_nonint_enum2, [:uint32], :uint32 + attach_function :test_tagged_nonint_enum3, [:uint64], :uint64 + attach_function :test_tagged_nonint_enum4, :test_tagged_nonint_enum1, [:enum_type4], :enum_type4 + attach_function :test_tagged_nonint_enum5, :test_tagged_nonint_enum2, [:enum_type5], :enum_type5 + attach_function :test_tagged_nonint_enum6, :test_tagged_nonint_enum3, [:enum_type6], :enum_type6 +end + describe "A library with no enum defined" do it "returns nil when asked for an enum" do expect(TestEnum0.enum_type(:foo)).to be_nil @@ -60,6 +82,14 @@ describe "An untagged enum" do expect(TestEnum1.test_untagged_enum(:c14)).to eq(4242) expect(TestEnum1.test_untagged_enum(:c15)).to eq(424242) expect(TestEnum1.test_untagged_enum(:c16)).to eq(42424242) + expect(TestEnum4.test_untagged_nonint_enum(:c1)).to eq(0) + expect(TestEnum4.test_untagged_nonint_enum(:c2)).to eq(1) + expect(TestEnum4.test_untagged_nonint_enum(:c3)).to eq(2) + expect(TestEnum4.test_untagged_nonint_enum(:c4)).to eq(3) + expect(TestEnum4.test_tagged_nonint_enum3(:c29)).to eq(0x4242424242424242) + expect(TestEnum4.test_tagged_nonint_enum3(:c30)).to eq(0x4242424242424243) + expect(TestEnum4.test_tagged_nonint_enum3(:c31)).to eq(0x4242424242424244) + expect(TestEnum4.test_tagged_nonint_enum3(:c32)).to eq(0x4242424242424245) end end @@ -69,6 +99,12 @@ describe "A tagged typedef enum" do expect(TestEnum3.enum_type(:enum_type2)).not_to be_nil expect(TestEnum3.enum_type(:enum_type3)).not_to be_nil expect(TestEnum3.enum_type(:enum_type4)).not_to be_nil + expect(TestEnum4.enum_type(:enum_type1)).not_to be_nil + expect(TestEnum4.enum_type(:enum_type2)).not_to be_nil + expect(TestEnum4.enum_type(:enum_type3)).not_to be_nil + expect(TestEnum4.enum_type(:enum_type4)).not_to be_nil + expect(TestEnum4.enum_type(:enum_type5)).not_to be_nil + expect(TestEnum4.enum_type(:enum_type6)).not_to be_nil end it "contains enum constants" do @@ -76,6 +112,12 @@ describe "A tagged typedef enum" do expect(TestEnum3.enum_type(:enum_type2).symbols.length).to eq(4) expect(TestEnum3.enum_type(:enum_type3).symbols.length).to eq(4) expect(TestEnum3.enum_type(:enum_type4).symbols.length).to eq(4) + expect(TestEnum4.enum_type(:enum_type1).symbols.length).to eq(4) + expect(TestEnum4.enum_type(:enum_type2).symbols.length).to eq(4) + expect(TestEnum4.enum_type(:enum_type3).symbols.length).to eq(4) + expect(TestEnum4.enum_type(:enum_type4).symbols.length).to eq(4) + expect(TestEnum4.enum_type(:enum_type5).symbols.length).to eq(4) + expect(TestEnum4.enum_type(:enum_type6).symbols.length).to eq(4) end it "constants can be used as function parameters and return value" do @@ -95,6 +137,30 @@ describe "A tagged typedef enum" do expect(TestEnum3.test_tagged_typedef_enum4(:c14)).to be :c14 expect(TestEnum3.test_tagged_typedef_enum4(:c15)).to be :c15 expect(TestEnum3.test_tagged_typedef_enum4(:c16)).to be :c16 + expect(TestEnum4.test_tagged_nonint_enum1(:c5)).to eq(0x42) + expect(TestEnum4.test_tagged_nonint_enum1(:c6)).to eq(0x43) + expect(TestEnum4.test_tagged_nonint_enum1(:c7)).to eq(0x44) + expect(TestEnum4.test_tagged_nonint_enum1(:c8)).to eq(0x45) + expect(TestEnum4.test_tagged_nonint_enum2(:c9)).to eq(0x42) + expect(TestEnum4.test_tagged_nonint_enum2(:c10)).to eq(0x43) + expect(TestEnum4.test_tagged_nonint_enum2(:c11)).to eq(0x4242) + expect(TestEnum4.test_tagged_nonint_enum2(:c12)).to eq(0x4243) + expect(TestEnum4.test_tagged_nonint_enum3(:c13)).to eq(0x42) + expect(TestEnum4.test_tagged_nonint_enum3(:c14)).to eq(0x4242) + expect(TestEnum4.test_tagged_nonint_enum3(:c15)).to eq(0x42424242) + expect(TestEnum4.test_tagged_nonint_enum3(:c16)).to eq(0x4242424242424242) + expect(TestEnum4.test_tagged_nonint_enum4(:c17)).to eq(:c17) + expect(TestEnum4.test_tagged_nonint_enum4(:c18)).to eq(:c18) + expect(TestEnum4.test_tagged_nonint_enum4(:c19)).to eq(:c19) + expect(TestEnum4.test_tagged_nonint_enum4(:c20)).to eq(:c20) + expect(TestEnum4.test_tagged_nonint_enum5(:c21)).to eq(:c21) + expect(TestEnum4.test_tagged_nonint_enum5(:c22)).to eq(:c22) + expect(TestEnum4.test_tagged_nonint_enum5(:c23)).to eq(:c23) + expect(TestEnum4.test_tagged_nonint_enum5(:c24)).to eq(:c24) + expect(TestEnum4.test_tagged_nonint_enum6(:c25)).to eq(:c25) + expect(TestEnum4.test_tagged_nonint_enum6(:c26)).to eq(:c26) + expect(TestEnum4.test_tagged_nonint_enum6(:c27)).to eq(:c27) + expect(TestEnum4.test_tagged_nonint_enum6(:c28)).to eq(:c28) end it "integers can be used instead of constants" do @@ -114,6 +180,18 @@ describe "A tagged typedef enum" do expect(TestEnum3.test_tagged_typedef_enum4(4242)).to be :c14 expect(TestEnum3.test_tagged_typedef_enum4(424242)).to be :c15 expect(TestEnum3.test_tagged_typedef_enum4(42424242)).to be :c16 + expect(TestEnum4.test_tagged_nonint_enum4(0x42)).to eq(:c17) + expect(TestEnum4.test_tagged_nonint_enum4(0x43)).to eq(:c18) + expect(TestEnum4.test_tagged_nonint_enum4(0x44)).to eq(:c19) + expect(TestEnum4.test_tagged_nonint_enum4(0x45)).to eq(:c20) + expect(TestEnum4.test_tagged_nonint_enum5(0x42)).to eq(:c21) + expect(TestEnum4.test_tagged_nonint_enum5(0x43)).to eq(:c22) + expect(TestEnum4.test_tagged_nonint_enum5(0x4242)).to eq(:c23) + expect(TestEnum4.test_tagged_nonint_enum5(0x4243)).to eq(:c24) + expect(TestEnum4.test_tagged_nonint_enum6(0x42)).to eq(:c25) + expect(TestEnum4.test_tagged_nonint_enum6(0x4242)).to eq(:c26) + expect(TestEnum4.test_tagged_nonint_enum6(0x42424242)).to eq(:c27) + expect(TestEnum4.test_tagged_nonint_enum6(0x4242424242424242)).to eq(:c28) end end @@ -128,6 +206,11 @@ describe "All enums" do expect(TestEnum3.enum_value(:c2)).to eq(1) expect(TestEnum3.enum_value(:c3)).to eq(2) expect(TestEnum3.enum_value(:c4)).to eq(3) + + expect(TestEnum4.enum_value(:c1)).to eq(0) + expect(TestEnum4.enum_value(:c2)).to eq(1) + expect(TestEnum4.enum_value(:c3)).to eq(2) + expect(TestEnum4.enum_value(:c4)).to eq(3) end it "can have an explicit first constant and autonumbered subsequent constants" do @@ -140,6 +223,16 @@ describe "All enums" do expect(TestEnum3.enum_value(:c6)).to eq(43) expect(TestEnum3.enum_value(:c7)).to eq(44) expect(TestEnum3.enum_value(:c8)).to eq(45) + + expect(TestEnum4.enum_value(:c5)).to eq(0x42) + expect(TestEnum4.enum_value(:c6)).to eq(0x43) + expect(TestEnum4.enum_value(:c7)).to eq(0x44) + expect(TestEnum4.enum_value(:c8)).to eq(0x45) + + expect(TestEnum4.enum_value(:c29)).to eq(0x4242424242424242) + expect(TestEnum4.enum_value(:c30)).to eq(0x4242424242424243) + expect(TestEnum4.enum_value(:c31)).to eq(0x4242424242424244) + expect(TestEnum4.enum_value(:c32)).to eq(0x4242424242424245) end it "can have a mix of explicit and autonumbered constants" do @@ -152,6 +245,16 @@ describe "All enums" do expect(TestEnum3.enum_value(:c10)).to eq(43) expect(TestEnum3.enum_value(:c11)).to eq(4242) expect(TestEnum3.enum_value(:c12)).to eq(4243) + + expect(TestEnum4.enum_value(:c9)).to eq(0x42) + expect(TestEnum4.enum_value(:c10)).to eq(0x43) + expect(TestEnum4.enum_value(:c11)).to eq(0x4242) + expect(TestEnum4.enum_value(:c12)).to eq(0x4243) + + expect(TestEnum4.enum_value(:c21)).to eq(0x42) + expect(TestEnum4.enum_value(:c22)).to eq(0x43) + expect(TestEnum4.enum_value(:c23)).to eq(0x4242) + expect(TestEnum4.enum_value(:c24)).to eq(0x4243) end it "can have all its constants explicitely valued" do @@ -164,6 +267,16 @@ describe "All enums" do expect(TestEnum3.enum_value(:c14)).to eq(4242) expect(TestEnum3.enum_value(:c15)).to eq(424242) expect(TestEnum3.enum_value(:c16)).to eq(42424242) + + expect(TestEnum4.enum_value(:c13)).to eq(0x42) + expect(TestEnum4.enum_value(:c14)).to eq(0x4242) + expect(TestEnum4.enum_value(:c15)).to eq(0x42424242) + expect(TestEnum4.enum_value(:c16)).to eq(0x4242424242424242) + + expect(TestEnum4.enum_value(:c25)).to eq(0x42) + expect(TestEnum4.enum_value(:c26)).to eq(0x4242) + expect(TestEnum4.enum_value(:c27)).to eq(0x42424242) + expect(TestEnum4.enum_value(:c28)).to eq(0x4242424242424242) end it "return the constant corresponding to a specific value" do @@ -190,6 +303,42 @@ describe "All enums" do expect(enum[4242]).to be :c14 expect(enum[424242]).to be :c15 expect(enum[42424242]).to be :c16 + + enum = TestEnum4.enum_type(:enum_type1) + expect(enum[0x42]).to eq(:c5) + expect(enum[0x43]).to eq(:c6) + expect(enum[0x44]).to eq(:c7) + expect(enum[0x45]).to eq(:c8) + + enum = TestEnum4.enum_type(:enum_type2) + expect(enum[0x42]).to eq(:c9) + expect(enum[0x43]).to eq(:c10) + expect(enum[0x4242]).to eq(:c11) + expect(enum[0x4243]).to eq(:c12) + + enum = TestEnum4.enum_type(:enum_type3) + expect(enum[0x42]).to eq(:c13) + expect(enum[0x4242]).to eq(:c14) + expect(enum[0x42424242]).to eq(:c15) + expect(enum[0x4242424242424242]).to eq(:c16) + + enum = TestEnum4.enum_type(:enum_type4) + expect(enum[0x42]).to eq(:c17) + expect(enum[0x43]).to eq(:c18) + expect(enum[0x44]).to eq(:c19) + expect(enum[0x45]).to eq(:c20) + + enum = TestEnum4.enum_type(:enum_type5) + expect(enum[0x42]).to eq(:c21) + expect(enum[0x43]).to eq(:c22) + expect(enum[0x4242]).to eq(:c23) + expect(enum[0x4243]).to eq(:c24) + + enum = TestEnum4.enum_type(:enum_type6) + expect(enum[0x42]).to eq(:c25) + expect(enum[0x4242]).to eq(:c26) + expect(enum[0x42424242]).to eq(:c27) + expect(enum[0x4242424242424242]).to eq(:c28) end it "return nil for values that don't have a symbol" do @@ -219,9 +368,56 @@ describe "All enums" do expect(enum[424243]).to be_nil expect(enum[42424241]).to be_nil expect(enum[42424243]).to be_nil + + enum = TestEnum4.enum_type(:enum_type1) + expect(enum[0x0]).to be_nil + expect(enum[0x41]).to be_nil + expect(enum[0x46]).to be_nil + + enum = TestEnum4.enum_type(:enum_type2) + expect(enum[0x0]).to be_nil + expect(enum[0x41]).to be_nil + expect(enum[0x44]).to be_nil + expect(enum[0x4241]).to be_nil + expect(enum[0x4244]).to be_nil + + enum = TestEnum4.enum_type(:enum_type3) + expect(enum[0x0]).to be_nil + expect(enum[0x41]).to be_nil + expect(enum[0x43]).to be_nil + expect(enum[0x4241]).to be_nil + expect(enum[0x4243]).to be_nil + expect(enum[0x42424241]).to be_nil + expect(enum[0x42424243]).to be_nil + expect(enum[0x4242424242424241]).to be_nil + expect(enum[0x4242424242424243]).to be_nil + + enum = TestEnum4.enum_type(:enum_type4) + expect(enum[0x0]).to be_nil + expect(enum[0x41]).to be_nil + expect(enum[0x46]).to be_nil + + enum = TestEnum4.enum_type(:enum_type5) + expect(enum[0x0]).to be_nil + expect(enum[0x41]).to be_nil + expect(enum[0x44]).to be_nil + expect(enum[0x4241]).to be_nil + expect(enum[0x4244]).to be_nil + + enum = TestEnum4.enum_type(:enum_type6) + expect(enum[0x0]).to be_nil + expect(enum[0x41]).to be_nil + expect(enum[0x43]).to be_nil + expect(enum[0x4241]).to be_nil + expect(enum[0x4243]).to be_nil + expect(enum[0x42424241]).to be_nil + expect(enum[0x42424243]).to be_nil + expect(enum[0x4242424242424241]).to be_nil + expect(enum[0x4242424242424243]).to be_nil end it "duplicate enum keys rejected" do expect { enum [ :a, 0xfee1dead, :b, 0xdeadbeef, :a, 0 ] }.to raise_error + expect { enum FFI::Type::UINT64, [ :a, 0xfee1dead, :b, 0xdeadbeef, :a, 0 ] }.to raise_error end end diff --git a/spec/ffi/fixtures/EnumTest.c b/spec/ffi/fixtures/EnumTest.c index 4c9dda9..4bf8d23 100644 --- a/spec/ffi/fixtures/EnumTest.c +++ b/spec/ffi/fixtures/EnumTest.c @@ -3,6 +3,7 @@ * * For licensing, see LICENSE.SPECS */ +#include <stdint.h> int test_untagged_enum(int val) { return val; @@ -12,6 +13,22 @@ int test_untagged_typedef_enum(int val) { return val; } +uint8_t test_untagged_nonint_enum(uint8_t val) { + return val; +} + +uint16_t test_tagged_nonint_enum1(uint16_t val) { + return val; +} + +uint32_t test_tagged_nonint_enum2(uint32_t val) { + return val; +} + +uint64_t test_tagged_nonint_enum3(uint64_t val) { + return val; +} + typedef enum {c1, c2, c3, c4} enum_type1; enum_type1 test_tagged_typedef_enum1(enum_type1 val) { return val; diff --git a/spec/ffi/function_spec.rb b/spec/ffi/function_spec.rb index 14fe31c..ab08f35 100644 --- a/spec/ffi/function_spec.rb +++ b/spec/ffi/function_spec.rb @@ -64,10 +64,13 @@ describe FFI::Function do it 'can wrap a blocking function' do fp = FFI::Function.new(:void, [ :int ], @libtest.find_function('testBlocking'), :blocking => true) - time = Time.now - threads = [] - threads << Thread.new { fp.call(2) } - threads << Thread.new(time) { expect(Time.now - time).to be < 1 } + threads = 10.times.map do |x| + Thread.new do + time = Time.now + fp.call(2) + expect(Time.now - time).to be >= 2 + end + end threads.each { |t| t.join } end |