Skip to content

Commit

Permalink
Merge pull request #57 from cooljeanius/cherrypicks
Browse files Browse the repository at this point in the history
Cherrypick some upstream commits
  • Loading branch information
cooljeanius authored Nov 19, 2024
2 parents 54a7c0c + 76c67da commit 7cb8d6a
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 43 deletions.
2 changes: 1 addition & 1 deletion script/vendorize_deps
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cd src/main/clojure/cljs

DJSON_RELEASE="2.4.0"
TRANSIT_RELEASE="1.0.329"
TREADER_RELEASE="1.3.7"
TREADER_RELEASE="1.4.2"

rm -rf data.json
git clone -b "v$DJSON_RELEASE" --depth 1 [email protected]:clojure/data.json.git
Expand Down
37 changes: 24 additions & 13 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,11 @@
(-hash [o]
(goog/getUid o)))

(extend-type symbol
IHash
(-hash [o]
(hash (.toString o))))

;;this is primitive because & emits call to array-seq
(defn inc
"Returns a number one greater than num."
Expand Down Expand Up @@ -2302,10 +2307,11 @@ reduces them without incurring seq initialization"
"Return true if the seq function is supported for s"
[s]
(or
(nil? s)
(satisfies? ISeqable s)
(array? s)
(string? s)))
(nil? s)
(satisfies? ISeqable s)
(js-iterable? s)
(array? s)
(string? s)))

(defn boolean
"Coerce to boolean"
Expand Down Expand Up @@ -7828,15 +7834,13 @@ reduces them without incurring seq initialization"

(inode-lookup [inode shift hash key not-found]
(let [idx (hash-collision-node-find-index arr cnt key)]
(cond (< idx 0) not-found
(key-test key (aget arr idx)) (aget arr (inc idx))
:else not-found)))
(cond (< idx 0) not-found
:else (aget arr (inc idx)))))

(inode-find [inode shift hash key not-found]
(let [idx (hash-collision-node-find-index arr cnt key)]
(cond (< idx 0) not-found
(key-test key (aget arr idx)) (MapEntry. (aget arr idx) (aget arr (inc idx)) nil)
:else not-found)))
(cond (< idx 0) not-found
:else (MapEntry. (aget arr idx) (aget arr (inc idx)) nil))))

(inode-seq [inode]
(create-inode-seq arr))
Expand Down Expand Up @@ -11883,10 +11887,17 @@ reduces them without incurring seq initialization"
x))

(defn test
"test [v] finds fn at key :test in var metadata and calls it,
presuming failure will throw exception"
"test [v] - if var, finds fn at key :test in var metadata, if function, finds
special test property. Calls it, presuming failure will throw exception.
Examples:
(test my-fn) ;; :ok
(test #'my-fn) ;; :ok"
[v]
(let [f (.-cljs$lang$test v)]
(let [f (if (instance? Var v)
(-> v meta :test)
(some-> v .-cljs$lang$test))]
(if f
(do (f) :ok)
:no-test)))
Expand Down
16 changes: 14 additions & 2 deletions src/main/clojure/cljs/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
:protocol-impl-recur-with-target true
:single-segment-namespace true
:munged-namespace true
:js-used-as-alias true
:ns-var-clash true
:non-dynamic-earmuffed-var true
:extend-type-invalid-method-shape true
Expand Down Expand Up @@ -439,6 +440,10 @@
(str "Namespace " name " contains a reserved JavaScript keyword,"
" the corresponding Google Closure namespace will be munged to " munged)))

(defmethod error-message :js-used-as-alias
[warning-type {:keys [spec] :as info}]
(str "In " (pr-str spec) ", the alias name js is reserved for JavaScript interop"))

(defmethod error-message :ns-var-clash
[warning-type {:keys [ns var] :as info}]
(str "Namespace " ns " clashes with var " var))
Expand Down Expand Up @@ -2994,6 +2999,9 @@
lib' ((alias-type @aliases) alias)]
(when (and (some? lib') (not= lib lib'))
(throw (error env (parse-ns-error-msg spec ":as alias must be unique"))))
(when (= alias 'js)
(when-not (= lib (get-in @aliases [(if macros? :fns :macros) 'js])) ; warn only once
(warning :js-used-as-alias env {:spec spec})))
(swap! aliases
update-in [alias-type]
conj [alias lib] (when js-module-provides [js-module-provides lib]))))
Expand Down Expand Up @@ -3946,8 +3954,12 @@
:cljs [(identical? "clojure.repl" nstr) (find-macros-ns 'cljs.repl)])
#?@(:clj [(.contains nstr ".") (find-ns (symbol nstr))]
:cljs [(goog.string/contains nstr ".") (find-macros-ns (symbol nstr))])
:else (some-> env :ns :require-macros (get (symbol nstr)) #?(:clj find-ns
:cljs find-macros-ns)))))
:else
(or (some-> env :ns :require-macros (get (symbol nstr)) #?(:clj find-ns
:cljs find-macros-ns))
;; single segment namespace case
#?(:clj (find-ns (symbol nstr))
:cljs (find-macros-ns (symbol nstr)))))))

(defn get-expander* [sym env]
(when-not (or (some? (gets env :locals sym)) ; locals hide macros
Expand Down
71 changes: 56 additions & 15 deletions src/main/clojure/cljs/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ present"
inits)))))

(defn default-main
"Default handler for the --main flag. Will start REPL, invoke -main with the
supplied arguments."
[repl-env {:keys [main script args repl-env-options options inits] :as cfg}]
(let [opts (cond-> options
(not (:output-dir options))
Expand Down Expand Up @@ -432,7 +434,9 @@ present"

(defn- main-opt
"Call the -main function from a namespace with string arguments from
the command line."
the command line. Can be customized with ::cljs.cli/main fn entry in
the map returned by cljs.repl/IReplEnvOptions. For default behavior
see default-main."
[repl-env [_ ns & args] cfg]
((::main (repl/repl-options (repl-env)) default-main)
repl-env (merge cfg {:main ns :args args})))
Expand All @@ -448,6 +452,10 @@ present"
(println (help-str repl-env)))

(defn- script-opt
"If no main option was given (compile, repl, main), handles running in
'script' mode. Can be customized with ::cljs.cli/main fn entry in
the map returned by cljs.repl/IReplEnvOptions. For default behavior see
default-main."
[repl-env [path & args] cfg]
((::main (repl/repl-options (repl-env)) default-main)
repl-env (merge cfg {:script path :args args})))
Expand Down Expand Up @@ -538,38 +546,57 @@ present"
(serve-opt repl-env args cfg)))))

(defn- compile-opt
"Handle the compile flag. Custom compilation is possible by providing
:cljs.cli/compile fn in the map returned by cljs.repl/IReplEnvOptions.
For default behavior see default-compile."
[repl-env [_ ns & args] cfg]
((::compile (repl/-repl-options (repl-env)) default-compile)
repl-env (merge cfg {:args args :ns ns})))

(defn get-options [commands k]
(if (= :all k)
(defn get-options
"Given a commands map and a phase (:init or :main), return all flags
which can be handled as a set. If phase is :all will return the entire
flag set (:init + :main)."
[commands phase]
(if (= :all phase)
(into (get-options commands :main) (get-options commands :init))
(-> (get commands (keyword (str (name k) "-dispatch")))
(-> (get commands (keyword (str (name phase) "-dispatch")))
keys set)))

(defn bool-init-options [commands]
(defn get-flags-set
"See get-options, this just provides a better name."
[commands phase]
(get-options commands phase))

(defn bool-init-options
[commands]
(reduce
(fn [ret [flags config]]
(cond-> ret
(= "bool" (:arg config))
(into flags)))
#{} (:init commands)))

(defn dispatch? [commands k opt]
(contains? (get-options commands k) opt))
(defn dispatch?
"Given a commands map, a phase (:init or :main) and a command line flag,
return true if the flag has a handler."
[commands phase opt]
(contains? (get-flags-set commands phase) opt))

(defn add-commands
"Given commands map (see below), create a commands map with :init-dispatch
and :main-dispatch keys where short and long arguments are mapped individually
to their processing fn."
([commands]
(add-commands {:main-dispatch nil :init-dispatch nil} commands))
([commands {:keys [groups main init]}]
(letfn [(merge-dispatch [st k options]
(update-in st [k]
(letfn [(merge-dispatch [commands dispatch-key options]
(update-in commands [dispatch-key]
(fn [m]
(reduce
(fn [ret [cs csm]]
(fn [ret [flag-names flag-config]]
(merge ret
(zipmap cs (repeat (:fn csm)))))
(zipmap flag-names (repeat (:fn flag-config)))))
m options))))]
(-> commands
(update-in [:groups] merge groups)
Expand All @@ -578,7 +605,12 @@ present"
(merge-dispatch :init-dispatch init)
(merge-dispatch :main-dispatch main)))))

(def default-commands
(def ^{:doc "Default commands for ClojureScript REPLs. :groups are to support
printing organized output for --help. a :main option must come at the end, they
specify things like running a -main fn, compile, repl, or web serving. Sometimes
:main options can be used together (i.e. --compile --repl), but this is not
generic - the combinations must be explicitly supported"}
default-commands
(add-commands
{:groups {::main&compile {:desc "init options"
:pseudos
Expand Down Expand Up @@ -662,9 +694,14 @@ present"
["-h" "--help" "-?"] {:fn help-opt
:doc "Print this help message and exit"}}}))

(defn normalize [commands args]
(defn normalize
"Given a commands map (flag + value -> option processor fn) and the sequence of
command line arguments passed to the process, normalize it. Boolean flags don't
need to specify anything, insert the implied trues and return the normalized
command line arguments."
[commands args]
(letfn [(normalize* [args*]
(if (not (contains? (get-options commands :main) (first args*)))
(if (not (contains? (get-flags-set commands :main) (first args*)))
(let [pred (complement (bool-init-options commands))
[pre post] ((juxt #(take-while pred %)
#(drop-while pred %))
Expand All @@ -685,7 +722,11 @@ present"
args'
(recur args' (normalize* args'))))))

(defn merged-commands [repl-env]
(defn merged-commands
"Given a repl environment combine the default commands with the custom
REPL commands. Commands are a mapping from a command line argument
(flag + value) to a function to handle that particular flag + value."
[repl-env]
(add-commands default-commands
(::commands (repl/repl-options (repl-env)))))

Expand Down
6 changes: 6 additions & 0 deletions src/main/clojure/cljs/closure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@

(def lang-level
[:ecmascript3 :ecmascript5 :ecmascript5-strict :ecmascript6 :ecmascript6-strict
:ecmascript8
:ecmascript-2015 :ecmascript-2016 :ecmascript-2017 :ecmascript-2018
:ecmascript-2019 :ecmascript-2020 :ecmascript-2021 :ecmascript-next
:no-transpile])
Expand All @@ -251,6 +252,7 @@
:ecmascript5-strict CompilerOptions$LanguageMode/ECMASCRIPT5_STRICT
:ecmascript6 CompilerOptions$LanguageMode/ECMASCRIPT_2015 ;; (deprecated and remapped)
:ecmascript6-strict CompilerOptions$LanguageMode/ECMASCRIPT_2015 ;; (deprecated and remapped)
:ecmascript8 CompilerOptions$LanguageMode/ECMASCRIPT_2017
:ecmascript-2015 CompilerOptions$LanguageMode/ECMASCRIPT_2015
:ecmascript-2016 CompilerOptions$LanguageMode/ECMASCRIPT_2016
:ecmascript-2017 CompilerOptions$LanguageMode/ECMASCRIPT_2017
Expand Down Expand Up @@ -3083,6 +3085,10 @@
[(-compile (io/resource "cljs/nodejs.cljs")
(assoc opts :output-file "nodejs.js"))]))
deps/dependency-order
;; NOTE: :preloads are compiled *after*
;; user specified inputs. Thus user code cannot
;; depend on anything (i.e. fn/macros) defined
;; in preloads via global access pattern
(add-preloads opts)
remove-goog-base
add-goog-base
Expand Down
8 changes: 5 additions & 3 deletions src/main/clojure/cljs/vendor/clojure/tools/reader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
java.util.regex.Pattern
(java.util List LinkedList)))

(set! *warn-on-reflection* true)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -759,7 +761,7 @@
(ns-name *ns*)

(= \: (first token))
(some-> token (subs 1) parse-symbol second' symbol resolve-ns ns-name)
(some-> token (subs 1) parse-symbol second' symbol resolve-ns)

:else
(some-> token parse-symbol second'))]
Expand Down Expand Up @@ -1022,12 +1024,12 @@
([] (read+string (source-logging-push-back-reader *in*)))
([stream] (read+string stream true nil))
([^SourceLoggingPushbackReader stream eof-error? eof-value]
(let [^StringBuilder buf (doto (:buffer @(.source-log-frames stream)) (.setLength 0))
(let [^StringBuilder buf (doto ^StringBuilder (:buffer @(.source-log-frames stream)) (.setLength 0))
o (log-source stream (read stream eof-error? eof-value))
s (.trim (str buf))]
[o s]))
([opts ^SourceLoggingPushbackReader stream]
(let [^StringBuilder buf (doto (:buffer @(.source-log-frames stream)) (.setLength 0))
(let [^StringBuilder buf (doto ^StringBuilder (:buffer @(.source-log-frames stream)) (.setLength 0))
o (log-source stream (read opts stream))
s (.trim (str buf))]
[o s])))
6 changes: 3 additions & 3 deletions src/test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@
IDeref
(-deref [_]
(:x @a))

ISwap
(-swap! [o f]
(:x (swap! a update :x f)))
Expand All @@ -2016,7 +2016,7 @@
(:x (swap! a update :x f x y)))
(-swap! [o f x y zs]
(:x (swap! a #(apply update % :x f x y zs))))

IReset
(-reset! [o new-value]
(:x (swap! a assoc :x new-value))))]
Expand All @@ -2031,7 +2031,7 @@
(is (= 11 @c))
(is (= 0 (reset! c 0)))
(is (= 0 @c))

(is (= [0 1] (swap-vals! c inc)))
(is (= 1 @c))
(is (= [1 2] (swap-vals! c + 1)))
Expand Down
9 changes: 9 additions & 0 deletions src/test/cljs/cljs/hashing_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@
(is (= (hash 0.5) (hash 0.5)))
(is (= (hash -0.32553251) (hash -0.32553251)))
(is (= (hash -0.0000032553251) (hash -0.0000032553251)))))

(deftest test-cljs-3290
(testing "JS Symbol hash"
(let [s (.for js/Symbol "foo")]
(is (number? (hash s)))
(is (== (hash s) (hash s)))
(is (not (== (hash s) (hash (.for js/Symbol "bar")))))
(let [m {s 2}]
(is (== 2 (get m s)))))))
8 changes: 6 additions & 2 deletions src/test/cljs/cljs/macro_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

(ns cljs.macro-test
(:refer-clojure :exclude [==])
(:require [cljs.test :refer-macros [deftest is]])
(:require [cljs.test :as test :refer-macros [deftest is]])
(:use-macros [cljs.macro-test.macros :only [== sm-cljs-3027]])
(:require-macros [cljs.macro-test.cljs2852]))
(:require-macros [cljs.macro-test.cljs2852]
[single-seg-macros]))

(deftest test-macros
(is (= (== 1 1) 2)))
Expand All @@ -31,3 +32,6 @@

(deftest test-cljs-3027
(is (= {"a" "b"} (sm-cljs-3027))))

(deftest test-cljs-3413
(is (= 5 (single-seg-macros/test-macro 2 3))))
2 changes: 1 addition & 1 deletion src/test/cljs/cljs/ns_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@

(deftest test-cljs-3399
(is (= ::fake/foo :fake.ns/foo)
(= `fake/foo `fake.ns/foo)))
(is (= `fake/foo 'fake.ns/foo))))
Loading

0 comments on commit 7cb8d6a

Please sign in to comment.