/
Types.pm6
183 lines (167 loc) Β· 6.97 KB
/
Types.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use nqp;
unit module NativeCall::Types;
sub nativecast($target-type, $source) {
nqp::nativecallcast(nqp::decont($target-type),
nqp::decont(map_return_type($target-type)), nqp::decont($source));
}
our native long is Int is ctype("long") is repr("P6int") { };
our native longlong is Int is ctype("longlong") is repr("P6int") { };
our native ulong is Int is ctype("long") is unsigned is repr("P6int") { };
our native ulonglong is Int is ctype("longlong") is unsigned is repr("P6int") { };
our native size_t is Int is ctype("size_t") is unsigned is repr("P6int") { };
our native ssize_t is Int is ctype("size_t") is repr("P6int") { };
our native bool is Int is ctype("bool") is repr("P6int") { };
our class void is repr('Uninstantiable') { };
# Expose a Pointer class for working with raw pointers.
our class Pointer is repr('CPointer') {
method of() { void }
multi method new() {
self.CREATE()
}
multi method new(int $addr) {
nqp::box_i($addr, ::?CLASS)
}
multi method new(Int $addr) {
nqp::box_i(nqp::unbox_i(nqp::decont($addr)), ::?CLASS)
}
method Numeric(::?CLASS:D:) { self.Int }
method Int(::?CLASS:D:) {
nqp::p6box_i(nqp::unbox_i(nqp::decont(self)))
}
method deref(::?CLASS:D \ptr:) { nativecast(void, ptr) }
multi method gist(::?CLASS:U:) { '(' ~ self.^name ~ ')' }
multi method gist(::?CLASS:D:) {
if self.Int -> $addr {
self.^name ~ '<' ~ $addr.fmt('%#x') ~ '>'
}
else {
self.^name ~ '<NULL>'
}
}
multi method perl(::?CLASS:U:) { self.^name }
multi method perl(::?CLASS:D:) { self.^name ~ '.new(' ~ self.Int ~ ')' }
my role TypedPointer[::TValue] {
method of() { TValue }
method deref(::?CLASS:D \ptr:) { nativecast(TValue, ptr) }
}
method ^parameterize(Mu:U \p, Mu:U \t) {
die "A typed pointer can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not {t.^name})"
unless t ~~ Int|Num|Bool || t === Str|void || t.REPR eq any <CStruct CUnion CPPStruct CPointer CArray>;
my $w := p.^mixin: TypedPointer[t.WHAT];
$w.^set_name: "{p.^name}[{t.^name}]";
$w;
}
}
# CArray class, used to represent C arrays.
our class CArray is repr('CArray') is array_type(Pointer) {
method AT-POS(CArray:D: $pos) { die "CArray cannot be used without a type" }
my role IntTypedCArray[::TValue] does Positional[TValue] is array_type(TValue) {
multi method AT-POS(::?CLASS:D \arr: $pos) is raw {
nqp::atposref_i(nqp::decont(arr), $pos);
}
multi method AT-POS(::?CLASS:D \arr: int $pos) is raw {
nqp::atposref_i(nqp::decont(arr), $pos);
}
multi method ASSIGN-POS(::?CLASS:D \arr: int $pos, int $assignee) {
nqp::bindpos_i(nqp::decont(arr), $pos, $assignee);
}
multi method ASSIGN-POS(::?CLASS:D \arr: Int $pos, int $assignee) {
nqp::bindpos_i(nqp::decont(arr), nqp::unbox_i($pos), $assignee);
}
multi method ASSIGN-POS(::?CLASS:D \arr: Int $pos, Int $assignee) {
nqp::bindpos_i(nqp::decont(arr), nqp::unbox_i($pos), nqp::unbox_i($assignee));
}
multi method ASSIGN-POS(::?CLASS:D \arr: int $pos, Int $assignee) {
nqp::bindpos_i(nqp::decont(arr), $pos, nqp::unbox_i($assignee));
}
}
my role NumTypedCArray[::TValue] does Positional[TValue] is array_type(TValue) {
multi method AT-POS(::?CLASS:D \arr: $pos) is raw {
nqp::atposref_n(nqp::decont(arr), $pos);
}
multi method AT-POS(::?CLASS:D \arr: int $pos) is raw {
nqp::atposref_n(nqp::decont(arr), $pos);
}
multi method ASSIGN-POS(::?CLASS:D \arr: int $pos, num $assignee) {
nqp::bindpos_n(nqp::decont(arr), $pos, $assignee);
}
multi method ASSIGN-POS(::?CLASS:D \arr: Int $pos, num $assignee) {
nqp::bindpos_n(nqp::decont(arr), nqp::unbox_i($pos), $assignee);
}
multi method ASSIGN-POS(::?CLASS:D \arr: Int $pos, Num $assignee) {
nqp::bindpos_n(nqp::decont(arr), nqp::unbox_i($pos), nqp::unbox_n($assignee));
}
multi method ASSIGN-POS(::?CLASS:D \arr: int $pos, Num $assignee) {
nqp::bindpos_n(nqp::decont(arr), $pos, nqp::unbox_n($assignee));
}
}
my role TypedCArray[::TValue] does Positional[TValue] is array_type(TValue) {
multi method AT-POS(::?CLASS:D \arr: $pos) is rw {
Proxy.new:
FETCH => method () {
nqp::atpos(nqp::decont(arr), nqp::unbox_i($pos.Int))
},
STORE => method ($v) {
nqp::bindpos(nqp::decont(arr), nqp::unbox_i($pos.Int), nqp::decont($v));
self
}
}
multi method AT-POS(::?CLASS:D \arr: int $pos) is rw {
Proxy.new:
FETCH => method () {
nqp::atpos(nqp::decont(arr), $pos)
},
STORE => method ($v) {
nqp::bindpos(nqp::decont(arr), $pos, nqp::decont($v));
self
}
}
multi method ASSIGN-POS(::?CLASS:D \arr: int $pos, \assignee) {
nqp::bindpos(nqp::decont(arr), $pos, nqp::decont(assignee));
}
multi method ASSIGN-POS(::?CLASS:D \arr: Int $pos, \assignee) {
nqp::bindpos(nqp::decont(arr), nqp::unbox_i($pos), nqp::decont(assignee));
}
}
method ^parameterize(Mu:U \arr, Mu:U \t) {
my $mixin;
if t ~~ Int {
$mixin := IntTypedCArray[t.WHAT];
}
elsif t ~~ Num {
$mixin := NumTypedCArray[t.WHAT];
}
else {
die "A C array can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not {t.^name})"
unless t === Str || t.REPR eq 'CStruct' | 'CPPStruct' | 'CUnion' | 'CPointer' | 'CArray';
$mixin := TypedCArray[t];
}
my $what := arr.^mixin: $mixin;
$what.^set_name("{arr.^name}[{t.^name}]");
$what;
}
method elems { nqp::elems(self) }
method list {
do for ^self.elems { self.AT-POS($_) }
}
multi method new() { nqp::create(self) }
multi method new(*@values) { self.new(@values) }
multi method new(@values) {
nextsame unless @values;
my $result := self.new();
my int $n = @values.elems;
my int $i;
$result.ASSIGN-POS($n - 1, @values.AT-POS($n - 1));
while $i < $n {
$result.ASSIGN-POS($i, @values.AT-POS($i));
$i = $i + 1;
}
$result;
}
}
# duplicated code from NativeCall.pm to support Pointer.deref
multi sub map_return_type(Mu $type) { Mu }
multi sub map_return_type($type) {
nqp::istype($type, Int) ?? Int
!! nqp::istype($type, Num) ?? Num !! $type;
}