1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-15 08:22:59 +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

@ -23,7 +23,7 @@ project(dst)
# Set Some Variables # Set Some Variables
set(TARGET_NAME ${PROJECT_NAME}) set(TARGET_NAME ${PROJECT_NAME})
set (CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
# Set configurations # Set configurations

View File

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

View File

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

View File

@ -676,7 +676,7 @@ static DstAssembleResult dst_asm1(DstAssembler *parent, Dst source, int flags) {
sizeof(DstInstructionDef), sizeof(DstInstructionDef),
dst_unwrap_symbol(t[0])); dst_unwrap_symbol(t[0]));
if (NULL == idef) 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); op = read_instruction(&a, idef, t);
} }
def->bytecode[a.bytecode_count++] = op; def->bytecode[a.bytecode_count++] = op;

View File

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

View File

@ -339,6 +339,24 @@ void dst_gcroot(Dst root) {
dst_vm_root_count = newcount; 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 /* Remove a root value from the GC. This allows the gc to potentially reclaim
* a value and all its children. */ * a value and all its children. */
int dst_gcunroot(Dst root) { int dst_gcunroot(Dst root) {
@ -346,7 +364,7 @@ int dst_gcunroot(Dst root) {
Dst *v = dst_vm_roots; Dst *v = dst_vm_roots;
/* Search from top to bottom as access is most likely LIFO */ /* Search from top to bottom as access is most likely LIFO */
for (v = dst_vm_roots; v < vtop; v++) { 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]; *v = dst_vm_roots[--dst_vm_root_count];
return 1; return 1;
} }
@ -361,7 +379,7 @@ int dst_gcunrootall(Dst root) {
int ret = 0; int ret = 0;
/* Search from top to bottom as access is most likely LIFO */ /* Search from top to bottom as access is most likely LIFO */
for (v = dst_vm_roots; v < vtop; v++) { 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]; *v = dst_vm_roots[--dst_vm_root_count];
vtop--; vtop--;
ret = 1; ret = 1;

View File

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