Skip to content

Commit bb18b4e

Browse files
committed
feat: load functions
1 parent 2ba2510 commit bb18b4e

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/uclj/core.clj

+30-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
[clojure.math.combinatorics :as combo]
2525
[clojure.pprint :as pprint :refer [pprint pp]]
2626
[clojure.set :as set]
27+
[clojure.spec.alpha]
2728
[clojure.string :as s]
2829
[clojure.test :refer [deftest testing is are]]
2930
[clojure.test.check :as check]
@@ -221,14 +222,38 @@
221222
(def custom-var-impls (atom {}))
222223
(defmacro custom-var! [v val] `(swap! custom-var-impls assoc ~v ~val))
223224

224-
(declare evaluator)
225+
(custom-var! #'clojure.core/load-reader
226+
(fn [rdr]
227+
(with-open [rdr (new java.io.PushbackReader rdr)]
228+
(->> (repeatedly #(read {:eof ::eof} rdr))
229+
(take-while (partial not= ::eof))
230+
(map (@custom-var-impls #'clojure.core/eval))
231+
(last)))))
232+
225233
(custom-var! #'clojure.core/load-file
226234
(fn [fname]
227235
(binding [*file* (io/file fname)]
228-
(with-open [in (new java.io.PushbackReader (io/reader *file*))]
229-
(doseq [read (repeatedly #(read {:eof ::eof} in))
230-
:while (not= ::eof read)]
231-
(evaluator read))))))
236+
(with-open [rdr (new java.io.PushbackReader (io/reader *file*))]
237+
((@custom-var-impls #'clojure.core/load-reader) rdr)))))
238+
239+
(custom-var! #'clojure.core/load
240+
(fn [& bodies] (throw (new RuntimeException "UCLJ does not yet support clojure.core/load!")))
241+
#_(fn [& paths]
242+
(doseq [^String path paths]
243+
(let [root-resource (fn [lib] (str \/ (.. (name lib) (replace \- \_) (replace \. \/))))
244+
root-directory (fn [lib] (let [d (root-resource lib)] (subs d 0 (.lastIndexOf (str d) "/"))))
245+
^String path (if (.startsWith path "/")
246+
path
247+
(str (root-directory (ns-name *ns*)) \/ path))
248+
res (or (io/resource (str path ".clj")) (io/resource (str path ".cljc")))]
249+
(assert res "No resource found!")
250+
((@custom-var-impls #'clojure.core/load-reader) (io/reader res))))))
251+
252+
(custom-var! #'clojure.core/compile
253+
(fn [_] (throw (new RuntimeException "UCLJ does not support clojure.core/compile!"))))
254+
255+
(custom-var! #'clojure.core/load-string
256+
(fn [s] ((@custom-var-impls #'clojure.core/load-reader) (java.io.StringReader. s))))
232257

233258
;; Due to the closed-world assumption, we cannot instantiate array of any types.
234259
(custom-var! #'clojure.core/into-array

test/clojure_core_test.clj

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
(ns clojure-core-test
2-
(:import clojure.lang.Var)
2+
#_(:import clojure.lang.Var)
33
(:require [clojure.data.xml]
44
[clojure.test :refer :all]))
55

6+
#_
67
(deftest test-imported
78
(is (= Var clojure.lang.Var)))
89

@@ -64,3 +65,12 @@
6465

6566
(deftest test-xml-lib
6667
(is (map? (clojure.data.xml/parse-str "<a>1</a>"))))
68+
69+
(deftest test-load-functions
70+
(is (= 2 (eval '(inc 1))))
71+
(is (= 2 (load-string "(inc 1)")))
72+
(let [tmpfile "/tmp/uclj-test-load-content"
73+
tmpfile-clj (str tmpfile ".clj")]
74+
(spit tmpfile-clj "(inc 1)")
75+
(is (= 2 (load-file tmpfile-clj)))
76+
(is (= 2 (load-reader (clojure.java.io/reader tmpfile-clj))))))

0 commit comments

Comments
 (0)