-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
245 lines (153 loc) · 6 KB
/
README
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
NAME
FFI::Platypus::Lang::Pascal - Documentation and tools for using
Platypus with the Free Pascal programming language
SYNOPSIS
Free Pascal:
{ compile and link with: fpc mylib.pas }
Library MyLib;
Function Add(A: Integer; B: Integer): Integer; Cdecl;
Begin
Add := A + B;
End;
Exports
Add;
End.
Perl:
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lang('Pascal');
$ffi->lib('./libmylib.so');
$ffi->attach(
Add => ['Integer','Integer'] => 'Integer'
);
print Add(1,2), "\n";
DESCRIPTION
This modules provides native types and demangling for the Free Pascal
Programming language when used with FFI::Platypus.
This module provides these types (case sensitive):
Byte
ShortInt
SmallInt
Word
Integer
This is an alias for SmallInt (which is appropriate for Free Pascal's
default mode)
Cardinal
LongInt
LongWord
Int64
QWord
Boolean
ByteBool
WordBool
LongBool
Single
Double
The following types are not (yet) supported:
Extended
Comp
Currency
ShortString
This module also has some support for demangled functions and
overloading, if you are using a dynamic library constructed from units
via ppumove.
You may also use Module::Build::FFI::Pascal to bundle Free Pascal code
with your distribution.
CAVEATS
I've been experimenting with Free Pascal 2.6.0 while working on this
module.
name mangling
If you compile one or more Pascal Units and link them using ppumove,
they symbols in the resulting dynamic library will include mangled
Pascal names. This module has at least some support for such names.
For example, suppose you had this Pascal Unit:
Unit Add;
Interface
Function Add( A: SmallInt; B: SmallInt) : SmallInt; Cdecl;
Function Add( A: Real; B: Real) : Real; Cdecl;
Implementation
Function Add( A: SmallInt; B: SmallInt) : SmallInt; Cdecl;
Begin
Add := A + B;
End;
Function Add( A: real; B: real) : real; Cdecl;
Begin
Add := A + B;
End;
End.
On Linux, you could compile and link this into a shared object with
these commands:
fpc add.pas
gcc -o add.so -shared add.o
And you could then use it from Perl:
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lang('Pascal');
$ffi->lib('./add.so');
$ffi->attach(
['Add.Add(SmallInt,SmallInt):SmallInt' => 'Add'] => ['SmallInt','SmallInt'] => 'SmallInt'
);
print Add(1,2), "\n";
When attaching the function you have to specify the argument and return
types because the Add function is overloaded and is ambiguous without
it. If there were just one Add function, then you could attach it like
this:
$ffi->attach('Add' => ['SmallInt','SmallInt'] => 'SmallInt');
The downside to using a shared library constructed from Pascal Units in
this way is that the resulting dynamic library does not include the
Pascal standard library so very simple capabilities such as IO and
ShortString are not available. It is recommended instead to use a
Pascal Library (possibly linked with one or more Pascal Units), as
inthe "SYNOPSIS" at the top.
METHODS
Generally you will not use this class directly, instead interacting
with the FFI::Platypus instance. However, the public methods used by
Platypus are documented here.
native_type_map
my $hashref = FFI::Platypus::Lang::Pascal->native_type_map;
This returns a hash reference containing the native aliases for the
Free Pascal programming languages. That is the keys are native Pascal
types and the values are libffi native types.
Types are in camel case. For example use ShortInt, not Shortint or
SHORTINT.
mangler
my $mangler = FFI::Platypus::Lang::Pascal->mangler($ffi->libs);
# prints ADD_ADD$SMALLINT$SMALLINT$$SMALLINT
print $mangler->("add(smallint,smallint):smallint");
Returns a subroutine reference that will "mangle" Pascal names.
EXAMPLES
See the above "SYNOPSIS" or the examples directory that came with this
distribution.
SUPPORT
If something does not work as advertised, or the way that you think it
should, or if you have a feature request, please open an issue on this
project's GitHub issue tracker:
https://github.com/PerlFFI/FFI-Platypus-Lang-Pascal/issues
This project's GitHub issue tracker listed above is not Write-Only. If
you want to contribute then feel free to browse through the existing
issues and see if there is something you feel you might be good at and
take a whack at the problem. I frequently open issues myself that I
hope will be accomplished by someone in the future but do not have time
to immediately implement myself.
Another good area to help out in is documentation. I try to make sure
that there is good document coverage, that is there should be
documentation describing all the public features and warnings about
common pitfalls, but an outsider's or alternate view point on such
things would be welcome; if you see something confusing or lacks
sufficient detail I encourage documentation only pull requests to
improve things.
CONTRIBUTING
If you have implemented a new feature or fixed a bug then you may make
a pull reequest on this project's GitHub repository:
https://github.com/PerlFFI/FFI-Platypus-Lang-Pascal/pulls
SEE ALSO
FFI::Platypus
The Core Platypus documentation.
Module::Build::FFI::Pascal
Bundle Free Pascal with your FFI / Perl extension.
AUTHOR
Graham Ollis <[email protected]>
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.