ffi 1.10.0 → 1.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.gitmodules +2 -1
  4. data/.travis.yml +17 -18
  5. data/CHANGELOG.md +33 -0
  6. data/Gemfile +1 -1
  7. data/README.md +20 -17
  8. data/Rakefile +10 -83
  9. data/appveyor.yml +6 -1
  10. data/ext/ffi_c/Call.c +16 -33
  11. data/ext/ffi_c/Call.h +2 -5
  12. data/ext/ffi_c/Function.c +24 -108
  13. data/ext/ffi_c/Platform.c +0 -47
  14. data/ext/ffi_c/Thread.c +6 -222
  15. data/ext/ffi_c/Thread.h +2 -13
  16. data/ext/ffi_c/Type.c +0 -18
  17. data/ext/ffi_c/Type.h +0 -1
  18. data/ext/ffi_c/Variadic.c +9 -15
  19. data/ext/ffi_c/extconf.rb +34 -20
  20. data/ext/ffi_c/ffi.c +3 -8
  21. data/ext/ffi_c/libffi/configure.ac +5 -1
  22. data/ext/ffi_c/libffi/include/ffi_common.h +1 -1
  23. data/ext/ffi_c/libffi/src/aarch64/ffi.c +2 -2
  24. data/ext/ffi_c/libffi/src/frv/ffi.c +1 -1
  25. data/ext/ffi_c/libffi/src/metag/ffi.c +1 -1
  26. data/ext/ffi_c/libffi/src/moxie/ffi.c +1 -1
  27. data/ext/ffi_c/libffi/src/riscv/ffi.c +42 -6
  28. data/ext/ffi_c/libffi/src/riscv/ffitarget.h +1 -0
  29. data/ext/ffi_c/libffi/src/riscv/sysv.S +86 -7
  30. data/ext/ffi_c/libffi/src/x86/ffi.c +2 -1
  31. data/ext/ffi_c/libffi/src/x86/ffiw64.c +1 -1
  32. data/ext/ffi_c/libffi/src/x86/sysv.S +88 -2
  33. data/ext/ffi_c/libffi/src/x86/unix64.S +41 -0
  34. data/ext/ffi_c/rbffi.h +0 -2
  35. data/ffi.gemspec +11 -4
  36. data/lib/ffi/data_converter.rb +67 -0
  37. data/lib/ffi/ffi.rb +1 -0
  38. data/lib/ffi/platform.rb +2 -0
  39. data/lib/ffi/pointer.rb +1 -1
  40. data/lib/ffi/struct.rb +3 -63
  41. data/lib/ffi/struct_by_reference.rb +72 -0
  42. data/lib/ffi/struct_layout.rb +96 -0
  43. data/lib/ffi/tools/const_generator.rb +5 -4
  44. data/lib/ffi/tools/generator.rb +47 -2
  45. data/lib/ffi/tools/generator_task.rb +13 -17
  46. data/lib/ffi/tools/struct_generator.rb +4 -4
  47. data/lib/ffi/types.rb +1 -1
  48. data/lib/ffi/version.rb +1 -1
  49. metadata +18 -13
  50. data/ext/ffi_c/DataConverter.c +0 -91
  51. data/ext/ffi_c/StructByReference.c +0 -190
  52. data/ext/ffi_c/StructByReference.h +0 -50
