Skip to content
This repository
Newer
Older
100644 269 lines (243 sloc) 9.366 kb
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
1 module NativeCall;
8e36c743 »
2010-03-03 CPointer becomes OpaquePointer for better naming win; start to stub i…
2
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
3 # Throwaway type just to get us some way to get at the NativeCall
4 # representation.
5 my class native_callsite is repr('NativeCall') { }
8e36c743 »
2010-03-03 CPointer becomes OpaquePointer for better naming win; start to stub i…
6
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
7 # Maps a chosen string encoding to a type recognized by the native call engine.
8 sub string_encoding_to_nci_type($enc) {
9 given $enc {
10 when 'utf8' { 'utf8str' }
11 when 'utf16' { 'utf16str' }
12 when 'ascii' { 'asciistr' }
13 default { die "Unknown string encoding for native call: $enc"; }
14 }
15 }
16
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
17 # Builds a hash of type information for the specified parameter.
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
18 sub param_hash_for(Parameter $p, :$with-typeobj) {
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
19 my Mu $result := nqp::hash();
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
20 my $type := $p.type();
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
21 nqp::bindkey($result, 'typeobj', $type) if $with-typeobj;
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
22 if $type ~~ Str {
23 my $enc := $p.?native_call_encoded() || 'utf8';
24 nqp::bindkey($result, 'type', nqp::unbox_s(string_encoding_to_nci_type($enc)));
25 nqp::bindkey($result, 'free_str', nqp::unbox_i(1));
26 }
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
27 elsif $type ~~ Callable {
28 nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($p.type)));
dc43e908 » arnsholt
2012-07-24 Change to right Parameter accessor for signatures.
29 my $info := param_list_for($p.sub_signature, :with-typeobj);
79429d93 » arnsholt
2012-07-30 Return values from callbacks seem to work now as well.
30 nqp::unshift($info, return_hash_for($p.sub_signature));
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
31 nqp::bindkey($result, 'callback_args', $info);
32 }
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
33 else {
34 nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($p.type)));
35 }
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
36 $result
8e36c743 »
2010-03-03 CPointer becomes OpaquePointer for better naming win; start to stub i…
37 }
a32dace9 »
2010-03-02 More types, plus CPointer that translates to some abstract pointer-y …
38
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
39 # Builds the list of parameter information for a callback argument.
40 sub param_list_for(Signature $sig, :$with-typeobj) {
41 my Mu $arg_info := nqp::list();
42 for $sig.params -> $p {
43 nqp::push($arg_info, param_hash_for($p, :with-typeobj($with-typeobj)))
44 }
45
2680acda » arnsholt
2012-07-29 Unbreak everything.
46 $arg_info;
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
47 }
48
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
49 # Builds a hash of type information for the specified return type.
79429d93 » arnsholt
2012-07-30 Return values from callbacks seem to work now as well.
50 sub return_hash_for(Signature $s) {
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
51 my Mu $result := nqp::hash();
79429d93 » arnsholt
2012-07-30 Return values from callbacks seem to work now as well.
52 my $returns := $s.returns;
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
53 if $returns ~~ Str {
54 my $enc := &r.?native_call_encoded() || 'utf8';
55 nqp::bindkey($result, 'type', nqp::unbox_s(string_encoding_to_nci_type($enc)));
56 nqp::bindkey($result, 'free_str', nqp::unbox_i(0));
57 }
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
58 # TODO: If we ever want to handle function pointers returned from C, this
59 # bit of code needs to handle that.
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
60 else {
61 nqp::bindkey($result, 'type',
62 $returns =:= Mu ?? 'void' !! nqp::unbox_s(type_code_for($returns)));
63 }
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
64 $result
8525e82e »
2010-03-03 Use the declared return type of the routine in signature generation.
65 }
66
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
67 # Gets the NCI type code to use based on a given Perl 6 type.
68 my %type_map =
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
69 'int8' => 'char',
70 'int16' => 'short',
71 'int32' => 'int',
72 'int' => 'long',
73 'Int' => 'longlong',
74 'num32' => 'float',
75 'num64' => 'double',
76 'num' => 'double',
77 'Num' => 'double',
78 'Callable' => 'callback';
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
79 sub type_code_for(Mu ::T) {
80 return %type_map{T.^name}
81 if %type_map.exists(T.^name);
e293ff0a »
2011-11-27 Add CStruct passing support. Improve error message when trying to pas…
82 return 'cstruct'
83 if T.REPR eq 'CStruct';
4561a4f4 »
2011-11-28 Support opaque pointers in NativeCall.
84 return 'cpointer'
85 if T.REPR eq 'CPointer';
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
86 return 'carray'
87 if T.REPR eq 'CArray';
e293ff0a »
2011-11-27 Add CStruct passing support. Improve error message when trying to pas…
88 die "Unknown type {T.^name} used in native call.\n" ~
89 "If you want to pass a struct, be sure to use the CStruct representation.\n" ~
90 "If you want to pass an array, be sure to use the CArray type.";
bd56459f »
2010-03-02 Add some initial experimental code that shows how we can call a Win32…
91 }
92
6424f48f »
2012-01-13 Unbust voids.
93 multi sub map_return_type(Mu $type) { Mu }
94 multi sub map_return_type($type) {
64ca08bd »
2012-01-13 Fix sized int/num returns.
95 $type === int8 || $type === int16 || $type === int32 || $type === int ?? Int !!
96 $type === num32 || $type === num64 || $type === num ?? Num !!
97 $type
98 }
99
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
100 # This role is mixed in to any routine that is marked as being a
101 # native call.
102 my role Native[Routine $r, Str $libname] {
103 has int $!setup;
104 has native_callsite $!call is box_target;
105
106 method postcircumfix:<( )>($args) {
107 unless $!setup {
46d9cce0 » arnsholt
2012-07-23 Functionality and tests for callbacks.
108 my Mu $arg_info := param_list_for($r.signature);
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
109 my str $conv = self.?native_call_convention || '';
4ed29aed »
2012-01-13 Fixes to loading from current executing module functionality.
110 my $realname =
111 !$libname.DEFINITE ?? "" !!
112 $libname ~~ /\.\w+$/ ?? $libname !!
113 "$libname$*VM<config><load_ext>";
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
114 nqp::buildnativecall(self,
b3e94c4d »
2012-01-13 Restore auto-appending the .dll or .so rather than requiring it be me…
115 nqp::unbox_s($realname), # library name
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
116 nqp::unbox_s($r.name), # symbol to call
117 nqp::unbox_s($conv), # calling convention
118 $arg_info,
79429d93 » arnsholt
2012-07-30 Return values from callbacks seem to work now as well.
119 return_hash_for($r.signature));
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
120 $!setup = 1;
8e36c743 »
2010-03-03 CPointer becomes OpaquePointer for better naming win; start to stub i…
121 }
64ca08bd »
2012-01-13 Fix sized int/num returns.
122 nqp::nativecall(nqp::p6decont(map_return_type($r.returns)), self,
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
123 nqp::getattr(nqp::p6decont($args), Capture, '$!list'))
8e36c743 »
2010-03-03 CPointer becomes OpaquePointer for better naming win; start to stub i…
124 }
125 }
126
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
127 # Role for carrying extra calling convention information.
128 my role NativeCallingConvention[$name] {
129 method native_call_convention() { $name };
130 }
d907f71c » mberends
2011-06-02 [lib/NativeCall.pm6] add patch provided by pmichaud++
131
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
132 # Role for carrying extra string encoding information.
133 my role NativeCallEncoded[$name] {
134 method native_call_encoded() { $name };
135 }
136
4561a4f4 »
2011-11-28 Support opaque pointers in NativeCall.
137 # Expose an OpaquePointer class for working with raw pointers.
138 my class OpaquePointer is export is repr('CPointer') { }
139
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
140 # CArray class, used to represent C arrays.
141 my class CArray is export is repr('CArray') {
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
142 method at_pos(CArray:D: $pos) { die "CArray cannot be used without a type" }
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
143
144 my role IntTypedCArray[::TValue] does Positional[TValue] {
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
145 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5b »
2011-12-18 Support creating and passing arrays to C also.
146 Proxy.new:
147 FETCH => method () {
148 nqp::p6box_i(nqp::r_atpos_i($arr, nqp::unbox_i($pos.Int)))
149 },
150 STORE => method (int $v) {
151 nqp::r_bindpos_i($arr, nqp::unbox_i($pos.Int), $v);
152 self
153 }
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
154 }
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
155 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5b »
2011-12-18 Support creating and passing arrays to C also.
156 Proxy.new:
157 FETCH => method () {
158 nqp::p6box_i(nqp::r_atpos_i($arr, $pos))
159 },
160 STORE => method (int $v) {
161 nqp::r_bindpos_i($arr, $pos, $v);
162 self
163 }
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
164 }
165 }
166 multi method PARAMETERIZE_TYPE(Int:U $t) {
167 self but IntTypedCArray[$t.WHAT]
168 }
169
170 my role NumTypedCArray[::TValue] does Positional[TValue] {
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
171 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5b »
2011-12-18 Support creating and passing arrays to C also.
172 Proxy.new:
173 FETCH => method () {
174 nqp::p6box_n(nqp::r_atpos_n($arr, nqp::unbox_i($pos.Int)))
175 },
176 STORE => method (num $v) {
177 nqp::r_bindpos_n($arr, nqp::unbox_i($pos.Int), $v);
178 self
179 }
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
180 }
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
181 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5b »
2011-12-18 Support creating and passing arrays to C also.
182 Proxy.new:
183 FETCH => method () {
184 nqp::p6box_n(nqp::r_atpos_n($arr, $pos))
185 },
186 STORE => method (num $v) {
187 nqp::r_bindpos_n($arr, $pos, $v);
188 self
189 }
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
190 }
191 }
192 multi method PARAMETERIZE_TYPE(Num:U $t) {
193 self but NumTypedCArray[$t.WHAT]
194 }
731f8412 »
2012-01-13 NativeCall library additions to prepare for arrays of strings.
195
196 my role TypedCArray[::TValue] does Positional[TValue] {
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
197 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
731f8412 »
2012-01-13 NativeCall library additions to prepare for arrays of strings.
198 Proxy.new:
199 FETCH => method () {
200 nqp::r_atpos($arr, nqp::unbox_i($pos.Int))
201 },
202 STORE => method ($v) {
203 nqp::r_bindpos($arr, nqp::unbox_i($pos.Int), nqp::p6decont($v));
204 self
205 }
206 }
9d96c04b » moritz
2012-04-21 now that we have autoviv, our at_pos invocants need to be defined to …
207 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
731f8412 »
2012-01-13 NativeCall library additions to prepare for arrays of strings.
208 Proxy.new:
209 FETCH => method () {
210 nqp::r_atpos($arr, $pos)
211 },
212 STORE => method ($v) {
213 nqp::r_bindpos($arr, $pos, nqp::p6decont($v));
214 self
215 }
216 }
217 }
218 multi method PARAMETERIZE_TYPE(Mu:U $t) {
219 die "A C array can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not $t.perl())"
220 unless $t === Str || $t.REPR eq 'CStruct' | 'CPointer' | 'CArray';
221 self but TypedCArray[$t.WHAT]
222 }
6803a5aa »
2011-12-18 Put in first few bits of C array handling support. Doesn't do much ye…
223 }
224
bcbdb85d »
2012-01-13 Overload of the native trait_mod that handles the 'call in current ex…
225 # Specifies that the routine is actually a native call, into the
d419ffa4 » moritz
2012-05-12 adapt to updated trait calling conventions
226 # current executable (platform specific) or into a named library
bcbdb85d »
2012-01-13 Overload of the native trait_mod that handles the 'call in current ex…
227 multi trait_mod:<is>(Routine $r, :$native!) is export {
d419ffa4 » moritz
2012-05-12 adapt to updated trait calling conventions
228 $r does Native[$r, $native === True ?? Str !! $native];
bcbdb85d »
2012-01-13 Overload of the native trait_mod that handles the 'call in current ex…
229 }
230
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
231 # Specifies the calling convention to use for a native call.
a78f01df » moritz
2012-05-12 update calling convention of other traits too
232 multi trait_mod:<is>(Routine $r, :$nativeconv!) is export {
233 $r does NativeCallingConvention[$nativeconv];
898c6457 »
2011-11-24 Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
234 }
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
235
236 # Ways to specify how to marshall strings.
a78f01df » moritz
2012-05-12 update calling convention of other traits too
237 multi trait_mod:<is>(Parameter $p, :$encoded!) is export {
238 $p does NativeCallEncoded[$encoded];
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
239 }
a78f01df » moritz
2012-05-12 update calling convention of other traits too
240 multi trait_mod:<is>(Routine $p, :$encoded!) is export {
241 $p does NativeCallEncoded[$encoded];
d22dd75a »
2011-11-27 Better string support, including multiple encodings.
242 }
8619523b » arnsholt
2012-05-01 Add vim modeline to NativeCall.pm6.
243
216d11cb » arnsholt
2012-05-01 Pass encoding parameter to CStr representation.
244 class CStr is repr('CStr') {
245 my role Encoding[$encoding] {
246 method encoding() { $encoding }
247 }
248
249 multi method PARAMETERIZE_TYPE(Str:D $encoding) {
250 die "Unknown string encoding for native call: $encoding" if not $encoding eq any('utf8', 'utf16', 'ascii');
251 self but Encoding[$encoding];
252 }
253 }
254
255 role ExplicitlyManagedString {
256 has CStr $.cstr is rw;
257 }
258
a4f37b45 » arnsholt
2012-05-02 Make Str parameter of explicitly-manage rw. moritz++
259 multi explicitly-manage(Str $x is rw, :$encoding = 'utf8') is export {
216d11cb » arnsholt
2012-05-01 Pass encoding parameter to CStr representation.
260 $x does ExplicitlyManagedString;
261 $x.cstr = pir::repr_box_str__PsP(nqp::unbox_s($x), CStr[$encoding]);
e94f45ca » arnsholt
2012-05-01 Tests and support functions for explicitly managing strings.
262 }
263
3bf67129 » arnsholt
2012-08-03 Expose refresh op through function, tests for refresh().
264 multi refresh($obj) is export {
265 nqp::nativecallrefresh($obj);
266 1;
267 }
268
8619523b » arnsholt
2012-05-01 Add vim modeline to NativeCall.pm6.
269 # vim:ft=perl6
Something went wrong with that request. Please try again.