tutorial
Embed Boomkat in a C program
Build the static library, register a C callback on the global object, evaluate JavaScript, and clean up.
1. Build the static library
From the repository root, build boomkat.a:
just build-lib
2. Write the host program
Create host.c. This program creates a context, registers a print function, runs a line of JavaScript, and destroys the context:
#include <boomkat.h>
#include <stdio.h>
static bk_value host_print(bk_ctx js, bk_value self, unsigned argc,
const bk_value *argv, void *out) {
(void)self;
fprintf(out, "%s\n", argc ? bk_cstr(js, argv[0], NULL) : "");
return BK_UNDEFINED;
}
int main(void) {
bk_ctx js = bk_create();
bk_register_fn(js, 0, "print", host_print, 1, 0, stdout);
int failed = bk_exec(js, "print('Hello, world from Boomkat!')");
if (failed) {
fprintf(stderr, "%s\n", bk_error(js));
}
bk_destroy(js);
return failed;
}
3. Compile and run
Compile against include/ and link the static library (or run the bundled example with just example-c-hello):
cc host.c -Iinclude out/boomkat.a -lm -lpthread -o host
./host
Expected output:
Hello, world from Boomkat!