Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 235 lines (215 sloc) 8.368 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.
18 sub param_hash_for(Parameter $p) {
19 my Mu $result := nqp::hash();
d22dd75 @jnthn Better string support, including multiple encodings.
authored
20 my $type := $p.type();
21 if $type ~~ Str {
22 my $enc := $p.?native_call_encoded() || 'utf8';
23 nqp::bindkey($result, 'type', nqp::unbox_s(string_encoding_to_nci_type($enc)));
24 nqp::bindkey($result, 'free_str', nqp::unbox_i(1));
25 }
26 else {
27 nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($p.type)));
28 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
29 $result
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
30 }
a32dace @jnthn More types, plus CPointer that translates to some abstract pointer-y thi...
authored
31
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
32 # Builds a hash of type information for the specified return type.
d22dd75 @jnthn Better string support, including multiple encodings.
authored
33 sub return_hash_for(&r) {
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
34 my Mu $result := nqp::hash();
d22dd75 @jnthn Better string support, including multiple encodings.
authored
35 my $returns := &r.returns;
36 if $returns ~~ Str {
37 my $enc := &r.?native_call_encoded() || 'utf8';
38 nqp::bindkey($result, 'type', nqp::unbox_s(string_encoding_to_nci_type($enc)));
39 nqp::bindkey($result, 'free_str', nqp::unbox_i(0));
40 }
41 else {
42 nqp::bindkey($result, 'type',
43 $returns =:= Mu ?? 'void' !! nqp::unbox_s(type_code_for($returns)));
44 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
45 $result
8525e82 @jnthn Use the declared return type of the routine in signature generation.
authored
46 }
47
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
48 # Gets the NCI type code to use based on a given Perl 6 type.
49 my %type_map =
50 'int8' => 'char',
51 'int16' => 'short',
52 'int32' => 'int',
53 'int' => 'long',
54 'Int' => 'longlong',
55 'num32' => 'float',
56 'num64' => 'double',
57 'num' => 'double',
d22dd75 @jnthn Better string support, including multiple encodings.
authored
58 'Num' => 'double';
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
59 sub type_code_for(Mu ::T) {
60 return %type_map{T.^name}
61 if %type_map.exists(T.^name);
e293ff0 @jnthn Add CStruct passing support. Improve error message when trying to pass a...
authored
62 return 'cstruct'
63 if T.REPR eq 'CStruct';
4561a4f @jnthn Support opaque pointers in NativeCall.
authored
64 return 'cpointer'
65 if T.REPR eq 'CPointer';
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
66 return 'carray'
67 if T.REPR eq 'CArray';
e293ff0 @jnthn Add CStruct passing support. Improve error message when trying to pass a...
authored
68 die "Unknown type {T.^name} used in native call.\n" ~
69 "If you want to pass a struct, be sure to use the CStruct representation.\n" ~
70 "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
71 }
72
6424f48 @jnthn Unbust voids.
authored
73 multi sub map_return_type(Mu $type) { Mu }
74 multi sub map_return_type($type) {
64ca08b @jnthn Fix sized int/num returns.
authored
75 $type === int8 || $type === int16 || $type === int32 || $type === int ?? Int !!
76 $type === num32 || $type === num64 || $type === num ?? Num !!
77 $type
78 }
79
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
80 # This role is mixed in to any routine that is marked as being a
81 # native call.
82 my role Native[Routine $r, Str $libname] {
83 has int $!setup;
f8b42a6 @moritz check the number of arguments passed to a native function.
moritz authored
84 has int $!param-count;
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
85 has native_callsite $!call is box_target;
86
87 method postcircumfix:<( )>($args) {
88 unless $!setup {
89 my Mu $arg_info := nqp::list();
90 for $r.signature.params -> $p {
91 nqp::push($arg_info, param_hash_for($p))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
92 }
f8b42a6 @moritz check the number of arguments passed to a native function.
moritz authored
93 $!param-count = nqp::unbox_i($r.signature.params.elems);
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
94 my str $conv = self.?native_call_convention || '';
4ed29ae @jnthn Fixes to loading from current executing module functionality.
authored
95 my $realname =
96 !$libname.DEFINITE ?? "" !!
97 $libname ~~ /\.\w+$/ ?? $libname !!
98 "$libname$*VM<config><load_ext>";
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
99 nqp::buildnativecall(self,
b3e94c4 @jnthn Restore auto-appending the .dll or .so rather than requiring it be menti...
authored
100 nqp::unbox_s($realname), # library name
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
101 nqp::unbox_s($r.name), # symbol to call
102 nqp::unbox_s($conv), # calling convention
103 $arg_info,
d22dd75 @jnthn Better string support, including multiple encodings.
authored
104 return_hash_for($r));
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
105 $!setup = 1;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
106 }
f8b42a6 @moritz check the number of arguments passed to a native function.
moritz authored
107 die "Wrong number of arguments in call to native routine $r.name(). Got: $args.elems(), expected: $!param-count" if nqp::unbox_i($args.elems) != $!param-count;
64ca08b @jnthn Fix sized int/num returns.
authored
108 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
109 nqp::getattr(nqp::p6decont($args), Capture, '$!list'))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub in s...
authored
110 }
111 }
112
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
113 # Role for carrying extra calling convention information.
114 my role NativeCallingConvention[$name] {
115 method native_call_convention() { $name };
116 }
d907f71 @mberends [lib/NativeCall.pm6] add patch provided by pmichaud++
mberends authored
117
d22dd75 @jnthn Better string support, including multiple encodings.
authored
118 # Role for carrying extra string encoding information.
119 my role NativeCallEncoded[$name] {
120 method native_call_encoded() { $name };
121 }
122
4561a4f @jnthn Support opaque pointers in NativeCall.
authored
123 # Expose an OpaquePointer class for working with raw pointers.
124 my class OpaquePointer is export is repr('CPointer') { }
125
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
126 # CArray class, used to represent C arrays.
127 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
128 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
129
130 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
131 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
132 Proxy.new:
133 FETCH => method () {
134 nqp::p6box_i(nqp::r_atpos_i($arr, nqp::unbox_i($pos.Int)))
135 },
136 STORE => method (int $v) {
137 nqp::r_bindpos_i($arr, nqp::unbox_i($pos.Int), $v);
138 self
139 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
140 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
141 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
142 Proxy.new:
143 FETCH => method () {
144 nqp::p6box_i(nqp::r_atpos_i($arr, $pos))
145 },
146 STORE => method (int $v) {
147 nqp::r_bindpos_i($arr, $pos, $v);
148 self
149 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
150 }
151 }
152 multi method PARAMETERIZE_TYPE(Int:U $t) {
153 self but IntTypedCArray[$t.WHAT]
154 }
155
156 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
157 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
158 Proxy.new:
159 FETCH => method () {
160 nqp::p6box_n(nqp::r_atpos_n($arr, nqp::unbox_i($pos.Int)))
161 },
162 STORE => method (num $v) {
163 nqp::r_bindpos_n($arr, nqp::unbox_i($pos.Int), $v);
164 self
165 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
166 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
167 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored
168 Proxy.new:
169 FETCH => method () {
170 nqp::p6box_n(nqp::r_atpos_n($arr, $pos))
171 },
172 STORE => method (num $v) {
173 nqp::r_bindpos_n($arr, $pos, $v);
174 self
175 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
176 }
177 }
178 multi method PARAMETERIZE_TYPE(Num:U $t) {
179 self but NumTypedCArray[$t.WHAT]
180 }
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored
181
182 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
183 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored
184 Proxy.new:
185 FETCH => method () {
186 nqp::r_atpos($arr, nqp::unbox_i($pos.Int))
187 },
188 STORE => method ($v) {
189 nqp::r_bindpos($arr, nqp::unbox_i($pos.Int), nqp::p6decont($v));
190 self
191 }
192 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to avo...
moritz authored
193 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored
194 Proxy.new:
195 FETCH => method () {
196 nqp::r_atpos($arr, $pos)
197 },
198 STORE => method ($v) {
199 nqp::r_bindpos($arr, $pos, nqp::p6decont($v));
200 self
201 }
202 }
203 }
204 multi method PARAMETERIZE_TYPE(Mu:U $t) {
205 die "A C array can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not $t.perl())"
206 unless $t === Str || $t.REPR eq 'CStruct' | 'CPointer' | 'CArray';
207 self but TypedCArray[$t.WHAT]
208 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much yet, ...
authored
209 }
210
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
211 # Specifies that the routine is actually a native call, and gives
212 # the name of the library to load it from.
213 multi trait_mod:<is>(Routine $r, $libname, :$native!) is export {
214 $r does Native[$r, $libname];
bd56459 @jnthn Add some initial experimental code that shows how we can call a Win32 AP...
authored
215 }
9766cd2 @jnthn If there's no returns type declared, we assume void return.
authored
216
bcbdb85 @jnthn Overload of the native trait_mod that handles the 'call in current execu...
authored
217 # Specifies that the routine is actually a native call, into the
218 # current executable (platform specific).
219 multi trait_mod:<is>(Routine $r, :$native!) is export {
220 $r does Native[$r, Str];
221 }
222
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array stuf...
authored
223 # Specifies the calling convention to use for a native call.
224 multi trait_mod:<is>(Routine $r, $name, :$nativeconv!) is export {
225 $r does NativeCallingConvention[$name];
226 }
d22dd75 @jnthn Better string support, including multiple encodings.
authored
227
228 # Ways to specify how to marshall strings.
229 multi trait_mod:<is>(Parameter $p, $name, :$encoded!) is export {
230 $p does NativeCallEncoded[$name];
231 }
232 multi trait_mod:<is>(Routine $p, $name, :$encoded!) is export {
233 $p does NativeCallEncoded[$name];
234 }
Something went wrong with that request. Please try again.