gir_ffi 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/History.txt +8 -0
  2. data/README.md +1 -1
  3. data/TODO.rdoc +12 -4
  4. data/lib/ffi-glib.rb +0 -5
  5. data/lib/ffi-glib/list.rb +1 -1
  6. data/lib/ffi-glib/s_list.rb +1 -1
  7. data/lib/ffi-gobject.rb +2 -3
  8. data/lib/ffi-gobject/closure.rb +4 -3
  9. data/lib/ffi-gobject_introspection/i_base_info.rb +11 -14
  10. data/lib/ffi-gobject_introspection/i_constant_info.rb +1 -1
  11. data/lib/ffi-gobject_introspection/i_function_info.rb +0 -6
  12. data/lib/ffi-gobject_introspection/lib.rb +8 -2
  13. data/lib/gir_ffi.rb +5 -0
  14. data/lib/gir_ffi/arg_helper.rb +1 -1
  15. data/lib/gir_ffi/argument_builder.rb +161 -0
  16. data/lib/gir_ffi/base_argument_builder.rb +146 -0
  17. data/lib/gir_ffi/builder/field.rb +7 -5
  18. data/lib/gir_ffi/builder/module.rb +3 -14
  19. data/lib/gir_ffi/builder/property.rb +0 -2
  20. data/lib/gir_ffi/builder/type/callback.rb +0 -8
  21. data/lib/gir_ffi/builder/type/constant.rb +0 -4
  22. data/lib/gir_ffi/builder/type/enum.rb +0 -4
  23. data/lib/gir_ffi/builder/type/interface.rb +0 -4
  24. data/lib/gir_ffi/builder/type/object.rb +0 -1
  25. data/lib/gir_ffi/builder/type/struct_based.rb +0 -6
  26. data/lib/gir_ffi/builder/type/union.rb +0 -4
  27. data/lib/gir_ffi/builder/type/with_layout.rb +0 -1
  28. data/lib/gir_ffi/builder/type/with_methods.rb +1 -7
  29. data/lib/gir_ffi/callback.rb +9 -0
  30. data/lib/gir_ffi/callback_helper.rb +8 -1
  31. data/lib/gir_ffi/class_base.rb +6 -26
  32. data/lib/gir_ffi/error_argument_builder.rb +17 -0
  33. data/lib/gir_ffi/{builder/function.rb → function_builder.rb} +14 -11
  34. data/lib/gir_ffi/in_out_pointer.rb +29 -36
  35. data/lib/gir_ffi/in_pointer.rb +1 -1
  36. data/lib/gir_ffi/info_ext.rb +6 -0
  37. data/lib/gir_ffi/info_ext/i_field_info.rb +0 -2
  38. data/lib/gir_ffi/info_ext/i_type_info.rb +29 -10
  39. data/lib/gir_ffi/info_ext/safe_constant_name.rb +21 -0
  40. data/lib/gir_ffi/info_ext/safe_function_name.rb +13 -0
  41. data/lib/gir_ffi/interface_base.rb +1 -11
  42. data/lib/gir_ffi/module_base.rb +0 -10
  43. data/lib/gir_ffi/null_argument_builder.rb +9 -0
  44. data/lib/gir_ffi/return_value_builder.rb +75 -0
  45. data/lib/gir_ffi/setter_argument_info.rb +16 -0
  46. data/lib/gir_ffi/type_map.rb +10 -1
  47. data/lib/gir_ffi/version.rb +1 -1
  48. data/tasks/test.rake +61 -0
  49. data/test/base_test_helper.rb +0 -2
  50. data/test/ffi-gobject/value_test.rb +15 -0
  51. data/test/ffi-gobject_introspection/i_base_info_test.rb +31 -6
  52. data/test/ffi-gobject_introspection/i_function_info_test.rb +0 -17
  53. data/test/ffi-gobject_introspection/lib_test.rb +0 -55
  54. data/test/ffi-gobject_test.rb +2 -1
  55. data/test/gir_ffi/argument_builder_test.rb +414 -0
  56. data/test/gir_ffi/base_argument_builder_test.rb +13 -0
  57. data/test/gir_ffi/builder/module_test.rb +4 -40
  58. data/test/gir_ffi/builder/type/callback_test.rb +0 -27
  59. data/test/gir_ffi/builder/type/constant_test.rb +0 -12
  60. data/test/gir_ffi/builder/type/enum_test.rb +0 -20
  61. data/test/gir_ffi/builder/type/interface_test.rb +0 -11
  62. data/test/gir_ffi/builder/type/object_test.rb +2 -2
  63. data/test/gir_ffi/builder/type/struct_test.rb +0 -39
  64. data/test/gir_ffi/builder/type/unintrospectable_test.rb +1 -1
  65. data/test/gir_ffi/builder/type/union_test.rb +0 -11
  66. data/test/gir_ffi/builder_test.rb +3 -11
  67. data/test/gir_ffi/callback_helper_test.rb +7 -0
  68. data/test/gir_ffi/{builder/function_test.rb → function_builder_test.rb} +16 -28
  69. data/test/gir_ffi/in_out_pointer_test.rb +0 -20
  70. data/test/gir_ffi/info_ext/i_type_info_test.rb +112 -26
  71. data/test/gir_ffi/info_ext/safe_constant_name_test.rb +16 -0
  72. data/test/gir_ffi/info_ext/safe_function_name_test.rb +22 -0
  73. data/test/gir_ffi/return_value_builder_test.rb +355 -0
  74. data/test/integration/generated_gimarshallingtests_test.rb +608 -296
  75. data/test/integration/generated_regress_test.rb +879 -494
  76. metadata +35 -24
  77. data/lib/gir_ffi/builder/argument.rb +0 -569
  78. data/lib/gir_ffi/builder/argument/base.rb +0 -151
  79. data/lib/gir_ffi/builder/argument/in_base.rb +0 -14
  80. data/lib/gir_ffi/builder/argument/in_out_base.rb +0 -18
  81. data/lib/gir_ffi/builder/argument/out_base.rb +0 -15
  82. data/test/gir_ffi/builder/argument/base_test.rb +0 -55
  83. data/test/integration/pretty_print_test.rb +0 -33
