/
inline_spec.rb
289 lines (244 loc) · 6.15 KB
/
inline_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
require 'ffi/inline'
describe FFI::Inline do
before do
module Foo
extend FFI::Inline
end
end
it 'should extend the module with inline methods' do
module Foo
inline %{
long factorial (int max) {
int i = max, result = 1;
while (i >= 2) {
result *= i--;
}
return result;
}
}
inline 'int simple_math() { return 1 + 1; }'
end
Foo.factorial(4).should == 24
Foo.simple_math.should == 2
end
it 'should correctly parse function signature' do
module Foo
inline %{
void* func_1 (void* ptr, unsigned int i, unsigned long l, char *c) {
return ptr;
}
}
end
ptr = FFI::MemoryPointer.new(:int)
Foo.func_1(ptr, 0xff, 0xffff, FFI::MemoryPointer.from_string('c')).should == ptr
end
it 'should load cached libraries' do
File.should_receive(:open).once
module Foo
inline "void* cached_func() {}"
end
module Foo
inline "void* cached_func() {}"
end
end
it 'should recompile if the code is updated' do
module Foo
inline "int updated_func() { return 1 + 1; }"
end
Foo.updated_func.should == 2
module Foo
inline "int updated_func() { return 2 + 2; }"
end
Foo.updated_func.should == 4
end
it 'should recompile if the code is changed after a failure' do
# unfortunately this doesn't check the real functionality, which is that if a dll is deleted, it isn't re-produced
begin
module Foo
inline "int updated_func2() { asdf }"
end
rescue
begin
Foo.updated_func2
raise 'should have failed'
rescue NoMethodError
end
end
module Foo
inline "int updated_func2() { return 2 + 2; }"
end
Foo.updated_func2.should == 4
end
it 'should be configured using the block form' do
module Foo
inline do |builder|
builder.function %{
int func_1 () {
return 0;
}
}
builder.function %{
int func_2 () {
return 1;
}
}
end
end
Foo.func_1.should == 0
Foo.func_2.should == 1
end
it 'should allow users to add type maps' do
class MyStruct < FFI::Struct
layout :dummy, :int
end
module Foo
inline do |builder|
builder.map 'my_struct_t *' => :pointer
builder.raw %q{
typedef struct {
int dummy;
} my_struct_t;
}
builder.function 'my_struct_t* use_my_struct (my_struct_t* my_struct) { return my_struct; }'
end
end
my_struct = MyStruct.new
Foo.use_my_struct(my_struct).should == my_struct.to_ptr
end
it 'should allow users to include header files' do
module Foo
inline do |builder|
builder.include "stdio.h"
builder.include "local_header.h", :quoted => true
builder.code.should == "#include <stdio.h>\n#include \"local_header.h\"\n"
builder.stub!(:build)
end
end
end
it 'should allow users to add external libraries' do
module Foo
inline do |builder|
builder.libraries 'foolib1', 'foolib2'
builder.stub!(:build)
builder.stub!(:symbols) { [] }
builder.libraries.should == ['foolib1', 'foolib2']
end
inline "int func() { return 0; }", :libraries => ['foolib1', 'foolib2'] do |builder|
builder.stub!(:build)
builder.stub!(:symbols) { [] }
builder.libraries.should == ['foolib1', 'foolib2']
end
end
end
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
it "should put library links at the end in mingw" do
module Foo
code = <<-CODE
#include <windows.h>
#include <mmsystem.h>
int go() {
mixerOpen(0, 0,0,0,0);
return 3;
}
CODE
inline do |builder|
builder.library 'Winmm'
builder.raw code
end
inline do |builder|
builder.use_compiler :gxx
builder.library 'Winmm'
builder.raw code
end
end
end
end
it 'should generate C struct from FFI::Struct' do
pending do
class MyStruct < FFI::Struct
layout :a, :int, \
:b, :char,
:c, :pointer
end
module Foo
extend FFI::Inline
inline do |builder|
builder.struct MyStruct
builder.code.should == <<EOC
typedef struct
{
int a;
char b;
void* c;
} my_struct_t;
EOC
end
end
end
end
it 'should return the current compiler' do
module Foo
inline do |builder|
builder.compiler.should == FFI::Inline::Compiler[:gcc]
end
end
end
it 'should raise errors' do
proc {
module Foo
inline "int boom("
end
}.should raise_error(/cannot parse/)
proc {
module Foo
inline 'int boom() { printf "Hello" }'
end
}.should raise_error(/compile error/)
end
it 'should respect blocking flag' do
module Foo
inline do |builder|
builder.function %{
void blocking_function () {
#if defined(_WIN32)
Sleep(1000);
#else
sleep(1);
#endif
}
}, :blocking=>true
end
end
st = Time.now
Array.new(4){ Thread.new{ Foo.blocking_function } }.map(&:join)
(Time.now - st).should < 4
end
describe 'GXX compiler' do
it 'should compile and link a shim C library that encapsulates C++ code' do
module Foo
inline :cpp do |builder|
builder.raw %{
class Greeter
{
public:
Greeter();
const char *say_hello();
};
Greeter::Greeter () { };
const char *Greeter::say_hello ()
{
return "Hello foos!";
};
}
builder.function %{
const char* say_hello () {
Greeter greeter;
return greeter.say_hello();
}
}, return: :string
end
end
Foo.say_hello.should == 'Hello foos!'
end
end
end