gir_ffi 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.0.3 / 2010-11-19
2
+
3
+ * Update to restore Ruby 1.9 support.
4
+ * Handle functions with the 'throws' property set.
5
+ * Handle classes without specified fields.
6
+
1
7
  == 0.0.2 / 2010-11-14
2
8
 
3
9
  * Several fixes to method creation.
data/lib/gir_ffi/base.rb CHANGED
@@ -14,16 +14,6 @@ module GirFFI
14
14
  self.class.ffi_structure
15
15
  end
16
16
 
17
- def _fake_missing *args, &block
18
- method_missing method_name.to_sym, *args, &block
19
- end
20
-
21
- private
22
-
23
- def method_name
24
- caller[0].gsub(/.*`(.*)'/, '\1')
25
- end
26
-
27
17
  class << self
28
18
  def ffi_structure
29
19
  self.const_get(:Struct)
@@ -109,6 +109,7 @@ module GirFFI
109
109
  end
110
110
  if info.type == :function
111
111
  types.unshift :pointer if info.method?
112
+ types << :pointer if info.throws?
112
113
  end
113
114
  types
114
115
  end
@@ -69,27 +69,40 @@ module GirFFI
69
69
  end
70
70
 
71
71
  def setup_layout
72
- layoutspec = []
73
- @info.fields.each do |f|
74
- layoutspec << f.name.to_sym
72
+ spec = layout_specification
73
+ @structklass.class_eval { layout(*spec) }
74
+ end
75
75
 
76
- ffitype = Builder.itypeinfo_to_ffitype f.type
77
- if ffitype.kind_of?(Class) and BuilderHelper.const_defined_for ffitype, :Struct
78
- ffitype = ffitype.const_get :Struct
76
+ def layout_specification
77
+ if @info.fields.empty?
78
+ if @parent
79
+ return [:parent, @superclass.const_get(:Struct), 0]
79
80
  end
81
+ end
82
+ spec = []
83
+ @info.fields.each do |f|
84
+ spec << f.name.to_sym
85
+ spec << itypeinfo_to_ffitype_for_struct(f.type)
86
+ spec << f.offset
87
+ end
88
+ spec
89
+ end
80
90
 
81
- layoutspec << ffitype
82
-
83
- layoutspec << f.offset
91
+ def itypeinfo_to_ffitype_for_struct typeinfo
92
+ ffitype = Builder.itypeinfo_to_ffitype typeinfo
93
+ if ffitype.kind_of?(Class) and BuilderHelper.const_defined_for ffitype, :Struct
94
+ ffitype = ffitype.const_get :Struct
84
95
  end
85
- @structklass.class_eval { layout(*layoutspec) }
96
+ ffitype
86
97
  end
87
98
 
88
99
  def alias_instance_methods
89
100
  @info.methods.each do |m|
90
- @klass.class_eval do
91
- alias_method m.name.to_sym, :_fake_missing
92
- end
101
+ @klass.class_eval "
102
+ def #{m.name} *args, &block
103
+ method_missing :#{m.name}, *args, &block
104
+ end
105
+ "
93
106
  end
94
107
  end
95
108
 
@@ -116,6 +116,13 @@ module GirFFI
116
116
  end
117
117
 
118
118
  def adjust_accumulators
119
+ if @info.throws?
120
+ errvar = new_var
121
+ @pre << "#{errvar} = FFI::MemoryPointer.new(:pointer).write_pointer nil"
122
+ @post << "GirFFI::ArgHelper.check_error(#{errvar})"
123
+ @callargs << errvar
124
+ end
125
+
119
126
  @post << "return #{@retvals.join(', ')}" unless @retvals.empty?
120
127
 
121
128
  if @info.method?
@@ -0,0 +1,8 @@
1
+ module GirFFI
2
+ # Wraps a GIErrorDomainInfo struct.
3
+ # Represents an error domain.
4
+ # Not implemented yet.
5
+ class IErrorDomainInfo < IRegisteredTypeInfo
6
+ end
7
+ end
8
+
@@ -1,5 +1,6 @@
1
1
  module GirFFI
2
2
  # Wraps a GIEnumInfo struct, if it represents a flag type.
3
+ # TODO: Perhaps just use IEnumInfo. Seems to make more sense.
3
4
  class IFlagsInfo < IEnumInfo
4
5
  end
5
6
  end
@@ -19,24 +19,27 @@ require 'gir_ffi/i_value_info'
19
19
  require 'gir_ffi/i_union_info'
20
20
  require 'gir_ffi/i_enum_info'
21
21
  require 'gir_ffi/i_flags_info'
22
+ require 'gir_ffi/i_error_domain_info'
22
23
 
23
24
  module GirFFI
24
25
  # The Gobject Introspection Repository. This class is the point of
25
26
  # access to the introspection typelibs.
26
27
  # This class wraps the GIRepository struct.
27
28
  class IRepository
29
+ # Map info type to class. Default is IBaseInfo.
28
30
  TYPEMAP = {
29
- #:invalid,
31
+ :invalid => IBaseInfo,
30
32
  :function => IFunctionInfo,
31
33
  :callback => ICallbackInfo,
32
34
  :struct => IStructInfo,
33
- #:boxed => ,
35
+ # TODO: There's no GIBoxedInfo, so what does :boxed mean?
36
+ :boxed => IBaseInfo,
34
37
  :enum => IEnumInfo,
35
38
  :flags => IFlagsInfo,
36
39
  :object => IObjectInfo,
37
40
  :interface => IInterfaceInfo,
38
41
  :constant => IConstantInfo,
39
- # :error_domain,
42
+ :error_domain => IErrorDomainInfo,
40
43
  :union => IUnionInfo,
41
44
  :value => IValueInfo,
42
45
  :signal => ISignalInfo,
@@ -45,7 +48,7 @@ module GirFFI
45
48
  :field => IFieldInfo,
46
49
  :arg => IArgInfo,
47
50
  :type => ITypeInfo,
48
- #:unresolved
51
+ :unresolved => IBaseInfo
49
52
  }
50
53
 
51
54
  def initialize
@@ -63,11 +66,7 @@ module GirFFI
63
66
  Lib.g_type_tag_to_string type
64
67
  end
65
68
 
66
- def n_infos namespace
67
- Lib.g_irepository_get_n_infos @gobj, namespace
68
- end
69
-
70
- def require namespace, version
69
+ def require namespace, version=nil
71
70
  errpp = FFI::MemoryPointer.new(:pointer).write_pointer nil
72
71
 
73
72
  Lib.g_irepository_require @gobj, namespace, version, 0, errpp
@@ -76,11 +75,22 @@ module GirFFI
76
75
  raise GError.new(errp)[:message] unless errp.null?
77
76
  end
78
77
 
78
+ def n_infos namespace
79
+ Lib.g_irepository_get_n_infos @gobj, namespace
80
+ end
81
+
79
82
  def info namespace, index
80
83
  ptr = Lib.g_irepository_get_info @gobj, namespace, index
81
84
  return wrap ptr
82
85
  end
83
86
 
87
+ # Utility method
88
+ def infos namespace
89
+ (0..(n_infos(namespace) - 1)).map do |i|
90
+ info namespace, i
91
+ end
92
+ end
93
+
84
94
  def find_by_name namespace, name
85
95
  ptr = Lib.g_irepository_find_by_name @gobj, namespace, name
86
96
  return wrap ptr
data/test/base_test.rb CHANGED
@@ -10,63 +10,9 @@ class BaseTest < Test::Unit::TestCase
10
10
  def self.new; self._real_new; end
11
11
  end
12
12
  end
13
-
14
- should "be able to use method_name to get the names of its methods" do
15
- @klass.class_eval do
16
- def this_is_my_name
17
- method_name
18
- end
19
- end
20
- assert_equal "this_is_my_name", @klass.new.this_is_my_name
21
- end
22
-
23
- context "its #_fake_missing method" do
24
- should "not be missing" do
25
- assert @klass.new.respond_to? :_fake_missing
26
- end
27
-
28
- should "call method_missing" do
29
- @klass.class_eval do
30
- def method_missing method, *args
31
- method
32
- end
33
- end
34
- assert_equal :_fake_missing, @klass.new._fake_missing
35
- end
36
-
37
- should "pass on its arguments" do
38
- @klass.class_eval do
39
- def method_missing method, *args
40
- args.join(', ')
41
- end
42
- end
43
- assert_equal "a, b", @klass.new._fake_missing("a", "b")
44
- end
45
-
46
- should "pass on a given block" do
47
- @klass.class_eval do
48
- def method_missing method, *args
49
- yield if block_given?
50
- end
51
- end
52
- assert_equal :called, @klass.new._fake_missing { :called }
53
- end
54
- end
55
-
56
- should "be able to use alias_method to create a self-defining method" do
57
- @klass.class_eval do
58
- def method_missing method, *args
59
- self.class.class_eval "
60
- undef #{method}
61
- def #{method}
62
- :redefined
63
- end
64
- "
65
- self.send method
66
- end
67
- alias_method :new_method, :_fake_missing
68
- end
69
- assert_equal :redefined, @klass.new.new_method
13
+ # TODO: See if we can test some part of Base again.
14
+ should "pass" do
15
+ assert true
70
16
  end
71
17
  end
72
18
  end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('test_helper.rb', File.dirname(__FILE__))
2
+ require 'gir_ffi'
3
+
4
+ class ClassBuilderTest < Test::Unit::TestCase
5
+ context "The ClassBuilder" do
6
+ setup do
7
+ @gir = GirFFI::IRepository.default
8
+ @gir.require 'GObject', nil
9
+ end
10
+ should "use parent struct as default layout" do
11
+ @classbuilder = GirFFI::ClassBuilder.new 'Foo', 'Bar'
12
+ stub(info = Object.new).parent { @gir.find_by_name 'GObject', 'Object' }
13
+ stub(info).fields { [] }
14
+ @classbuilder.instance_eval { @info = info }
15
+ @classbuilder.instance_eval { @parent = info.parent }
16
+ @classbuilder.instance_eval { @superclass = GObject::Object }
17
+
18
+ spec = @classbuilder.send :layout_specification
19
+ assert_equal [:parent, GObject::Object::Struct, 0], spec
20
+ end
21
+ end
22
+ end
23
+
@@ -55,5 +55,21 @@ class FunctionDefinitionBuilderTest < Test::Unit::TestCase
55
55
 
56
56
  assert_equal cws(expected), cws(code)
57
57
  end
58
+
59
+ should "build correct definition of Everything::TestObj#new_from_file" do
60
+ go = get_method_introspection_data 'Everything', 'TestObj', 'new_from_file'
61
+ fbuilder = GirFFI::FunctionDefinitionBuilder.new go, Lib
62
+ code = fbuilder.generate
63
+
64
+ expected =
65
+ "def new_from_file x
66
+ _v2 = FFI::MemoryPointer.new(:pointer).write_pointer nil
67
+ _v1 = Lib.test_obj_new_from_file x, _v2
68
+ GirFFI::ArgHelper.check_error(_v2)
69
+ return Everything::TestObj._real_new(_v1)
70
+ end"
71
+
72
+ assert_equal cws(expected), cws(code)
73
+ end
58
74
  end
59
75
  end
@@ -31,7 +31,13 @@ module GirFFI
31
31
 
32
32
  should "allow version to be nil" do
33
33
  assert_nothing_raised do
34
- IRepository.default.require 'Gtk', nil
34
+ IRepository.default.require 'Everything', nil
35
+ end
36
+ end
37
+
38
+ should "allow version to be left out" do
39
+ assert_nothing_raised do
40
+ IRepository.default.require 'Everything'
35
41
  end
36
42
  end
37
43
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'shoulda'
2
+ require 'rr'
2
3
  require 'ffi'
3
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
5
 
@@ -23,6 +24,7 @@ module Lib
23
24
  end
24
25
 
25
26
  class Test::Unit::TestCase
27
+ include RR::Adapters::TestUnit
26
28
  def cws code
27
29
  code.gsub(/(^\s*|\s*$)/, "")
28
30
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gir_ffi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matijs van Zuijlen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-14 00:00:00 +01:00
18
+ date: 2010-11-19 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,22 @@ dependencies:
50
50
  version: 2.11.3
51
51
  type: :development
52
52
  version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rr
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 19
62
+ segments:
63
+ - 1
64
+ - 0
65
+ - 2
66
+ version: 1.0.2
67
+ type: :development
68
+ version_requirements: *id003
53
69
  description: Ruby-FFI-based binding of the GObject Introspection Repository
54
70
  email:
55
71
  - matijs@matijs.net
@@ -88,6 +104,7 @@ files:
88
104
  - lib/gir_ffi/i_callable_info.rb
89
105
  - lib/gir_ffi/function_definition_builder.rb
90
106
  - lib/gir_ffi/i_function_info.rb
107
+ - lib/gir_ffi/i_error_domain_info.rb
91
108
  - lib/gir_ffi/version.rb
92
109
  - lib/gir_ffi/overrides/gtk.rb
93
110
  - lib/gir_ffi/allocation_helper.rb
@@ -105,6 +122,7 @@ files:
105
122
  - test/function_definition_builder_test.rb
106
123
  - test/arg_helper_test.rb
107
124
  - test/test_helper.rb
125
+ - test/class_builder_test.rb
108
126
  - test/girffi_test.rb
109
127
  - test/constructor_definition_builder_test.rb
110
128
  - test/base_test.rb
@@ -167,6 +185,7 @@ test_files:
167
185
  - test/arg_helper_test.rb
168
186
  - test/base_test.rb
169
187
  - test/builder_test.rb
188
+ - test/class_builder_test.rb
170
189
  - test/constructor_definition_builder_test.rb
171
190
  - test/everything_test.rb
172
191
  - test/function_definition_builder_test.rb