@@ -1,5 +1,7 @@
1
- require 'gir_ffi/builder/argument'
1
+ require 'gir_ffi/argument_builder'
2
+ require 'gir_ffi/return_value_builder'
2
3
  require 'gir_ffi/variable_name_generator'
4
+ require 'gir_ffi/setter_argument_info'
3
5
 
4
6
  module GirFFI
5
7
  module Builder
@@ -44,14 +46,14 @@ module GirFFI
44
46
  end
45
47
 
46
48
  def return_value_builder
47
- @rv_builder ||= ReturnValueFactory.builder_for_field_getter(
48
- VariableNameGenerator.new, @info.name, @info.field_type, :return)
49
+ vargen = VariableNameGenerator.new
50
+ @rv_builder ||= ReturnValueBuilder.new vargen, @info.field_type, false
49
51
  end
50
52
 
51
53
  def setter_builder
52
- type = @info.field_type
53
54
  vargen = VariableNameGenerator.new
54
- Builder::InArgument.builder_for vargen, "value", type, :in, @libmodule
55
+ argument_info = SetterArgumentInfo.new "value", @info.field_type
56
+ ArgumentBuilder.new vargen, argument_info
55
57
  end
56
58
  end
57
59
  end
@@ -1,6 +1,6 @@
1
1
  require 'gir_ffi/builder_helper'
2
2
  require 'gir_ffi/module_base'
3
- require 'gir_ffi/builder/function'
3
+ require 'gir_ffi/function_builder'
4
4
  require 'indentation'
5
5
 
