Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement typed Pointer type
  • Loading branch information
FROGGS committed Feb 23, 2015
1 parent b640002 commit 875f50b
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/NativeCall.pm
Expand Up @@ -65,7 +65,27 @@ sub return_hash_for(Signature $s, &r?, :$with-typeobj) {
$result
}

my native long is repr("P6int") is Int is ctype("long") is export(:types, :DEFAULT) { };
my native long is Int is ctype("long") is repr("P6int") is export(:types, :DEFAULT) { };
#~ my native longlong is Int is ctype("longlong") is repr("P6int") is export(:types, :DEFAULT) { };
#~ my native longdouble is Int is ctype("longdouble") is repr("P6num") is export(:types, :DEFAULT) { };
my class void is repr('CPointer') is export(:types, :DEFAULT) { };
my class Pointer is repr('CPointer') is export(:types, :DEFAULT) { };

# need to introduce the roles in there in an augment, because you can't
# inherit from types that haven't been properly composed.
use MONKEY_TYPING;
augment class Pointer {
my role TypedPointer[::TValue = void] is Pointer is repr('CPointer') {
method of() { ::TValue }
method deref(::?CLASS:D \ptr:) { nativecast(::TValue, ptr) }
}
multi method PARAMETERIZE_TYPE(Mu:U \t) {
die "A types pointer can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not {t.^name})"
unless t ~~ Int || t ~~ Num || t === Str || t.REPR eq 'CStruct' | 'CUnion' | 'CPPStruct' | 'CPointer' | 'CArray';
my \typed := TypedPointer[t];
typed.HOW.make_pun(typed);
}
}

# Gets the NCI type code to use based on a given Perl 6 type.
my %type_map =
Expand Down Expand Up @@ -96,8 +116,8 @@ sub type_code_for(Mu ::T) {
# the REPR of a Buf or Blob type object is Uninstantiable, so
# needs an extra special case here that isn't covered in the
# hash lookup above.
return 'vmarray'
if T ~~ Blob;
return 'vmarray' if T ~~ Blob;
return 'cpointer' if T ~~ Pointer;
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.";
Expand Down

0 comments on commit 875f50b

Please sign in to comment.