Skip to content

Instantly share code, notes, and snippets.

@saikyun
Created November 22, 2021 14:02
Show Gist options
  • Save saikyun/5c968070e69aa0b6fb9451058dcb446f to your computer and use it in GitHub Desktop.
Save saikyun/5c968070e69aa0b6fb9451058dcb446f to your computer and use it in GitHub Desktop.
(defn import-c*
[module-name src]
(def so-name (string module-name "-" (hash src) ".so"))
(unless (os/stat so-name)
(spit (string module-name ".c") src)
(os/execute ["cc"
"-I" "/usr/local/include/janet"
"-shared"
"-o" so-name
(string module-name ".c")] :p))
(import* (string "./" module-name "-" (hash src)) :as module-name))
(def c-src
(string
``
#include <janet.h>
static Janet print_hello(int32_t argc, Janet *argv) {
printf("hello\n");
return janet_wrap_nil();
}
static const JanetReg cfuns[] = {
{"print-hello", print_hello, ""},
{NULL, NULL, NULL}
};
JANET_MODULE_ENTRY(JanetTable *env) {
janet_cfuns (env, "hello", cfuns);
}
``))
(import-c* "hello" c-src)
(hello/print-hello)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment