mirror of
https://github.com/janet-lang/janet
synced 2024-11-24 17:27:18 +00:00
Preliminary support for loading c libraries.
This commit is contained in:
parent
5ec05136c7
commit
4197f918a0
15
Makefile
15
Makefile
@ -24,15 +24,16 @@
|
||||
|
||||
PREFIX?=/usr/local
|
||||
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)
|
||||
CLIBS=-lm
|
||||
CFLAGS=-std=c99 -Wall -Wextra -I./include -I./libs -g -DDST_VERSION=\"$(VERSION)\"
|
||||
CLIBS=-lm -ldl
|
||||
PREFIX=/usr/local
|
||||
DST_TARGET=dst
|
||||
DEBUGGER=lldb
|
||||
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_C_LIBS=$(addprefix libs/,testlib.so)
|
||||
|
||||
#############################
|
||||
##### Generated headers #####
|
||||
@ -58,6 +59,14 @@ DST_CLIENT_SOURCES=$(addprefix client/,\
|
||||
$(DST_TARGET): $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(DST_ALL_HEADERS)
|
||||
$(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 #####
|
||||
###################
|
||||
|
26
core/stl.c
26
core/stl.c
@ -23,6 +23,31 @@
|
||||
#include <dst/dst.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) {
|
||||
const uint8_t *src;
|
||||
int32_t len;
|
||||
@ -385,6 +410,7 @@ DST_DEFINE_COMPARATOR(notdescending, > 0)
|
||||
DST_DEFINE_COMPARATOR(notascending, < 0)
|
||||
|
||||
static DstReg stl[] = {
|
||||
{"load-native", dst_stl_loadc},
|
||||
{"parse", dst_stl_parse},
|
||||
{"compile", dst_stl_compile},
|
||||
{"int", dst_int},
|
||||
|
11
libs/stl.dst
11
libs/stl.dst
@ -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
19
libs/testlib.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user