diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 4a764068..f2f7d222 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -167,6 +167,9 @@ Everything after that is bound to *command-line-args*.")) (defn load-file* [ctx file] (let [s (slurp file)] (sci/eval-string s ctx))) + +(defn eval* [ctx form] + (sci/eval-string (pr-str form) ctx)) (defn start-socket-repl! [address ctx read-next] (let [ctx (update ctx :bindings assoc @@ -223,7 +226,8 @@ Everything after that is bound to *command-line-args*.")) :bindings (assoc bindings '*command-line-args* command-line-args) :env env :features #{:bb}} - ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %)) + ctx (update ctx :bindings assoc 'eval #(eval* ctx %) + 'load-file #(load-file* ctx %)) _preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (sci/eval-string ctx)) exit-code (or diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 747900ad..05f5858a 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -132,6 +132,11 @@ (is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (bar (foo 10 30) 3)" (.getPath tmp))))))) +(deftest eval-test + (is (= "120\n" (test-utils/bb nil "(eval '(do (defn foo [x y] (+ x y)) + (defn bar [x y] (* x y)) + (bar (foo 10 30) 3)))")))) + (deftest preloads-test ;; THIS TEST REQUIRES: ;; export BABASHKA_PRELOADS='(defn __bb__foo [] "foo") (defn __bb__bar [] "bar")'