6
6
  module GirFFI
@@ -60,15 +60,6 @@ module GirFFI
60
60
  @module
61
61
  end
62
62
 
63
- def pretty_print
64
- buf = "module #{@safe_namespace}\n"
65
- gir.infos(@namespace).each do |info|
66
- buf << sub_builder(info).pretty_print.indent
67
- buf << "\n"
68
- end
69
- buf << "end"
70
- end
71
-
72
63
  private
73
64
 
74
65
  def build_dependencies
@@ -103,13 +94,11 @@ module GirFFI
103
94
  @lib.ffi_lib(*libspec.split(/,/))
104
95
  end
105
96
  end
106
-
107
- optionally_define_constant(@lib, :CALLBACKS) { [] }
108
97
  end
109
98
 
110
99
  def sub_builder info
111
100
  if info.info_type == :function
112
- Builder::Function.new(info, libmodule)
101
+ FunctionBuilder.new(info, libmodule)
113
102
  else
114
103
  Builder::Type.builder_for info
115
104
  end
@@ -126,7 +115,7 @@ module GirFFI
126
115
  end
127
116
 
128
117
  def function_definition info, libmodule
129
- Builder::Function.new(info, libmodule).generate
118
+ FunctionBuilder.new(info, libmodule).generate
130
119
  end
131
120
 
132
121
  def gir
@@ -1,5 +1,3 @@
1
- require 'gir_ffi/info_ext/i_property_info'
2
-
3
1
  module GirFFI
4
2
  module Builder
5
3
  # Creates property getter and setter code for a given IPropertyInfo.
@@ -13,14 +13,6 @@ module GirFFI
13
13
  end
14
14
  end
15
15
 
16
- def pretty_print
17
- args = argument_types.map do |arg|
18
- arg.is_a?(FFI::Enum) ? arg.tag : arg.inspect
19
- end
20
- return "#{@classname} = Lib.callback #{callback_sym.inspect}, " +
21
- "[#{args.join(', ')}], #{return_type.inspect}"
22
- end
23
-
24
16
  def callback_sym
25
17
  @classname.to_sym
26
18
  end
@@ -8,10 +8,6 @@ module GirFFI
8
8
  # triggered by a missing constant in the parent namespace. The
9
9
  # constant will be attached to the appropriate namespace module.
10
10
  class Constant < Base
11
- def pretty_print
12
- "#{@classname} = #{info.value.inspect}"
13
- end
14
-
15
11
  private
16
12
 
17
13
  def instantiate_class
@@ -7,10 +7,6 @@ module GirFFI
7
7
  # attached to the appropriate namespace module, and will be defined
8
8
  # as an enum for FFI.
9
9
  class Enum < RegisteredType
10
- def pretty_print
11
- "#{@classname} = Lib.enum #{enum_sym.inspect}, #{value_spec.inspect}"
12
- end
13
-
14
10
  private
15
11
 
16
12
  def enum_sym
@@ -10,10 +10,6 @@ module GirFFI
10
10
  class Interface < RegisteredType
11
11
  include WithMethods
12
12
 
13
- def pretty_print
14
- "module #{@classname}\n extend InterfaceBase\nend"
15
- end
16
-
17
13
  private
18
14
 
19
15
  # FIXME: The word 'class' is not really correct.
@@ -1,6 +1,5 @@
1
1
  require 'gir_ffi/builder/type/struct_based'
2
2
  require 'gir_ffi/builder/property'
3
- require 'gir_ffi/info_ext/i_property_info'
4
3
  require 'gir_ffi/object_base'
5
4
 
6
5
  module GirFFI
@@ -12,12 +12,6 @@ module GirFFI
12
12
  include WithMethods
13
13
  include WithLayout
14
14
 
15
- def pretty_print
16
- buf = "class #{@classname}\n"
17
- buf << pretty_print_methods
18
- buf << "end"
19
- end
20
-
21
15
  private
