Skip to content

Instantly share code, notes, and snippets.

@Papierkorb

Papierkorb/foo.c Secret

Last active August 27, 2017 23:19
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 Papierkorb/55c32f9ad3aa61e20eeec6e045b9b582 to your computer and use it in GitHub Desktop.
Save Papierkorb/55c32f9ad3aa61e20eeec6e045b9b582 to your computer and use it in GitHub Desktop.
#include <stdio.h>
struct CrystalString {
char *ptr;
int size;
};
void dump(struct CrystalString str) {
printf("%iB @ %p\n", str.size, str.ptr);
}
void bla(void *eins, struct CrystalString zwei, struct CrystalString drei, struct CrystalString vier) {
printf("eins: %p\n", eins); // This is fine
printf("zwei: "); dump(zwei); // Same
printf("drei: "); dump(drei); // Great too
printf("vier: "); dump(vier); // What's going on?!
}
@[Link(ldflags: "#{__DIR__}/foo.c")] # <- Don't try this at home
lib Bind
struct CrystalString
ptr : LibC::Char*
size : LibC::Int
end
fun bla(eins : Void*, zwei : CrystalString, drei : CrystalString, vier : CrystalString) : Void
end
def wrap(str)
Bind::CrystalString.new(ptr: str.to_unsafe, size: str.size)
end
Bind.bla(nil, wrap("Zwei"), wrap("Drei"), wrap("Vier"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment