Skip to content

Commit

Permalink
CLJS-2377: The CLJS compiled uses deprecated modules on Java 9
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes authored and dnolen committed Dec 1, 2017
1 parent 1e7e01f commit 544c1b7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/main/clojure/cljs/repl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
[cljs.source-map :as sm])
(:import [java.io File PushbackReader FileWriter PrintWriter]
[java.net URL]
[javax.xml.bind DatatypeConverter]
[java.util Base64]
[clojure.lang IExceptionInfo]
[java.util.regex Pattern]
[com.google.common.base Throwables]))
Expand Down Expand Up @@ -449,6 +449,11 @@
(println st)))
(println st))))))

(defn- bytes-to-base64-str
"Convert a byte array into a base-64 encoded string."
[^bytes bytes]
(.encodeToString (Base64/getEncoder) bytes))

(defn evaluate-form
"Evaluate a ClojureScript form in the JavaScript environment. Returns a
string which is the ClojureScript return value. This string may or may
Expand Down Expand Up @@ -488,7 +493,7 @@
(str js
"\n//# sourceURL=repl-" t ".js"
"\n//# sourceMappingURL=data:application/json;base64,"
(DatatypeConverter/printBase64Binary
(bytes-to-base64-str
(.getBytes
(sm/encode
{(str "repl-" t ".cljs")
Expand Down
19 changes: 16 additions & 3 deletions src/main/clojure/cljs/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
[clojure.edn :as edn])
(:import [java.io File]
[java.net URL URLDecoder]
[java.security MessageDigest]
[javax.xml.bind DatatypeConverter]))
[java.security MessageDigest]))

;; next line is auto-generated by the build-script - Do not edit!
(def ^:dynamic *clojurescript-version*)
Expand Down Expand Up @@ -297,14 +296,28 @@
xs seen)))]
(step coll #{}))))

(def ^:private hex-digits (char-array "0123456789ABCDEF"))

(defn- bytes-to-hex-str
"Convert an array of bytes into a hex encoded string."
[^bytes bytes]
(loop [index (int 0)
buffer (StringBuilder. (int (* 2 (alength bytes))))]
(if (== (alength bytes) index)
(.toString buffer)
(let [byte (aget bytes index)]
(.append buffer (aget ^chars hex-digits (bit-and (bit-shift-right byte 4) 0xF)))
(.append buffer (aget ^chars hex-digits (bit-and byte 0xF)))
(recur (inc index) buffer)))))

(defn content-sha
([^String s]
(content-sha s nil))
([^String s ^Long n]
(let [digest (MessageDigest/getInstance "SHA-1")
_ (.reset digest)
_ (.update digest (.getBytes s "utf8"))
sha (DatatypeConverter/printHexBinary (.digest digest))]
sha (bytes-to-hex-str (.digest digest))]
(if-not (nil? n)
(apply str (take n sha))
sha))))
Expand Down
3 changes: 3 additions & 0 deletions src/test/clojure/cljs/repl_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@
(is file)
(and file
(is (io/resource file))))))

(deftest test-bytes-to-base64-str
(is (= "YWJj" (#'repl/bytes-to-base64-str (.getBytes "abc")))))
7 changes: 7 additions & 0 deletions src/test/clojure/cljs/util_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@
(deftest test-path
(is (= (.getAbsolutePath (io/file "src/main/clojure/cljs/closure.clj"))
(util/path (io/as-url (io/file "src/main/clojure/cljs/closure.clj"))))))

(deftest test-bytes-to-hex-str
(is (= "09616263" (#'util/bytes-to-hex-str (.getBytes "\u0009abc")))))

(deftest test-content-sha
(is (= "40BD001563085FC35165329EA1FF5C5ECBDBBEEF" (util/content-sha "123")))
(is (= "40BD0" (util/content-sha "123" 5))))

0 comments on commit 544c1b7

Please sign in to comment.