Skip to content

Instantly share code, notes, and snippets.

@skids
Created February 6, 2015 02:13
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 skids/b33fcda50e7dad774835 to your computer and use it in GitHub Desktop.
Save skids/b33fcda50e7dad774835 to your computer and use it in GitHub Desktop.
fun with NativeCall callbacks
# should be size_t
my sub ca8_mal(int) returns CArray[int8]
is native
is symbol('malloc') { * };
my sub ca8_free(CArray[int8])
is native
is symbol('free') { * };
# int should be size_t
my sub end_m(Instance, &callback (int --> CArray[int8]))
returns CArray[int8]
is native('libmhash')
is symbol('mhash_end_m') { * };
method finalize() {
my $sizeof; # try to capture the malloc size using the end_m cb
my $c := end_m(self, sub (int $s) { say "<<$s>>"; $sizeof = $s; ca8_mal($s) });
my $res := buf8.new($c[0..^$sizeof].list);
ca8_free($c);
$res := buf8.new($res.values.reverse)
if $swab_4byte_digests and $sizeof == 4;
$res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment