1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Preliminary support for loading c libraries.

This commit is contained in:
bakpakin 2018-01-13 23:38:58 -05:00
parent 5ec05136c7
commit 4197f918a0
6 changed files with 57 additions and 14 deletions

View File

@ -24,15 +24,16 @@
PREFIX?=/usr/local PREFIX?=/usr/local
BINDIR=$(PREFIX)/bin BINDIR=$(PREFIX)/bin
VERSION=\"0.0.0-beta\" VERSION=0.0.0-beta
CFLAGS=-std=c99 -Wall -Wextra -I./include -I./libs -g -DDST_VERSION=$(VERSION) CFLAGS=-std=c99 -Wall -Wextra -I./include -I./libs -g -DDST_VERSION=\"$(VERSION)\"
CLIBS=-lm CLIBS=-lm -ldl
PREFIX=/usr/local PREFIX=/usr/local
DST_TARGET=dst DST_TARGET=dst
DEBUGGER=lldb DEBUGGER=lldb
DST_INTERNAL_HEADERS=$(addprefix core/,symcache.h opcodes.h strtod.h compile.h gc.h sourcemap.h util.h) DST_INTERNAL_HEADERS=$(addprefix core/,symcache.h opcodes.h strtod.h compile.h gc.h sourcemap.h util.h)
DST_HEADERS=$(addprefix include/dst/,dst.h dstconfig.h dsttypes.h dststate.h dststl.h) DST_HEADERS=$(addprefix include/dst/,dst.h dstconfig.h dsttypes.h dststate.h dststl.h)
DST_C_LIBS=$(addprefix libs/,testlib.so)
############################# #############################
##### Generated headers ##### ##### Generated headers #####
@ -58,6 +59,14 @@ DST_CLIENT_SOURCES=$(addprefix client/,\
$(DST_TARGET): $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(DST_ALL_HEADERS) $(DST_TARGET): $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(DST_ALL_HEADERS)
$(CC) $(CFLAGS) -o $(DST_TARGET) $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(CLIBS) $(CC) $(CFLAGS) -o $(DST_TARGET) $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(CLIBS)
#######################
##### C Libraries #####
#######################
%.so: %.c $(DST_HEADERS)
$(CC) $(CFLAGS) -shared -undefined dynamic_lookup -o $@ $<
################### ###################
##### Testing ##### ##### Testing #####
################### ###################

View File

@ -23,6 +23,31 @@
#include <dst/dst.h> #include <dst/dst.h>
#include <dst/dststl.h> #include <dst/dststl.h>
#include <dlfcn.h>
int dst_stl_loadc(int32_t argn, Dst *argv, Dst *ret) {
void *lib;
DstCFunction init;
if (argn < 1) {
*ret = dst_cstringv("expected at least on argument");
return 1;
}
if (!dst_checktype(argv[0], DST_STRING)) {
*ret = dst_cstringv("expected string");
return 1;
}
lib = dlopen((const char *)dst_unwrap_string(argv[0]), RTLD_NOW);
if (NULL == lib) {
*ret = dst_cstringv(dlerror());
return 1;
}
init = dlsym(lib, "_dst_init");
if (NULL == init) {
*ret = dst_cstringv("could not find lib init function");
return 1;
}
return init(argn, argv, ret);
}
int dst_stl_parse(int32_t argn, Dst *argv, Dst *ret) { int dst_stl_parse(int32_t argn, Dst *argv, Dst *ret) {
const uint8_t *src; const uint8_t *src;
int32_t len; int32_t len;
@ -385,6 +410,7 @@ DST_DEFINE_COMPARATOR(notdescending, > 0)
DST_DEFINE_COMPARATOR(notascending, < 0) DST_DEFINE_COMPARATOR(notascending, < 0)
static DstReg stl[] = { static DstReg stl[] = {
{"load-native", dst_stl_loadc},
{"parse", dst_stl_parse}, {"parse", dst_stl_parse},
{"compile", dst_stl_compile}, {"compile", dst_stl_compile},
{"int", dst_int}, {"int", dst_int},

View File

@ -1,11 +0,0 @@
(print "Hello, World!")
(do (+ 1 2 3))
(print (+ 1 2 3))
# Comment
(var i 0)
(while (< i 1000)
(print i)
(varset! i (+ i 1)))

19
libs/testlib.c Normal file
View File

@ -0,0 +1,19 @@
#include <dst/dst.h>
static void additem(DstTable *t, const char *name, Dst val) {
DstTable *subt = dst_table(1);
dst_table_put(subt, dst_csymbolv("value"), val);
dst_table_put(t, dst_csymbolv(name), dst_wrap_table(subt));
}
int _dst_init(int32_t argn, Dst *argv, Dst *ret) {
DstTable *table;
if (argn >= 2 && dst_checktype(argv[1], DST_TABLE)) {
table = dst_unwrap_table(argv[1]);
} else {
table = dst_table(0);
}
additem(table, "pi", dst_wrap_real(M_PI));
*ret = dst_wrap_table(table);
return 0;
}