1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-12 08:30:26 +00:00

Update require to handle natives better.

This commit is contained in:
Calvin Rose 2018-07-16 22:55:45 -04:00
parent 6b8b21ce77
commit 11292c6bb3
7 changed files with 116 additions and 95 deletions

View File

@ -130,6 +130,10 @@ natives: $(DST_TARGET)
$(MAKE) -C natives/hello
$(MAKE) -j 8 -C natives/sqlite3
clean-natives:
$(MAKE) -C natives/hello clean
$(MAKE) -C natives/sqlite3 clean
#################
##### Other #####
#################

View File

@ -393,7 +393,7 @@ static const DstReg cfuns[] = {
{NULL, NULL}
};
int _dst_init(DstArgs args) {
DST_MODULE_ENTRY(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
return 0;

View File

@ -676,7 +676,7 @@ static DstAssembleResult dst_asm1(DstAssembler *parent, Dst source, int flags) {
sizeof(DstInstructionDef),
dst_unwrap_symbol(t[0]));
if (NULL == idef)
dst_asm_errorv(&a, dst_formatc("unknown instruction %v", instr));
dst_asm_errorv(&a, dst_formatc("unknown instruction %v", t[0]));
op = read_instruction(&a, idef, t);
}
def->bytecode[a.bytecode_count++] = op;

View File

@ -57,14 +57,14 @@
(defn defglobal
"Dynamically create a global def."
[name value]
(def name* (string name))
(def name* (symbol name))
(put *env* name* @{:value value})
nil)
(defn varglobal
"Dynamically create a global var."
[name init]
(def name* (string name))
(def name* (symbol name))
(put *env* name* @{:ref @[init]})
nil)
@ -909,16 +909,15 @@
(def args (map macroexpand-1 (tuple.slice t 2)))
(apply tuple 'fn (get t 1) args))
(def specs {
':= expanddef
(def specs
{':= expanddef
'def expanddef
'do expandall
'fn expandfn
'if expandall
'quote identity
'var expanddef
'while expandall
})
'while expandall})
(defn dotup [t]
(def h (get t 0))
@ -931,7 +930,8 @@
m? (apply1 m (tuple.slice t 1))
(apply1 tuple (map macroexpand-1 t))))
(def ret (case (type x)
(def ret
(case (type x)
:tuple (dotup x)
:array (map macroexpand-1 x)
:struct (table.to-struct (dotable x macroexpand-1))
@ -1088,16 +1088,14 @@
(when f
(def st (fiber.stack f))
(loop
[{
:function func
[{:function func
:tail tail
:pc pc
:c c
:name name
:source source
:line source-line
:column source-col
} :in st]
:column source-col} :in st]
(file.write stdout " in")
(when c (file.write stdout " cfunction"))
(if name
@ -1132,28 +1130,26 @@
(run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval")
returnval)
(def module.syslibpath
(or (os.getenv "DST_PATH") "/usr/local/lib/dst/"))
(def module.paths
(do
(def syspath (or (os.getenv "DST_PATH") "/usr/local/lib/dst/"))
(defglobal 'module.paths
@["./?.dst"
"./?/init.dst"
"./dst_modules/?.dst"
"./dst_modules/?/init.dst"
(string module.syslibpath VERSION "/?.dst")
(string module.syslibpath VERSION "/?/init.dst")
(string module.syslibpath "/?.dst")
(string module.syslibpath "/?/init.dst")])
(def module.native-paths
(string syspath VERSION "/?.dst")
(string syspath VERSION "/?/init.dst")
(string syspath "/?.dst")
(string syspath "/?/init.dst")])
(defglobal 'module.native-paths
@["./?.so"
"./?/??.so"
"./dst_modules/?.so"
"./dst_modules/?/??.so"
(string module.syslibpath VERSION "/?.so")
(string module.syslibpath VERSION "/?/??.so")
(string module.syslibpath "/?.so")
(string module.syslibpath "/?/??.so")])
(string syspath VERSION "/?.so")
(string syspath VERSION "/?/??.so")
(string syspath "/?.so")
(string syspath "/?/??.so")]))
(defn module.find
[path paths]
@ -1216,15 +1212,15 @@
(fn [a b c d] (default-error-handler a b c d) (os.exit 1))
default-error-handler)
path)
(file.close f)
(put loading path nil)
newenv)
(file.close f))
(do
# Try native module
(def n (find-native path))
(if (not n)
(error (string "could not open file for module " path)))
((native n)))))))))
((native n) newenv)))
(put loading path false)
newenv)))))
(defn import* [env path & args]
(def targs (apply1 table args))

View File

@ -339,6 +339,24 @@ void dst_gcroot(Dst root) {
dst_vm_root_count = newcount;
}
/* Identity equality for GC purposes */
static int dst_gc_idequals(Dst lhs, Dst rhs) {
if (dst_type(lhs) != dst_type(rhs))
return 0;
switch (dst_type(lhs)) {
case DST_TRUE:
case DST_FALSE:
case DST_NIL:
return 1;
case DST_INTEGER:
return dst_unwrap_integer(lhs) == dst_unwrap_integer(rhs);
case DST_REAL:
return dst_unwrap_real(lhs) == dst_unwrap_real(rhs);
default:
return dst_unwrap_pointer(lhs) == dst_unwrap_pointer(rhs);
}
}
/* Remove a root value from the GC. This allows the gc to potentially reclaim
* a value and all its children. */
int dst_gcunroot(Dst root) {
@ -346,7 +364,7 @@ int dst_gcunroot(Dst root) {
Dst *v = dst_vm_roots;
/* Search from top to bottom as access is most likely LIFO */
for (v = dst_vm_roots; v < vtop; v++) {
if (dst_equals(root, *v)) {
if (dst_gc_idequals(root, *v)) {
*v = dst_vm_roots[--dst_vm_root_count];
return 1;
}
@ -361,7 +379,7 @@ int dst_gcunrootall(Dst root) {
int ret = 0;
/* Search from top to bottom as access is most likely LIFO */
for (v = dst_vm_roots; v < vtop; v++) {
if (dst_equals(root, *v)) {
if (dst_gc_idequals(root, *v)) {
*v = dst_vm_roots[--dst_vm_root_count];
vtop--;
ret = 1;

View File

@ -1059,6 +1059,9 @@ int dst_lib_parse(DstArgs args);
int dst_lib_asm(DstArgs args);
int dst_lib_compile(DstArgs args);
/* Helpers for writing modules */
#define DST_MODULE_ENTRY int _dst_init
/***** END SECTION MAIN *****/
/***** START SECTION MACROS *****/