| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package FFI::Raw; |
|
2
|
|
|
|
|
|
|
$FFI::Raw::VERSION = '0.32'; |
|
3
|
12
|
|
|
12
|
|
250999
|
use strict; |
|
|
12
|
|
|
|
|
28
|
|
|
|
12
|
|
|
|
|
450
|
|
|
4
|
12
|
|
|
12
|
|
61
|
use warnings; |
|
|
12
|
|
|
|
|
26
|
|
|
|
12
|
|
|
|
|
972
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require XSLoader; |
|
7
|
|
|
|
|
|
|
XSLoader::load('FFI::Raw', $FFI::Raw::VERSION); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require FFI::Raw::Ptr; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use overload |
|
12
|
12
|
|
|
|
|
135
|
'&{}' => \&coderef, |
|
13
|
12
|
|
|
12
|
|
27882
|
'bool' => \&_bool; |
|
|
12
|
|
|
|
|
15263
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub _bool { |
|
16
|
1
|
|
|
1
|
|
203566
|
my $ffi = shift; |
|
17
|
1
|
|
|
|
|
12
|
return $ffi; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
FFI::Raw - Perl bindings to the portable FFI library (libffi) |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 VERSION |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
version 0.32 |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use FFI::Raw; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $cos = FFI::Raw -> new( |
|
33
|
|
|
|
|
|
|
'libm.so', 'cos', |
|
34
|
|
|
|
|
|
|
FFI::Raw::double, # return value |
|
35
|
|
|
|
|
|
|
FFI::Raw::double # arg #1 |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
say $cos -> call(2.0); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
B provides a low-level foreign function interface (FFI) for Perl based |
|
43
|
|
|
|
|
|
|
on L. In essence, it can access and call |
|
44
|
|
|
|
|
|
|
functions exported by shared libraries without the need to write C/XS code. |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Dynamic symbols can be automatically resolved at runtime so that the only |
|
47
|
|
|
|
|
|
|
information needed to use B is the name (or path) of the target |
|
48
|
|
|
|
|
|
|
library, the name of the function to call and its signature (though it is also |
|
49
|
|
|
|
|
|
|
possible to pass a function pointer obtained, for example, using L). |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Note that this module has nothing to do with L. |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 METHODS |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head2 new( $library, $function, $return_type [, $arg_type ...] ) |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Create a new C object. It loads C<$library>, finds the function |
|
58
|
|
|
|
|
|
|
C<$function> with return type C<$return_type> and creates a calling interface. |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
If C<$library> is C then the function is searched in the main program. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
This method also takes a variable number of types, representing the arguments |
|
63
|
|
|
|
|
|
|
of the wanted function. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 new_from_ptr( $function_ptr, $return_type [, $arg_type ...] ) |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Create a new C object from the C<$function_ptr> function pointer. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
This method also takes a variable number of types, representing the arguments |
|
70
|
|
|
|
|
|
|
of the wanted function. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 call( [$arg ...] ) |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Execute the C function. This methoed also takes a variable number of |
|
75
|
|
|
|
|
|
|
arguments, which are passed to the called function. The argument types must |
|
76
|
|
|
|
|
|
|
match the types passed to C (or C). |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
The C object can be used as a CODE reference as well. Dereferencing |
|
79
|
|
|
|
|
|
|
the object will work just like call(): |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$cos -> call(2.0); # normal call() call |
|
82
|
|
|
|
|
|
|
$cos -> (2.0); # dereference as CODE ref |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This works because FFI::Raw overloads the C<&{}> operator. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 coderef( ) |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Return a code reference of a given C. |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub coderef { |
|
93
|
35
|
|
|
35
|
1
|
1201025
|
my $ffi = shift; |
|
94
|
35
|
|
|
36
|
|
351
|
return sub { $ffi -> call(@_) }; |
|
|
36
|
|
|
|
|
1893
|
|
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 SUBROUTINES |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 memptr( $length ) |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Create a L. This is a shortcut for Cnew(...)>. |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
|
104
|
|
|
|
|
|
|
|
|
105
|
1
|
|
|
1
|
1
|
315088
|
sub memptr { FFI::Raw::MemPtr -> new(@_) } |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 callback( $coderef, $ret_type [, $arg_type ...] ) |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Create a L. This is a shortcut for Cnew(...)>. |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
|
112
|
|
|
|
|
|
|
|
|
113
|
7
|
|
|
7
|
1
|
236844
|
sub callback { FFI::Raw::Callback -> new(@_) } |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 TYPES |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 FFI::Raw::void |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Return a C void type. |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub void () { ord 'v' } |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 FFI::Raw::int |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Return a C integer type. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub int () { ord 'i' } |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 FFI::Raw::uint |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Return a C unsigned integer type. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub uint () { ord 'I' } |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 FFI::Raw::short |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Return a C short integer type. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub short () { ord 'z' } |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 FFI::Raw::ushort |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Return a C unsigned short integer type. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub ushort () { ord 'Z' } |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 FFI::Raw::long |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Return a C long integer type. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub long () { ord 'l' } |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 FFI::Raw::ulong |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Return a C unsigned long integer type. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub ulong () { ord 'L' } |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 FFI::Raw::int64 |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Return a C 64 bit integer type. This requires L to work. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub int64 () { ord 'x' } |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 FFI::Raw::uint64 |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Return a C unsigned 64 bit integer type. This requires L |
|
184
|
|
|
|
|
|
|
to work. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub uint64 () { ord 'X' } |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 FFI::Raw::char |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Return a C char type. |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub char () { ord 'c' } |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 FFI::Raw::uchar |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Return a C unsigned char type. |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=cut |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub uchar () { ord 'C' } |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 FFI::Raw::float |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Return a C float type. |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub float () { ord 'f' } |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 FFI::Raw::double |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Return a C double type. |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub double () { ord 'd' } |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head2 FFI::Raw::str |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Return a C string type. |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=cut |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub str () { ord 's' } |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head2 FFI::Raw::ptr |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Return a C pointer type. |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=cut |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub ptr () { ord 'p' } |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 AUTHOR |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Alessandro Ghedini |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
L, L |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Copyright 2012 Alessandro Ghedini. |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
251
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
252
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=cut |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
1; # End of FFI::Raw |