@@ -0,0 +1,96 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ # Copyright (C) 2008, 2009 Andrea Fazzi
4
+ # Copyright (C) 2008, 2009 Luc Heinrich
5
+ #
6
+ # This file is part of ruby-ffi.
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice, this
14
+ # list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
19
+ # may be used to endorse or promote products derived from this software
20
+ # without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
26
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ #
33
+
34
+ module FFI
35
+
36
+ class StructLayout
37
+
38
+ # @return [Array<Array(Symbol, Numeric)>
39
+ # Get an array of tuples (field name, offset of the field).
40
+ def offsets
41
+ members.map { |m| [ m, self[m].offset ] }
42
+ end
43
+
44
+ # @return [Numeric]
45
+ # Get the offset of a field.
46
+ def offset_of(field_name)
47
+ self[field_name].offset
48
+ end
49
+
50
+ # An enum {Field} in a {StructLayout}.
51
+ class Enum < Field
52
+
53
+ # @param [AbstractMemory] ptr pointer on a {Struct}
54
+ # @return [Object]
55
+ # Get an object of type {#type} from memory pointed by +ptr+.
56
+ def get(ptr)
57
+ type.find(ptr.get_int(offset))
58
+ end
59
+
60
+ # @param [AbstractMemory] ptr pointer on a {Struct}
61
+ # @param value
62
+ # @return [nil]
63
+ # Set +value+ into memory pointed by +ptr+.
64
+ def put(ptr, value)
65
+ ptr.put_int(offset, type.find(value))
66
+ end
67
+
68
+ end
69
+
70
+ class InnerStruct < Field
71
+ def get(ptr)
72
+ type.struct_class.new(ptr.slice(self.offset, self.size))
73
+ end
74
+
75
+ def put(ptr, value)
76
+ raise TypeError, "wrong value type (expected #{type.struct_class})" unless value.is_a?(type.struct_class)
77
+ ptr.slice(self.offset, self.size).__copy_from__(value.pointer, self.size)
78
+ end
79
+ end
80
+
81
+ class Mapped < Field
82
+ def initialize(name, offset, type, orig_field)
83
+ super(name, offset, type)
84
+ @orig_field = orig_field
85
+ end
86
+
87
+ def get(ptr)
88
+ type.from_native(@orig_field.get(ptr), nil)
89
+ end
90
+
91
+ def put(ptr, value)
92
+ @orig_field.put(ptr, type.to_native(value, nil))
93
+ end
94
+ end
95
+ end
96
+ end
@@ -6,6 +6,7 @@ module FFI
6
6
  # ConstGenerator turns C constants into ruby values.
7
7
  #
8
8
  # @example a simple example for stdio
9
+ # require 'ffi/tools/const_generator'
9
10
  # cg = FFI::ConstGenerator.new('stdio') do |gen|
10
11
  # gen.const(:SEEK_SET)
11
12
  # gen.const('SEEK_CUR')
@@ -25,10 +26,10 @@ module FFI
25
26
  #
26
27
  # The only option is +:required+, which if set to +true+ raises an error if a
27
28
  # constant you have requested was not found.
28
- #
29
+ #
29
30
  # @param [#to_s] prefix
30
31
  # @param [Hash] options
31
- # @return
32
+ # @return
32
33
  # @option options [Boolean] :required
33
34
  # @overload initialize(prefix, options)
34
35
  # @overload initialize(prefix, options) { |gen| ... }
@@ -79,7 +80,7 @@ module FFI
79
80
  # @param [#call] converter convert the value from a string to the appropriate
80
81
  # type for {#to_ruby}.
81
82
  # @overload const(name, format=nil, cast='', ruby_name=nil) { |value| ... }
82
- # Use a converter block. This block convert the value from a string to the
83
+ # Use a converter block. This block convert the value from a string to the
83
84
  # appropriate type for {#to_ruby}.
84
85
  # @yieldparam value constant value
