Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Put in first few bits of C array handling support. Doesn't do much ye…

…t, though it just managed to successfully access a C array of doubles passed returned to it.
  • Loading branch information...
commit 6803a5aae45951f9294eecb39523adb15665f542 1 parent b0bedfa
@jnthn authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 lib/NativeCall.pm6
View
31 lib/NativeCall.pm6
@@ -63,6 +63,8 @@ sub type_code_for(Mu ::T) {
if T.REPR eq 'CStruct';
return 'cpointer'
if T.REPR eq 'CPointer';
+ return 'carray'
+ if T.REPR eq 'CArray';
die "Unknown type {T.^name} used in native call.\n" ~
"If you want to pass a struct, be sure to use the CStruct representation.\n" ~
"If you want to pass an array, be sure to use the CArray type.";
@@ -107,6 +109,35 @@ my role NativeCallEncoded[$name] {
# Expose an OpaquePointer class for working with raw pointers.
my class OpaquePointer is export is repr('CPointer') { }
+# CArray class, used to represent C arrays.
+my class CArray is export is repr('CArray') {
+ method at_pos($pos) { die "CArray cannot be used without a type" }
+
+ my role IntTypedCArray[::TValue] does Positional[TValue] {
+ multi method at_pos($pos) {
+ nqp::r_atpos_i(self, nqp::unbox_i($pos.Int))
+ }
+ multi method at_pos(int $pos) {
+ nqp::r_atpos_i(self, $pos)
+ }
+ }
+ multi method PARAMETERIZE_TYPE(Int:U $t) {
+ self but IntTypedCArray[$t.WHAT]
+ }
+
+ my role NumTypedCArray[::TValue] does Positional[TValue] {
+ multi method at_pos($pos) {
+ nqp::r_atpos_n(self, nqp::unbox_i($pos.Int))
+ }
+ multi method at_pos(int $pos) {
+ nqp::r_atpos_n(self, $pos)
+ }
+ }
+ multi method PARAMETERIZE_TYPE(Num:U $t) {
+ self but NumTypedCArray[$t.WHAT]
+ }
+}
+
# Specifies that the routine is actually a native call, and gives
# the name of the library to load it from.
multi trait_mod:<is>(Routine $r, $libname, :$native!) is export {
Please sign in to comment.
Something went wrong with that request. Please try again.