22
16
 
23
17
  def setup_class
@@ -12,10 +12,6 @@ module GirFFI
12
12
  include WithMethods
13
13
  include WithLayout
14
14
 
15
- def pretty_print
16
- "class #{@classname}\nend"
17
- end
18
-
19
15
  private
20
16
 
21
17
  def setup_class
@@ -1,5 +1,4 @@
1
1
  require 'gir_ffi/builder/field'
2
- require 'gir_ffi/info_ext/i_field_info'
3
2
 
4
3
  module GirFFI
5
4
  module Builder
@@ -38,7 +38,7 @@ module GirFFI
38
38
  end
39
39
 
40
40
  def function_definition_builder go
41
- Builder::Function.new(go, lib)
41
+ FunctionBuilder.new(go, lib)
42
42
  end
43
43
 
44
44
  def function_definition go
@@ -58,12 +58,6 @@ module GirFFI
58
58
  @klass.class_eval MethodStubber.new(minfo).method_stub
59
59
  end
60
60
  end
61
-
62
- def pretty_print_methods
63
- info.get_methods.map do |minfo|
64
- function_definition_builder(minfo).pretty_print.indent + "\n"
65
- end.join
66
- end
67
61
  end
68
62
  end
69
63
  end
@@ -0,0 +1,9 @@
1
+ module GirFFI
2
+ class Callback < Proc
3
+ def self.from namespace, name, prc
4
+ GirFFI::CallbackHelper.wrap_in_callback_args_mapper(namespace, name, prc).tap do |cb|
5
+ GirFFI::CallbackHelper.store_callback cb
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,10 +1,13 @@
1
1
  module GirFFI
2
+ # TODO: Turn module into a class, use instance methods.
2
3
  module CallbackHelper
4
+ CALLBACKS = []
5
+
3
6
  def self.wrap_in_callback_args_mapper namespace, name, prc
4
7
  return prc if FFI::Function === prc
5
8
  return nil if prc.nil?
6
9
  info = GObjectIntrospection::IRepository.default.find_by_name namespace, name
7
- return Proc.new do |*args|
10
+ return Callback.new do |*args|
8
11
  prc.call(*map_callback_args(args, info))
9
12
  end
10
13
  end
@@ -55,6 +58,10 @@ module GirFFI
55
58
  GirFFI::ArgHelper::OBJECT_STORE[arg.address]
56
59
  end
57
60
  end
61
+
62
+ def self.store_callback prc
63
+ CALLBACKS << prc
64
+ end
58
65
  end
59
66
  end
60
67
 
@@ -11,8 +11,8 @@ module GirFFI
11
11
 
12
12
  def setup_and_call method, *arguments, &block
13
13
  result = self.class.ancestors.any? do |klass|
14
- klass.respond_to?(:_setup_instance_method) &&
15
- klass._setup_instance_method(method.to_s)
14
+ klass.respond_to?(:setup_instance_method) &&
15
+ klass.setup_instance_method(method.to_s)
16
16
  end
17
17
 
18
18
  unless result
@@ -24,8 +24,8 @@ module GirFFI
24
24
 
25
25
  def self.setup_and_call method, *arguments, &block
26
26
  result = self.ancestors.any? do |klass|
27
- klass.respond_to?(:_setup_method) &&
28
- klass._setup_method(method.to_s)
27
+ klass.respond_to?(:setup_method) &&
28
+ klass.setup_method(method.to_s)
29
29
  end
30
30
 
31
31
  unless result
@@ -36,40 +36,20 @@ module GirFFI
36
36
  end
37
37
 
38
38
  class << self
39
- # @deprecated Compatibility function. Remove in version 0.5.0.
40
- def ffi_structure
41
- self::Struct
42
- end
43
-
44
39
  def gir_info
45
40
  self.const_get :GIR_INFO
46
41
  end
47
42
 