85
86
  def const(name, format = nil, cast = '', ruby_name = nil, converter = nil,
@@ -224,6 +225,6 @@ module FFI
224
225
  "#{ruby_name} = #{converted_value}"
225
226
  end
226
227
 
227
- end
228
+ end
228
229
 
229
230
  end
@@ -1,6 +1,51 @@
1
+ require 'ffi/tools/struct_generator'
2
+ require 'ffi/tools/const_generator'
3
+
1
4
  module FFI
2
5
 
3
- # @private
6
+ ##
7
+ # Generate files with C structs for FFI::Struct and C constants.
8
+ #
9
+ # == A simple example
10
+ #
11
+ # In file +zlib.rb.ffi+:
12
+ # module Zlib
13
+ # @@@
14
+ # constants do |c|
15
+ # c.include "zlib.h"
16
+ # c.const :ZLIB_VERNUM
17
+ # end
18
+ # @@@
19
+ #
20
+ # class ZStream < FFI::Struct
21
+ #
22
+ # struct do |s|
23
+ # s.name "struct z_stream_s"
24
+ # s.include "zlib.h"
25
+ #
26
+ # s.field :next_in, :pointer
27
+ # s.field :avail_in, :uint
28
+ # s.field :total_in, :ulong
29
+ # end
30
+ # @@@
31
+ # end
32
+ # end
33
+ #
34
+ # Translate the file:
35
+ # require "ffi/tools/generator"
36
+ # FFI::Generator.new "zlib.rb.ffi", "zlib.rb"
37
+ #
38
+ # Generates the file +zlib.rb+ with constant values and offsets:
39
+ # module Zlib
40
+ # ZLIB_VERNUM = 4784
41
+ #
42
+ # class ZStream < FFI::Struct
43
+ # layout :next_in, :pointer, 0,
44
+ # :avail_in, :uint, 8,
45
+ # :total_in, :ulong, 16
46
+ # end
47
+ #
48
+ # @see FFI::Generator::Task for easy integration in a Rakefile
4
49
  class Generator
5
50
 
6
51
  def initialize(ffi_name, rb_name, options = {})
@@ -34,7 +79,7 @@ module FFI
34
79
  end
35
80
 
36
81
  open @rb_name, 'w' do |f|
37
- f.puts "# This file is generated by rake. Do not edit."
82
+ f.puts "# This file is generated from `#{@ffi_name}'. Do not edit."
38
83
  f.puts
39
84
  f.puts new_file
40
85
  end
@@ -1,25 +1,21 @@
1
- begin
2
- require 'ffi/struct_generator'
3
- require 'ffi/const_generator'
4
- require 'ffi/generator'
5
- rescue LoadError
6
- # from Rakefile
7
- require 'lib/ffi/struct_generator'
8
- require 'lib/ffi/const_generator'
9
- require 'lib/ffi/generator'
10
- end
11
-
1
+ require 'ffi/tools/generator'
12
2
  require 'rake'
13
3
  require 'rake/tasklib'
14
- require 'tempfile'
15
4
 
16
5
  ##
17
- # Rake task that calculates C structs for FFI::Struct.
18
-
19
- # @private
6
+ # Add Rake tasks that generate files with C structs for FFI::Struct and C constants.
7
+ #
8
+ # @example a simple example for your Rakefile
9
+ # require "ffi/tools/generator_task"
10
+ # # Add a task to generate my_object.rb out of my_object.rb.ffi
11
+ # FFI::Generator::Task.new ["my_object.rb"], cflags: "-I/usr/local/mylibrary"
12
+ #
13
+ # The generated files are also added to the 'clear' task.
14
+ #
15
+ # @see FFI::Generator for a description of the file content
20
16
  class FFI::Generator::Task < Rake::TaskLib
21
17
 
22
- def initialize(rb_names)
18
+ def initialize(rb_names, options={})
23
19
  task :clean do rm_f rb_names end
24
20
 
25
21
  rb_names.each do |rb_name|
@@ -28,7 +24,7 @@ class FFI::Generator::Task < Rake::TaskLib
28
24
  file rb_name => ffi_name do |t|
29
25
  puts "Generating #{rb_name}..." if Rake.application.options.trace
30
26
 
31
- FFI::Generator.new ffi_name, rb_name
27
+ FFI::Generator.new ffi_name, rb_name, options
32
28
  end
33
29
  end
34
30
  end
@@ -7,15 +7,15 @@ module FFI
7
7
  #
8
8
  # Given the @@@ portion in:
9
9
  #
10
- # module Zlib::ZStream < FFI::Struct
10
+ # class Zlib::ZStream < FFI::Struct
11
11
  # @@@
12
12
  # name "struct z_stream_s"
13
13
  # include "zlib.h"
14
- #
14
+ #
15
15
  # field :next_in, :pointer
16
16
  # field :avail_in, :uint
17
17
  # field :total_in, :ulong
18
- #
18
+ #
19
19
  # # ...
20
20
  # @@@
21
21
  # end
@@ -90,7 +90,7 @@ module FFI
90
90
  raise "Compilation error generating struct #{@name} (#{@struct_name}):\n#{output}"
91
91
  end
92
92
  end
93
-
93
+
94
94
  output = `#{binary}`.split "\n"
95
95
  File.unlink(binary + (FFI::Platform.windows? ? ".exe" : ""))
96
96
  sizeof = output.shift
@@ -152,7 +152,7 @@ module FFI
152
152
  # also allow to work with the pointer itself. This is useful when you want
153
153
  # a Ruby string already containing a copy of the data, but also the pointer
154
154
  # to the data for you to do something with it, like freeing it, in case the
155
- # library handed the memory to off to the caller (Ruby-FFI).
155
+ # library handed the memory off to the caller (Ruby-FFI).
156
156
  #
157
157
  # It's {typedef}'d as +:strptr+.
158
158
  class StrPtrConverter
@@ -1,3 +1,3 @@
1
1
  module FFI
2
- VERSION = '1.10.0'
2
+ VERSION = '1.11.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.0
4
+ version: 1.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wayne Meissner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-06 00:00:00.000000000 Z
11
+ date: 2019-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '10.1'
19
+ version: '12.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '10.1'
26
+ version: '12.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake-compiler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.6.2
47
+ version: 0.7.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.6.2
54
+ version: 0.7.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +108,6 @@ files:
108
108
  - ext/ffi_c/Call.h
109
109
  - ext/ffi_c/ClosurePool.c
110
110
  - ext/ffi_c/ClosurePool.h
111
- - ext/ffi_c/DataConverter.c
112
111
  - ext/ffi_c/DynamicLibrary.c
113
112
  - ext/ffi_c/DynamicLibrary.h
114
113
  - ext/ffi_c/Function.c
@@ -130,8 +129,6 @@ files:
130
129
  - ext/ffi_c/Pointer.h
131
130
  - ext/ffi_c/Struct.c
132
131
  - ext/ffi_c/Struct.h
133
- - ext/ffi_c/StructByReference.c
134
- - ext/ffi_c/StructByReference.h
135
132
  - ext/ffi_c/StructByValue.c
136
133
  - ext/ffi_c/StructByValue.h
137
134
  - ext/ffi_c/StructLayout.c
@@ -557,6 +554,7 @@ files:
557
554
  - lib/ffi/autopointer.rb
558
555
  - lib/ffi/buffer.rb
559
556
  - lib/ffi/callback.rb
557
+ - lib/ffi/data_converter.rb
560
558
  - lib/ffi/enum.rb
561
559
  - lib/ffi/errno.rb
562
560
  - lib/ffi/ffi.rb
@@ -611,6 +609,8 @@ files:
611
609
  - lib/ffi/platform/x86_64-windows/types.conf
612
610
  - lib/ffi/pointer.rb
613
611
  - lib/ffi/struct.rb
612
+ - lib/ffi/struct_by_reference.rb
613
+ - lib/ffi/struct_layout.rb
614
614
  - lib/ffi/struct_layout_builder.rb
615
615
  - lib/ffi/tools/const_generator.rb
616
616
  - lib/ffi/tools/generator.rb
@@ -632,7 +632,13 @@ files:
632
632
  homepage: http://wiki.github.com/ffi/ffi
633
633
  licenses:
634
634
  - BSD-3-Clause
635
- metadata: {}
635
+ metadata:
636
+ bug_tracker_uri: https://github.com/ffi/ffi/issues
637
+ changelog_uri: https://github.com/ffi/ffi/blob/master/CHANGELOG.md
638
+ documentation_uri: https://github.com/ffi/ffi/wiki
639
+ wiki_uri: https://github.com/ffi/ffi/wiki
640
+ source_code_uri: https://github.com/ffi/ffi/
641
+ mailing_list_uri: http://groups.google.com/group/ruby-ffi
636
642
  post_install_message:
637
643
  rdoc_options:
638
644
  - "--exclude=ext/ffi_c/.*\\.o$"
@@ -643,15 +649,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
643
649
  requirements:
644
650
  - - ">="
645
651
  - !ruby/object:Gem::Version
646
- version: '1.9'
652
+ version: '2.0'
647
653
  required_rubygems_version: !ruby/object:Gem::Requirement
648
654
  requirements:
649
655
  - - ">="
650
656
  - !ruby/object:Gem::Version
651
657
  version: '0'
652
658
  requirements: []
653
- rubyforge_project:
654
- rubygems_version: 2.7.8
659
+ rubygems_version: 3.0.3
655
660
  signing_key:
656
661
  specification_version: 4
657
662
  summary: Ruby FFI
@@ -1,91 +0,0 @@
1
-
2
- #include <ruby.h>
3
-
4
- #include <ffi.h>
5
- #include "rbffi.h"
6
-
7
- #include "Type.h"
8
- #include "MappedType.h"
9
-
10
-
11
- VALUE rbffi_DataConverterClass = Qnil;
12
- static ID id_native_type_ivar;
13
-
14
- /*
15
- * Get native type.
16
- * @overload native_type(type)
17
- * @param [String, Symbol, Type] type
18
- * @return [Type]
19
- * Get native type from +type+.
20
- * @overload native_type
21
- * @raise {NotImplementedError} This method must be overriden.
22
- */
23
- static VALUE
24
- conv_native_type(int argc, VALUE* argv, VALUE self)
25
- {
26
- if (argc == 0) {
27
- if (!rb_ivar_defined(self, id_native_type_ivar)) {
28
- rb_raise(rb_eNotImpError, "native_type method not overridden and no native_type set");
29
- }
30
-
31
- return rb_ivar_get(self, id_native_type_ivar);
32
-
33
- } else if (argc == 1) {
34
- VALUE type = rbffi_Type_Find(argv[0]);
35
-
36
- rb_ivar_set(self, id_native_type_ivar, type);
37
-
38
- return type;
39
-
40
- } else {
41
- rb_raise(rb_eArgError, "incorrect arguments");
42
- }
43
- }
44
-
45
- /*
46
- * call-seq: to_native(value, ctx)
47
- * @param value
48
- * @param ctx
49
- * @return [value]
50
- * Convert to a native type.
51
- */
52
- static VALUE
53
- conv_to_native(VALUE self, VALUE value, VALUE ctx)
54
- {
55
- return value;
56
- }
57
-
58
- /*
59
- * call-seq: from_native(value, ctx)
60
- * @param value
61
- * @param ctx
62
- * @return [value]
63
- * Convert from a native type.
64
- */
65
- static VALUE
66
- conv_from_native(VALUE self, VALUE value, VALUE ctx)
67
- {
68
- return value;
69
- }
70
-
71
-
72
-
73
- void
74
- rbffi_DataConverter_Init(VALUE moduleFFI)
75
- {
76
- /*
77
- * Document-module: FFI::DataConverter
78
- * This module is used to extend somes classes and give then a common API.
79
- *
80
- * Most of methods defined here must be overriden.
81
- */
82
- rbffi_DataConverterClass = rb_define_module_under(moduleFFI, "DataConverter");
83
-
84
- rb_define_method(rbffi_DataConverterClass, "native_type", conv_native_type, -1);
85
- rb_define_method(rbffi_DataConverterClass, "to_native", conv_to_native, 2);
86
- rb_define_method(rbffi_DataConverterClass, "from_native", conv_from_native, 2);
87
-
88
- id_native_type_ivar = rb_intern("@native_type");
89
- }
90
-
91
-