fiddley 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40c44de1a787ff01ff58cbd885e28e4aebffb063
4
- data.tar.gz: b2bba367d2214502e68bbb86f4f9ebea98dc52d6
3
+ metadata.gz: bf2d272d2170487e17fb8f3c96e70076228361f6
4
+ data.tar.gz: c1a3515ec44871af8a51f0e482f1bcc0352903f0
5
5
  SHA512:
6
- metadata.gz: b160ea03d9468ac5481b0cf9a19112ceda1bc3f991ffe01b925cc6fae96fdad428b083f6bd32b7452a5e09f2668feaae6bca93c0fb96412cd37248538fea8ac8
7
- data.tar.gz: 334b5e0d787047ab6bc0b43390b375769424d663cef00d11d1eaab7822db13dd4c845a65b1e27e3764a6fa2c4e00f0f25778daf3e6302b6dcb2dbcc1e4623116
6
+ metadata.gz: 8679a00557727ed2d2590f82f7d85f9977c415f5001338d4463653f28d407b0270dbd24b6e51a20a42ee4139e2cfa7c8de197a4b371b519cabd49a1e1e88b270
7
+ data.tar.gz: 538b0728b44e29d737fbfb6a3f0d8ff0f572049853ed45a2a4cdd554eaec1ddc95528659b648dde7a8d65b5e6a4cee1d4e321631801b879cff6159b978067fb5
data/README.md CHANGED
@@ -9,6 +9,8 @@ Status
9
9
 
10
10
  Under development.
11
11
 
