/usr/lib/perl6/sources/51E302443A2C8FF185ABC10CA1E5520EFEE885A1 is in rakudo 2018.03-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 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)))
}
proto method Bool() {*}
multi method Bool(::?CLASS:U: --> False) { }
multi method Bool(::?CLASS:D:) { so self.Int }
method deref(::?CLASS:D \ptr:) { self ?? nativecast(void, ptr) !! fail("Can't dereference a Null Pointer") }
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:) { self ?? nativecast(TValue, ptr) !! fail("Can't dereference a Null Pointer"); }
method add(Int $off) returns Pointer {
die "Can't do arithmetic with a void pointer"
if TValue.isa(void);
nqp::box_i(self.Int + nqp::nativecallsizeof(TValue) * $off, self.WHAT);
}
method succ {
self.add(1);
}
method pred {
self.add(-1);
}
method AT-POS(Int $pos) {
nqp::nativecallcast(
TValue,
nqp::istype(TValue, Int) ?? Int
!! nqp::istype(TValue, Num) ?? Num !! TValue,
nqp::box_i(nqp::unbox_i(nqp::decont(self)) + nqp::nativecallsizeof(TValue) * $pos, Pointer)
)
}
}
method ^parameterize(Mu:U \p, Mu:U \t) {
die "A typed pointer can only hold:\n" ~
" (u)int8, (u)int16, (u)int32, (u)int64, (u)long, (u)longlong, num16, num32, (s)size_t, bool, Str\n" ~
" and types with representation: CArray, CPointer, CStruct, CPPStruct and CUnion" ~
"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:\n" ~
" (u)int8, (u)int16, (u)int32, (u)int64, (u)long, (u)longlong, num16, num32, (s)size_t, bool, Str\n" ~
" and types with representation: CArray, CPointer, CStruct, CPPStruct and CUnion\n" ~
"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) {
if @values.elems -> $n {
my int $elems = $n - 1;
my $result := nqp::create(self); # XXX setelems would be nice
$result.ASSIGN-POS($elems,@values.AT-POS($elems)); # fake setelems
my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
$result.ASSIGN-POS($i,@values.AT-POS($i)),
);
$result
}
else {
nqp::create(self)
}
}
}
# 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;
}
|