From b535c91ee19795753489c2d7f9b8de5842f25d8d Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 5 Jan 2019 22:52:28 -0500 Subject: [PATCH] Fix native module issue. --- src/core/core.janet | 71 ++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git a/src/core/core.janet b/src/core/core.janet index 221add38..b97512ff 100644 --- a/src/core/core.janet +++ b/src/core/core.janet @@ -1568,7 +1568,7 @@ value, one key will be ignored." path)) (def require - "(require module)\n\n + "(require module & args)\n\n Require a module with the given name. Will search all of the paths in module/paths, then the path as a raw file path. Returns the new environment returned from compiling and running the file." @@ -1596,56 +1596,47 @@ value, one key will be ignored." (def cache @{}) (def loading @{}) - (fn require [path args &] + (fn require [path & args] (when loading.path (error (string "circular dependency: module " path " is loading"))) - (def {:exit exit-on-error} (or args {})) - (def check cache.path) - (if check + (def {:exit exit-on-error} (table ;args)) + (if-let [check cache.path] check - (do - (def newenv (make-env)) - (set cache.path newenv) - (set loading.path true) - (def f (find-mod path)) - (if f - (do - # Normal janet module - (defn chunks [buf _] (file/read f 1024 buf)) - (run-context newenv chunks - (fn [sig x f source] - (when (not= sig :dead) - (status-pp sig x f source) - (if exit-on-error (os/exit 1)))) - path) - (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) newenv))) - (set loading.path false) - newenv))))) + (if-let [f (find-mod path)] + (do + # Normal janet module + (def newenv (make-env)) + (set cache.path newenv) + (set loading.path true) + (defn chunks [buf _] (file/read f 1024 buf)) + (run-context newenv chunks + (fn [sig x f source] + (when (not= sig :dead) + (status-pp sig x f source) + (if exit-on-error (os/exit 1)))) + path) + (file/close f) + (set loading.path false) + newenv) + (do + # Try native module + (def n (find-native path)) + (if (not n) + (error (string "could not open file for module " path))) + (native n))))))) (defn import* "Import a module into a given environment table. This is the functional form of (import ...) that expects and explicit environment table." [env path & args] - (def targs (table ;args)) (def {:as as - :prefix prefix} targs) - (def newenv (require path targs)) - (var k (next newenv nil)) - (def {:meta meta} newenv) + :prefix prefix} (table ;args)) + (def newenv (require path ;args)) (def prefix (or (and as (string as "/")) prefix (string path "/"))) - (while k - (def v newenv.k) - (when (not v:private) - (def newv (table/setproto @{:private true} v)) - (put env (symbol prefix k) newv)) - (set k (next newenv k)))) + (loop [[k v] :pairs newenv :when (not v:private)] + (def newv (table/setproto @{:private true} v)) + (put env (symbol prefix k) newv))) (defmacro import "Import a module. First requires the module, and then merges its