Last active
February 28, 2017 16:37
-
-
Save araraloren/f9a647a4d95eab08f3d94a593df2fec9 to your computer and use it in GitHub Desktop.
NativeCall callback
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
c code, compiler to libgetint.so : | |
#include <stdio.h> | |
int getint() | |
{ | |
return 42; | |
} | |
int setint(int* ptr, int(*get)()) | |
{ | |
*ptr = get(); | |
return *ptr; | |
} | |
perl6 code - Rakudo version 2017.02 built on MoarVM version 2017.02: | |
#!/usr/bin/env perl6 | |
use v6; | |
use NativeCall; | |
sub getint() returns int32 is native('./getint') { * } | |
sub setint(int32 is rw, &cb (--> int32)) returns int32 is native('./getint') { * } | |
my int32 $i = 89; | |
sub x() { | |
return 42; | |
} | |
say setint($i, &getint); # rakudo complain - Cannot invoke object with invocation handler in this context | |
say setint($i, &x); # ok - 42 | |
# ? Why use getint as callback throw a exception ? Is my code incorrect ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment