-
-
Save Skarsnik/31a1099ceb0ea595e173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#ifdef _WIN32 | |
#define DLLEXPORT __declspec(dllexport) | |
#else | |
#define DLLEXPORT extern | |
#endif | |
struct Point { | |
int x; | |
int y; | |
}; | |
DLLEXPORT void * ReturnSomePointer() | |
{ | |
return strdup("Got passed back the pointer I returned"); | |
} | |
DLLEXPORT int CompareSomePointer(void *ptr) | |
{ | |
int x = strcmp("Got passed back the pointer I returned", ptr) == 0; | |
free(ptr); | |
return x; | |
} | |
DLLEXPORT void * ReturnNullPointer() | |
{ | |
return NULL; | |
} | |
DLLEXPORT void * TakeTwoPointersToInt(int *ptr1, int *ptr2) | |
{ | |
return NULL; | |
} | |
DLLEXPORT void * TakeCArrayToInt8(int array[]) { | |
return NULL; | |
} | |
DLLEXPORT void NewPoint(struct Point *p) | |
{ | |
p = malloc(sizeof(struct Point)); | |
p->x = 42; | |
p->y = 42; | |
} | |
DLLEXPORT void NewPointPtr(struct Point **p) | |
{ | |
printf("NewPointPtr\n"); | |
*p = malloc(sizeof(struct Point)); | |
(*p)->x = 42; | |
(*p)->y = 42; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point is repr('CStruct') { | |
has int32 $.x; | |
has int32 $.y; | |
} | |
sub NewPointPtr(Pointer[Pointer[Point]] is rw) is native("./04-pointers") { * }; | |
my Pointer[Pointer[Point]] $ptr2 .= new; | |
say "Pass ptr to C"; | |
NewPointPtr($ptr2); | |
say "Hello"; | |
my Point $p = $ptr2.deref.deref; | |
ok $p.x == 42 and $p.y == 42; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment