1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Only set :current-file in run-context if source is a path

This commit is contained in:
Michael Camilleri 2021-12-13 11:28:54 +09:00
parent 9d5cc5c11f
commit 939d1dcae9
No known key found for this signature in database
GPG Key ID: 7EB218A48DF8B572
2 changed files with 17 additions and 19 deletions

View File

@ -2263,7 +2263,7 @@
* `:chunks` - callback to read into a buffer - default is getline * `:chunks` - callback to read into a buffer - default is getline
* `:on-parse-error` - callback when parsing fails - default is bad-parse * `:on-parse-error` - callback when parsing fails - default is bad-parse
* `:env` - the environment to compile against - default is the current env * `:env` - the environment to compile against - default is the current env
* `:source` - string path of source for better errors - default is "<anonymous>" * `:source` - source path for better errors (use keywords for non-paths) - default is :<anonymous>
* `:on-compile-error` - callback when compilation fails - default is bad-compile * `:on-compile-error` - callback when compilation fails - default is bad-compile
* `:on-compile-warning` - callback for any linting error - default is warn-compile * `:on-compile-warning` - callback for any linting error - default is warn-compile
* `:evaluator` - callback that executes thunks. Signature is (evaluator thunk source env where) * `:evaluator` - callback that executes thunks. Signature is (evaluator thunk source env where)
@ -2295,13 +2295,13 @@
(default on-compile-warning warn-compile) (default on-compile-warning warn-compile)
(default on-parse-error bad-parse) (default on-parse-error bad-parse)
(default evaluator (fn evaluate [x &] (x))) (default evaluator (fn evaluate [x &] (x)))
(default default-where :<anonymous>)
(default guard :ydt) (default guard :ydt)
(var where default-where) (var where default-where)
(if where (if (string? where)
(put env :current-file where) (put env :current-file where))
(set where "<anonymous>"))
# Evaluate 1 source form in a protected manner # Evaluate 1 source form in a protected manner
(def lints @[]) (def lints @[])
@ -2380,13 +2380,10 @@
(buffer/clear buf)) (buffer/clear buf))
[:source new-where] [:source new-where]
(if (string? new-where)
(do (do
(set where new-where) (set where new-where)
(put env :current-file new-where)) (if (string? new-where)
(do (put env :current-file new-where)))
(set where default-where)
(put env :current-file nil)))
(do (do
(var pindex 0) (var pindex 0)
@ -2447,14 +2444,14 @@
(if-not (= (fiber/status f) :dead) (if-not (= (fiber/status f) :dead)
(error val)) (error val))
(set returnval val)) (set returnval val))
:source "eval-string"}) :source :eval-string})
returnval) returnval)
(defn eval (defn eval
``Evaluates a form in the current environment. If more control over the ``Evaluates a form in the current environment. If more control over the
environment is needed, use `run-context`.`` environment is needed, use `run-context`.``
[form] [form]
(def res (compile form (fiber/getenv (fiber/current)) "eval")) (def res (compile form (fiber/getenv (fiber/current)) :eval))
(if (= (type res) :function) (if (= (type res) :function)
(res) (res)
(error (get res :error)))) (error (get res :error))))
@ -2616,7 +2613,7 @@
(defn dofile (defn dofile
`Evaluate a file, file path, or stream and return the resulting environment. :env, :expander, `Evaluate a file, file path, or stream and return the resulting environment. :env, :expander,
:evaluator, :read, and :parser are passed through to the underlying :source, :evaluator, :read, and :parser are passed through to the underlying
run-context call. If exit is true, any top level errors will trigger a run-context call. If exit is true, any top level errors will trigger a
call to (os/exit 1) after printing the error.` call to (os/exit 1) after printing the error.`
[path &keys [path &keys
@ -2634,7 +2631,6 @@
(def path-is-file (= f path)) (def path-is-file (= f path))
(default env (make-env)) (default env (make-env))
(def spath (string path)) (def spath (string path))
(put env :current-file (or src (if-not path-is-file spath)))
(put env :source (or src (if-not path-is-file spath path))) (put env :source (or src (if-not path-is-file spath path)))
(var exit-error nil) (var exit-error nil)
(var exit-fiber nil) (var exit-fiber nil)
@ -2678,7 +2674,7 @@
:expander expander :expander expander
:read read :read read
:parser parser :parser parser
:source (or src (if path-is-file "<anonymous>" spath))})) :source (or src (if path-is-file :<anonymous> spath))}))
(if-not path-is-file (:close f)) (if-not path-is-file (:close f))
(when exit-error (when exit-error
(if exit-fiber (if exit-fiber
@ -3381,7 +3377,7 @@
:on-status (or onsignal (make-onsignal env 1)) :on-status (or onsignal (make-onsignal env 1))
:parser parser :parser parser
:read read :read read
:source "repl"})) :source :repl}))
### ###
### ###
@ -3676,7 +3672,7 @@
(if (or should-repl no-file) (if (or should-repl no-file)
(if (if
compile-only (flycheck stdin :source "stdin" :exit exit-on-error) compile-only (flycheck stdin :source :stdin :exit exit-on-error)
(do (do
(if-not quiet (if-not quiet
(print "Janet " janet/version "-" janet/build " " (os/which) "/" (os/arch) " - '(doc)' for help")) (print "Janet " janet/version "-" janet/build " " (os/which) "/" (os/arch) " - '(doc)' for help"))

View File

@ -958,7 +958,9 @@ JANET_CORE_FN(cfun,
} }
const uint8_t *source = NULL; const uint8_t *source = NULL;
if (argc >= 3) { if (argc >= 3) {
source = janet_getstring(argv, 2); source = janet_checktype(argv[2], JANET_STRING) ?
janet_unwrap_string(argv[2]) :
janet_unwrap_keyword(argv[2]);
} }
JanetArray *lints = (argc >= 4) ? janet_getarray(argv, 3) : NULL; JanetArray *lints = (argc >= 4) ? janet_getarray(argv, 3) : NULL;
JanetCompileResult res = janet_compile_lint(argv[0], env, source, lints); JanetCompileResult res = janet_compile_lint(argv[0], env, source, lints);