Created
November 22, 2021 14:02
-
-
Save saikyun/5c968070e69aa0b6fb9451058dcb446f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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