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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
use nqp;
unit module NativeCall::Types;
#- lookups ---------------------------------------------------------------------
# Quick lookup of REPRs allowed for parameterization
my constant $ok-REPRs = nqp::hash(
'CArray', 1, 'CPointer', 1, 'CPPStruct', 1, 'CStruct', 1, 'CUnion', 1
);
#- helper subs -----------------------------------------------------------------
# Helper sub for type mapping, also used in NativeCall
my proto sub map_return_type(|) is export {*}
my multi sub map_return_type(Mu $type) { $type }
my multi sub map_return_type(Int ) { Int }
my multi sub map_return_type(Num ) { Num }
# This version of nativecast only used here
my sub nativecast($target-type, $source) {
nqp::nativecallcast(
nqp::decont($target-type),
nqp::decont(map_return_type($target-type)),
nqp::decont($source)
)
}
# Handle parameterization issues
my sub wrong-type(str $what, \whatnot) is hidden-from-backtrace {
my str $name = whatnot.^name;
die qq:to/MESSAGE/.chomp.naive-word-wrapper;
A $what can only hold: (u)int8, (u)int16, (u)int32, (u)int64, (u)long,
(u)longlong, num16, num32, (s)size_t, bool, Str and types with representation:
CArray, CPointer, CStruct, CPPStruct and CUnion, not: $name
MESSAGE
}
#- representations -------------------------------------------------------------
our role ExplicitlyManagedString { has $.cstr is rw }
our class void is repr<Uninstantiable> { }
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> { }
#- Pointer----------------------------------------------------------------------
# Expose a Pointer class for working with raw pointers.
our class Pointer is repr<CPointer> {
method of() { void }
proto method new(|) {*}
multi method new() { nqp::create(self) }
multi method new(int $addr) { nqp::box_i($addr, ::?CLASS)
}
multi method new(Int:D $addr) {
nqp::box_i(nqp::unbox_i(nqp::decont($addr)), ::?CLASS)
}
proto method Numeric(|) {*}
multi method Numeric(::?CLASS:U: --> 0) { }
multi method Numeric(::?CLASS:D:) { nqp::p6box_i(nqp::unbox_i(self)) }
proto method Int(|) {*}
multi method Int(::?CLASS:U: --> 0) { }
multi method Int(::?CLASS:D:) { nqp::p6box_i(nqp::unbox_i(self)) }
proto method Bool(|) {*}
multi method Bool(::?CLASS:U: --> False) { }
multi method Bool(::?CLASS:D:) { nqp::hllbool(nqp::unbox_i(self)) }
method deref(::?CLASS:D \ptr:) {
self
?? nativecast(void, ptr)
!! "Can't dereference a Null Pointer".Failure
}
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 raku(::?CLASS:U:) { self.^name }
multi method raku(::?CLASS:D:) { self.^name ~ '.new(' ~ self.Int ~ ')' }
my role TypedPointer[::TValue] {
method of() { TValue }
method deref(::?CLASS:D \ptr:) {
self
?? nativecast(TValue, ptr)
!! "Can't dereference a Null Pointer".Failure
}
method add(::?CLASS:D: Int:D $off --> Pointer:D) {
TValue.isa(void)
?? die("Can't do arithmetic with a void pointer")
!! nqp::box_i(
self.Int + nqp::nativecallsizeof(TValue) * $off,
self.WHAT
)
}
method succ(::?CLASS:D:) { self.add( 1) }
method pred(::?CLASS:D:) { self.add(-1) }
method AT-POS(::?CLASS:D: Int:D $pos) {
nqp::nativecallcast(
TValue,
map_return_type(TValue),
nqp::box_i(
nqp::unbox_i(self) + nqp::nativecallsizeof(TValue) * $pos,
Pointer
)
)
}
}
method ^parameterize(Mu:U \p, Mu:U \t) {
if nqp::istype(t, Int)
|| nqp::istype(t, Num)
|| nqp::istype(t, Bool)
|| t === Str
|| t === void
|| nqp::existskey($ok-REPRs, t.REPR) {
my $w := p.^mixin: TypedPointer[t.WHAT];
$w.^set_name(p.^name ~ '[' ~ t.^name ~ ']');
$w
}
else {
wrong-type('typed pointer', t);
}
}
}
#- CArray ----------------------------------------------------------------------
# CArray class, used to represent C arrays.
our class CArray is repr('CArray') is array_type(Pointer) {
method AT-POS(::?CLASS:D: $pos) {
die "CArray cannot be used without a type"
}
# For parameterization to ints
my role IntTypedCArray[::TValue]
does Positional[TValue]
is array_type(TValue)
{
multi method AT-POS(::?CLASS:D: int $pos) is raw {
nqp::atposref_i(self, $pos)
}
multi method AT-POS(::?CLASS:D: Int:D $pos) is raw {
nqp::atposref_i(self, nqp::unbox_i($pos))
}
multi method AT-POS(::?CLASS:D: Any:D $pos) is raw {
nqp::atposref_i(self, nqp::unbox_i($pos.Int))
}
multi method ASSIGN-POS(::?CLASS:D: int $pos, int $value) {
nqp::bindpos_i(self, $pos, $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, int $value) {
nqp::bindpos_i(self, nqp::unbox_i($pos), $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, Int:D $value) {
nqp::bindpos_i(self, nqp::unbox_i($pos), nqp::unbox_i($value))
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, Any:D $value) {
nqp::bindpos_i(self, nqp::unbox_i($pos), nqp::unbox_i($value.Int))
}
multi method ASSIGN-POS(::?CLASS:D: Any:D $pos, Any:D $value) {
nqp::bindpos_i(self,nqp::unbox_i($pos.Int),nqp::unbox_i($value.Int))
}
method !allocate(::?CLASS:U: int $elems) {
my $array := nqp::create(self);
nqp::bindpos_i($array, $elems - 1, 0);
$array
}
multi method allocate(::?CLASS:U: int $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Int:D $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Any:D $elems) {
self!allocate($elems.Int)
}
}
# For parameterization to unsigned ints
my role UIntTypedCArray[::TValue]
does Positional[TValue]
is array_type(TValue)
{
multi method AT-POS(::?CLASS:D: int $pos) is raw {
nqp::atposref_u(self, $pos);
}
multi method AT-POS(::?CLASS:D: Int:D $pos) is raw {
nqp::atposref_u(self, nqp::unbox_i($pos));
}
multi method AT-POS(::?CLASS:D: Any:D $pos) is raw {
nqp::atposref_u(self, nqp::unbox_i($pos.Int));
}
multi method ASSIGN-POS(::?CLASS:D: int $pos, int $value) {
nqp::bindpos_u(self, $pos, $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, int $value) {
nqp::bindpos_u(self, nqp::unbox_i($pos), $value)
}
multi method ASSIGN-POS(::?CLASS:D: int $pos, uint $value) {
nqp::bindpos_u(self, $pos, $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, uint $value) {
nqp::bindpos_u(self, nqp::unbox_i($pos), $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, Int:D $value) {
nqp::bindpos_u(self, nqp::unbox_i($pos), nqp::unbox_u($value))
}
multi method ASSIGN-POS(::?CLASS:D: Any:D $pos, Int:D $value) {
nqp::bindpos_u(self, nqp::unbox_i($pos.Int), nqp::unbox_u($value))
}
multi method ASSIGN-POS(::?CLASS:D: Any:D $pos, Any:D $value) {
nqp::bindpos_u(self,nqp::unbox_i($pos.Int),nqp::unbox_u($value.Int))
}
method !allocate(::?CLASS:U: int $elems) {
my $array := nqp::create(self);
nqp::bindpos_u($array, $elems - 1, 0);
$array
}
multi method allocate(::?CLASS:U: int $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Int:D $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Any:D $elems) {
self!allocate($elems.Int)
}
}
# For parameterization to nums
my role NumTypedCArray[::TValue]
does Positional[TValue]
is array_type(TValue)
{
multi method AT-POS(::?CLASS:D: int $pos) is raw {
nqp::atposref_n(self, $pos);
}
multi method AT-POS(::?CLASS:D: Int:D $pos) is raw {
nqp::atposref_n(self, nqp::unbox_i($pos))
}
multi method AT-POS(::?CLASS:D: Any:D $pos) is raw {
nqp::atposref_n(self, nqp::unbox_i($pos.Int))
}
multi method ASSIGN-POS(::?CLASS:D: int $pos, num $value) {
nqp::bindpos_n(self, $pos, $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, num $value) {
nqp::bindpos_n(self, nqp::unbox_i($pos), $value)
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, Num:D $value) {
nqp::bindpos_n(self, nqp::unbox_i($pos), nqp::unbox_n($value))
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, Any:D $value) {
nqp::bindpos_n(self, nqp::unbox_i($pos), nqp::unbox_n($value.Num))
}
method !allocate(::?CLASS:U: int $elems) {
my $array := nqp::create(self);
nqp::bindpos_n($array, $elems - 1, 0e0);
$array
}
multi method allocate(::?CLASS:U: int $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Int:D $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Any:D $elems) {
self!allocate($elems.Int)
}
}
# For parameterization to all other allowed REPRs
my role TypedCArray[::TValue]
does Positional[TValue]
is array_type(TValue)
{
multi method AT-POS(::?CLASS:D: int $pos) is rw {
Proxy.new:
FETCH => -> $ {
nqp::atpos(self, $pos)
},
STORE => -> $invocant, $value {
nqp::bindpos(self, $pos, nqp::decont($value));
$invocant
}
}
multi method AT-POS(::?CLASS:D: Int:D $pos) is rw {
Proxy.new:
FETCH => -> $ {
nqp::atpos(self, nqp::unbox_i($pos))
},
STORE => -> $invocant, $value {
nqp::bindpos(self, nqp::unbox_i($pos), nqp::decont($value));
$invocant
}
}
multi method AT-POS(::?CLASS:D: Any:D $pos) is rw {
Proxy.new:
FETCH => -> $ {
nqp::atpos(self, nqp::unbox_i($pos.Int))
},
STORE => -> $self, $value {
nqp::bindpos(self,nqp::unbox_i($pos.Int),nqp::decont($value));
$self
}
}
multi method ASSIGN-POS(::?CLASS:D: int $pos, \value) {
nqp::bindpos(self, $pos, nqp::decont(value))
}
multi method ASSIGN-POS(::?CLASS:D: Int:D $pos, \value) {
nqp::bindpos(self, nqp::unbox_i($pos), nqp::decont(value))
}
multi method ASSIGN-POS(::?CLASS:D: Any:D $pos, \value) {
nqp::bindpos(self, nqp::unbox_i($pos.Int), nqp::decont(value))
}
method !allocate(int $elems) {
my $array := nqp::create(self);
my $type := ::?CLASS.^array_type;
my int $i;
nqp::while(
$i < $elems,
nqp::bindpos($array, $i++, nqp::create($type))
);
$array
}
multi method allocate(::?CLASS:U: int $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Int:D $elems) {
self!allocate($elems)
}
multi method allocate(::?CLASS:U: Any:D $elems) {
self!allocate($elems.Int)
}
}
method ^parameterize(Mu:U \array, Mu:U \t) {
my $WHAT := t.WHAT;
my $mixin := nqp::istype(t, Int)
?? t.^unsigned
?? UIntTypedCArray[$WHAT]
!! IntTypedCArray[$WHAT]
!! nqp::istype(t, Num)
?? NumTypedCArray[$WHAT]
!! (t === Str || nqp::existskey($ok-REPRs, t.REPR))
?? TypedCArray[$WHAT]
!! wrong-type('C array', t);
my $what := array.^mixin: $mixin;
$what.^set_name(array.^name ~ '[' ~ t.^name ~ ']');
$what
}
method Str(::?CLASS:D:) { self.join(' ') }
method elems(::?CLASS:D:) { nqp::elems(self) }
method list(::?CLASS:D:) {
my int $m = nqp::elems(self);
my $buffer := nqp::setelems( # presize buffer
nqp::setelems(nqp::create(IterationBuffer), $m),
0
);
my int $i;
nqp::while(
$i < $m,
nqp::push($buffer, self.AT-POS($i++))
);
$buffer.List
}
multi method new(::?CLASS:) { nqp::create(self) }
multi method new(::?CLASS: *@values) { self.new(@values) }
multi method new(::?CLASS: @values) {
my $result := nqp::create(self);
my int $n = @values.elems; # reifies
# By counting down, we're effectively doing a setelems for the
# right size on the first iteration
nqp::if(
$n,
nqp::while(
--$n >= 0,
$result.ASSIGN-POS($n, @values.AT-POS($n))
)
);
$result
}
}
# vim: expandtab shiftwidth=4
|