Sci: reify core vars, implement repl/doc (#265)

This commit is contained in:
Michiel Borkent 2020-02-16 12:54:54 +01:00 committed by GitHub
parent 018371d90c
commit e98a92c283
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 169 additions and 162 deletions

2
sci

@ -1 +1 @@
Subproject commit 0fdb743ad833d74fb86bed1ae2eacaa9d883f652 Subproject commit 57209f08d18ee168dbc1dca26e60b4958add3c0c

View file

@ -5,7 +5,7 @@ set -eo pipefail
export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {spartan.spec {:git/url "https://github.com/borkdude/spartan.spec" :sha "16f7eec4b6589c77c96c9fcf989f78fffcee7c4c"}}}' -Spath) export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {spartan.spec {:git/url "https://github.com/borkdude/spartan.spec" :sha "16f7eec4b6589c77c96c9fcf989f78fffcee7c4c"}}}' -Spath)
./bb -e " ./bb -e "
(require '[spartan.spec :as s]) (time (require '[spartan.spec :as s]))
(s/explain (s/cat :i int? :s string?) [1 :foo]) (time (s/explain (s/cat :i int? :s string?) [1 :foo]))
(s/conform (s/cat :i int? :s string?) [1 \"foo\"]) (time (s/conform (s/cat :i int? :s string?) [1 \"foo\"]))
" "

View file

@ -31,7 +31,7 @@
(sio/println "Use :repl/quit or :repl/exit to quit the REPL.") (sio/println "Use :repl/quit or :repl/exit to quit the REPL.")
(sio/println "Clojure rocks, Bash reaches.") (sio/println "Clojure rocks, Bash reaches.")
(sio/println) (sio/println)
(eval-form sci-ctx '(require '[clojure.repl :refer [dir]])))) (eval-form sci-ctx '(require '[clojure.repl :refer [dir doc]]))))
:read (or read :read (or read
(fn [_request-prompt request-exit] (fn [_request-prompt request-exit]
;; (prn "PEEK" @sci/in (r/peek-char @sci/in)) ;; (prn "PEEK" @sci/in (r/peek-char @sci/in))

View file

@ -25,12 +25,14 @@
[sci.core :as sci] [sci.core :as sci]
[sci.impl.interpreter :refer [eval-string*]] [sci.impl.interpreter :refer [eval-string*]]
[sci.impl.opts :as sci-opts] [sci.impl.opts :as sci-opts]
[sci.impl.vars :as vars]) [sci.impl.vars :as vars]
[sci.impl.unrestrict :refer [*unrestricted*]])
(:gen-class)) (:gen-class))
(binding [*unrestricted* true]
(sci/alter-var-root sci/in (constantly *in*)) (sci/alter-var-root sci/in (constantly *in*))
(sci/alter-var-root sci/out (constantly *out*)) (sci/alter-var-root sci/out (constantly *out*))
(sci/alter-var-root sci/err (constantly *err*)) (sci/alter-var-root sci/err (constantly *err*)))
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
;; To detect problems when generating the image, run: ;; To detect problems when generating the image, run:
@ -217,10 +219,6 @@ Everything after that is bound to *command-line-args*."))
(defn exit [n] (defn exit [n]
(throw (ex-info "" {:bb/exit-code n}))) (throw (ex-info "" {:bb/exit-code n})))
;; (sci/set-var-root! sci/*in* *in*)
;; (sci/set-var-root! sci/*out* *out*)
;; (sci/set-var-root! sci/*err* *err*)
(def aliases (def aliases
'{tools.cli 'clojure.tools.cli '{tools.cli 'clojure.tools.cli
edn clojure.edn edn clojure.edn
@ -283,8 +281,9 @@ Everything after that is bound to *command-line-args*."))
(handle-pipe!) (handle-pipe!)
#_(binding [*out* *err*] #_(binding [*out* *err*]
(prn "M" (meta (get bindings 'future)))) (prn "M" (meta (get bindings 'future))))
(sci/with-bindings {reflection-var false (binding [*unrestricted* true]
vars/current-ns (vars/->SciNamespace 'user)} (sci/binding [reflection-var false
vars/current-ns (vars/->SciNamespace 'user)]
(let [t0 (System/currentTimeMillis) (let [t0 (System/currentTimeMillis)
{:keys [:version :shell-in :edn-in :shell-out :edn-out {:keys [:version :shell-in :edn-in :shell-out :edn-out
:help? :file :command-line-args :help? :file :command-line-args
@ -431,7 +430,7 @@ Everything after that is bound to *command-line-args*."))
(spit uberscript-out expression :append true))) (spit uberscript-out expression :append true)))
(when time? (binding [*out* *err*] (when time? (binding [*out* *err*]
(println "bb took" (str (- t1 t0) "ms.")))) (println "bb took" (str (- t1 t0) "ms."))))
exit-code))) exit-code))))
(defn -main (defn -main
[& args] [& args]

View file

@ -342,6 +342,14 @@
(is (empty? (bb nil "--uberscript" (.getPath tmp-file) "-e" "(System/exit 1)"))) (is (empty? (bb nil "--uberscript" (.getPath tmp-file) "-e" "(System/exit 1)")))
(is (= "(System/exit 1)" (slurp tmp-file))))) (is (= "(System/exit 1)" (slurp tmp-file)))))
(deftest unrestricted-access
(testing "babashka is allowed to mess with built-in vars"
(is (= 1 (bb nil "
(def inc2 inc) (alter-var-root #'clojure.core/inc (constantly dec))
(let [res (inc 2)]
(alter-var-root #'clojure.core/inc (constantly inc2))
res)")))))
;;;; Scratch ;;;; Scratch
(comment (comment