Skip to content
Newer
Older
100644 236 lines (216 sloc) 8.17 KB
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
1 module NativeCall;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
2
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
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 i…
authored Mar 3, 2010
6
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
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 s…
authored Nov 24, 2011
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 Nov 27, 2011
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 s…
authored Nov 24, 2011
29 $result
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
30 }
a32dace @jnthn More types, plus CPointer that translates to some abstract pointer-y …
authored Mar 2, 2010
31
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
32 # Builds a hash of type information for the specified return type.
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
33 sub return_hash_for(&r) {
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
34 my Mu $result := nqp::hash();
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
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 s…
authored Nov 24, 2011
45 $result
8525e82 @jnthn Use the declared return type of the routine in signature generation.
authored Mar 3, 2010
46 }
47
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
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 Nov 27, 2011
58 'Num' => 'double';
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
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 pas…
authored Nov 27, 2011
62 return 'cstruct'
63 if T.REPR eq 'CStruct';
4561a4f @jnthn Support opaque pointers in NativeCall.
authored Nov 28, 2011
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 ye…
authored Dec 18, 2011
66 return 'carray'
67 if T.REPR eq 'CArray';
e293ff0 @jnthn Add CStruct passing support. Improve error message when trying to pas…
authored Nov 27, 2011
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…
authored Mar 2, 2010
71 }
72
6424f48 @jnthn Unbust voids.
authored Jan 13, 2012
73 multi sub map_return_type(Mu $type) { Mu }
74 multi sub map_return_type($type) {
64ca08b @jnthn Fix sized int/num returns.
authored Jan 13, 2012
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 s…
authored Nov 24, 2011
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;
84 has native_callsite $!call is box_target;
85
86 method postcircumfix:<( )>($args) {
87 unless $!setup {
88 my Mu $arg_info := nqp::list();
89 for $r.signature.params -> $p {
d7e7855 @moritz munge types of the original signature
moritz authored Apr 27, 2012
90 nqp::push($arg_info, param_hash_for($p));
91 nqp::bindattr(nqp::p6decont($p), Parameter, '$!nominal_type', nqp::p6decont(map_return_type($p.type)));
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
92 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
93 my str $conv = self.?native_call_convention || '';
4ed29ae @jnthn Fixes to loading from current executing module functionality.
authored Jan 13, 2012
94 my $realname =
95 !$libname.DEFINITE ?? "" !!
96 $libname ~~ /\.\w+$/ ?? $libname !!
97 "$libname$*VM<config><load_ext>";
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
98 nqp::buildnativecall(self,
b3e94c4 @jnthn Restore auto-appending the .dll or .so rather than requiring it be me…
authored Jan 13, 2012
99 nqp::unbox_s($realname), # library name
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
100 nqp::unbox_s($r.name), # symbol to call
101 nqp::unbox_s($conv), # calling convention
102 $arg_info,
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
103 return_hash_for($r));
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
104 $!setup = 1;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
105 }
5e45892 @moritz try to use the original signature for type checking
moritz authored Apr 27, 2012
106 unless $r.candidates_matching(|$args) {
107 die "Signature mismatch while calling native function {$r.name}";
108 }
64ca08b @jnthn Fix sized int/num returns.
authored Jan 13, 2012
109 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 s…
authored Nov 24, 2011
110 nqp::getattr(nqp::p6decont($args), Capture, '$!list'))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
111 }
112 }
113
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
114 # Role for carrying extra calling convention information.
115 my role NativeCallingConvention[$name] {
116 method native_call_convention() { $name };
117 }
d907f71 @mberends [lib/NativeCall.pm6] add patch provided by pmichaud++
mberends authored Jun 2, 2011
118
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
119 # Role for carrying extra string encoding information.
120 my role NativeCallEncoded[$name] {
121 method native_call_encoded() { $name };
122 }
123
4561a4f @jnthn Support opaque pointers in NativeCall.
authored Nov 28, 2011
124 # Expose an OpaquePointer class for working with raw pointers.
125 my class OpaquePointer is export is repr('CPointer') { }
126
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much ye…
authored Dec 17, 2011
127 # CArray class, used to represent C arrays.
128 my class CArray is export is repr('CArray') {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
129 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 ye…
authored Dec 17, 2011
130
131 my role IntTypedCArray[::TValue] does Positional[TValue] {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
132 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored Dec 18, 2011
133 Proxy.new:
134 FETCH => method () {
135 nqp::p6box_i(nqp::r_atpos_i($arr, nqp::unbox_i($pos.Int)))
136 },
137 STORE => method (int $v) {
138 nqp::r_bindpos_i($arr, nqp::unbox_i($pos.Int), $v);
139 self
140 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much ye…
authored Dec 17, 2011
141 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
142 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored Dec 18, 2011
143 Proxy.new:
144 FETCH => method () {
145 nqp::p6box_i(nqp::r_atpos_i($arr, $pos))
146 },
147 STORE => method (int $v) {
148 nqp::r_bindpos_i($arr, $pos, $v);
149 self
150 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much ye…
authored Dec 17, 2011
151 }
152 }
153 multi method PARAMETERIZE_TYPE(Int:U $t) {
154 self but IntTypedCArray[$t.WHAT]
155 }
156
157 my role NumTypedCArray[::TValue] does Positional[TValue] {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
158 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored Dec 18, 2011
159 Proxy.new:
160 FETCH => method () {
161 nqp::p6box_n(nqp::r_atpos_n($arr, nqp::unbox_i($pos.Int)))
162 },
163 STORE => method (num $v) {
164 nqp::r_bindpos_n($arr, nqp::unbox_i($pos.Int), $v);
165 self
166 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much ye…
authored Dec 17, 2011
167 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
168 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
9ae5ba5 @jnthn Support creating and passing arrays to C also.
authored Dec 18, 2011
169 Proxy.new:
170 FETCH => method () {
171 nqp::p6box_n(nqp::r_atpos_n($arr, $pos))
172 },
173 STORE => method (num $v) {
174 nqp::r_bindpos_n($arr, $pos, $v);
175 self
176 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much ye…
authored Dec 17, 2011
177 }
178 }
179 multi method PARAMETERIZE_TYPE(Num:U $t) {
180 self but NumTypedCArray[$t.WHAT]
181 }
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored Jan 13, 2012
182
183 my role TypedCArray[::TValue] does Positional[TValue] {
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
184 multi method at_pos(::?CLASS:D \$arr: $pos) is rw {
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored Jan 13, 2012
185 Proxy.new:
186 FETCH => method () {
187 nqp::r_atpos($arr, nqp::unbox_i($pos.Int))
188 },
189 STORE => method ($v) {
190 nqp::r_bindpos($arr, nqp::unbox_i($pos.Int), nqp::p6decont($v));
191 self
192 }
193 }
9d96c04 @moritz now that we have autoviv, our at_pos invocants need to be defined to …
moritz authored Apr 21, 2012
194 multi method at_pos(::?CLASS:D \$arr: int $pos) is rw {
731f841 @jnthn NativeCall library additions to prepare for arrays of strings.
authored Jan 13, 2012
195 Proxy.new:
196 FETCH => method () {
197 nqp::r_atpos($arr, $pos)
198 },
199 STORE => method ($v) {
200 nqp::r_bindpos($arr, $pos, nqp::p6decont($v));
201 self
202 }
203 }
204 }
205 multi method PARAMETERIZE_TYPE(Mu:U $t) {
206 die "A C array can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not $t.perl())"
207 unless $t === Str || $t.REPR eq 'CStruct' | 'CPointer' | 'CArray';
208 self but TypedCArray[$t.WHAT]
209 }
6803a5a @jnthn Put in first few bits of C array handling support. Doesn't do much ye…
authored Dec 17, 2011
210 }
211
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
212 # Specifies that the routine is actually a native call, and gives
213 # the name of the library to load it from.
214 multi trait_mod:<is>(Routine $r, $libname, :$native!) is export {
215 $r does Native[$r, $libname];
bd56459 @jnthn Add some initial experimental code that shows how we can call a Win32…
authored Mar 2, 2010
216 }
9766cd2 @jnthn If there's no returns type declared, we assume void return.
authored Apr 15, 2010
217
bcbdb85 @jnthn Overload of the native trait_mod that handles the 'call in current ex…
authored Jan 13, 2012
218 # Specifies that the routine is actually a native call, into the
219 # current executable (platform specific).
220 multi trait_mod:<is>(Routine $r, :$native!) is export {
221 $r does Native[$r, Str];
222 }
223
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
224 # Specifies the calling convention to use for a native call.
225 multi trait_mod:<is>(Routine $r, $name, :$nativeconv!) is export {
226 $r does NativeCallingConvention[$name];
227 }
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
228
229 # Ways to specify how to marshall strings.
230 multi trait_mod:<is>(Parameter $p, $name, :$encoded!) is export {
231 $p does NativeCallEncoded[$name];
232 }
233 multi trait_mod:<is>(Routine $p, $name, :$encoded!) is export {
234 $p does NativeCallEncoded[$name];
235 }
Something went wrong with that request. Please try again.