-
-
Save tony-o/5c8b950657869d5672dfc47a043b6a08 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> | |
struct TODD { | |
int x; | |
}; | |
struct TODD * parray(int n) { | |
int sz = sizeof(size_t); | |
struct TODD *ts = malloc(n*sz); | |
for(int i=0; i<n; i++){ | |
ts[i].x = i; | |
} | |
return ts; | |
} | |
# gcc -shared -ohelp.so help.c |
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
use NativeCall; | |
class TODD is repr('CStruct') { | |
has int32 $.x; | |
} | |
sub parray(int32) returns Pointer is native('./help.so') {*} | |
my $head = parray(10); | |
(0..^10).map(-> $i { | |
say nativecast( | |
TODD, | |
Pointer.new($head + ($i * nativesizeof(TODD))), | |
).x; | |
}); | |
# must be run in same dir as help.c or you must correct the path on line 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment