Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 85 lines (75 sloc) 2.794 kb
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
1 module NativeCall;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored
2
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
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 i…
authored
6
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
7 # Builds a hash of type information for the specified parameter.
8 sub param_hash_for(Parameter $p) {
9 my Mu $result := nqp::hash();
10 nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($p.type)));
11 $result
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored
12 }
a32dace @jnthn More types, plus CPointer that translates to some abstract pointer-y …
authored
13
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
14 # Builds a hash of type information for the specified return type.
15 sub return_hash_for(Mu ::T) {
16 my Mu $result := nqp::hash();
17 nqp::bindkey($result, 'type',
18 T =:= Mu ?? 'void' !! nqp::unbox_s(type_code_for(T)));
19 $result
8525e82 @jnthn Use the declared return type of the routine in signature generation.
authored
20 }
21
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
22 # Gets the NCI type code to use based on a given Perl 6 type.
23 my %type_map =
24 'int8' => 'char',
25 'int16' => 'short',
26 'int32' => 'int',
27 'int' => 'long',
28 'Int' => 'longlong',
29 'num32' => 'float',
30 'num64' => 'double',
31 'num' => 'double',
32 'Num' => 'double',
33 'Str' => 'utf8str';
34 sub type_code_for(Mu ::T) {
35 return %type_map{T.^name}
36 if %type_map.exists(T.^name);
e293ff0 @jnthn Add CStruct passing support. Improve error message when trying to pas…
authored
37 return 'cstruct'
38 if T.REPR eq 'CStruct';
39 die "Unknown type {T.^name} used in native call.\n" ~
40 "If you want to pass a struct, be sure to use the CStruct representation.\n" ~
41 "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
42 }
43
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
44 # This role is mixed in to any routine that is marked as being a
45 # native call.
46 my role Native[Routine $r, Str $libname] {
47 has int $!setup;
48 has native_callsite $!call is box_target;
49
50 method postcircumfix:<( )>($args) {
51 unless $!setup {
52 my Mu $arg_info := nqp::list();
53 for $r.signature.params -> $p {
54 nqp::push($arg_info, param_hash_for($p))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored
55 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
56 my str $conv = self.?native_call_convention || '';
57 nqp::buildnativecall(self,
58 nqp::unbox_s($libname), # library name
59 nqp::unbox_s($r.name), # symbol to call
60 nqp::unbox_s($conv), # calling convention
61 $arg_info,
62 return_hash_for($r.returns));
63 $!setup = 1;
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored
64 }
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
65 nqp::nativecall(nqp::p6decont($r.returns), self,
66 nqp::getattr(nqp::p6decont($args), Capture, '$!list'))
8e36c74 @jnthn CPointer becomes OpaquePointer for better naming win; start to stub i…
authored
67 }
68 }
69
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
70 # Role for carrying extra calling convention information.
71 my role NativeCallingConvention[$name] {
72 method native_call_convention() { $name };
73 }
d907f71 @mberends [lib/NativeCall.pm6] add patch provided by pmichaud++
mberends authored
74
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
75 # Specifies that the routine is actually a native call, and gives
76 # the name of the library to load it from.
77 multi trait_mod:<is>(Routine $r, $libname, :$native!) is export {
78 $r does Native[$r, $libname];
bd56459 @jnthn Add some initial experimental code that shows how we can call a Win32…
authored
79 }
9766cd2 @jnthn If there's no returns type declared, we assume void return.
authored
80
898c645 @jnthn Add first cut of version 2 of Zavolaj. Doesn't do any pointer/array s…
authored
81 # Specifies the calling convention to use for a native call.
82 multi trait_mod:<is>(Routine $r, $name, :$nativeconv!) is export {
83 $r does NativeCallingConvention[$name];
84 }
Something went wrong with that request. Please try again.