12
+ For example, now can run [optcarrot](https://github.com/mame/optcarrot).
13
+
12
14
 
13
15
 
14
16
  License
@@ -1,5 +1,5 @@
1
1
  module Fiddley
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
4
4
 
5
5
  require "fiddley/function"
@@ -1,14 +1,12 @@
1
- require "fiddle/import"
1
+ require "fiddle"
2
2
  require "fiddley/utils"
3
3
 
4
4
  module Fiddley
5
- class Function
5
+ class Function < Fiddle::Closure::BlockCaller
6
+ include Fiddley::Utils
7
+
6
8
  def initialize(ret, params, blk)
7
- Module.new do
8
- extend Fiddle::Importer
9
- dlload Fiddley::Library::LIBC
10
- @@func = bind("#{Fiddley::Utils.type2str(ret)} callback(#{params.map{|e| Fiddley::Utils.type2str(e)}.join(', ')})", &blk)
11
- end
9
+ super(type2type(ret), params.map{|e| type2type(e)}, &blk)
12
10
  end
13
11
  end
14
12
  end
@@ -6,15 +6,27 @@ module Fiddley
6
6
  include Fiddley::Utils
7
7
  include Fiddle::Importer
8
8
 
9
- def ffi_lib(so)
10
- begin
11
- dlload so
12
- rescue Fiddle::DLError
13
- begin
14
- dlload LIBPREFIX + so
15
- rescue Fiddle::DLError
16
- dlload LIBPREFIX + so + "." + LIBSUFFIX
9
+ def ffi_lib(*args)
10
+ args.each do |soes|
11
+ soes = [soes] unless soes.is_a?(Array)
12
+ loaded = false
13
+ soes.each do |so|
14
+ begin
15
+ begin
16
+ dlload so
17
+ rescue Fiddle::DLError
18
+ begin
19
+ dlload LIBPREFIX + so
20
+ rescue Fiddle::DLError
21
+ dlload LIBPREFIX + so + "." + LIBSUFFIX
22
+ end
23
+ end
24
+ loaded = true
25
+ break
26
+ rescue Fiddle::DLError
27
+ end
17
28
  end
29
+ raise Fiddle::DLError, "can't load #{soes.join(' or ')}" unless loaded
18
30
  end
19
31
  end
20
32
 
@@ -11,7 +11,7 @@ module Fiddley
11
11
  @size = 0
12
12
  args.each_slice(2) do |name, type|
13
13
  @members[name] = [type, @size]
14
- @size += type2size(type)
14
+ @size += type2offset_size(type)
15
15
  end
16
16
  end
17
17
 
@@ -1,3 +1,5 @@
1
+ require "fiddle"
2
+
1
3
  module Fiddley
2
4
  unless "".respond_to?(:unpack1)
3
5
  module RefineStringUnpack1
@@ -13,27 +15,56 @@ module Fiddley
13
15
 
14
16
  module Utils
15
17
  # assumes short = 16bit, int = 32bit, long long = 64bit
16
- LONG_SIZE = [0].pack('l!').bytesize
17
- POINTER_SIZE = [nil].pack('p').bytesize
18
- SIZET_FORMAT = POINTER_SIZE == LONG_SIZE ? 'l!' : 'q'
19
- SIZET_TYPE = POINTER_SIZE == LONG_SIZE ? 'unsigned long' : 'unsigned long long'
18
+ SIZET_FORMAT = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'l!' : 'q'
19
+ SIZET_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'unsigned long' : 'unsigned long long'
20
20
 
21
21
  module_function def type2size(type)
22
22
  case type
23
23
  when :char, :uchar, :int8, :uint8
24
- 1
24
+ Fiddle::SIZEOF_CHAR
25
+ when :short, :ushort, :int16, :uint16
26
+ Fiddle::SIZEOF_SHORT
27
+ when :int, :uint, :int32, :uint32, :bool
28
+ Fiddle::SIZEOF_INT
29
+ when :long, :ulong
30
+ Fiddle::SIZEOF_LONG
31
+ when :int64, :uint64, :long_long, :ulong_long
32
+ Fiddle::SIZEOF_LONG_LONG
33
+ when :float
34
+ Fiddle::SIZEOF_FLOAT
35
+ when :double
36
+ Fiddle::SIZEOF_DOUBLE
37
+ when :size_t
38
+ Fiddle::SIZEOF_SIZE_T
39
+ when :string, :pointer
40
+ Fiddle::SIZEOF_VOIDP
41
+ else
42
+ raise RuntimeError, "unknown type #{type}"
43
+ end
44
+ end
45
+
46
+ module_function def type2offset_size(type)
47
+ case type
48
+ when :char, :uchar, :int8, :uint8
49
+ Fiddle::ALIGN_CHAR
25
50
  when :short, :ushort, :int16, :uint16
26
- 2
51
+ Fiddle::ALIGN_SHORT
27
52
  when :int, :uint, :int32, :uint32, :bool
28
- 4
53
+ Fiddle::ALIGN_INT
29
54
  when :long, :ulong
30
- LONG_SIZE
55
+ Fiddle::ALIGN_LONG
31
56
  when :int64, :uint64, :long_long, :ulong_long
32
- 8
33
- when :string, :pointer, :size_t
34
- POINTER_SIZE
57
+ Fiddle::ALIGN_LONG_LONG
58
+ when :float
59
+ Fiddle::ALIGN_FLOAT
60
+ when :double
61
+ Fiddle::ALIGN_DOUBLE
62
+ when :size_t
63
+ Fiddle::ALIGN_SIZE_T
64
+ when :string, :pointer
65
+ Fiddle::ALIGN_VOIDP
35
66
  else
36
- raise ArgumentError, "unknown type #{type}"
67
+ raise RuntimeError, "unknown type #{type}"
37
68
  end
38
69
  end
39
70
 
@@ -65,12 +96,16 @@ module Fiddley
65
96
  str.unpack1('q')
66
97
  when :ulong_long, :uint64
67
98
  str.unpack1('Q')
68
- when :string, :pointer
69
- str.unpack1('p')
70
99
  when :size_t
71
100
  str.unpack1(SIZET_FORMAT)
101
+ when :float
102
+ str.unpack1('f')
103
+ when :double
104
+ str.unpack1('d')
105
+ when :string, :pointer
106
+ str.unpack1('p')
72
107
  else
73
- raise ArgumentError, "unknown type #{type}"
108
+ raise RuntimeError, "unknown type #{type}"
74
109
  end
75
110
  end
76
111
 
@@ -102,12 +137,16 @@ module Fiddley
102
137
  [value].pack('q')
103
138
  when :ulong_long, :uint64
104
139
  [value].pack('Q')
105
- when :string, :pointer
106
- [value].pack('p')
107
140
  when :size_t
108
141
  [value].pack(SIZET_FORMAT)
142
+ when :float
143
+ [value].pack('f')
144
+ when :double
145
+ [value].pack('d')
146
+ when :string, :pointer
147
+ [value].pack('p')
109
148
  else
110
- raise ArgumentError, "unknown type #{type}"
149
+ raise RuntimeError, "unknown type #{type}"
111
150
  end
112
151
  end
113
152
 
@@ -133,13 +172,54 @@ module Fiddley
133
172
  "long long"
134
173
  when :ulong_long, :uint64
135
174
  "unsigned long long"
136
- when :string, :pointer
137
- "void *"
138
175
  when :size_t
139
176
  SIZET_TYPE
177
+ when :float
178
+ "float"
179
+ when :double
180
+ "double"
181
+ when :string, :pointer
182
+ "void *"
140
183
  else
141
184
  type.to_s
142
185
  end
143
186
  end
187
+
188
+ module_function def type2type(type)
189
+ case type
190
+ when :char, :int8
191
+ Fiddle::TYPE_CHAR
192
+ when :uchar, :uint8
193
+ -Fiddle::TYPE_CHAR
194
+ when :short, :int16
195
+ Fiddle::TYPE_SHORT
196
+ when :ushort, :uint16
197
+ -Fiddle::TYPE_SHORT
198
+ when :int, :int32
199
+ Fiddle::TYPE_INT
200
+ when :uint, :uint32
201
+ -Fiddle::TYPE_INT
202
+ when :long
203
+ Fiddle::TYPE_LONG
204
+ when :ulong
205
+ -Fiddle::TYPE_LONG
206
+ when :long_long, :int64
207
+ Fiddle::TYPE_LONG_LONG
208
+ when :ulong_long, :uint64
209
+ -Fiddle::TYPE_LONG_LONG
210
+ when :float
211
+ Fiddle::TYPE_FLOAT
212
+ when :double
213
+ Fiddle::TYPE_DOUBLE
214
+ when :size_t
215
+ Fiddle::TYPE_SIZE_T
216
+ when :string, :pointer
217
+ Fiddle::TYPE_VOIDP
218
+ when :void
219
+ Fiddle::TYPE_VOID
220
+ else
221
+ raise RuntimeError, "unknown type #{type}"
222
+ end
223
+ end
144
224
  end
145
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiddley
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - U.Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-31 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use Fiddle instead of Ruby-FFI !!!
14
14
  email: