Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 265 lines (239 sloc) 9.29 kb
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
1 module NativeCall;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
2
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
3 # Throwaway type just to get us some way to get at the NativeCall
4 # representation.
5 my class native_callsite is repr('NativeCall') { }
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
6
d22dd75 @jnthn Better string support, including multiple encodings.
authored
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
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
17 # Builds a hash of type information for the specified parameter.
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
18 sub param_hash_for(Parameter $p, :$with-typeobj) {
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
19 my Mu $result := nqp::hash();
d22dd75 @jnthn Better string support, including multiple encodings.
authored
20 my $type := $p.type();
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
21 nqp::bindkey($result, 'typeobj', $type) if $with-typeobj;
d22dd75 @jnthn Better string support, including multiple encodings.
authored
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 }
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
27 elsif $type ~~ Callable {
28 nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($p.type)));
dc43e90 @arnsholt Change to right Parameter accessor for signatures.
arnsholt authored
29 my $info := param_list_for($p.sub_signature, :with-typeobj);
79429d9 @arnsholt Return values from callbacks seem to work now as well.
arnsholt authored
30 nqp::unshift($info, return_hash_for($p.sub_signature));
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
31 nqp::bindkey($result, 'callback_args', $info);
32 }
d22dd75 @jnthn Better string support, including multiple encodings.
authored
33 else {
34 nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($p.type)));
35 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
36 $result
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
37 }
a32dace @jnthn More types, plus CPointer that translates to some abstract pointer-y thi...
authored
38
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
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
2680acd @arnsholt Unbreak everything.
arnsholt authored
46 $arg_info;
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
47 }
48
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
49 # Builds a hash of type information for the specified return type.
79429d9 @arnsholt Return values from callbacks seem to work now as well.
arnsholt authored
50 sub return_hash_for(Signature $s) {
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
51 my Mu $result := nqp::hash();
79429d9 @arnsholt Return values from callbacks seem to work now as well.
arnsholt authored
52 my $returns := $s.returns;
d22dd75 @jnthn Better string support, including multiple encodings.
authored
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 }
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
58 # TODO: If we ever want to handle function pointers returned from C, this
59 # bit of code needs to handle that.
d22dd75 @jnthn Better string support, including multiple encodings.
authored
60 else {
61 nqp::bindkey($result, 'type',
62 $returns =:= Mu ?? 'void' !! nqp::unbox_s(type_code_for($returns)));
63 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
64 $result
8525e82 @jnthn Use the declared return type of the routine in signature generation.
authored
65 }
66
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
67 # Gets the NCI type code to use based on a given Perl 6 type.
68 my %type_map =
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
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';
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
79 sub type_code_for(Mu ::T) {
80 return %type_map{T.^name}
81 if %type_map.exists(T.^name);
e293ff0 @jnthn Add CStruct passing support. Improve error message when trying to pass a...
authored
82 return 'cstruct'
83 if T.REPR eq 'CStruct';
4561a4f @jnthn Support opaque pointers in NativeCall.
authored
84 return 'cpointer'
85 if T.REPR eq 'CPointer';
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
86 return 'carray'
87 if T.REPR eq 'CArray';
e293ff0 @jnthn Add CStruct passing support. Improve error message when trying to pass a...
authored
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.";
bd56459 @jnthn Add some initial experimental code that shows how we can call a Win32 AP...
authored
91 }
92
6424f48 @jnthn Unbust voids.
authored
93 multi sub map_return_type(Mu $type) { Mu }
94 multi sub map_return_type($type) {
64ca08b @jnthn Fix sized int/num returns.
authored
95 $type === int8 || $type === int16 || $type === int32 || $type === int ?? Int !!
96 $type === num32 || $type === num64 || $type === num ?? Num !!
97 $type
98 }
99
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
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 {
46d9cce @arnsholt Functionality and tests for callbacks.
arnsholt authored
108 my Mu $arg_info := param_list_for($r.signature);
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
109 my str $conv = self.?native_call_convention || '';
4ed29ae @jnthn Fixes to loading from current executing module functionality.
authored
110 my $realname =
111 !$libname.DEFINITE ?? "" !!
112 $libname ~~ /\.\w+$/ ?? $libname !!
113 "$libname$*VM<config><load_ext>";
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
114 nqp::buildnativecall(self,
b3e94c4 @jnthn Restore auto-appending the .dll or .so rather than requiring it be menti...
authored
115 nqp::unbox_s($realname), # library name
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
116 nqp::unbox_s($r.name), # symbol to call
117 nqp::unbox_s($conv), # calling convention
118 $arg_info,
79429d9 @arnsholt Return values from callbacks seem to work now as well.
arnsholt authored
119 return_hash_for($r.signature));
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
120 $!setup = 1;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
121 }
64ca08b @jnthn Fix sized int/num returns.
authored
122 nqp::nativecall(nqp::p6decont(map_return_type($r.returns)), self,
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
123 nqp::getattr(nqp::p6decont($args), Capture, '$!list'))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
124 }
125 }
126
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
127 # Role for carrying extra calling convention information.
128 my role NativeCallingConvention[$name] {
129 method native_call_convention() { $name };
130 }
d907f71 @mberends [lib/NativeCall.pm6] add patch provided by pmichaud++
mberends authored
131
d22dd75 @jnthn Better string support, including multiple encodings.
authored
132 # Role for carrying extra string encoding information.
133 my role NativeCallEncoded[$name] {
134 method native_call_encoded() { $name };
135 }
136
4561a4f @jnthn Support opaque pointers in NativeCall.
authored
137 # Expose an OpaquePointer class for working with raw pointers.
138 my class OpaquePointer is export is repr('CPointer') { }
139
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
140 # CArray class, used to represent C arrays.
141 my class CArray is export is repr('CArray') {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
142 method at_pos(CArray:D: $pos) { die "CArray cannot be used without a type" }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
143
144 my role IntTypedCArray[::TValue] does Positional[TValue] {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
145 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
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 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
154 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
155 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
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 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
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] {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
171 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
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 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
180 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
181 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
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 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
190 }
191 }
192 multi method PARAMETERIZE_TYPE(Num:U $t) {
193 self but NumTypedCArray[$t.WHAT]
194 }
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored
195
196 my role TypedCArray[::TValue] does Positional[TValue] {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
197 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored
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 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
207 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored
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 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
223 }
224
bcbdb85 @jnthn Overload of the native trait_mod that handles the 'call in current execu...
authored
225 # Specifies that the routine is actually a native call, into the
d419ffa @moritz adapt to updated trait calling conventions
moritz authored
226 # current executable (platform specific) or into a named library
bcbdb85 @jnthn Overload of the native trait_mod that handles the 'call in current execu...
authored
227 multi trait_mod:<is>(Routine $r, :$native!) is export {
d419ffa @moritz adapt to updated trait calling conventions
moritz authored
228 $r does Native[$r, $native === True ?? Str !! $native];
bcbdb85 @jnthn Overload of the native trait_mod that handles the 'call in current execu...
authored
229 }
230
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
231 # Specifies the calling convention to use for a native call.
a78f01d @moritz update calling convention of other traits too
moritz authored
232 multi trait_mod:<is>(Routine $r, :$nativeconv!) is export {
233 $r does NativeCallingConvention[$nativeconv];
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
234 }
d22dd75 @jnthn Better string support, including multiple encodings.
authored
235
236 # Ways to specify how to marshall strings.
a78f01d @moritz update calling convention of other traits too
moritz authored
237 multi trait_mod:<is>(Parameter $p, :$encoded!) is export {
238 $p does NativeCallEncoded[$encoded];
d22dd75 @jnthn Better string support, including multiple encodings.
authored
239 }
a78f01d @moritz update calling convention of other traits too
moritz authored
240 multi trait_mod:<is>(Routine $p, :$encoded!) is export {
241 $p does NativeCallEncoded[$encoded];
d22dd75 @jnthn Better string support, including multiple encodings.
authored
242 }
8619523 @arnsholt Add vim modeline to NativeCall.pm6.
arnsholt authored
243
216d11c @arnsholt Pass encoding parameter to CStr representation.
arnsholt authored
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
a4f37b4 @arnsholt Make Str parameter of explicitly-manage rw. moritz++
arnsholt authored
259 multi explicitly-manage(Str $x is rw, :$encoding = 'utf8') is export {
216d11c @arnsholt Pass encoding parameter to CStr representation.
arnsholt authored
260 $x does ExplicitlyManagedString;
261 $x.cstr = pir::repr_box_str__PsP(nqp::unbox_s($x), CStr[$encoding]);
e94f45c @arnsholt Tests and support functions for explicitly managing strings.
arnsholt authored
262 }
263
8619523 @arnsholt Add vim modeline to NativeCall.pm6.
arnsholt authored
264 # vim:ft=perl6
Something went wrong with that request. Please try again.