Skip to content

Instantly share code, notes, and snippets.

@salortiz
Created March 12, 2019 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salortiz/4cf07738fe247ec89d824731f825b7de to your computer and use it in GitHub Desktop.
Save salortiz/4cf07738fe247ec89d824731f825b7de to your computer and use it in GitHub Desktop.
use v6.c;
use NativeCall;
# A proof of concept for NativeCall "varargs" calls,
my $printf = cglobal(Str,'printf',Pointer);
my @sxa := (
(Nil => :(Str --> int32)).Map, # arity 0
(Str => :(Str,Str --> int32), Int => :(Str,Int --> int32)).Map # arity 1
);
sub choose-sig(Capture \c) {
@sxa[c-1]{c.list[1].^name};
}
sub make-callable(Pointer \sym, Capture \c) {
my \sig = choose-sig(c);
nativecast(sig, sym);
}
for (\("hi\n"), \("Hello %s\n","perl6"), \("Age: %d\n", 10)) -> \c {
my &fun = make-callable($printf, c);
fun(|c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment