Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/a027fecd527c2871b2b6 to your computer and use it in GitHub Desktop.
Save FROGGS/a027fecd527c2871b2b6 to your computer and use it in GitHub Desktop.
diff --git a/src/6model/reprs/CPointer.c b/src/6model/reprs/CPointer.c
index 61ed697..9861753 100644
--- a/src/6model/reprs/CPointer.c
+++ b/src/6model/reprs/CPointer.c
@@ -28,6 +28,16 @@ static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *d
dest_body->ptr = src_body->ptr;
}
+static void set_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 value) {
+ MVMCPointerBody *body = (MVMCPointerBody *)OBJECT_BODY(root);
+ body->ptr = (void *)value;
+}
+
+static MVMint64 get_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
+ MVMCPointerBody *body = (MVMCPointerBody *)OBJECT_BODY(root);
+ return (MVMint64)body->ptr;
+}
+
/* Gets the storage specification for this representation. */
static MVMStorageSpec get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
MVMStorageSpec spec;
@@ -54,7 +64,15 @@ static const MVMREPROps this_repr = {
NULL, /* initialize */
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
- MVM_REPR_DEFAULT_BOX_FUNCS,
+ {
+ set_int,
+ get_int,
+ MVM_REPR_DEFAULT_SET_NUM,
+ MVM_REPR_DEFAULT_GET_NUM,
+ MVM_REPR_DEFAULT_SET_STR,
+ MVM_REPR_DEFAULT_GET_STR,
+ MVM_REPR_DEFAULT_GET_BOXED_REF
+ }, /* box_funcs */
MVM_REPR_DEFAULT_POS_FUNCS,
MVM_REPR_DEFAULT_ASS_FUNCS,
MVM_REPR_DEFAULT_ELEMS,
diff --git a/lib/NativeCall.pm6 b/lib/NativeCall.pm6
index 00ea334..db0cc94 100644
--- a/lib/NativeCall.pm6
+++ b/lib/NativeCall.pm6
@@ -148,7 +148,32 @@ my role NativeCallEncoded[$name] {
}
# Expose an OpaquePointer class for working with raw pointers.
-my class OpaquePointer is export(:types, :DEFAULT) is repr('CPointer') { }
+my class OpaquePointer is export(:types, :DEFAULT) is repr('CPointer') {
+ multi method new() {
+ self.CREATE()
+ }
+ multi method new(int $addr) {
+ nqp::box_i($addr, OpaquePointer)
+ }
+ multi method new(Int $addr) {
+ nqp::box_i(nqp::unbox_i(nqp::decont($addr)), OpaquePointer)
+ }
+ method Int(OpaquePointer:D:) {
+ nqp::p6box_i(nqp::unbox_i(nqp::decont(self)))
+ }
+ method Numeric(OpaquePointer:D:) { self.Int }
+ multi method gist(OpaquePointer:U:) { '(OpaquePointer)' }
+ multi method gist(OpaquePointer:D:) {
+ if self.Int -> $addr {
+ 'OpaquePointer<' ~ $addr.fmt('%#x') ~ '>'
+ }
+ else {
+ 'OpaquePointer<NULL>'
+ }
+ }
+ multi method perl(OpaquePointer:U:) { 'OpaquePointer' }
+ multi method perl(OpaquePointer:D:) { 'OpaquePointer.new(' ~ self.Int ~ ')' }
+}
# CArray class, used to represent C arrays.
my class CArray is export(:types, :DEFAULT) is repr('CArray') is array_type(OpaquePointer) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment