clementine 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +4 -2
  3. data/LICENSE.txt +22 -0
  4. data/README.md +11 -6
  5. data/Rakefile +22 -0
  6. data/clementine.gemspec +2 -1
  7. data/ext/clojure-clojurescript-bef56a7/.gitignore +13 -0
  8. data/ext/clojure-clojurescript-bef56a7/Clojurescript.iml +12 -0
  9. data/ext/clojure-clojurescript-bef56a7/README.md +29 -0
  10. data/ext/clojure-clojurescript-bef56a7/benchmark/cljs/benchmark_runner.cljs +155 -0
  11. data/ext/clojure-clojurescript-bef56a7/bin/cljsc +21 -0
  12. data/ext/clojure-clojurescript-bef56a7/bin/cljsc.bat +18 -0
  13. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/bin/cljsc.clj +0 -0
  14. data/ext/clojure-clojurescript-bef56a7/devnotes/README.org +35 -0
  15. data/ext/clojure-clojurescript-bef56a7/devnotes/bcrepl.org +13 -0
  16. data/ext/clojure-clojurescript-bef56a7/devnotes/cljs.org +500 -0
  17. data/ext/clojure-clojurescript-bef56a7/devnotes/corelib.org +583 -0
  18. data/ext/clojure-clojurescript-bef56a7/devnotes/day1.org +203 -0
  19. data/ext/clojure-clojurescript-bef56a7/devnotes/day2.org +44 -0
  20. data/ext/clojure-clojurescript-bef56a7/devnotes/talk.org +126 -0
  21. data/ext/clojure-clojurescript-bef56a7/devnotes/testing +13 -0
  22. data/ext/clojure-clojurescript-bef56a7/devnotes/todo.org +121 -0
  23. data/ext/clojure-clojurescript-bef56a7/epl-v10.html +261 -0
  24. data/ext/clojure-clojurescript-bef56a7/pom.template.xml +88 -0
  25. data/ext/clojure-clojurescript-bef56a7/samples/dom/.gitignore +2 -0
  26. data/ext/clojure-clojurescript-bef56a7/samples/dom/src/dom/test.cljs +48 -0
  27. data/ext/clojure-clojurescript-bef56a7/samples/dom/test.html +30 -0
  28. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/.gitignore +2 -0
  29. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/README.md +53 -0
  30. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/externed-lib.js +7 -0
  31. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/externs.js +3 -0
  32. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-extern.html +14 -0
  33. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-js-dev.html +18 -0
  34. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-js.html +17 -0
  35. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/my-external-lib.js +3 -0
  36. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/src/hello-js/core.cljs +9 -0
  37. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/src/hello-js/extern-example.cljs +5 -0
  38. data/ext/clojure-clojurescript-bef56a7/samples/hello/.gitignore +2 -0
  39. data/ext/clojure-clojurescript-bef56a7/samples/hello/README.md +34 -0
  40. data/ext/clojure-clojurescript-bef56a7/samples/hello/hello-dev.html +18 -0
  41. data/ext/clojure-clojurescript-bef56a7/samples/hello/hello.html +13 -0
  42. data/ext/clojure-clojurescript-bef56a7/samples/hello/src/hello/core.cljs +8 -0
  43. data/ext/clojure-clojurescript-bef56a7/samples/hello/src/hello/foo/bar.cljs +4 -0
  44. data/ext/clojure-clojurescript-bef56a7/samples/nodehello.cljs +18 -0
  45. data/ext/clojure-clojurescript-bef56a7/samples/nodels.cljs +17 -0
  46. data/ext/clojure-clojurescript-bef56a7/samples/repl/.gitignore +2 -0
  47. data/ext/clojure-clojurescript-bef56a7/samples/repl/README.md +101 -0
  48. data/ext/clojure-clojurescript-bef56a7/samples/repl/index.html +27 -0
  49. data/ext/clojure-clojurescript-bef56a7/samples/repl/src/repl/test.cljs +73 -0
  50. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/.gitignore +2 -0
  51. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/README.md +42 -0
  52. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/index-advanced.html +80 -0
  53. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/index.html +88 -0
  54. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/reset.css +48 -0
  55. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/anneal.cljs +66 -0
  56. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/core.cljs +307 -0
  57. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/dom-helpers.cljs +95 -0
  58. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/layout.cljs +100 -0
  59. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/leaderboard.cljs +40 -0
  60. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/radial.cljs +91 -0
  61. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/showgraph.cljs +121 -0
  62. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/timeline.cljs +39 -0
  63. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/style.css +301 -0
  64. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/test_data.txt +1 -0
  65. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/tweet_maps.txt +1 -0
  66. data/ext/clojure-clojurescript-bef56a7/script/benchmark +30 -0
  67. data/ext/clojure-clojurescript-bef56a7/script/bootstrap +70 -0
  68. data/ext/clojure-clojurescript-bef56a7/script/browser-repl +16 -0
  69. data/ext/clojure-clojurescript-bef56a7/script/build +59 -0
  70. data/ext/clojure-clojurescript-bef56a7/script/clean +5 -0
  71. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/google-closure-library-third-party.pom.template +59 -0
  72. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/google-closure-library.pom.template +54 -0
  73. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/make-closure-library-jars.sh +87 -0
  74. data/ext/clojure-clojurescript-bef56a7/script/compile +41 -0
  75. data/ext/clojure-clojurescript-bef56a7/script/repl +13 -0
  76. data/ext/clojure-clojurescript-bef56a7/script/repl.bat +13 -0
  77. data/ext/clojure-clojurescript-bef56a7/script/repljs +15 -0
  78. data/ext/clojure-clojurescript-bef56a7/script/repljs.bat +14 -0
  79. data/ext/clojure-clojurescript-bef56a7/script/test +38 -0
  80. data/ext/clojure-clojurescript-bef56a7/script/test-compile +30 -0
  81. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/analyzer.clj +975 -0
  82. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/closure.clj +173 -73
  83. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/compiler.clj +1081 -0
  84. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/core.clj +1158 -0
  85. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/repl.clj +51 -25
  86. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/browser.clj +258 -0
  87. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/reflect.clj +75 -0
  88. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/repl/rhino.clj +6 -5
  89. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/server.clj +173 -0
  90. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/tagged_literals.clj +30 -0
  91. data/ext/clojure-clojurescript-bef56a7/src/cljs/cljs/core.cljs +7197 -0
  92. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejs.cljs +1 -1
  93. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejs_externs.js +0 -0
  94. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejscli.cljs +1 -1
  95. data/ext/clojure-clojurescript-bef56a7/src/cljs/cljs/reader.cljs +551 -0
  96. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/dom.cljs +59 -13
  97. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/event.cljs +0 -0
  98. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/net.cljs +8 -7
  99. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/repl.cljs +2 -2
  100. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/core/reducers.cljs +298 -0
  101. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/data.cljs +162 -0
  102. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/reflect.cljs +48 -0
  103. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/set.cljs +0 -0
  104. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/string.cljs +4 -10
  105. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/walk.cljs +0 -0
  106. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/zip.cljs +0 -0
  107. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/binding_test.cljs +7 -0
  108. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/binding_test_other_ns.cljs +3 -0
  109. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/core_test.cljs +1678 -0
  110. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/import_test.cljs +11 -0
  111. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/import_test/foo.cljs +5 -0
  112. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/letfn_test.cljs +19 -0
  113. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/macro_test.cljs +6 -0
  114. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/macro_test/macros.clj +5 -0
  115. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test.cljs +14 -0
  116. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test/bar.cljs +3 -0
  117. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test/foo.cljs +7 -0
  118. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/reader_test.cljs +124 -0
  119. data/ext/clojure-clojurescript-bef56a7/test/cljs/clojure/data_test.cljs +22 -0
  120. data/ext/clojure-clojurescript-bef56a7/test/cljs/clojure/string_test.cljs +97 -0
  121. data/ext/clojure-clojurescript-bef56a7/test/cljs/foo/ns_shadow_test.cljs +9 -0
  122. data/ext/clojure-clojurescript-bef56a7/test/cljs/test_runner.cljs +26 -0
  123. data/lib/clementine.rb +3 -24
  124. data/lib/clementine/clojurescript_engine.rb +9 -48
  125. data/lib/clementine/clojurescript_engine/base.rb +15 -0
  126. data/lib/clementine/clojurescript_engine/jruby.rb +46 -0
  127. data/lib/clementine/{clojurescript_engine_mri.rb → clojurescript_engine/mri.rb} +17 -10
  128. data/lib/clementine/version.rb +1 -1
  129. data/test/clojurescript_engine_test.rb +36 -14
  130. metadata +177 -83
  131. data/vendor/assets/lib/clojure.jar +0 -0
  132. data/vendor/assets/lib/compiler.jar +0 -0
  133. data/vendor/assets/lib/goog.jar +0 -0
  134. data/vendor/assets/lib/js.jar +0 -0
  135. data/vendor/assets/src/clj/cljs/compiler.clj +0 -1341
  136. data/vendor/assets/src/clj/cljs/core.clj +0 -702
  137. data/vendor/assets/src/clj/cljs/repl/browser.clj +0 -341
  138. data/vendor/assets/src/cljs/cljs/core.cljs +0 -3330
  139. data/vendor/assets/src/cljs/cljs/reader.cljs +0 -360
@@ -0,0 +1,1158 @@
1
+ ; Copyright (c) Rich Hickey. All rights reserved.
2
+ ; The use and distribution terms for this software are covered by the
3
+ ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4
+ ; which can be found in the file epl-v10.html at the root of this distribution.
5
+ ; By using this software in any fashion, you are agreeing to be bound by
6
+ ; the terms of this license.
7
+ ; You must not remove this notice, or any other, from this software.
8
+
9
+ (ns cljs.core
10
+ (:refer-clojure :exclude [-> ->> .. amap and areduce alength aclone assert binding bound-fn case comment cond condp
11
+ declare definline definterface defmethod defmulti defn defn- defonce
12
+ defprotocol defrecord defstruct deftype delay destructure doseq dosync dotimes doto
13
+ extend-protocol extend-type fn for future gen-class gen-interface
14
+ if-let if-not import io! lazy-cat lazy-seq let letfn locking loop
15
+ memfn ns or proxy proxy-super pvalues refer-clojure reify sync time
16
+ when when-first when-let when-not while with-bindings with-in-str
17
+ with-loading-context with-local-vars with-open with-out-str with-precision with-redefs
18
+ satisfies? identical? true? false? nil? str get
19
+
20
+ aget aset
21
+ + - * / < <= > >= == zero? pos? neg? inc dec max min mod
22
+ bit-and bit-and-not bit-clear bit-flip bit-not bit-or bit-set
23
+ bit-test bit-shift-left bit-shift-right bit-xor])
24
+ (:require clojure.walk))
25
+
26
+ (alias 'core 'clojure.core)
27
+
28
+ (defmacro import-macros [ns [& vars]]
29
+ (core/let [ns (find-ns ns)
30
+ vars (map #(ns-resolve ns %) vars)
31
+ syms (map (core/fn [^clojure.lang.Var v] (core/-> v .sym (with-meta {:macro true}))) vars)
32
+ defs (map (core/fn [sym var]
33
+ `(def ~sym (deref ~var))) syms vars)]
34
+ `(do ~@defs
35
+ :imported)))
36
+
37
+ (import-macros clojure.core
38
+ [-> ->> .. and assert comment cond
39
+ declare defn defn-
40
+ doto
41
+ extend-protocol fn for
42
+ if-let if-not letfn
43
+ memfn or
44
+ when when-first when-let when-not while])
45
+
46
+ (defmacro ^{:private true} assert-args [fnname & pairs]
47
+ `(do (when-not ~(first pairs)
48
+ (throw (IllegalArgumentException.
49
+ ~(core/str fnname " requires " (second pairs)))))
50
+ ~(core/let [more (nnext pairs)]
51
+ (when more
52
+ (list* `assert-args fnname more)))))
53
+
54
+ (defn destructure [bindings]
55
+ (core/let [bents (partition 2 bindings)
56
+ pb (fn pb [bvec b v]
57
+ (core/let [pvec
58
+ (fn [bvec b val]
59
+ (core/let [gvec (gensym "vec__")]
60
+ (core/loop [ret (-> bvec (conj gvec) (conj val))
61
+ n 0
62
+ bs b
63
+ seen-rest? false]
64
+ (if (seq bs)
65
+ (core/let [firstb (first bs)]
66
+ (cond
67
+ (= firstb '&) (recur (pb ret (second bs) (list `nthnext gvec n))
68
+ n
69
+ (nnext bs)
70
+ true)
71
+ (= firstb :as) (pb ret (second bs) gvec)
72
+ :else (if seen-rest?
73
+ (throw (new Exception "Unsupported binding form, only :as can follow & parameter"))
74
+ (recur (pb ret firstb (list `nth gvec n nil))
75
+ (core/inc n)
76
+ (next bs)
77
+ seen-rest?))))
78
+ ret))))
79
+ pmap
80
+ (fn [bvec b v]
81
+ (core/let [gmap (gensym "map__")
82
+ defaults (:or b)]
83
+ (core/loop [ret (-> bvec (conj gmap) (conj v)
84
+ (conj gmap) (conj `(if (seq? ~gmap) (apply hash-map ~gmap) ~gmap))
85
+ ((fn [ret]
86
+ (if (:as b)
87
+ (conj ret (:as b) gmap)
88
+ ret))))
89
+ bes (reduce
90
+ (fn [bes entry]
91
+ (reduce #(assoc %1 %2 ((val entry) %2))
92
+ (dissoc bes (key entry))
93
+ ((key entry) bes)))
94
+ (dissoc b :as :or)
95
+ {:keys #(keyword (core/str %)), :strs core/str, :syms #(list `quote %)})]
96
+ (if (seq bes)
97
+ (core/let [bb (key (first bes))
98
+ bk (val (first bes))
99
+ has-default (contains? defaults bb)]
100
+ (recur (pb ret bb (if has-default
101
+ (list `get gmap bk (defaults bb))
102
+ (list `get gmap bk)))
103
+ (next bes)))
104
+ ret))))]
105
+ (cond
106
+ (symbol? b) (-> bvec (conj b) (conj v))
107
+ (vector? b) (pvec bvec b v)
108
+ (map? b) (pmap bvec b v)
109
+ :else (throw (new Exception (core/str "Unsupported binding form: " b))))))
110
+ process-entry (fn [bvec b] (pb bvec (first b) (second b)))]
111
+ (if (every? symbol? (map first bents))
112
+ bindings
113
+ (reduce process-entry [] bents))))
114
+
115
+ (defmacro let
116
+ "binding => binding-form init-expr
117
+
118
+ Evaluates the exprs in a lexical context in which the symbols in
119
+ the binding-forms are bound to their respective init-exprs or parts
120
+ therein."
121
+ [bindings & body]
122
+ (assert-args
123
+ (vector? bindings) "a vector for its binding"
124
+ (even? (count bindings)) "an even number of forms in binding vector")
125
+ `(let* ~(destructure bindings) ~@body))
126
+
127
+ (defmacro loop
128
+ "Evaluates the exprs in a lexical context in which the symbols in
129
+ the binding-forms are bound to their respective init-exprs or parts
130
+ therein. Acts as a recur target."
131
+ [bindings & body]
132
+ (assert-args
133
+ (vector? bindings) "a vector for its binding"
134
+ (even? (count bindings)) "an even number of forms in binding vector")
135
+ (let [db (destructure bindings)]
136
+ (if (= db bindings)
137
+ `(loop* ~bindings ~@body)
138
+ (let [vs (take-nth 2 (drop 1 bindings))
139
+ bs (take-nth 2 bindings)
140
+ gs (map (fn [b] (if (symbol? b) b (gensym))) bs)
141
+ bfs (reduce (fn [ret [b v g]]
142
+ (if (symbol? b)
143
+ (conj ret g v)
144
+ (conj ret g v b g)))
145
+ [] (map vector bs vs gs))]
146
+ `(let ~bfs
147
+ (loop* ~(vec (interleave gs gs))
148
+ (let ~(vec (interleave bs gs))
149
+ ~@body)))))))
150
+
151
+ (def fast-path-protocols
152
+ "protocol fqn -> [partition number, bit]"
153
+ (zipmap (map #(symbol "cljs.core" (core/str %))
154
+ '[IFn ICounted IEmptyableCollection ICollection IIndexed ASeq ISeq INext
155
+ ILookup IAssociative IMap IMapEntry ISet IStack IVector IDeref
156
+ IDerefWithTimeout IMeta IWithMeta IReduce IKVReduce IEquiv IHash
157
+ ISeqable ISequential IList IRecord IReversible ISorted IPrintable IWriter
158
+ IPrintWithWriter IPending IWatchable IEditableCollection ITransientCollection
159
+ ITransientAssociative ITransientMap ITransientVector ITransientSet
160
+ IMultiFn IChunkedSeq IChunkedNext IComparable])
161
+ (iterate (fn [[p b]]
162
+ (if (core/== 2147483648 b)
163
+ [(core/inc p) 1]
164
+ [p (core/bit-shift-left b 1)]))
165
+ [0 1])))
166
+
167
+ (def fast-path-protocol-partitions-count
168
+ "total number of partitions"
169
+ (let [c (count fast-path-protocols)
170
+ m (core/mod c 32)]
171
+ (if (core/zero? m)
172
+ (core/quot c 32)
173
+ (core/inc (core/quot c 32)))))
174
+
175
+ (defmacro str [& xs]
176
+ (let [strs (->> (repeat (count xs) "cljs.core.str(~{})")
177
+ (interpose ",")
178
+ (apply core/str))]
179
+ (concat (list 'js* (core/str "[" strs "].join('')")) xs)))
180
+
181
+ (defn bool-expr [e]
182
+ (vary-meta e assoc :tag 'boolean))
183
+
184
+ (defmacro nil? [x]
185
+ `(coercive-= ~x nil))
186
+
187
+ ;; internal - do not use.
188
+ (defmacro coercive-not [x]
189
+ (bool-expr (list 'js* "(!~{})" x)))
190
+
191
+ ;; internal - do not use.
192
+ (defmacro coercive-not= [x y]
193
+ (bool-expr (list 'js* "(~{} != ~{})" x y)))
194
+
195
+ ;; internal - do not use.
196
+ (defmacro coercive-= [x y]
197
+ (bool-expr (list 'js* "(~{} == ~{})" x y)))
198
+
199
+ (defmacro true? [x]
200
+ (bool-expr (list 'js* "~{} === true" x)))
201
+
202
+ (defmacro false? [x]
203
+ (bool-expr (list 'js* "~{} === false" x)))
204
+
205
+ (defmacro undefined? [x]
206
+ (bool-expr (list 'js* "(void 0 === ~{})" x)))
207
+
208
+ (defmacro identical? [a b]
209
+ (bool-expr (list 'js* "(~{} === ~{})" a b)))
210
+
211
+ (defmacro aget
212
+ ([a i]
213
+ (list 'js* "(~{}[~{}])" a i))
214
+ ([a i & idxs]
215
+ (let [astr (apply core/str (repeat (count idxs) "[~{}]"))]
216
+ `(~'js* ~(core/str "(~{}[~{}]" astr ")") ~a ~i ~@idxs))))
217
+
218
+ (defmacro aset [a i v]
219
+ (list 'js* "(~{}[~{}] = ~{})" a i v))
220
+
221
+ (defmacro +
222
+ ([] 0)
223
+ ([x] x)
224
+ ([x y] (list 'js* "(~{} + ~{})" x y))
225
+ ([x y & more] `(+ (+ ~x ~y) ~@more)))
226
+
227
+ (defmacro -
228
+ ([x] (list 'js* "(- ~{})" x))
229
+ ([x y] (list 'js* "(~{} - ~{})" x y))
230
+ ([x y & more] `(- (- ~x ~y) ~@more)))
231
+
232
+ (defmacro *
233
+ ([] 1)
234
+ ([x] x)
235
+ ([x y] (list 'js* "(~{} * ~{})" x y))
236
+ ([x y & more] `(* (* ~x ~y) ~@more)))
237
+
238
+ (defmacro /
239
+ ([x] `(/ 1 ~x))
240
+ ([x y] (list 'js* "(~{} / ~{})" x y))
241
+ ([x y & more] `(/ (/ ~x ~y) ~@more)))
242
+
243
+ (defmacro <
244
+ ([x] true)
245
+ ([x y] (bool-expr (list 'js* "(~{} < ~{})" x y)))
246
+ ([x y & more] `(and (< ~x ~y) (< ~y ~@more))))
247
+
248
+ (defmacro <=
249
+ ([x] true)
250
+ ([x y] (bool-expr (list 'js* "(~{} <= ~{})" x y)))
251
+ ([x y & more] `(and (<= ~x ~y) (<= ~y ~@more))))
252
+
253
+ (defmacro >
254
+ ([x] true)
255
+ ([x y] (bool-expr (list 'js* "(~{} > ~{})" x y)))
256
+ ([x y & more] `(and (> ~x ~y) (> ~y ~@more))))
257
+
258
+ (defmacro >=
259
+ ([x] true)
260
+ ([x y] (bool-expr (list 'js* "(~{} >= ~{})" x y)))
261
+ ([x y & more] `(and (>= ~x ~y) (>= ~y ~@more))))
262
+
263
+ (defmacro ==
264
+ ([x] true)
265
+ ([x y] (bool-expr (list 'js* "(~{} === ~{})" x y)))
266
+ ([x y & more] `(and (== ~x ~y) (== ~y ~@more))))
267
+
268
+ (defmacro dec [x]
269
+ `(- ~x 1))
270
+
271
+ (defmacro inc [x]
272
+ `(+ ~x 1))
273
+
274
+ (defmacro zero? [x]
275
+ `(== ~x 0))
276
+
277
+ (defmacro pos? [x]
278
+ `(> ~x 0))
279
+
280
+ (defmacro neg? [x]
281
+ `(< ~x 0))
282
+
283
+ (defmacro max
284
+ ([x] x)
285
+ ([x y] (list 'js* "((~{} > ~{}) ? ~{} : ~{})" x y x y))
286
+ ([x y & more] `(max (max ~x ~y) ~@more)))
287
+
288
+ (defmacro min
289
+ ([x] x)
290
+ ([x y] (list 'js* "((~{} < ~{}) ? ~{} : ~{})" x y x y))
291
+ ([x y & more] `(min (min ~x ~y) ~@more)))
292
+
293
+ (defmacro mod [num div]
294
+ (list 'js* "(~{} % ~{})" num div))
295
+
296
+ (defmacro bit-not [x]
297
+ (list 'js* "(~ ~{})" x))
298
+
299
+ (defmacro bit-and
300
+ ([x y] (list 'js* "(~{} & ~{})" x y))
301
+ ([x y & more] `(bit-and (bit-and ~x ~y) ~@more)))
302
+
303
+ ;; internal do not use
304
+ (defmacro unsafe-bit-and
305
+ ([x y] (bool-expr (list 'js* "(~{} & ~{})" x y)))
306
+ ([x y & more] `(unsafe-bit-and (unsafe-bit-and ~x ~y) ~@more)))
307
+
308
+ (defmacro bit-or
309
+ ([x y] (list 'js* "(~{} | ~{})" x y))
310
+ ([x y & more] `(bit-or (bit-or ~x ~y) ~@more)))
311
+
312
+ (defmacro bit-xor
313
+ ([x y] (list 'js* "(~{} ^ ~{})" x y))
314
+ ([x y & more] `(bit-xor (bit-xor ~x ~y) ~@more)))
315
+
316
+ (defmacro bit-and-not
317
+ ([x y] (list 'js* "(~{} & ~~{})" x y))
318
+ ([x y & more] `(bit-and-not (bit-and-not ~x ~y) ~@more)))
319
+
320
+ (defmacro bit-clear [x n]
321
+ (list 'js* "(~{} & ~(1 << ~{}))" x n))
322
+
323
+ (defmacro bit-flip [x n]
324
+ (list 'js* "(~{} ^ (1 << ~{}))" x n))
325
+
326
+ (defmacro bit-test [x n]
327
+ (list 'js* "((~{} & (1 << ~{})) != 0)" x n))
328
+
329
+ (defmacro bit-shift-left [x n]
330
+ (list 'js* "(~{} << ~{})" x n))
331
+
332
+ (defmacro bit-shift-right [x n]
333
+ (list 'js* "(~{} >> ~{})" x n))
334
+
335
+ (defmacro bit-shift-right-zero-fill [x n]
336
+ (list 'js* "(~{} >>> ~{})" x n))
337
+
338
+ (defmacro bit-set [x n]
339
+ (list 'js* "(~{} | (1 << ~{}))" x n))
340
+
341
+ ;; internal
342
+ (defmacro mask [hash shift]
343
+ (list 'js* "((~{} >>> ~{}) & 0x01f)" hash shift))
344
+
345
+ ;; internal
346
+ (defmacro bitpos [hash shift]
347
+ (list 'js* "(1 << ~{})" `(mask ~hash ~shift)))
348
+
349
+ ;; internal
350
+ (defmacro caching-hash [coll hash-fn hash-key]
351
+ `(let [h# ~hash-key]
352
+ (if-not (nil? h#)
353
+ h#
354
+ (let [h# (~hash-fn ~coll)]
355
+ (set! ~hash-key h#)
356
+ h#))))
357
+
358
+ (defmacro get
359
+ ([coll k]
360
+ `(-lookup ~coll ~k nil))
361
+ ([coll k not-found]
362
+ `(-lookup ~coll ~k ~not-found)))
363
+
364
+ ;;; internal -- reducers-related macros
365
+
366
+ (defn- do-curried
367
+ [name doc meta args body]
368
+ (let [cargs (vec (butlast args))]
369
+ `(defn ~name ~doc ~meta
370
+ (~cargs (fn [x#] (~name ~@cargs x#)))
371
+ (~args ~@body))))
372
+
373
+ (defmacro ^:private defcurried
374
+ "Builds another arity of the fn that returns a fn awaiting the last
375
+ param"
376
+ [name doc meta args & body]
377
+ (do-curried name doc meta args body))
378
+
379
+ (defn- do-rfn [f1 k fkv]
380
+ `(fn
381
+ ([] (~f1))
382
+ ~(clojure.walk/postwalk
383
+ #(if (sequential? %)
384
+ ((if (vector? %) vec identity)
385
+ (core/remove #{k} %))
386
+ %)
387
+ fkv)
388
+ ~fkv))
389
+
390
+ (defmacro ^:private rfn
391
+ "Builds 3-arity reducing fn given names of wrapped fn and key, and k/v impl."
392
+ [[f1 k] fkv]
393
+ (do-rfn f1 k fkv))
394
+
395
+ ;;; end of reducers macros
396
+
397
+ (defn protocol-prefix [psym]
398
+ (core/str (-> (core/str psym) (.replace \. \$) (.replace \/ \$)) "$"))
399
+
400
+ (def #^:private base-type
401
+ {nil "null"
402
+ 'object "object"
403
+ 'string "string"
404
+ 'number "number"
405
+ 'array "array"
406
+ 'function "function"
407
+ 'boolean "boolean"
408
+ 'default "_"})
409
+
410
+ (defmacro reify [& impls]
411
+ (let [t (gensym "t")
412
+ meta-sym (gensym "meta")
413
+ this-sym (gensym "_")
414
+ locals (keys (:locals &env))
415
+ ns (-> &env :ns :name)
416
+ munge cljs.compiler/munge
417
+ ns-t (list 'js* (core/str (munge ns) "." (munge t)))]
418
+ `(do
419
+ (when (undefined? ~ns-t)
420
+ (deftype ~t [~@locals ~meta-sym]
421
+ IWithMeta
422
+ (~'-with-meta [~this-sym ~meta-sym]
423
+ (new ~t ~@locals ~meta-sym))
424
+ IMeta
425
+ (~'-meta [~this-sym] ~meta-sym)
426
+ ~@impls))
427
+ (new ~t ~@locals nil))))
428
+
429
+ (defmacro this-as
430
+ "Defines a scope where JavaScript's implicit \"this\" is bound to the name provided."
431
+ [name & body]
432
+ `(let [~name (~'js* "this")]
433
+ ~@body))
434
+
435
+ (defn to-property [sym]
436
+ (symbol (core/str "-" sym)))
437
+
438
+ (defmacro extend-type [tsym & impls]
439
+ (let [resolve #(let [ret (:name (cljs.analyzer/resolve-var (dissoc &env :locals) %))]
440
+ (assert ret (core/str "Can't resolve: " %))
441
+ ret)
442
+ impl-map (loop [ret {} s impls]
443
+ (if (seq s)
444
+ (recur (assoc ret (first s) (take-while seq? (next s)))
445
+ (drop-while seq? (next s)))
446
+ ret))
447
+ warn-if-not-protocol #(when-not (= 'Object %)
448
+ (if cljs.analyzer/*cljs-warn-on-undeclared*
449
+ (if-let [var (cljs.analyzer/resolve-existing-var (dissoc &env :locals) %)]
450
+ (do
451
+ (when-not (:protocol-symbol var)
452
+ (cljs.analyzer/warning &env
453
+ (core/str "WARNING: Symbol " % " is not a protocol")))
454
+ (when (and cljs.analyzer/*cljs-warn-protocol-deprecated*
455
+ (-> var :deprecated)
456
+ (not (-> % meta :deprecation-nowarn)))
457
+ (cljs.analyzer/warning &env
458
+ (core/str "WARNING: Protocol " % " is deprecated"))))
459
+ (cljs.analyzer/warning &env
460
+ (core/str "WARNING: Can't resolve protocol symbol " %)))))
461
+ skip-flag (set (-> tsym meta :skip-protocol-flag))]
462
+ (if (base-type tsym)
463
+ (let [t (base-type tsym)
464
+ assign-impls (fn [[p sigs]]
465
+ (warn-if-not-protocol p)
466
+ (let [psym (resolve p)
467
+ pfn-prefix (subs (core/str psym) 0 (clojure.core/inc (.indexOf (core/str psym) "/")))]
468
+ (cons `(aset ~psym ~t true)
469
+ (map (fn [[f & meths :as form]]
470
+ `(aset ~(symbol (core/str pfn-prefix f)) ~t ~(with-meta `(fn ~@meths) (meta form))))
471
+ sigs))))]
472
+ `(do ~@(mapcat assign-impls impl-map)))
473
+ (let [t (resolve tsym)
474
+ prototype-prefix (fn [sym]
475
+ `(.. ~tsym -prototype ~(to-property sym)))
476
+ assign-impls (fn [[p sigs]]
477
+ (warn-if-not-protocol p)
478
+ (let [psym (resolve p)
479
+ pprefix (protocol-prefix psym)]
480
+ (if (= p 'Object)
481
+ (let [adapt-params (fn [[sig & body]]
482
+ (let [[tname & args] sig]
483
+ (list (vec args) (list* 'this-as (vary-meta tname assoc :tag t) body))))]
484
+ (map (fn [[f & meths :as form]]
485
+ `(set! ~(prototype-prefix f)
486
+ ~(with-meta `(fn ~@(map adapt-params meths)) (meta form))))
487
+ sigs))
488
+ (concat (when-not (skip-flag psym)
489
+ [`(set! ~(prototype-prefix pprefix) true)])
490
+ (mapcat (fn [[f & meths :as form]]
491
+ (if (= psym 'cljs.core/IFn)
492
+ (let [adapt-params (fn [[[targ & args :as sig] & body]]
493
+ (let [this-sym (with-meta (gensym "this-sym") {:tag t})]
494
+ `(~(vec (cons this-sym args))
495
+ (this-as ~this-sym
496
+ (let [~targ ~this-sym]
497
+ ~@body)))))
498
+ meths (map adapt-params meths)
499
+ this-sym (with-meta (gensym "this-sym") {:tag t})
500
+ argsym (gensym "args")]
501
+ [`(set! ~(prototype-prefix 'call) ~(with-meta `(fn ~@meths) (meta form)))
502
+ `(set! ~(prototype-prefix 'apply)
503
+ ~(with-meta
504
+ `(fn ~[this-sym argsym]
505
+ (.apply (.-call ~this-sym) ~this-sym
506
+ (.concat (array ~this-sym) (aclone ~argsym))))
507
+ (meta form)))])
508
+ (let [pf (core/str pprefix f)
509
+ adapt-params (fn [[[targ & args :as sig] & body]]
510
+ (cons (vec (cons (vary-meta targ assoc :tag t) args))
511
+ body))]
512
+ (if (vector? (first meths))
513
+ [`(set! ~(prototype-prefix (core/str pf "$arity$" (count (first meths)))) ~(with-meta `(fn ~@(adapt-params meths)) (meta form)))]
514
+ (map (fn [[sig & body :as meth]]
515
+ `(set! ~(prototype-prefix (core/str pf "$arity$" (count sig)))
516
+ ~(with-meta `(fn ~(adapt-params meth)) (meta form))))
517
+ meths)))))
518
+ sigs)))))]
519
+ `(do ~@(mapcat assign-impls impl-map))))))
520
+
521
+ (defn- prepare-protocol-masks [env t impls]
522
+ (let [resolve #(let [ret (:name (cljs.analyzer/resolve-var (dissoc env :locals) %))]
523
+ (assert ret (core/str "Can't resolve: " %))
524
+ ret)
525
+ impl-map (loop [ret {} s impls]
526
+ (if (seq s)
527
+ (recur (assoc ret (first s) (take-while seq? (next s)))
528
+ (drop-while seq? (next s)))
529
+ ret))]
530
+ (if-let [fpp-pbs (seq (keep fast-path-protocols
531
+ (map resolve
532
+ (keys impl-map))))]
533
+ (let [fpps (into #{} (filter (partial contains? fast-path-protocols)
534
+ (map resolve
535
+ (keys impl-map))))
536
+ fpp-partitions (group-by first fpp-pbs)
537
+ fpp-partitions (into {} (map (juxt key (comp (partial map peek) val))
538
+ fpp-partitions))
539
+ fpp-partitions (into {} (map (juxt key (comp (partial reduce core/bit-or) val))
540
+ fpp-partitions))]
541
+ [fpps
542
+ (reduce (fn [ps p]
543
+ (update-in ps [p] (fnil identity 0)))
544
+ fpp-partitions
545
+ (range fast-path-protocol-partitions-count))]))))
546
+
547
+ (defn dt->et
548
+ ([t specs fields] (dt->et t specs fields false))
549
+ ([t specs fields inline]
550
+ (loop [ret [] s specs]
551
+ (if (seq s)
552
+ (recur (-> ret
553
+ (conj (first s))
554
+ (into
555
+ (reduce (fn [v [f sigs]]
556
+ (conj v (vary-meta (cons f (map #(cons (second %) (nnext %)) sigs))
557
+ assoc :cljs.analyzer/type t
558
+ :cljs.analyzer/fields fields
559
+ :protocol-impl true
560
+ :protocol-inline inline)))
561
+ []
562
+ (group-by first (take-while seq? (next s))))))
563
+ (drop-while seq? (next s)))
564
+ ret))))
565
+
566
+ (defn collect-protocols [impls env]
567
+ (->> impls
568
+ (filter symbol?)
569
+ (map #(:name (cljs.analyzer/resolve-var (dissoc env :locals) %)))
570
+ (into #{})))
571
+
572
+ (defmacro deftype [t fields & impls]
573
+ (let [r (:name (cljs.analyzer/resolve-var (dissoc &env :locals) t))
574
+ [fpps pmasks] (prepare-protocol-masks &env t impls)
575
+ protocols (collect-protocols impls &env)
576
+ t (vary-meta t assoc
577
+ :protocols protocols
578
+ :skip-protocol-flag fpps) ]
579
+ (if (seq impls)
580
+ `(do
581
+ (deftype* ~t ~fields ~pmasks)
582
+ (set! (.-cljs$lang$type ~t) true)
583
+ (set! (.-cljs$lang$ctorPrSeq ~t) (fn [this#] (list ~(core/str r))))
584
+ (set! (.-cljs$lang$ctorPrWriter ~t) (fn [this# writer#] (-write writer# ~(core/str r))))
585
+ (extend-type ~t ~@(dt->et t impls fields true))
586
+ ~t)
587
+ `(do
588
+ (deftype* ~t ~fields ~pmasks)
589
+ (set! (.-cljs$lang$type ~t) true)
590
+ (set! (.-cljs$lang$ctorPrSeq ~t) (fn [this#] (list ~(core/str r))))
591
+ (set! (.-cljs$lang$ctorPrWriter ~t) (fn [this# writer#] (-write writer# ~(core/str r))))
592
+ ~t))))
593
+
594
+ (defn- emit-defrecord
595
+ "Do not use this directly - use defrecord"
596
+ [env tagname rname fields impls]
597
+ (let [hinted-fields fields
598
+ fields (vec (map #(with-meta % nil) fields))
599
+ base-fields fields
600
+ fields (conj fields '__meta '__extmap (with-meta '__hash {:mutable true}))]
601
+ (let [gs (gensym)
602
+ ksym (gensym "k")
603
+ impls (concat
604
+ impls
605
+ ['IRecord
606
+ 'IHash
607
+ `(~'-hash [this#] (caching-hash this# ~'hash-imap ~'__hash))
608
+ 'IEquiv
609
+ `(~'-equiv [this# other#]
610
+ (if (and other#
611
+ (identical? (.-constructor this#)
612
+ (.-constructor other#))
613
+ (equiv-map this# other#))
614
+ true
615
+ false))
616
+ 'IMeta
617
+ `(~'-meta [this#] ~'__meta)
618
+ 'IWithMeta
619
+ `(~'-with-meta [this# ~gs] (new ~tagname ~@(replace {'__meta gs} fields)))
620
+ 'ILookup
621
+ `(~'-lookup [this# k#] (-lookup this# k# nil))
622
+ `(~'-lookup [this# ~ksym else#]
623
+ (cond
624
+ ~@(mapcat (fn [f] [`(identical? ~ksym ~(keyword f)) f]) base-fields)
625
+ :else (get ~'__extmap ~ksym else#)))
626
+ 'ICounted
627
+ `(~'-count [this#] (+ ~(count base-fields) (count ~'__extmap)))
628
+ 'ICollection
629
+ `(~'-conj [this# entry#]
630
+ (if (vector? entry#)
631
+ (-assoc this# (-nth entry# 0) (-nth entry# 1))
632
+ (reduce -conj
633
+ this#
634
+ entry#)))
635
+ 'IAssociative
636
+ `(~'-assoc [this# k# ~gs]
637
+ (condp identical? k#
638
+ ~@(mapcat (fn [fld]
639
+ [(keyword fld) (list* `new tagname (replace {fld gs '__hash nil} fields))])
640
+ base-fields)
641
+ (new ~tagname ~@(remove #{'__extmap '__hash} fields) (assoc ~'__extmap k# ~gs) nil)))
642
+ 'IMap
643
+ `(~'-dissoc [this# k#] (if (contains? #{~@(map keyword base-fields)} k#)
644
+ (dissoc (with-meta (into {} this#) ~'__meta) k#)
645
+ (new ~tagname ~@(remove #{'__extmap '__hash} fields)
646
+ (not-empty (dissoc ~'__extmap k#))
647
+ nil)))
648
+ 'ISeqable
649
+ `(~'-seq [this#] (seq (concat [~@(map #(list `vector (keyword %) %) base-fields)]
650
+ ~'__extmap)))
651
+
652
+ 'IPrintWithWriter
653
+ `(~'-pr-writer [this# writer# opts#]
654
+ (let [pr-pair# (fn [keyval#] (pr-sequential-writer writer# pr-writer "" " " "" opts# keyval#))]
655
+ (pr-sequential-writer
656
+ writer# pr-pair# (core/str "#" ~(name rname) "{") ", " "}" opts#
657
+ (concat [~@(map #(list `vector (keyword %) %) base-fields)]
658
+ ~'__extmap))))
659
+ ])
660
+ [fpps pmasks] (prepare-protocol-masks env tagname impls)
661
+ protocols (collect-protocols impls env)
662
+ tagname (vary-meta tagname assoc
663
+ :protocols protocols
664
+ :skip-protocol-flag fpps)]
665
+ `(do
666
+ (~'defrecord* ~tagname ~hinted-fields ~pmasks)
667
+ (extend-type ~tagname ~@(dt->et tagname impls fields true))))))
668
+
669
+ (defn- build-positional-factory
670
+ [rsym rname fields]
671
+ (let [fn-name (symbol (core/str '-> rsym))]
672
+ `(defn ~fn-name
673
+ [~@fields]
674
+ (new ~rname ~@fields))))
675
+
676
+ (defn- build-map-factory
677
+ [rsym rname fields]
678
+ (let [fn-name (symbol (core/str 'map-> rsym))
679
+ ms (gensym)
680
+ ks (map keyword fields)
681
+ getters (map (fn [k] `(~k ~ms)) ks)]
682
+ `(defn ~fn-name
683
+ [~ms]
684
+ (new ~rname ~@getters nil (dissoc ~ms ~@ks)))))
685
+
686
+ (defmacro defrecord [rsym fields & impls]
687
+ (let [r (:name (cljs.analyzer/resolve-var (dissoc &env :locals) rsym))]
688
+ `(let []
689
+ ~(emit-defrecord &env rsym r fields impls)
690
+ (set! (.-cljs$lang$type ~r) true)
691
+ (set! (.-cljs$lang$ctorPrSeq ~r) (fn [this#] (list ~(core/str r))))
692
+ (set! (.-cljs$lang$ctorPrWriter ~r) (fn [this# writer#] (-write writer# ~(core/str r))))
693
+ ~(build-positional-factory rsym r fields)
694
+ ~(build-map-factory rsym r fields)
695
+ ~r)))
696
+
697
+ (defmacro defprotocol [psym & doc+methods]
698
+ (let [p (:name (cljs.analyzer/resolve-var (dissoc &env :locals) psym))
699
+ psym (vary-meta psym assoc :protocol-symbol true)
700
+ ns-name (-> &env :ns :name)
701
+ fqn (fn [n] (symbol (core/str ns-name "." n)))
702
+ prefix (protocol-prefix p)
703
+ methods (if (core/string? (first doc+methods)) (next doc+methods) doc+methods)
704
+ expand-sig (fn [fname slot sig]
705
+ `(~sig
706
+ (if (and ~(first sig) (. ~(first sig) ~(symbol (core/str "-" slot)))) ;; Property access needed here.
707
+ (. ~(first sig) ~slot ~@sig)
708
+ (let [x# (if (nil? ~(first sig)) nil ~(first sig))]
709
+ ((or
710
+ (aget ~(fqn fname) (goog.typeOf x#))
711
+ (aget ~(fqn fname) "_")
712
+ (throw (missing-protocol
713
+ ~(core/str psym "." fname) ~(first sig))))
714
+ ~@sig)))))
715
+ method (fn [[fname & sigs]]
716
+ (let [sigs (take-while vector? sigs)
717
+ slot (symbol (core/str prefix (name fname)))
718
+ fname (vary-meta fname assoc :protocol p)]
719
+ `(defn ~fname ~@(map (fn [sig]
720
+ (expand-sig fname
721
+ (symbol (core/str slot "$arity$" (count sig)))
722
+ sig))
723
+ sigs))))]
724
+ `(do
725
+ (set! ~'*unchecked-if* true)
726
+ (def ~psym (~'js* "{}"))
727
+ ~@(map method methods)
728
+ (set! ~'*unchecked-if* false))))
729
+
730
+ (defmacro satisfies?
731
+ "Returns true if x satisfies the protocol"
732
+ [psym x]
733
+ (let [p (:name (cljs.analyzer/resolve-var (dissoc &env :locals) psym))
734
+ prefix (protocol-prefix p)
735
+ xsym (bool-expr (gensym))
736
+ [part bit] (fast-path-protocols p)
737
+ msym (symbol (core/str "-cljs$lang$protocol_mask$partition" part "$"))]
738
+ `(let [~xsym ~x]
739
+ (if ~xsym
740
+ (if (or ~(if bit `(unsafe-bit-and (. ~xsym ~msym) ~bit))
741
+ ~(bool-expr `(. ~xsym ~(symbol (core/str "-" prefix)))))
742
+ true
743
+ (if (coercive-not (. ~xsym ~msym))
744
+ (cljs.core/type_satisfies_ ~psym ~xsym)
745
+ false))
746
+ (cljs.core/type_satisfies_ ~psym ~xsym)))))
747
+
748
+ (defmacro lazy-seq [& body]
749
+ `(new cljs.core/LazySeq nil false (fn [] ~@body) nil))
750
+
751
+ (defmacro delay [& body]
752
+ "Takes a body of expressions and yields a Delay object that will
753
+ invoke the body only the first time it is forced (with force or deref/@), and
754
+ will cache the result and return it on all subsequent force
755
+ calls."
756
+ `(new cljs.core/Delay (atom {:done false, :value nil}) (fn [] ~@body)))
757
+
758
+ (defmacro binding
759
+ "binding => var-symbol init-expr
760
+
761
+ Creates new bindings for the (already-existing) vars, with the
762
+ supplied initial values, executes the exprs in an implicit do, then
763
+ re-establishes the bindings that existed before. The new bindings
764
+ are made in parallel (unlike let); all init-exprs are evaluated
765
+ before the vars are bound to their new values."
766
+ [bindings & body]
767
+ (let [names (take-nth 2 bindings)
768
+ vals (take-nth 2 (drop 1 bindings))
769
+ tempnames (map (comp gensym name) names)
770
+ binds (map vector names vals)
771
+ resets (reverse (map vector names tempnames))]
772
+ (cljs.analyzer/confirm-bindings &env names)
773
+ `(let [~@(interleave tempnames names)]
774
+ (try
775
+ ~@(map
776
+ (fn [[k v]] (list 'set! k v))
777
+ binds)
778
+ ~@body
779
+ (finally
780
+ ~@(map
781
+ (fn [[k v]] (list 'set! k v))
782
+ resets))))))
783
+
784
+ (defmacro condp
785
+ "Takes a binary predicate, an expression, and a set of clauses.
786
+ Each clause can take the form of either:
787
+
788
+ test-expr result-expr
789
+
790
+ test-expr :>> result-fn
791
+
792
+ Note :>> is an ordinary keyword.
793
+
794
+ For each clause, (pred test-expr expr) is evaluated. If it returns
795
+ logical true, the clause is a match. If a binary clause matches, the
796
+ result-expr is returned, if a ternary clause matches, its result-fn,
797
+ which must be a unary function, is called with the result of the
798
+ predicate as its argument, the result of that call being the return
799
+ value of condp. A single default expression can follow the clauses,
800
+ and its value will be returned if no clause matches. If no default
801
+ expression is provided and no clause matches, an
802
+ IllegalArgumentException is thrown."
803
+ {:added "1.0"}
804
+
805
+ [pred expr & clauses]
806
+ (let [gpred (gensym "pred__")
807
+ gexpr (gensym "expr__")
808
+ emit (fn emit [pred expr args]
809
+ (let [[[a b c :as clause] more]
810
+ (split-at (if (= :>> (second args)) 3 2) args)
811
+ n (count clause)]
812
+ (cond
813
+ (= 0 n) `(throw (js/Error. (core/str "No matching clause: " ~expr)))
814
+ (= 1 n) a
815
+ (= 2 n) `(if (~pred ~a ~expr)
816
+ ~b
817
+ ~(emit pred expr more))
818
+ :else `(if-let [p# (~pred ~a ~expr)]
819
+ (~c p#)
820
+ ~(emit pred expr more)))))
821
+ gres (gensym "res__")]
822
+ `(let [~gpred ~pred
823
+ ~gexpr ~expr]
824
+ ~(emit gpred gexpr clauses))))
825
+
826
+ (defmacro case [e & clauses]
827
+ (let [default (if (odd? (count clauses))
828
+ (last clauses)
829
+ `(throw (js/Error. (core/str "No matching clause: " ~e))))
830
+ assoc-test (fn assoc-test [m test expr]
831
+ (if (contains? m test)
832
+ (throw (clojure.core/IllegalArgumentException.
833
+ (core/str "Duplicate case test constant '"
834
+ test "'"
835
+ (when (:line &env)
836
+ (core/str " on line " (:line &env) " "
837
+ cljs.analyzer/*cljs-file*)))))
838
+ (assoc m test expr)))
839
+ pairs (reduce (fn [m [test expr]]
840
+ (cond
841
+ (seq? test) (reduce (fn [m test]
842
+ (let [test (if (symbol? test)
843
+ (list 'quote test)
844
+ test)]
845
+ (assoc-test m test expr)))
846
+ m test)
847
+ (symbol? test) (assoc-test m (list 'quote test) expr)
848
+ :else (assoc-test m test expr)))
849
+ {} (partition 2 clauses))
850
+ esym (gensym)]
851
+ `(let [~esym ~e]
852
+ (cond
853
+ ~@(mapcat (fn [[m c]] `((cljs.core/= ~m ~esym) ~c)) pairs)
854
+ :else ~default))))
855
+
856
+ (defmacro try
857
+ "(try expr* catch-clause* finally-clause?)
858
+
859
+ Special Form
860
+
861
+ catch-clause => (catch protoname name expr*)
862
+ finally-clause => (finally expr*)
863
+
864
+ Catches and handles JavaScript exceptions."
865
+ [& forms]
866
+ (let [catch? #(and (list? %) (= (first %) 'catch))
867
+ [body catches] (split-with (complement catch?) forms)
868
+ [catches fin] (split-with catch? catches)
869
+ e (gensym "e")]
870
+ (assert (every? #(clojure.core/> (count %) 2) catches) "catch block must specify a prototype and a name")
871
+ (if (seq catches)
872
+ `(~'try*
873
+ ~@body
874
+ (catch ~e
875
+ (cond
876
+ ~@(mapcat
877
+ (fn [[_ type name & cb]]
878
+ `[(instance? ~type ~e) (let [~name ~e] ~@cb)])
879
+ catches)
880
+ :else (throw ~e)))
881
+ ~@fin)
882
+ `(~'try*
883
+ ~@body
884
+ ~@fin))))
885
+
886
+ (defmacro assert
887
+ "Evaluates expr and throws an exception if it does not evaluate to
888
+ logical true."
889
+ ([x]
890
+ (when *assert*
891
+ `(when-not ~x
892
+ (throw (js/Error.
893
+ (cljs.core/str "Assert failed: " (cljs.core/pr-str '~x)))))))
894
+ ([x message]
895
+ (when *assert*
896
+ `(when-not ~x
897
+ (throw (js/Error.
898
+ (cljs.core/str "Assert failed: " ~message "\n" (cljs.core/pr-str '~x))))))))
899
+
900
+ (defmacro for
901
+ "List comprehension. Takes a vector of one or more
902
+ binding-form/collection-expr pairs, each followed by zero or more
903
+ modifiers, and yields a lazy sequence of evaluations of expr.
904
+ Collections are iterated in a nested fashion, rightmost fastest,
905
+ and nested coll-exprs can refer to bindings created in prior
906
+ binding-forms. Supported modifiers are: :let [binding-form expr ...],
907
+ :while test, :when test.
908
+
909
+ (take 100 (for [x (range 100000000) y (range 1000000) :while (< y x)] [x y]))"
910
+ [seq-exprs body-expr]
911
+ (assert-args for
912
+ (vector? seq-exprs) "a vector for its binding"
913
+ (even? (count seq-exprs)) "an even number of forms in binding vector")
914
+ (let [to-groups (fn [seq-exprs]
915
+ (reduce (fn [groups [k v]]
916
+ (if (keyword? k)
917
+ (conj (pop groups) (conj (peek groups) [k v]))
918
+ (conj groups [k v])))
919
+ [] (partition 2 seq-exprs)))
920
+ err (fn [& msg] (throw (apply core/str msg)))
921
+ emit-bind (fn emit-bind [[[bind expr & mod-pairs]
922
+ & [[_ next-expr] :as next-groups]]]
923
+ (let [giter (gensym "iter__")
924
+ gxs (gensym "s__")
925
+ do-mod (fn do-mod [[[k v :as pair] & etc]]
926
+ (cond
927
+ (= k :let) `(let ~v ~(do-mod etc))
928
+ (= k :while) `(when ~v ~(do-mod etc))
929
+ (= k :when) `(if ~v
930
+ ~(do-mod etc)
931
+ (recur (rest ~gxs)))
932
+ (keyword? k) (err "Invalid 'for' keyword " k)
933
+ next-groups
934
+ `(let [iterys# ~(emit-bind next-groups)
935
+ fs# (seq (iterys# ~next-expr))]
936
+ (if fs#
937
+ (concat fs# (~giter (rest ~gxs)))
938
+ (recur (rest ~gxs))))
939
+ :else `(cons ~body-expr
940
+ (~giter (rest ~gxs)))))]
941
+ `(fn ~giter [~gxs]
942
+ (lazy-seq
943
+ (loop [~gxs ~gxs]
944
+ (when-first [~bind ~gxs]
945
+ ~(do-mod mod-pairs)))))))]
946
+ `(let [iter# ~(emit-bind (to-groups seq-exprs))]
947
+ (iter# ~(second seq-exprs)))))
948
+
949
+ (defmacro doseq
950
+ "Repeatedly executes body (presumably for side-effects) with
951
+ bindings and filtering as provided by \"for\". Does not retain
952
+ the head of the sequence. Returns nil."
953
+ [seq-exprs & body]
954
+ (assert-args doseq
955
+ (vector? seq-exprs) "a vector for its binding"
956
+ (even? (count seq-exprs)) "an even number of forms in binding vector")
957
+ (let [step (fn step [recform exprs]
958
+ (if-not exprs
959
+ [true `(do ~@body)]
960
+ (let [k (first exprs)
961
+ v (second exprs)
962
+
963
+ seqsym (when-not (keyword? k) (gensym))
964
+ recform (if (keyword? k) recform `(recur (next ~seqsym)))
965
+ steppair (step recform (nnext exprs))
966
+ needrec (steppair 0)
967
+ subform (steppair 1)]
968
+ (cond
969
+ (= k :let) [needrec `(let ~v ~subform)]
970
+ (= k :while) [false `(when ~v
971
+ ~subform
972
+ ~@(when needrec [recform]))]
973
+ (= k :when) [false `(if ~v
974
+ (do
975
+ ~subform
976
+ ~@(when needrec [recform]))
977
+ ~recform)]
978
+ :else [true `(loop [~seqsym (seq ~v)]
979
+ (when ~seqsym
980
+ (let [~k (first ~seqsym)]
981
+ ~subform
982
+ ~@(when needrec [recform]))))]))))]
983
+ (nth (step nil (seq seq-exprs)) 1)))
984
+
985
+ (defmacro array [& rest]
986
+ (let [xs-str (->> (repeat "~{}")
987
+ (take (count rest))
988
+ (interpose ",")
989
+ (apply core/str))]
990
+ (concat
991
+ (list 'js* (core/str "[" xs-str "]"))
992
+ rest)))
993
+
994
+ (defmacro js-obj [& rest]
995
+ (let [kvs-str (->> (repeat "~{}:~{}")
996
+ (take (quot (count rest) 2))
997
+ (interpose ",")
998
+ (apply core/str))]
999
+ (concat
1000
+ (list 'js* (core/str "{" kvs-str "}"))
1001
+ rest)))
1002
+
1003
+ (defmacro alength [a]
1004
+ (list 'js* "~{}.length" a))
1005
+
1006
+ (defmacro aclone [a]
1007
+ (list 'js* "~{}.slice()" a))
1008
+
1009
+ (defmacro amap
1010
+ "Maps an expression across an array a, using an index named idx, and
1011
+ return value named ret, initialized to a clone of a, then setting
1012
+ each element of ret to the evaluation of expr, returning the new
1013
+ array ret."
1014
+ [a idx ret expr]
1015
+ `(let [a# ~a
1016
+ ~ret (aclone a#)]
1017
+ (loop [~idx 0]
1018
+ (if (< ~idx (alength a#))
1019
+ (do
1020
+ (aset ~ret ~idx ~expr)
1021
+ (recur (inc ~idx)))
1022
+ ~ret))))
1023
+
1024
+ (defmacro areduce
1025
+ "Reduces an expression across an array a, using an index named idx,
1026
+ and return value named ret, initialized to init, setting ret to the
1027
+ evaluation of expr at each step, returning ret."
1028
+ [a idx ret init expr]
1029
+ `(let [a# ~a]
1030
+ (loop [~idx 0 ~ret ~init]
1031
+ (if (< ~idx (alength a#))
1032
+ (recur (inc ~idx) ~expr)
1033
+ ~ret))))
1034
+
1035
+ (defmacro dotimes
1036
+ "bindings => name n
1037
+
1038
+ Repeatedly executes body (presumably for side-effects) with name
1039
+ bound to integers from 0 through n-1."
1040
+ [bindings & body]
1041
+ (let [i (first bindings)
1042
+ n (second bindings)]
1043
+ `(let [n# ~n]
1044
+ (loop [~i 0]
1045
+ (when (< ~i n#)
1046
+ ~@body
1047
+ (recur (inc ~i)))))))
1048
+
1049
+ (defn ^:private check-valid-options
1050
+ "Throws an exception if the given option map contains keys not listed
1051
+ as valid, else returns nil."
1052
+ [options & valid-keys]
1053
+ (when (seq (apply disj (apply hash-set (keys options)) valid-keys))
1054
+ (throw
1055
+ (apply core/str "Only these options are valid: "
1056
+ (first valid-keys)
1057
+ (map #(core/str ", " %) (rest valid-keys))))))
1058
+
1059
+ (defmacro defmulti
1060
+ "Creates a new multimethod with the associated dispatch function.
1061
+ The docstring and attribute-map are optional.
1062
+
1063
+ Options are key-value pairs and may be one of:
1064
+ :default the default dispatch value, defaults to :default
1065
+ :hierarchy the isa? hierarchy to use for dispatching
1066
+ defaults to the global hierarchy"
1067
+ [mm-name & options]
1068
+ (let [docstring (if (core/string? (first options))
1069
+ (first options)
1070
+ nil)
1071
+ options (if (core/string? (first options))
1072
+ (next options)
1073
+ options)
1074
+ m (if (map? (first options))
1075
+ (first options)
1076
+ {})
1077
+ options (if (map? (first options))
1078
+ (next options)
1079
+ options)
1080
+ dispatch-fn (first options)
1081
+ options (next options)
1082
+ m (if docstring
1083
+ (assoc m :doc docstring)
1084
+ m)
1085
+ m (if (meta mm-name)
1086
+ (conj (meta mm-name) m)
1087
+ m)]
1088
+ (when (= (count options) 1)
1089
+ (throw "The syntax for defmulti has changed. Example: (defmulti name dispatch-fn :default dispatch-value)"))
1090
+ (let [options (apply hash-map options)
1091
+ default (core/get options :default :default)]
1092
+ (check-valid-options options :default :hierarchy)
1093
+ `(def ~(with-meta mm-name m)
1094
+ (let [method-table# (atom {})
1095
+ prefer-table# (atom {})
1096
+ method-cache# (atom {})
1097
+ cached-hierarchy# (atom {})
1098
+ hierarchy# (get ~options :hierarchy cljs.core/global-hierarchy)]
1099
+ (cljs.core/MultiFn. ~(name mm-name) ~dispatch-fn ~default hierarchy#
1100
+ method-table# prefer-table# method-cache# cached-hierarchy#))))))
1101
+
1102
+ (defmacro defmethod
1103
+ "Creates and installs a new method of multimethod associated with dispatch-value. "
1104
+ [multifn dispatch-val & fn-tail]
1105
+ `(-add-method ~(with-meta multifn {:tag 'cljs.core/MultiFn}) ~dispatch-val (fn ~@fn-tail)))
1106
+
1107
+ (defmacro time
1108
+ "Evaluates expr and prints the time it took. Returns the value of expr."
1109
+ [expr]
1110
+ `(let [start# (.getTime (js/Date.))
1111
+ ret# ~expr]
1112
+ (prn (core/str "Elapsed time: " (- (.getTime (js/Date.)) start#) " msecs"))
1113
+ ret#))
1114
+
1115
+ (defmacro simple-benchmark
1116
+ "Runs expr iterations times in the context of a let expression with
1117
+ the given bindings, then prints out the bindings and the expr
1118
+ followed by number of iterations and total time. The optional
1119
+ argument print-fn, defaulting to println, sets function used to
1120
+ print the result. expr's string representation will be produced
1121
+ using pr-str in any case."
1122
+ [bindings expr iterations & {:keys [print-fn] :or {print-fn 'println}}]
1123
+ (let [bs-str (pr-str bindings)
1124
+ expr-str (pr-str expr)]
1125
+ `(let ~bindings
1126
+ (let [start# (.getTime (js/Date.))
1127
+ ret# (dotimes [_# ~iterations] ~expr)
1128
+ end# (.getTime (js/Date.))
1129
+ elapsed# (- end# start#)]
1130
+ (~print-fn (str ~bs-str ", " ~expr-str ", "
1131
+ ~iterations " runs, " elapsed# " msecs"))))))
1132
+
1133
+ (def cs (into [] (map (comp symbol core/str char) (range 97 118))))
1134
+
1135
+ (defn gen-apply-to-helper
1136
+ ([] (gen-apply-to-helper 1))
1137
+ ([n]
1138
+ (let [prop (symbol (core/str "-cljs$lang$arity$" n))
1139
+ f (symbol (core/str "cljs$lang$arity$" n))]
1140
+ (if (core/<= n 20)
1141
+ `(let [~(cs (core/dec n)) (-first ~'args)
1142
+ ~'args (-rest ~'args)]
1143
+ (if (core/== ~'argc ~n)
1144
+ (if (. ~'f ~prop)
1145
+ (. ~'f (~f ~@(take n cs)))
1146
+ (~'f ~@(take n cs)))
1147
+ ~(gen-apply-to-helper (core/inc n))))
1148
+ `(throw (js/Error. "Only up to 20 arguments supported on functions"))))))
1149
+
1150
+ (defmacro gen-apply-to []
1151
+ `(do
1152
+ (set! ~'*unchecked-if* true)
1153
+ (defn ~'apply-to [~'f ~'argc ~'args]
1154
+ (let [~'args (seq ~'args)]
1155
+ (if (zero? ~'argc)
1156
+ (~'f)
1157
+ ~(gen-apply-to-helper))))
1158
+ (set! ~'*unchecked-if* false)))