Skip to content

Instantly share code, notes, and snippets.

@araraloren
Last active February 28, 2017 16:37
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 araraloren/f9a647a4d95eab08f3d94a593df2fec9 to your computer and use it in GitHub Desktop.
Save araraloren/f9a647a4d95eab08f3d94a593df2fec9 to your computer and use it in GitHub Desktop.
NativeCall callback
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