Skip to content
Newer
Older
100644 123 lines (110 sloc) 3.97 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';
64 die "Unknown type {T.^name} used in native call.\n" ~
65 "If you want to pass a struct, be sure to use the CStruct representation.\n" ~
66 "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
67 }
68
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
69 # This role is mixed in to any routine that is marked as being a
70 # native call.
71 my role Native[Routine $r, Str $libname] {
72 has int $!setup;
73 has native_callsite $!call is box_target;
74
75 method postcircumfix:<( )>($args) {
76 unless $!setup {
77 my Mu $arg_info := nqp::list();
78 for $r.signature.params -> $p {
79 nqp::push($arg_info, param_hash_for($p))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
80 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
81 my str $conv = self.?native_call_convention || '';
82 nqp::buildnativecall(self,
83 nqp::unbox_s($libname), # library name
84 nqp::unbox_s($r.name), # symbol to call
85 nqp::unbox_s($conv), # calling convention
86 $arg_info,
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
87 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
88 $!setup = 1;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
89 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
90 nqp::nativecall(nqp::p6decont($r.returns), self,
91 nqp::getattr(nqp::p6decont($args), Capture, '$!list'))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored Mar 3, 2010
92 }
93 }
94
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
95 # Role for carrying extra calling convention information.
96 my role NativeCallingConvention[$name] {
97 method native_call_convention() { $name };
98 }
d907f71 @mberends [lib/NativeCall.pm6] add patch provided by pmichaud++
mberends authored Jun 2, 2011
99
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
100 # Role for carrying extra string encoding information.
101 my role NativeCallEncoded[$name] {
102 method native_call_encoded() { $name };
103 }
104
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
105 # Specifies that the routine is actually a native call, and gives
106 # the name of the library to load it from.
107 multi trait_mod:<is>(Routine $r, $libname, :$native!) is export {
108 $r does Native[$r, $libname];
bd56459 @jnthn Add some initial experimental code that shows how we can call a Win32…
authored Mar 2, 2010
109 }
9766cd2 @jnthn If there's no returns type declared, we assume void return.
authored Apr 15, 2010
110
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored Nov 24, 2011
111 # Specifies the calling convention to use for a native call.
112 multi trait_mod:<is>(Routine $r, $name, :$nativeconv!) is export {
113 $r does NativeCallingConvention[$name];
114 }
d22dd75 @jnthn Better string support, including multiple encodings.
authored Nov 27, 2011
115
116 # Ways to specify how to marshall strings.
117 multi trait_mod:<is>(Parameter $p, $name, :$encoded!) is export {
118 $p does NativeCallEncoded[$name];
119 }
120 multi trait_mod:<is>(Routine $p, $name, :$encoded!) is export {
121 $p does NativeCallEncoded[$name];
122 }
Something went wrong with that request. Please try again.