1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Merge branch 'master' into ffi

This commit is contained in:
Calvin Rose 2022-06-10 16:24:55 -05:00
commit 33bb08d53b
3 changed files with 83 additions and 46 deletions

View File

@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Add `debugger` - an easy to use debugger function that just takes a fiber.
- `dofile` will now start a debugger on errors if the environment it is passed has `:debug` set.
- Add `debugger-on-status` function, which can be passed to `run-context` to start a debugger on
abnormal fiber signals.
- Allow running scripts with the `-d` flag to use the built-in debugger on errors and breakpoints.
- Add `raw-native`, `native-lookup`, and `native-close` for interfacing with dynamic libraries. - Add `raw-native`, `native-lookup`, and `native-close` for interfacing with dynamic libraries.
- Add mutexes (locks) and reader-writer locks to ev module for thread coordination. - Add mutexes (locks) and reader-writer locks to ev module for thread coordination.
- Add `parse-all` as a generalization of the `parse` function. - Add `parse-all` as a generalization of the `parse` function.

View File

@ -89,6 +89,8 @@ cd somewhere/my/projects/janet
make make
make test make test
make repl make repl
make install
make install-jpm-git
``` ```
Find out more about the available make targets by running `make help`. Find out more about the available make targets by running `make help`.
@ -103,6 +105,8 @@ cd somewhere/my/projects/janet
make CC=gcc-x86 make CC=gcc-x86
make test make test
make repl make repl
make install
make install-jpm-git
``` ```
### FreeBSD ### FreeBSD
@ -116,6 +120,8 @@ cd somewhere/my/projects/janet
gmake gmake
gmake test gmake test
gmake repl gmake repl
gmake install
gmake install-jpm-git
``` ```
### NetBSD ### NetBSD

View File

