ffi 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ffi might be problematic. Click here for more details.
- data/Rakefile +52 -29
- data/ext/AbstractMemory.c +72 -28
- data/ext/AutoPointer.c +54 -0
- data/ext/AutoPointer.h +18 -0
- data/ext/Buffer.c +21 -17
- data/ext/Callback.c +81 -43
- data/ext/Callback.h +1 -1
- data/ext/Invoker.c +465 -108
- data/ext/MemoryPointer.c +25 -90
- data/ext/NativeLibrary.c +90 -0
- data/ext/NativeLibrary.h +22 -0
- data/ext/Platform.c +21 -2
- data/ext/Pointer.c +107 -0
- data/ext/Pointer.h +21 -0
- data/ext/Types.c +16 -5
- data/ext/Types.h +3 -1
- data/ext/compat.h +14 -0
- data/ext/extconf.rb +13 -1
- data/ext/ffi.c +11 -1
- data/ext/ffi.mk +3 -3
- data/ext/libffi.darwin.mk +19 -8
- data/gen/Rakefile +12 -0
- data/lib/ffi/autopointer.rb +61 -0
- data/lib/ffi/errno.rb +8 -0
- data/lib/ffi/ffi.rb +38 -201
- data/lib/ffi/io.rb +7 -0
- data/lib/ffi/library.rb +116 -0
- data/lib/ffi/managedstruct.rb +55 -0
- data/lib/ffi/memorypointer.rb +3 -96
- data/lib/ffi/platform.rb +8 -5
- data/lib/ffi/pointer.rb +105 -0
- data/lib/ffi/struct.rb +97 -42
- data/lib/ffi/tools/const_generator.rb +177 -0
- data/lib/ffi/tools/generator.rb +58 -0
- data/lib/ffi/tools/generator_task.rb +35 -0
- data/lib/ffi/tools/struct_generator.rb +194 -0
- data/lib/ffi/tools/types_generator.rb +123 -0
- data/lib/ffi/types.rb +150 -0
- data/lib/ffi/variadic.rb +30 -0
- data/nbproject/Makefile-Default.mk +6 -3
- data/nbproject/Makefile-impl.mk +5 -5
- data/nbproject/Package-Default.bash +72 -0
- data/nbproject/configurations.xml +139 -25
- data/nbproject/private/configurations.xml +1 -1
- data/nbproject/project.xml +4 -0
- data/samples/gettimeofday.rb +6 -2
- data/samples/inotify.rb +59 -0
- data/samples/pty.rb +75 -0
- data/specs/buffer_spec.rb +64 -9
- data/specs/callback_spec.rb +308 -4
- data/specs/errno_spec.rb +13 -0
- data/specs/library_spec.rb +55 -0
- data/specs/managed_struct_spec.rb +40 -0
- data/specs/number_spec.rb +183 -0
- data/specs/pointer_spec.rb +126 -0
- data/specs/rbx/memory_pointer_spec.rb +7 -7
- data/specs/spec_helper.rb +7 -0
- data/specs/string_spec.rb +34 -0
- data/specs/struct_spec.rb +223 -0
- data/specs/typedef_spec.rb +48 -0
- data/specs/variadic_spec.rb +84 -0
- metadata +270 -237
data/specs/buffer_spec.rb
CHANGED
@@ -1,14 +1,8 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
LongSize = JRuby::FFI::Platform::LONG_SIZE / 8
|
7
|
-
else
|
8
|
-
Buffer = FFI::Buffer
|
9
|
-
Platform = FFI::Platform
|
10
|
-
LongSize = FFI::Platform::LONG_SIZE / 8
|
11
|
-
end
|
3
|
+
include FFI
|
4
|
+
LongSize = FFI::Platform::LONG_SIZE / 8
|
5
|
+
|
12
6
|
describe "Buffer#total" do
|
13
7
|
[1,2,3].each do |i|
|
14
8
|
{ :char => 1, :uchar => 1, :short => 2, :ushort => 2, :int => 4,
|
@@ -128,6 +122,67 @@ describe "Buffer#put_ulong_long" do
|
|
128
122
|
end
|
129
123
|
end
|
130
124
|
end
|
125
|
+
describe "Reading/Writing binary strings" do
|
126
|
+
it "Buffer#put_bytes" do
|
127
|
+
str = "hello\0world"
|
128
|
+
buf = FFI::Buffer.new 1024
|
129
|
+
buf.put_bytes(0, str);
|
130
|
+
s2 = buf.get_bytes(0, 11);
|
131
|
+
s2.should == str
|
132
|
+
end
|
133
|
+
it "Buffer#put_bytes with index and length" do
|
134
|
+
str = "hello\0world"
|
135
|
+
buf = FFI::Buffer.new 1024
|
136
|
+
buf.put_bytes(0, str, 5, 6);
|
137
|
+
s2 = buf.get_bytes(0, 6);
|
138
|
+
s2.should == str[5..-1]
|
139
|
+
end
|
140
|
+
it "Buffer#put_bytes with only index" do
|
141
|
+
str = "hello\0world"
|
142
|
+
buf = FFI::Buffer.new 1024
|
143
|
+
buf.put_bytes(0, str, 5);
|
144
|
+
s2 = buf.get_bytes(0, 6);
|
145
|
+
s2.should == str[5..-1]
|
146
|
+
end
|
147
|
+
it "Buffer#put_bytes with index > str.length" do
|
148
|
+
str = "hello\0world"
|
149
|
+
buf = FFI::Buffer.new 1024
|
150
|
+
lambda { buf.put_bytes(0, str, 12); }.should raise_error
|
151
|
+
end
|
152
|
+
it "Buffer#put_bytes with length > str.length" do
|
153
|
+
str = "hello\0world"
|
154
|
+
buf = FFI::Buffer.new 1024
|
155
|
+
lambda { buf.put_bytes(0, str, 0, 12); }.should raise_error
|
156
|
+
end
|
157
|
+
it "Buffer#put_bytes with negative index" do
|
158
|
+
str = "hello\0world"
|
159
|
+
buf = FFI::Buffer.new 1024
|
160
|
+
lambda { buf.put_bytes(0, str, -1, 12); }.should raise_error
|
161
|
+
end
|
162
|
+
end
|
163
|
+
describe "Reading/Writing ascii strings" do
|
164
|
+
it "Buffer#put_string with string containing zero byte" do
|
165
|
+
str = "hello\0world"
|
166
|
+
buf = FFI::Buffer.new 1024
|
167
|
+
buf.put_string(0, str);
|
168
|
+
s2 = buf.get_bytes(0, 11);
|
169
|
+
s2.should == str
|
170
|
+
end
|
171
|
+
it "Buffer#get_string with string containing zero byte" do
|
172
|
+
str = "hello\0world"
|
173
|
+
buf = FFI::Buffer.new 1024
|
174
|
+
buf.put_bytes(0, str);
|
175
|
+
s2 = buf.get_string(0, 11);
|
176
|
+
s2.should == "hello"
|
177
|
+
end
|
178
|
+
it "Buffer#put_string without length should NUL terminate" do
|
179
|
+
str = "hello"
|
180
|
+
buf = FFI::Buffer.new 1024
|
181
|
+
buf.put_string(0, str);
|
182
|
+
s2 = buf.get_bytes(0, 6);
|
183
|
+
s2.should == "hello\0"
|
184
|
+
end
|
185
|
+
end
|
131
186
|
describe "Buffer#put_pointer" do
|
132
187
|
it "put_pointer(0, p).get_pointer(0) == p" do
|
133
188
|
p = MemoryPointer.new :ulong_long
|
data/specs/callback_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
2
2
|
|
3
3
|
describe "Callback" do
|
4
4
|
module LibC
|
@@ -21,11 +21,315 @@ describe "Callback" do
|
|
21
21
|
p.put_array_of_int32(0, [ 1 , 2 ])
|
22
22
|
args = []
|
23
23
|
# this is a bit dodgey, as it relies on qsort passing the args in order
|
24
|
-
LibC.qsort(p, 2, 4) do |p1, p2|
|
24
|
+
LibC.qsort(p, 2, 4) do |p1, p2|
|
25
25
|
args.push(p1.get_int(0))
|
26
26
|
args.push(p2.get_int(0))
|
27
27
|
0
|
28
28
|
end
|
29
29
|
args.should == [ 1, 2 ]
|
30
|
-
end
|
31
|
-
|
30
|
+
end
|
31
|
+
module LibTest
|
32
|
+
extend FFI::Library
|
33
|
+
ffi_lib TestLibrary::PATH
|
34
|
+
callback :cbVrS8, [ ], :char
|
35
|
+
callback :cbVrU8, [ ], :uchar
|
36
|
+
callback :cbVrS16, [ ], :short
|
37
|
+
callback :cbVrU16, [ ], :ushort
|
38
|
+
callback :cbVrS32, [ ], :int
|
39
|
+
callback :cbVrU32, [ ], :uint
|
40
|
+
callback :cbVrS64, [ ], :long_long
|
41
|
+
callback :cbVrU64, [ ], :ulong_long
|
42
|
+
callback :cbCrV, [ :char ], :void
|
43
|
+
callback :cbSrV, [ :short ], :void
|
44
|
+
callback :cbIrV, [ :int ], :void
|
45
|
+
callback :cbLrV, [ :long_long ], :void
|
46
|
+
attach_function :testCallbackVrS8, :testClosureVrB, [ :cbVrS8 ], :char
|
47
|
+
attach_function :testCallbackVrU8, :testClosureVrB, [ :cbVrU8 ], :uchar
|
48
|
+
attach_function :testCallbackVrS16, :testClosureVrS, [ :cbVrS16 ], :short
|
49
|
+
attach_function :testCallbackVrU16, :testClosureVrS, [ :cbVrU16 ], :ushort
|
50
|
+
attach_function :testCallbackVrS32, :testClosureVrI, [ :cbVrS32 ], :int
|
51
|
+
attach_function :testCallbackVrU32, :testClosureVrI, [ :cbVrU32 ], :uint
|
52
|
+
attach_function :testCallbackVrS64, :testClosureVrL, [ :cbVrS64 ], :long_long
|
53
|
+
attach_function :testCallbackVrU64, :testClosureVrL, [ :cbVrU64 ], :ulong_long
|
54
|
+
attach_function :testCallbackCrV, :testClosureBrV, [ :cbCrV, :char ], :void
|
55
|
+
end
|
56
|
+
it "function with Callback plus another arg should raise error if no arg given" do
|
57
|
+
lambda { LibTest.testCallbackCrV { |*a| }}.should raise_error
|
58
|
+
end
|
59
|
+
it "Callback returning :char (0)" do
|
60
|
+
LibTest.testCallbackVrS8 { 0 }.should == 0
|
61
|
+
end
|
62
|
+
it "Callback returning :char (127)" do
|
63
|
+
LibTest.testCallbackVrS8 { 127 }.should == 127
|
64
|
+
end
|
65
|
+
it "Callback returning :char (-128)" do
|
66
|
+
LibTest.testCallbackVrS8 { -128 }.should == -128
|
67
|
+
end
|
68
|
+
# test wrap around
|
69
|
+
it "Callback returning :char (128)" do
|
70
|
+
LibTest.testCallbackVrS8 { 128 }.should == -128
|
71
|
+
end
|
72
|
+
it "Callback returning :char (255)" do
|
73
|
+
LibTest.testCallbackVrS8 { 0xff }.should == -1
|
74
|
+
end
|
75
|
+
it "Callback returning :uchar (0)" do
|
76
|
+
LibTest.testCallbackVrU8 { 0 }.should == 0
|
77
|
+
end
|
78
|
+
it "Callback returning :uchar (0xff)" do
|
79
|
+
LibTest.testCallbackVrU8 { 0xff }.should == 0xff
|
80
|
+
end
|
81
|
+
it "Callback returning :uchar (-1)" do
|
82
|
+
LibTest.testCallbackVrU8 { -1 }.should == 0xff
|
83
|
+
end
|
84
|
+
it "Callback returning :uchar (128)" do
|
85
|
+
LibTest.testCallbackVrU8 { 128 }.should == 128
|
86
|
+
end
|
87
|
+
it "Callback returning :uchar (-128)" do
|
88
|
+
LibTest.testCallbackVrU8 { -128 }.should == 128
|
89
|
+
end
|
90
|
+
it "Callback returning :short (0)" do
|
91
|
+
LibTest.testCallbackVrS16 { 0 }.should == 0
|
92
|
+
end
|
93
|
+
it "Callback returning :short (0x7fff)" do
|
94
|
+
LibTest.testCallbackVrS16 { 0x7fff }.should == 0x7fff
|
95
|
+
end
|
96
|
+
# test wrap around
|
97
|
+
it "Callback returning :short (0x8000)" do
|
98
|
+
LibTest.testCallbackVrS16 { 0x8000 }.should == -0x8000
|
99
|
+
end
|
100
|
+
it "Callback returning :short (0xffff)" do
|
101
|
+
LibTest.testCallbackVrS16 { 0xffff }.should == -1
|
102
|
+
end
|
103
|
+
it "Callback returning :ushort (0)" do
|
104
|
+
LibTest.testCallbackVrU16 { 0 }.should == 0
|
105
|
+
end
|
106
|
+
it "Callback returning :ushort (0x7fff)" do
|
107
|
+
LibTest.testCallbackVrU16 { 0x7fff }.should == 0x7fff
|
108
|
+
end
|
109
|
+
it "Callback returning :ushort (0x8000)" do
|
110
|
+
LibTest.testCallbackVrU16 { 0x8000 }.should == 0x8000
|
111
|
+
end
|
112
|
+
it "Callback returning :ushort (0xffff)" do
|
113
|
+
LibTest.testCallbackVrU16 { 0xffff }.should == 0xffff
|
114
|
+
end
|
115
|
+
it "Callback returning :ushort (-1)" do
|
116
|
+
LibTest.testCallbackVrU16 { -1 }.should == 0xffff
|
117
|
+
end
|
118
|
+
|
119
|
+
it "Callback returning :int (0)" do
|
120
|
+
LibTest.testCallbackVrS32 { 0 }.should == 0
|
121
|
+
end
|
122
|
+
it "Callback returning :int (0x7fffffff)" do
|
123
|
+
LibTest.testCallbackVrS32 { 0x7fffffff }.should == 0x7fffffff
|
124
|
+
end
|
125
|
+
# test wrap around
|
126
|
+
it "Callback returning :int (-0x80000000)" do
|
127
|
+
LibTest.testCallbackVrS32 { -0x80000000 }.should == -0x80000000
|
128
|
+
end
|
129
|
+
it "Callback returning :int (-1)" do
|
130
|
+
LibTest.testCallbackVrS32 { -1 }.should == -1
|
131
|
+
end
|
132
|
+
|
133
|
+
it "Callback returning :uint (0)" do
|
134
|
+
LibTest.testCallbackVrU32 { 0 }.should == 0
|
135
|
+
end
|
136
|
+
it "Callback returning :uint (0x7fffffff)" do
|
137
|
+
LibTest.testCallbackVrU32 { 0x7fffffff }.should == 0x7fffffff
|
138
|
+
end
|
139
|
+
# test wrap around
|
140
|
+
it "Callback returning :uint (0x80000000)" do
|
141
|
+
LibTest.testCallbackVrU32 { 0x80000000 }.should == 0x80000000
|
142
|
+
end
|
143
|
+
it "Callback returning :uint (0xffffffff)" do
|
144
|
+
LibTest.testCallbackVrU32 { 0xffffffff }.should == 0xffffffff
|
145
|
+
end
|
146
|
+
it "Callback returning :uint (-1)" do
|
147
|
+
LibTest.testCallbackVrU32 { -1 }.should == 0xffffffff
|
148
|
+
end
|
149
|
+
|
150
|
+
it "Callback returning :long_long (0)" do
|
151
|
+
LibTest.testCallbackVrS64 { 0 }.should == 0
|
152
|
+
end
|
153
|
+
it "Callback returning :long_long (0x7fffffffffffffff)" do
|
154
|
+
LibTest.testCallbackVrS64 { 0x7fffffffffffffff }.should == 0x7fffffffffffffff
|
155
|
+
end
|
156
|
+
# test wrap around
|
157
|
+
it "Callback returning :long_long (-0x8000000000000000)" do
|
158
|
+
LibTest.testCallbackVrS64 { -0x8000000000000000 }.should == -0x8000000000000000
|
159
|
+
end
|
160
|
+
it "Callback returning :long_long (-1)" do
|
161
|
+
LibTest.testCallbackVrS64 { -1 }.should == -1
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
end
|
166
|
+
describe "Callback with primitive argument" do
|
167
|
+
#
|
168
|
+
# Test callbacks that take an argument, returning void
|
169
|
+
#
|
170
|
+
module LibTest
|
171
|
+
extend FFI::Library
|
172
|
+
ffi_lib TestLibrary::PATH
|
173
|
+
callback :cbS8rV, [ :char ], :void
|
174
|
+
callback :cbU8rV, [ :uchar ], :void
|
175
|
+
callback :cbS16rV, [ :short ], :void
|
176
|
+
callback :cbU16rV, [ :ushort ], :void
|
177
|
+
callback :cbS32rV, [ :int ], :void
|
178
|
+
callback :cbU32rV, [ :uint ], :void
|
179
|
+
callback :cbS64rV, [ :long_long ], :void
|
180
|
+
attach_function :testCallbackCrV, :testClosureBrV, [ :cbS8rV, :char ], :void
|
181
|
+
attach_function :testCallbackU8rV, :testClosureBrV, [ :cbU8rV, :uchar ], :void
|
182
|
+
attach_function :testCallbackSrV, :testClosureSrV, [ :cbS16rV, :short ], :void
|
183
|
+
attach_function :testCallbackU16rV, :testClosureSrV, [ :cbU16rV, :ushort ], :void
|
184
|
+
attach_function :testCallbackIrV, :testClosureIrV, [ :cbS32rV, :int ], :void
|
185
|
+
attach_function :testCallbackU32rV, :testClosureIrV, [ :cbU32rV, :uint ], :void
|
186
|
+
attach_function :testCallbackLrV, :testClosureLrV, [ :cbS64rV, :long_long ], :void
|
187
|
+
end
|
188
|
+
it "Callback with :char (0) argument" do
|
189
|
+
v = 0xdeadbeef
|
190
|
+
LibTest.testCallbackCrV(0) { |i| v = i }
|
191
|
+
v.should == 0
|
192
|
+
end
|
193
|
+
it "Callback with :char (127) argument" do
|
194
|
+
v = 0xdeadbeef
|
195
|
+
LibTest.testCallbackCrV(127) { |i| v = i }
|
196
|
+
v.should == 127
|
197
|
+
end
|
198
|
+
it "Callback with :char (-128) argument" do
|
199
|
+
v = 0xdeadbeef
|
200
|
+
LibTest.testCallbackCrV(-128) { |i| v = i }
|
201
|
+
v.should == -128
|
202
|
+
end
|
203
|
+
it "Callback with :char (-1) argument" do
|
204
|
+
v = 0xdeadbeef
|
205
|
+
LibTest.testCallbackCrV(-1) { |i| v = i }
|
206
|
+
v.should == -1
|
207
|
+
end
|
208
|
+
|
209
|
+
it "Callback with :uchar (0) argument" do
|
210
|
+
v = 0xdeadbeef
|
211
|
+
LibTest.testCallbackU8rV(0) { |i| v = i }
|
212
|
+
v.should == 0
|
213
|
+
end
|
214
|
+
it "Callback with :uchar (127) argument" do
|
215
|
+
v = 0xdeadbeef
|
216
|
+
LibTest.testCallbackU8rV(127) { |i| v = i }
|
217
|
+
v.should == 127
|
218
|
+
end
|
219
|
+
it "Callback with :uchar (128) argument" do
|
220
|
+
v = 0xdeadbeef
|
221
|
+
LibTest.testCallbackU8rV(128) { |i| v = i }
|
222
|
+
v.should == 128
|
223
|
+
end
|
224
|
+
it "Callback with :uchar (255) argument" do
|
225
|
+
v = 0xdeadbeef
|
226
|
+
LibTest.testCallbackU8rV(255) { |i| v = i }
|
227
|
+
v.should == 255
|
228
|
+
end
|
229
|
+
|
230
|
+
it "Callback with :short (0) argument" do
|
231
|
+
v = 0xdeadbeef
|
232
|
+
LibTest.testCallbackSrV(0) { |i| v = i }
|
233
|
+
v.should == 0
|
234
|
+
end
|
235
|
+
it "Callback with :short (0x7fff) argument" do
|
236
|
+
v = 0xdeadbeef
|
237
|
+
LibTest.testCallbackSrV(0x7fff) { |i| v = i }
|
238
|
+
v.should == 0x7fff
|
239
|
+
end
|
240
|
+
it "Callback with :short (-0x8000) argument" do
|
241
|
+
v = 0xdeadbeef
|
242
|
+
LibTest.testCallbackSrV(-0x8000) { |i| v = i }
|
243
|
+
v.should == -0x8000
|
244
|
+
end
|
245
|
+
it "Callback with :short (-1) argument" do
|
246
|
+
v = 0xdeadbeef
|
247
|
+
LibTest.testCallbackSrV(-1) { |i| v = i }
|
248
|
+
v.should == -1
|
249
|
+
end
|
250
|
+
|
251
|
+
it "Callback with :ushort (0) argument" do
|
252
|
+
v = 0xdeadbeef
|
253
|
+
LibTest.testCallbackU16rV(0) { |i| v = i }
|
254
|
+
v.should == 0
|
255
|
+
end
|
256
|
+
it "Callback with :ushort (0x7fff) argument" do
|
257
|
+
v = 0xdeadbeef
|
258
|
+
LibTest.testCallbackU16rV(0x7fff) { |i| v = i }
|
259
|
+
v.should == 0x7fff
|
260
|
+
end
|
261
|
+
it "Callback with :ushort (0x8000) argument" do
|
262
|
+
v = 0xdeadbeef
|
263
|
+
LibTest.testCallbackU16rV(0x8000) { |i| v = i }
|
264
|
+
v.should == 0x8000
|
265
|
+
end
|
266
|
+
it "Callback with :ushort (0xffff) argument" do
|
267
|
+
v = 0xdeadbeef
|
268
|
+
LibTest.testCallbackU16rV(0xffff) { |i| v = i }
|
269
|
+
v.should == 0xffff
|
270
|
+
end
|
271
|
+
|
272
|
+
it "Callback with :int (0) argument" do
|
273
|
+
v = 0xdeadbeef
|
274
|
+
LibTest.testCallbackIrV(0) { |i| v = i }
|
275
|
+
v.should == 0
|
276
|
+
end
|
277
|
+
it "Callback with :int (0x7fffffff) argument" do
|
278
|
+
v = 0xdeadbeef
|
279
|
+
LibTest.testCallbackIrV(0x7fffffff) { |i| v = i }
|
280
|
+
v.should == 0x7fffffff
|
281
|
+
end
|
282
|
+
it "Callback with :int (-0x80000000) argument" do
|
283
|
+
v = 0xdeadbeef
|
284
|
+
LibTest.testCallbackIrV(-0x80000000) { |i| v = i }
|
285
|
+
v.should == -0x80000000
|
286
|
+
end
|
287
|
+
it "Callback with :int (-1) argument" do
|
288
|
+
v = 0xdeadbeef
|
289
|
+
LibTest.testCallbackIrV(-1) { |i| v = i }
|
290
|
+
v.should == -1
|
291
|
+
end
|
292
|
+
|
293
|
+
it "Callback with :uint (0) argument" do
|
294
|
+
v = 0xdeadbeef
|
295
|
+
LibTest.testCallbackU32rV(0) { |i| v = i }
|
296
|
+
v.should == 0
|
297
|
+
end
|
298
|
+
it "Callback with :uint (0x7fffffff) argument" do
|
299
|
+
v = 0xdeadbeef
|
300
|
+
LibTest.testCallbackU32rV(0x7fffffff) { |i| v = i }
|
301
|
+
v.should == 0x7fffffff
|
302
|
+
end
|
303
|
+
it "Callback with :uint (0x80000000) argument" do
|
304
|
+
v = 0xdeadbeef
|
305
|
+
LibTest.testCallbackU32rV(0x80000000) { |i| v = i }
|
306
|
+
v.should == 0x80000000
|
307
|
+
end
|
308
|
+
it "Callback with :uint (0xffffffff) argument" do
|
309
|
+
v = 0xdeadbeef
|
310
|
+
LibTest.testCallbackU32rV(0xffffffff) { |i| v = i }
|
311
|
+
v.should == 0xffffffff
|
312
|
+
end
|
313
|
+
|
314
|
+
it "Callback with :long_long (0) argument" do
|
315
|
+
v = 0xdeadbeef
|
316
|
+
LibTest.testCallbackLrV(0) { |i| v = i }
|
317
|
+
v.should == 0
|
318
|
+
end
|
319
|
+
it "Callback with :long_long (0x7fffffffffffffff) argument" do
|
320
|
+
v = 0xdeadbeef
|
321
|
+
LibTest.testCallbackLrV(0x7fffffffffffffff) { |i| v = i }
|
322
|
+
v.should == 0x7fffffffffffffff
|
323
|
+
end
|
324
|
+
it "Callback with :long_long (-0x8000000000000000) argument" do
|
325
|
+
v = 0xdeadbeef
|
326
|
+
LibTest.testCallbackLrV(-0x8000000000000000) { |i| v = i }
|
327
|
+
v.should == -0x8000000000000000
|
328
|
+
end
|
329
|
+
it "Callback with :long_long (-1) argument" do
|
330
|
+
v = 0xdeadbeef
|
331
|
+
LibTest.testCallbackLrV(-1) { |i| v = i }
|
332
|
+
v.should == -1
|
333
|
+
end
|
334
|
+
|
335
|
+
end unless true
|
data/specs/errno_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
2
|
+
describe "FFI.errno" do
|
3
|
+
module LibTest
|
4
|
+
extend FFI::Library
|
5
|
+
ffi_lib TestLibrary::PATH
|
6
|
+
attach_function :setLastError, [ :int ], :void
|
7
|
+
end
|
8
|
+
it "FFI.errno contains errno from last function" do
|
9
|
+
LibTest.setLastError(0)
|
10
|
+
LibTest.setLastError(0x12345678)
|
11
|
+
FFI.errno.should == 0x12345678
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
2
|
+
describe "Library" do
|
3
|
+
it "attach_function with no library specified" do
|
4
|
+
lambda {
|
5
|
+
Module.new do |m|
|
6
|
+
m.extend FFI::Library
|
7
|
+
attach_function :getpid, [ ], :uint
|
8
|
+
end
|
9
|
+
}.should_not raise_error
|
10
|
+
end
|
11
|
+
it "attach_function :getpid from this process" do
|
12
|
+
lambda {
|
13
|
+
Module.new do |m|
|
14
|
+
m.extend FFI::Library
|
15
|
+
attach_function :getpid, [ ], :uint
|
16
|
+
end.getpid.should == Process.pid
|
17
|
+
}.should_not raise_error
|
18
|
+
end
|
19
|
+
it "attach_function :getpid from [ 'c', 'libc.so.6'] " do
|
20
|
+
lambda {
|
21
|
+
Module.new do |m|
|
22
|
+
m.extend FFI::Library
|
23
|
+
ffi_lib 'c', 'libc.so.6'
|
24
|
+
attach_function :getpid, [ ], :uint
|
25
|
+
end.getpid.should == Process.pid
|
26
|
+
}.should_not raise_error
|
27
|
+
end
|
28
|
+
it "attach_function :getpid from [ 'libc.so.6', 'c' ] " do
|
29
|
+
lambda {
|
30
|
+
Module.new do |m|
|
31
|
+
m.extend FFI::Library
|
32
|
+
ffi_lib 'libc.so.6', 'c'
|
33
|
+
attach_function :getpid, [ ], :uint
|
34
|
+
end.getpid.should == Process.pid
|
35
|
+
}.should_not raise_error
|
36
|
+
end
|
37
|
+
it "attach_function :getpid from [ 'libfubar.so.0xdeadbeef', nil, 'c' ] " do
|
38
|
+
lambda {
|
39
|
+
Module.new do |m|
|
40
|
+
m.extend FFI::Library
|
41
|
+
ffi_lib 'libfubar.so.0xdeadbeef', nil, 'c'
|
42
|
+
attach_function :getpid, [ ], :uint
|
43
|
+
end.getpid.should == Process.pid
|
44
|
+
}.should_not raise_error
|
45
|
+
end
|
46
|
+
it "attach_function :getpid from [ 'libfubar.so.0xdeadbeef' ] " do
|
47
|
+
lambda {
|
48
|
+
Module.new do |m|
|
49
|
+
m.extend FFI::Library
|
50
|
+
ffi_lib 'libfubar.so.0xdeadbeef'
|
51
|
+
attach_function :getpid, [ ], :uint
|
52
|
+
end.getpid.should == Process.pid
|
53
|
+
}.should raise_error(LoadError)
|
54
|
+
end
|
55
|
+
end
|