1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-24 07:20:27 +00:00

Add failure on top level error.

This commit is contained in:
bakpakin 2018-06-08 15:58:23 -04:00
parent 6d1ab414e4
commit 1690fee446
5 changed files with 42 additions and 32 deletions

View File

@ -1092,21 +1092,26 @@ returned from compiling and running the file."
(def cache @{}) (def cache @{})
(def loading @{}) (def loading @{})
(fn [path] (fn [path args]
(when (get loading path) (when (get loading path)
(error (string "circular dependency: module " path " is loading"))) (error (string "circular dependency: module " path " is loading")))
(def {
:exit exit-on-error
} args)
(def check (get cache path)) (def check (get cache path))
(if check check (do (if check check (do
(def newenv (make-env)) (def newenv (make-env))
(put cache path newenv) (put cache path newenv)
(put loading path true) (put loading path true)
(def f (find-mod path)) (def f (find-mod path))
(if f (if f
(do (do
# Normal dst module # Normal dst module
(defn chunks [buf] (file.read f 1024 buf)) (defn chunks [buf] (file.read f 1024 buf))
(run-context newenv chunks identity default-error-handler) (run-context newenv chunks identity
(if exit-on-error
(fn [a b c] (default-error-handler a b c) (os.exit 1))
default-error-handler))
(file.close f) (file.close f)
(put loading path nil) (put loading path nil)
newenv) newenv)
@ -1118,11 +1123,12 @@ returned from compiling and running the file."
((native n))))))))) ((native n)))))))))
(defn import* [env path & args] (defn import* [env path & args]
(def newenv (require path)) (def targs (apply1 table args))
(def { (def {
:as as :as as
:prefix prefix :prefix prefix
} (apply1 table args)) } targs)
(def newenv (require path targs))
(var k (next newenv nil)) (var k (next newenv nil))
(def {:meta meta} newenv) (def {:meta meta} newenv)
(def prefix (or (and as (string as ".")) prefix (string path "."))) (def prefix (or (and as (string as ".")) prefix (string path ".")))

View File

@ -45,9 +45,10 @@ static int os_execute(DstArgs args) {
} }
static int os_getenv(DstArgs args) { static int os_getenv(DstArgs args) {
if (args.n != 1 || !dst_checktype(args.v[0], DST_STRING)) const uint8_t *k;
DST_THROW(args, "expected string"); DST_FIXARITY(args, 1);
const char *cstr = (const char *) dst_unwrap_string(args.v[0]); DST_ARG_STRING(k, args, 0);
const char *cstr = (const char *) k;
const char *res = getenv(cstr); const char *res = getenv(cstr);
DST_RETURN(args, cstr DST_RETURN(args, cstr
? dst_cstringv(res) ? dst_cstringv(res)
@ -55,29 +56,27 @@ static int os_getenv(DstArgs args) {
} }
static int os_setenv(DstArgs args) { static int os_setenv(DstArgs args) {
int t2;
if (args.n < 2) DST_THROW(args, "expected 2 arguments");
t2 = dst_type(args.v[1]);
if (!dst_checktype(args.v[0], DST_STRING)
|| (t2 != DST_STRING && t2 != DST_NIL))
DST_THROW(args, "expected string");
const char *k = (const char *) dst_unwrap_string(args.v[0]);
#ifdef DST_WINDOWS #ifdef DST_WINDOWS
if (t2 == DST_NIL) { #define SETENV(K,V) _putenv_s(K, V)
// Investigate best way to delete env vars on windows. Use winapi? #define UNSETENV(K) _putenv_s(K, "")
_putenv_s(k, "");
} else {
const char *v = (const char *) dst_unwrap_string(args.v[1]);
_putenv_s(k, v);
}
#else #else
if (t2 == DST_NIL) { #define SETENV(K,V) setenv(K, V, 1)
unsetenv(k); #define UNSETENV(K) unsetenv(K)
} else {
const char *v = (const char *) dst_unwrap_string(args.v[1]);
setenv(k, v, 1);
}
#endif #endif
const uint8_t *k;
const char *ks;
DST_MAXARITY(args, 2);
DST_MINARITY(args, 1);
DST_ARG_STRING(k, args, 0);
ks = (const char *) k;
if (args.n == 1 || dst_checktype(args.v[1], DST_NIL)) {
UNSETENV(ks);
} else {
const uint8_t *v;
DST_ARG_STRING(v, args, 1);
const char *vc = (const char *) v;
SETENV(ks, vc);
}
return 0; return 0;
} }
@ -89,8 +88,10 @@ static int os_exit(DstArgs args) {
|| dst_checktype(args.v[0], DST_FALSE)) { || dst_checktype(args.v[0], DST_FALSE)) {
exit(dst_unwrap_boolean(args.v[0]) ? EXIT_SUCCESS : EXIT_FAILURE); exit(dst_unwrap_boolean(args.v[0]) ? EXIT_SUCCESS : EXIT_FAILURE);
return 0; return 0;
} else if (dst_checktype(args.v[0], DST_INTEGER)) {
exit(dst_unwrap_integer(args.v[0]));
} else { } else {
exit(dst_hash(args.v[0])); exit(EXIT_FAILURE);
} }
return 0; return 0;
} }

View File

@ -6,6 +6,7 @@
(var *no-file* :private true) (var *no-file* :private true)
(var *raw-stdin* :private false) (var *raw-stdin* :private false)
(var *handleopts* :private true) (var *handleopts* :private true)
(var *exit-on-error* :private true)
# Flag handlers # Flag handlers
(def handlers :private { (def handlers :private {
@ -17,12 +18,14 @@
-s Use raw stdin instead of getline like functionality -s Use raw stdin instead of getline like functionality
-e Execute a string of dst -e Execute a string of dst
-r Enter the repl after running all scripts -r Enter the repl after running all scripts
-p Keep on executing if there is a top level error (persistent)
-- Stop handling options`) -- Stop handling options`)
(os.exit 0) (os.exit 0)
1) 1)
"v" (fn [] (print VERSION) (os.exit 0) 1) "v" (fn [] (print VERSION) (os.exit 0) 1)
"s" (fn [] (:= *raw-stdin* true) (:= *should-repl* true) 1) "s" (fn [] (:= *raw-stdin* true) (:= *should-repl* true) 1)
"r" (fn [] (:= *should-repl* true) 1) "r" (fn [] (:= *should-repl* true) 1)
"p" (fn [] (:= *exit-on-error* false) 1)
"-" (fn [] (:= *handleopts* false) 1) "-" (fn [] (:= *handleopts* false) 1)
"e" (fn [i] "e" (fn [i]
(:= *no-file* false) (:= *no-file* false)
@ -43,7 +46,7 @@
(+= i (dohandler (string.slice arg 1 2) i)) (+= i (dohandler (string.slice arg 1 2) i))
(do (do
(:= *no-file* false) (:= *no-file* false)
(import* _env arg) (import* _env arg :exit *exit-on-error*)
(++ i)))) (++ i))))
(when (or *should-repl* *no-file*) (when (or *should-repl* *no-file*)

View File

@ -18,7 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
(import test.helper :prefix "") (import test.helper :prefix "" :exit true)
(start-suite 0) (start-suite 0)
(assert (= 10 (+ 1 2 3 4)) "addition") (assert (= 10 (+ 1 2 3 4)) "addition")

View File

@ -18,7 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
(import test.helper :prefix "") (import test.helper :prefix "" :exit true)
(start-suite 1) (start-suite 1)
(assert (= 400.0 (sqrt 160000)) "sqrt(160000)=400") (assert (= 400.0 (sqrt 160000)) "sqrt(160000)=400")