48
- # @deprecated Compatibility function. Remove in version 0.5.0.
49
- def _builder
50
- gir_ffi_builder
51
- end
52
-
53
43
  def gir_ffi_builder
54
44
  self.const_get :GIR_FFI_BUILDER
55
45
  end
56
46
 
57
- # @deprecated Compatibility function. Remove in version 0.5.0.
58
- def _setup_method name
59
- setup_method name
60
- end
61
-
62
47
  def setup_method name
63
- _builder.setup_method name
64
- end
65
-
66
- # @deprecated Compatibility function. Remove in version 0.5.0.
67
- def _setup_instance_method name
68
- setup_instance_method name
48
+ gir_ffi_builder.setup_method name
69
49
  end
70
50
 
71
51
  def setup_instance_method name
72
- _builder.setup_instance_method name
52
+ gir_ffi_builder.setup_instance_method name
73
53
  end
74
54
 
75
55
  alias_method :_real_new, :new
@@ -0,0 +1,17 @@
1
+ require 'gir_ffi/base_argument_builder'
2
+
3
+ module GirFFI
4
+ # Implements argument processing for error handling arguments. These
5
+ # arguments are not part of the introspected signature, but their
6
+ # presence is indicated by the 'throws' attribute of the function.
7
+ class ErrorArgumentBuilder < BaseArgumentBuilder
8
+ def pre
9
+ [ "#{callarg} = FFI::MemoryPointer.new(:pointer).write_pointer nil" ]
10
+ end
11
+
12
+ def post
13
+ [ "GirFFI::ArgHelper.check_error(#{callarg})" ]
14
+ end
15
+ end
16
+ end
17
+
@@ -1,10 +1,13 @@
1
- require 'gir_ffi/builder/argument'
1
+ require 'gir_ffi/argument_builder'
2
+ require 'gir_ffi/return_value_builder'
3
+ require 'gir_ffi/error_argument_builder'
4
+ require 'gir_ffi/null_argument_builder'
2
5
  require 'gir_ffi/variable_name_generator'
3
6
 
4
- module GirFFI::Builder
7
+ module GirFFI
5
8
  # Implements the creation of a Ruby function definition out of a GIR
6
9
  # IFunctionInfo.
7
- class Function
10
+ class FunctionBuilder
8
11
  def initialize info, libmodule
9
12
  @info = info
10
13
  @libmodule = libmodule
@@ -12,8 +15,9 @@ module GirFFI::Builder
12
15
 
13
16
  def generate
14
17
  vargen = GirFFI::VariableNameGenerator.new
15
- @data = @info.args.map {|arg| Argument.build vargen, arg, @libmodule}
16
- @rvdata = ReturnValueFactory.build vargen, @info
18
+ @data = @info.args.map {|arg| ArgumentBuilder.new vargen, arg }
19
+ @rvdata = ReturnValueBuilder.new(vargen, @info.return_type,
20
+ @info.constructor?)
17
21
 
18
22
  alldata = @data.dup << @rvdata
19
23
 
@@ -29,14 +33,10 @@ module GirFFI::Builder
29
33
  return filled_out_template
30
34
  end
31
35
 
32
- def pretty_print
33
- generate
34
- end
35
-
36
36
  private
37
37
 
38
38
  def setup_error_argument vargen
39
- klass = @info.throws? ? ErrorArgument : NullArgument
39
+ klass = @info.throws? ? ErrorArgumentBuilder : NullArgumentBuilder
40
40
  @errarg = klass.new vargen, nil, nil, :error
41
41
  end
42
42
 
@@ -78,7 +78,10 @@ module GirFFI::Builder
78
78
  end
79
79
 
80
80
  def post
81
- po = (@data.map(&:post) + @data.map(&:postpost) + @rvdata.post)
81
+ args = @data.sort_by {|arg| arg.type_info.array_length}
82
+
83
+ po = args.map {|arg|arg.post} +
84
+ @rvdata.post
82
85
  po.unshift @errarg.post
