From 4f488ca684b47787a67ffc91130610bae4e73d28 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 19 Dec 2019 23:34:27 +0100 Subject: [PATCH] [#159] set sci var roots to *in*, *out* and *err* --- reflection.json | 1 + src/babashka/impl/classes.clj | 7 ++++--- src/babashka/impl/utils.clj | 13 ++++++++----- test/babashka/main_test.clj | 5 ++++- test/babashka/test_utils.clj | 33 +++++++++++++++++++++------------ 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/reflection.json b/reflection.json index ee23dd4c..e010ef02 100644 --- a/reflection.json +++ b/reflection.json @@ -294,6 +294,7 @@ "allPublicFields" : true, "allPublicConstructors" : true }, { + "allPublicConstructors" : true, "methods" : [ { "name" : "activeCount" }, { diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 36965fc3..987197f2 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -65,9 +65,10 @@ sun.nio.fs.UnixPath ;; included because of permission check ] :custom-classes {'java.lang.Thread - ;; generated with `public-declared-method-names`, see in - ;; `comment` below - {:methods [{:name "activeCount"} + {:allPublicConstructors true + ;; generated with `public-declared-method-names`, see in + ;; `comment` below + :methods [{:name "activeCount"} {:name "checkAccess"} {:name "currentThread"} {:name "dumpStack"} diff --git a/src/babashka/impl/utils.clj b/src/babashka/impl/utils.clj index 55387bc3..8000e5ac 100644 --- a/src/babashka/impl/utils.clj +++ b/src/babashka/impl/utils.clj @@ -1,9 +1,12 @@ (ns babashka.impl.utils {:no-doc true} - (:require [sci.core :as sci])) + (:require + [sci.impl.vars :as vars] + [sci.core :as sci])) + +(sci.impl.vars/bindRoot sci/in *in*) +(sci.impl.vars/bindRoot sci/out *out*) +(sci.impl.vars/bindRoot sci/err *err*) (defn eval-string [expr ctx] - (sci/with-bindings {sci/out *out* - sci/in *in* - sci/err *err*} - (sci/eval-string expr ctx))) + (sci/eval-string expr ctx)) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 492bd252..e4286ebd 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -308,8 +308,11 @@ (is (= "babashka" (bb nil "(String. (.decode (java.util.Base64/getDecoder) (.encode (java.util.Base64/getEncoder) (.getBytes \"babashka\"))))")))) +(deftest Thread-test + (is (= "hello" (bb nil "(doto (java.lang.Thread. (fn [] (prn \"hello\"))) (.start) (.join)) nil")))) + ;;;; Scratch (comment - (dotimes [i 10] (wait-for-port-test)) + (dotimes [_ 10] (wait-for-port-test)) ) diff --git a/test/babashka/test_utils.clj b/test/babashka/test_utils.clj index 5a42415d..167737c8 100644 --- a/test/babashka/test_utils.clj +++ b/test/babashka/test_utils.clj @@ -2,7 +2,8 @@ (:require [babashka.main :as main] [me.raynes.conch :refer [let-programs] :as sh] - [sci.core :as sci])) + [sci.core :as sci] + [sci.impl.vars :as vars])) (set! *warn-on-reflection* true) @@ -14,17 +15,25 @@ bindings-map (cond-> {sci/out os sci/err es} is (assoc sci/in is))] - (sci/with-bindings bindings-map - (let [res (binding [*out* os - *err* es] - (if input - (with-in-str input (apply main/main args)) - (apply main/main args)))] - (if (zero? res) - (str os) - (throw (ex-info (str es) - {:stdout (str os) - :stderr (str es)}))))))) + (try + (when input (vars/bindRoot sci/in is)) + (vars/bindRoot sci/out os) + (vars/bindRoot sci/err es) + (sci/with-bindings bindings-map + (let [res (binding [*out* os + *err* es] + (if input + (with-in-str input (apply main/main args)) + (apply main/main args)))] + (if (zero? res) + (str os) + (throw (ex-info (str es) + {:stdout (str os) + :stderr (str es)}))))) + (finally + (when input (vars/bindRoot sci/in *in*)) + (vars/bindRoot sci/out *out*) + (vars/bindRoot sci/err *err*))))) (defn bb-native [input & args] (let-programs [bb "./bb"]