@ -2746,6 +2746,58 @@
(get r 0) (get r 0)
v)))) v))))
(def debugger-env
"An environment that contains dot prefixed functions for debugging."
@{})
(var- debugger-on-status-var nil)
(defn debugger
"Run a repl-based debugger on a fiber. Optionally pass in a level
to differentiate nested debuggers."
[fiber &opt level]
(default level 1)
(def nextenv (make-env (fiber/getenv fiber)))
(put nextenv :fiber fiber)
(put nextenv :debug-level level)
(put nextenv :signal (fiber/last-value fiber))
(merge-into nextenv debugger-env)
(defn debugger-chunks [buf p]
(def status (:state p :delimiters))
(def c ((:where p) 0))
(def prpt (string "debug[" level "]:" c ":" status "> "))
(getline prpt buf nextenv))
(print "entering debug[" level "] - (quit) to exit")
(flush)
(run-context
{:chunks debugger-chunks
:on-status (debugger-on-status-var nextenv (+ 1 level) true)
:env nextenv})
(print "exiting debug[" level "]")
(flush)
(nextenv :resume-value))
(defn debugger-on-status
"Create a function that can be passed to `run-context`'s `:on-status`
argument that will drop into a debugger on errors. The debugger will
only start on abmnormal signals if the env table has the `:debug` dyn
set to a truthy value."
[env &opt level is-repl]
(default level 1)
(fn [f x]
(def fs (fiber/status f))
(if (= :dead fs)
(when is-repl
(put env '_ @{:value x})
(printf (get env :pretty-format "%q") x)
(flush))
(do
(debug/stacktrace f x "")
(eflush)
(if (get env :debug) (debugger f level))))))
(set debugger-on-status-var debugger-on-status)
(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,
:source, :evaluator, :read, and :parser are passed through to the underlying :source, :evaluator, :read, and :parser are passed through to the underlying
@ -2802,9 +2854,12 @@
(debug/stacktrace f x "") (debug/stacktrace f x "")
(eflush) (eflush)
(os/exit 1)) (os/exit 1))
(put env :exit true) (if (get env :debug)
(set exit-error x) ((debugger-on-status env) f x)
(set exit-fiber f))) (do
(put env :exit true)
(set exit-error x)
(set exit-fiber f)))))
:evaluator evaluator :evaluator evaluator
:expander expander :expander expander
:read read :read read
@ -3362,7 +3417,8 @@
(def pc (frame :pc)) (def pc (frame :pc))
(def sourcemap (in dasm :sourcemap)) (def sourcemap (in dasm :sourcemap))
(var last-loc [-2 -2]) (var last-loc [-2 -2])
(print "\n signal: " (.signal)) (print "\n signal: " (.signal))
(print " status: " (fiber/status (.fiber)))
(print " function: " (dasm :name) " [" (in dasm :source "") "]") (print " function: " (dasm :name) " [" (in dasm :source "") "]")
(when-let [constants (dasm :constants)] (when-let [constants (dasm :constants)]
(printf " constants: %.4q" constants)) (printf " constants: %.4q" constants))
@ -3448,10 +3504,6 @@
(set res (debug/step (.fiber)))) (set res (debug/step (.fiber))))
res) res)
(def debugger-env
"An environment that contains dot prefixed functions for debugging."
@{})
(def- debugger-keys (filter (partial string/has-prefix? ".") (keys root-env))) (def- debugger-keys (filter (partial string/has-prefix? ".") (keys root-env)))
(each k debugger-keys (put debugger-env k (root-env k)) (put root-env k nil)) (each k debugger-keys (put debugger-env k (root-env k)) (put root-env k nil))
@ -3479,43 +3531,9 @@
":" ":"
(:state p :delimiters) "> ") (:state p :delimiters) "> ")
buf env))) buf env)))
(defn make-onsignal
[e level]
(defn enter-debugger
[f x]
(def nextenv (make-env env))
(put nextenv :fiber f)
(put nextenv :debug-level level)
(put nextenv :signal x)
(merge-into nextenv debugger-env)
(defn debugger-chunks [buf p]
(def status (:state p :delimiters))
(def c ((:where p) 0))
(def prpt (string "debug[" level "]:" c ":" status "> "))
(getline prpt buf nextenv))
(print "entering debug[" level "] - (quit) to exit")
(flush)
(repl debugger-chunks (make-onsignal nextenv (+ 1 level)) nextenv)
(print "exiting debug[" level "]")
(flush)
(nextenv :resume-value))
(fn [f x]
(def fs (fiber/status f))
(if (= :dead fs)
(do
(put e '_ @{:value x})
(printf (get e :pretty-format "%q") x)
(flush))
(do
(debug/stacktrace f x "")
(eflush)
(if (e :debug) (enter-debugger f x))))))
(run-context {:env env (run-context {:env env
:chunks chunks :chunks chunks
:on-status (or onsignal (make-onsignal env 1)) :on-status (or onsignal (debugger-on-status env 1 true))
:parser parser :parser parser
:read read :read read
:source :repl})) :source :repl}))
@ -3682,10 +3700,18 @@
(defn- run-main (defn- run-main
[env subargs arg] [env subargs arg]
(if-let [entry (in env 'main) (when-let [entry (in env 'main)
main (or (get entry :value) (in (get entry :ref) 0))] main (or (get entry :value) (in (get entry :ref) 0))]
(let [thunk (compile [main ;subargs] env arg)] (def guard (if (get env :debug) :ydt :y))
(if (function? thunk) (thunk) (error (thunk :error)))))) (defn wrap-main [&]
(main ;subargs))
(def f (fiber/new wrap-main guard))
(fiber/setenv f env)
(var res nil)
(while (fiber/can-resume? f)
(set res (resume f res))
(when (not= :dead (fiber/status f))
((debugger-on-status env) f res)))))
(defdyn *args* (defdyn *args*
"Dynamic bindings that will contain command line arguments at program start.") "Dynamic bindings that will contain command line arguments at program start.")