83
86
 
84
87
  po += @data.map {|item| item.cleanup}
@@ -4,11 +4,17 @@ module GirFFI
4
4
  class InOutPointer < FFI::Pointer
5
5
  attr_reader :value_type, :sub_type
6
6
 
7
- def initialize ptr, type, ffi_type, sub_type=nil
8
- super ptr
9
- @ffi_type = ffi_type
7
+ def initialize value, type, sub_type=nil
8
+ @ffi_type = TypeMap.map_basic_type_or_string type
10
9
  @value_type = type
11
10
  @sub_type = sub_type
11
+
12
+ value = adjust_value_in value
13
+
14
+ ptr = AllocationHelper.safe_malloc(FFI.type_size @ffi_type)
15
+ ptr.send "put_#{@ffi_type}", 0, value
16
+
17
+ super ptr
12
18
  end
13
19
 
14
20
  private :initialize
@@ -20,30 +26,26 @@ module GirFFI
20
26
 
21
27
  def to_sized_array_value size
22
28
  # FIXME: Simulated Polymorphism.
23
- raise "Not allowed" if @value_type != :pointer or @sub_type.nil?
29
+ raise "Not allowed" if @ffi_type != :pointer or @sub_type.nil?
24
30
  block = self.read_pointer
25
31
  return nil if block.null?
26
32
  ArgHelper.ptr_to_typed_array @sub_type, block, size
27
33
  end
28
34
 
29
35
  def self.for type, sub_type=nil
30
- ffi_type = TypeMap.map_basic_type_or_string type
31
- ptr = AllocationHelper.safe_malloc(FFI.type_size ffi_type)
32
- ptr.send "put_#{ffi_type}", 0, nil_value_for(type)
33
- self.new ptr, type, ffi_type, sub_type
36
+ if Array === type
37
+ return self.new nil, *type
38
+ end
39
+ self.new nil, type, sub_type
34
40
  end
35
41
 
36
42
  def self.from type, value, sub_type=nil
37
43
  if Array === type
38
- arr_t, sub_t = *type
44
+ _, sub_t = *type
39
45
  # TODO: Take array type into account (zero-terminated or not)
40
46
  return self.from_array sub_t, value
41
47
  end
42
- value = adjust_value_in type, value
43
- ffi_type = TypeMap.map_basic_type_or_string type
44
- ptr = AllocationHelper.safe_malloc(FFI.type_size ffi_type)
45
- ptr.send "put_#{ffi_type}", 0, value
46
- self.new ptr, type, ffi_type, sub_type
48
+ self.new value, type, sub_type
47
49
  end
48
50
 
49
51
  def self.from_array type, array
@@ -52,33 +54,24 @@ module GirFFI
52
54
  self.from :pointer, ptr, type
53
55
  end
54
56
 
55
- def self.for_array type
56
- self.for :pointer, type
57
- end
57
+ private
58
58
 
59
- class << self
60
- def adjust_value_in type, value
61
- case type
62
- when :gboolean
63
- (value ? 1 : 0)
64
- when :utf8
65
- InPointer.from :utf8, value
66
- else
67
- value
68
- end
69
- end
59
+ def adjust_value_in value
60
+ return nil_value if value.nil?
70
61
 
71
- def nil_value_for type
72
- case type
73
- when :utf8, :pointer
74
- nil
75
- else
76
- 0
77
- end
62
+ case @value_type
63
+ when :gboolean
64
+ (value ? 1 : 0)
65
+ when :utf8
66
+ InPointer.from :utf8, value
67
+ else
68
+ value
78
69
  end
79
70
  end
80
71
 
81
- private
72
+ def nil_value
73
+ @ffi_type == :pointer ? nil : 0
74
+ end
82
75
 
83
76
  def adjust_value_out value
84
77
  case @value_type