-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathcljs_SLASH_core.cljc
3270 lines (2913 loc) · 133 KB
/
cljs_SLASH_core.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns cljs.core
(:refer-clojure :exclude [-> ->> .. amap and areduce alength aclone assert assert-args binding bound-fn case comment
cond condp declare definline definterface defmethod defmulti defn defn- defonce
defprotocol defrecord defstruct deftype delay destructure doseq dosync dotimes doto
extend-protocol extend-type fn for future gen-class gen-interface
if-let if-not import io! lazy-cat lazy-seq let letfn locking loop
memfn ns or proxy proxy-super pvalues refer-clojure reify sync time
when when-first when-let when-not while with-bindings with-in-str
with-loading-context with-local-vars with-open with-out-str with-precision with-redefs
satisfies? identical? true? false? number? nil? instance? symbol? keyword? string? str get
make-array vector list hash-map array-map hash-set
aget aset
+ - * / < <= > >= == zero? pos? neg? inc dec max min mod
byte char short int long float double
unchecked-byte unchecked-char unchecked-short unchecked-int
unchecked-long unchecked-float unchecked-double
unchecked-add unchecked-add-int unchecked-dec unchecked-dec-int
unchecked-divide unchecked-divide-int unchecked-inc unchecked-inc-int
unchecked-multiply unchecked-multiply-int unchecked-negate unchecked-negate-int
unchecked-subtract unchecked-subtract-int unchecked-remainder-int
unsigned-bit-shift-right
bit-and bit-and-not bit-clear bit-flip bit-not bit-or bit-set
bit-test bit-shift-left bit-shift-right bit-xor defmacro
cond-> cond->> as-> some-> some->>
require use refer-clojure
if-some when-some test ns-publics ns-imports ns-interns
ns-unmap var vswap! macroexpand-1 macroexpand
some? resolve
#?@(:cljs [alias coercive-not coercive-not= coercive-= coercive-boolean
truth_ js-arguments js-delete js-in js-debugger exists? divide js-mod
unsafe-bit-and bit-shift-right-zero-fill mask bitpos caching-hash
defcurried rfn specify! js-this this-as implements? array js-obj
simple-benchmark gen-apply-to js-str es6-iterable load-file* undefined?
specify copy-arguments goog-define js-comment js-inline-comment
unsafe-cast require-macros use-macros gen-apply-to-simple unchecked-get unchecked-set])])
#?(:cljs (:require-macros [cljs.core :as core]
[cljs.support :refer [assert-args]]))
(:require clojure.walk
clojure.set
[clojure.string :as string]
[cljs.compiler :as comp]
[cljs.env :as env]
#?(:clj [cljs.support :refer [assert-args]])
#?(:cljs [cljs.core :as core])
#?(:cljs [cljs.analyzer :as ana])))
#?(:clj (alias 'core 'clojure.core))
#?(:clj (alias 'ana 'cljs.analyzer))
#?(:clj
(core/defmacro import-macros [ns [& vars]]
(core/let [ns (find-ns ns)
vars (map #(ns-resolve ns %) vars)
syms (map
(core/fn [^clojure.lang.Var v]
(core/-> v .sym
(with-meta
(merge
{:macro true}
(update-in (select-keys (meta v) [:arglists :doc :file :line])
[:arglists] (core/fn [arglists] `(quote ~arglists)))))))
vars)
defs (map
(core/fn [sym var]
(core/let [{:keys [arglists doc file line]} (meta sym)]
`(do
(def ~sym (deref ~var))
;for AOT compilation
(alter-meta! (var ~sym) assoc
:macro true
:arglists ~arglists
:doc ~doc
:file ~file
:line ~line))))
syms vars)]
`(do ~@defs
:imported))))
#?(:clj
(import-macros clojure.core
[-> ->> .. assert comment cond
declare defn-
doto
extend-protocol fn for
if-let if-not letfn
memfn
when when-first when-let when-not while
cond-> cond->> as-> some-> some->>
if-some when-some]))
#?(:cljs
(core/defmacro ->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
[x & forms]
(core/loop [x x, forms forms]
(if forms
(core/let [form (first forms)
threaded (if (seq? form)
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(core/list form x))]
(recur threaded (next forms)))
x))))
#?(:cljs
(core/defmacro ->>
"Threads the expr through the forms. Inserts x as the
last item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc."
[x & forms]
(core/loop [x x, forms forms]
(if forms
(core/let [form (first forms)
threaded (if (seq? form)
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(core/list form x))]
(recur threaded (next forms)))
x))))
#?(:cljs
(core/defmacro ..
"form => fieldName-symbol or (instanceMethodName-symbol args*)
Expands into a member access (.) of the first member on the first
argument, followed by the next member on the result, etc. For
instance:
(.. System (getProperties) (get \"os.name\"))
expands to:
(. (. System (getProperties)) (get \"os.name\"))
but is easier to write, read, and understand."
([x form] `(. ~x ~form))
([x form & more] `(.. (. ~x ~form) ~@more))))
#?(:cljs
(core/defmacro comment
"Ignores body, yields nil"
[& body]))
#?(:cljs
(core/defmacro cond
"Takes a set of test/expr pairs. It evaluates each test one at a
time. If a test returns logical true, cond evaluates and returns
the value of the corresponding expr and doesn't evaluate any of the
other tests or exprs. (cond) returns nil."
{:added "1.0"}
[& clauses]
(core/when clauses
(core/list 'if (first clauses)
(if (next clauses)
(second clauses)
(throw (js/Error. "cond requires an even number of forms")))
(cons 'cljs.core/cond (next (next clauses)))))))
#?(:cljs
(core/defmacro declare
"defs the supplied var names with no bindings, useful for making forward declarations."
[& names] `(do ~@(map #(core/list 'def (vary-meta % assoc :declared true)) names))))
#?(:cljs
(core/defmacro doto
"Evaluates x then calls all of the methods and functions with the
value of x supplied at the front of the given arguments. The forms
are evaluated in order. Returns x.
(doto (new java.util.HashMap) (.put \"a\" 1) (.put \"b\" 2))"
[x & forms]
(core/let [gx (gensym)]
`(let [~gx ~x]
~@(map (core/fn [f]
(if (seq? f)
`(~(first f) ~gx ~@(next f))
`(~f ~gx)))
forms)
~gx))))
#?(:cljs
(core/defn- parse-impls [specs]
(core/loop [ret {} s specs]
(if (seq s)
(recur (assoc ret (first s) (take-while seq? (next s)))
(drop-while seq? (next s)))
ret))))
#?(:cljs
(core/defn- emit-extend-protocol [p specs]
(core/let [impls (parse-impls specs)]
`(do
~@(map (core/fn [[t fs]]
`(extend-type ~t ~p ~@fs))
impls)))))
#?(:cljs
(core/defmacro extend-protocol
"Useful when you want to provide several implementations of the same
protocol all at once. Takes a single protocol and the implementation
of that protocol for one or more types. Expands into calls to
extend-type:
(extend-protocol Protocol
AType
(foo [x] ...)
(bar [x y] ...)
BType
(foo [x] ...)
(bar [x y] ...)
AClass
(foo [x] ...)
(bar [x y] ...)
nil
(foo [x] ...)
(bar [x y] ...))
expands into:
(do
(clojure.core/extend-type AType Protocol
(foo [x] ...)
(bar [x y] ...))
(clojure.core/extend-type BType Protocol
(foo [x] ...)
(bar [x y] ...))
(clojure.core/extend-type AClass Protocol
(foo [x] ...)
(bar [x y] ...))
(clojure.core/extend-type nil Protocol
(foo [x] ...)
(bar [x y] ...)))"
[p & specs]
(emit-extend-protocol p specs)))
#?(:cljs
(core/defn ^{:private true}
maybe-destructured
[params body]
(if (every? core/symbol? params)
(cons params body)
(core/loop [params params
new-params (with-meta [] (meta params))
lets []]
(if params
(if (core/symbol? (first params))
(recur (next params) (conj new-params (first params)) lets)
(core/let [gparam (gensym "p__")]
(recur (next params) (conj new-params gparam)
(core/-> lets (conj (first params)) (conj gparam)))))
`(~new-params
(let ~lets
~@body)))))))
#?(:cljs
(core/defmacro fn
"params => positional-params* , or positional-params* & next-param
positional-param => binding-form
next-param => binding-form
name => symbol
Defines a function"
{:forms '[(fn name? [params*] exprs*) (fn name? ([params*] exprs*) +)]}
[& sigs]
(core/let [name (if (core/symbol? (first sigs)) (first sigs) nil)
sigs (if name (next sigs) sigs)
sigs (if (vector? (first sigs))
(core/list sigs)
(if (seq? (first sigs))
sigs
;; Assume single arity syntax
(throw (js/Error.
(if (seq sigs)
(core/str "Parameter declaration "
(core/first sigs)
" should be a vector")
(core/str "Parameter declaration missing"))))))
psig (fn* [sig]
;; Ensure correct type before destructuring sig
(core/when (not (seq? sig))
(throw (js/Error.
(core/str "Invalid signature " sig
" should be a list"))))
(core/let [[params & body] sig
_ (core/when (not (vector? params))
(throw (js/Error.
(if (seq? (first sigs))
(core/str "Parameter declaration " params
" should be a vector")
(core/str "Invalid signature " sig
" should be a list")))))
conds (core/when (core/and (next body) (map? (first body)))
(first body))
body (if conds (next body) body)
conds (core/or conds (meta params))
pre (:pre conds)
post (:post conds)
body (if post
`((let [~'% ~(if (core/< 1 (count body))
`(do ~@body)
(first body))]
~@(map (fn* [c] `(assert ~c)) post)
~'%))
body)
body (if pre
(concat (map (fn* [c] `(assert ~c)) pre)
body)
body)]
(maybe-destructured params body)))
new-sigs (map psig sigs)]
(with-meta
(if name
(list* 'fn* name new-sigs)
(cons 'fn* new-sigs))
(meta &form)))))
#?(:cljs
(core/defmacro defn-
"same as defn, yielding non-public def"
[name & decls]
(list* `defn (with-meta name (assoc (meta name) :private true)) decls)))
#?(:cljs
(core/defmacro if-let
"bindings => binding-form test
If test is true, evaluates then with binding-form bound to the value of
test, if not, yields else"
([bindings then]
`(if-let ~bindings ~then nil))
([bindings then else & oldform]
(assert-args if-let
(vector? bindings) "a vector for its binding"
(empty? oldform) "1 or 2 forms after binding vector"
(= 2 (count bindings)) "exactly 2 forms in binding vector")
(core/let [form (bindings 0) tst (bindings 1)]
`(let [temp# ~tst]
(if temp#
(let [~form temp#]
~then)
~else))))))
#?(:cljs
(core/defmacro if-not
"Evaluates test. If logical false, evaluates and returns then expr,
otherwise else expr, if supplied, else nil."
([test then] `(if-not ~test ~then nil))
([test then else]
`(if (not ~test) ~then ~else))))
#?(:cljs
(core/defmacro letfn
"fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)
Takes a vector of function specs and a body, and generates a set of
bindings of functions to their names. All of the names are available
in all of the definitions of the functions, as well as the body."
{:forms '[(letfn [fnspecs*] exprs*)],
:special-form true, :url nil}
[fnspecs & body]
`(letfn* ~(vec (interleave (map first fnspecs)
(map #(cons `fn %) fnspecs)))
~@body)))
(core/defmacro memfn
"Expands into code that creates a fn that expects to be passed an
object and any args and calls the named instance method on the
object passing the args. Use when you want to treat a JavaScript
method as a first-class fn."
[name & args]
(core/let [t (with-meta (gensym "target")
(meta name))]
`(fn [~t ~@args]
(. ~t (~name ~@args)))))
#?(:cljs
(core/defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
[test & body]
(core/list 'if test (cons 'do body))))
#?(:cljs
(core/defmacro when-first
"bindings => x xs
Roughly the same as (when (seq xs) (let [x (first xs)] body)) but xs is evaluated only once"
[bindings & body]
(assert-args when-first
(vector? bindings) "a vector for its binding"
(= 2 (count bindings)) "exactly 2 forms in binding vector")
(core/let [[x xs] bindings]
`(when-let [xs# (seq ~xs)]
(let [~x (first xs#)]
~@body)))))
#?(:cljs
(core/defmacro when-let
"bindings => binding-form test
When test is true, evaluates body with binding-form bound to the value of test"
[bindings & body]
(assert-args when-let
(vector? bindings) "a vector for its binding"
(= 2 (count bindings)) "exactly 2 forms in binding vector")
(core/let [form (bindings 0) tst (bindings 1)]
`(let [temp# ~tst]
(when temp#
(let [~form temp#]
~@body))))))
#?(:cljs
(core/defmacro when-not
"Evaluates test. If logical false, evaluates body in an implicit do."
[test & body]
(core/list 'if test nil (cons 'do body))))
#?(:cljs
(core/defmacro while
"Repeatedly executes body while test expression is true. Presumes
some side-effect will cause test to become false/nil. Returns nil"
[test & body]
`(loop []
(when ~test
~@body
(recur)))))
#?(:cljs
(core/defmacro cond->
"Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test
expression is true. Note that, unlike cond branching, cond-> threading does
not short circuit after the first true test expression."
[expr & clauses]
(core/assert (even? (count clauses)))
(core/let [g (gensym)
steps (map (core/fn [[test step]] `(if ~test (-> ~g ~step) ~g))
(partition 2 clauses))]
`(let [~g ~expr
~@(interleave (repeat g) (butlast steps))]
~(if (empty? steps)
g
(last steps))))))
#?(:cljs
(core/defmacro cond->>
"Takes an expression and a set of test/form pairs. Threads expr (via ->>)
through each form for which the corresponding test expression
is true. Note that, unlike cond branching, cond->> threading does not short circuit
after the first true test expression."
[expr & clauses]
(core/assert (even? (count clauses)))
(core/let [g (gensym)
steps (map (core/fn [[test step]] `(if ~test (->> ~g ~step) ~g))
(partition 2 clauses))]
`(let [~g ~expr
~@(interleave (repeat g) (butlast steps))]
~(if (empty? steps)
g
(last steps))))))
#?(:cljs
(core/defmacro as->
"Binds name to expr, evaluates the first form in the lexical context
of that binding, then binds name to that result, repeating for each
successive form, returning the result of the last form."
[expr name & forms]
`(let [~name ~expr
~@(interleave (repeat name) (butlast forms))]
~(if (empty? forms)
name
(last forms)))))
#?(:cljs
(core/defmacro some->
"When expr is not nil, threads it into the first form (via ->),
and when that result is not nil, through the next etc"
[expr & forms]
(core/let [g (gensym)
steps (map (core/fn [step] `(if (nil? ~g) nil (-> ~g ~step)))
forms)]
`(let [~g ~expr
~@(interleave (repeat g) (butlast steps))]
~(if (empty? steps)
g
(last steps))))))
#?(:cljs
(core/defmacro some->>
"When expr is not nil, threads it into the first form (via ->>),
and when that result is not nil, through the next etc"
[expr & forms]
(core/let [g (gensym)
steps (map (core/fn [step] `(if (nil? ~g) nil (->> ~g ~step)))
forms)]
`(let [~g ~expr
~@(interleave (repeat g) (butlast steps))]
~(if (empty? steps)
g
(last steps))))))
#?(:cljs
(core/defmacro if-some
"bindings => binding-form test
If test is not nil, evaluates then with binding-form bound to the
value of test, if not, yields else"
([bindings then]
`(if-some ~bindings ~then nil))
([bindings then else & oldform]
(assert-args if-some
(vector? bindings) "a vector for its binding"
(empty? oldform) "1 or 2 forms after binding vector"
(= 2 (count bindings)) "exactly 2 forms in binding vector")
(core/let [form (bindings 0) tst (bindings 1)]
`(let [temp# ~tst]
(if (nil? temp#)
~else
(let [~form temp#]
~then)))))))
#?(:cljs
(core/defmacro when-some
"bindings => binding-form test
When test is not nil, evaluates body with binding-form bound to the
value of test"
[bindings & body]
(assert-args when-some
(vector? bindings) "a vector for its binding"
(= 2 (count bindings)) "exactly 2 forms in binding vector")
(core/let [form (bindings 0) tst (bindings 1)]
`(let [temp# ~tst]
(if (nil? temp#)
nil
(let [~form temp#]
~@body))))))
(core/defn- ^{:dynamic true} assert-valid-fdecl
"A good fdecl looks like (([a] ...) ([a b] ...)) near the end of defn."
[fdecl]
(core/when (empty? fdecl)
(throw
#?(:clj (IllegalArgumentException. "Parameter declaration missing")
:cljs (js/Error. "Parameter declaration missing"))))
(core/let [argdecls
(map
#(if (seq? %)
(first %)
(throw
#?(:clj (IllegalArgumentException.
(if (seq? (first fdecl))
(core/str "Invalid signature \""
%
"\" should be a list")
(core/str "Parameter declaration \""
%
"\" should be a vector")))
:cljs (js/Error.
(if (seq? (first fdecl))
(core/str "Invalid signature \""
%
"\" should be a list")
(core/str "Parameter declaration \""
%
"\" should be a vector"))))))
fdecl)
bad-args (seq (remove #(vector? %) argdecls))]
(core/when bad-args
(throw
#?(:clj (IllegalArgumentException.
(core/str "Parameter declaration \"" (first bad-args)
"\" should be a vector"))
:cljs (js/Error.
(core/str "Parameter declaration \"" (first bad-args)
"\" should be a vector")))))))
(def
^{:private true}
sigs
(core/fn [fdecl]
(assert-valid-fdecl fdecl)
(core/let [asig
(core/fn [fdecl]
(core/let [arglist (first fdecl)
;elide implicit macro args
arglist (if #?(:clj (clojure.lang.Util/equals '&form (first arglist))
:cljs (= '&form (first arglist)))
#?(:clj (clojure.lang.RT/subvec arglist 2 (clojure.lang.RT/count arglist))
:cljs (subvec arglist 2 (count arglist)))
arglist)
body (next fdecl)]
(if (map? (first body))
(if (next body)
(with-meta arglist (conj (if (meta arglist) (meta arglist) {}) (first body)))
arglist)
arglist)))]
(if (seq? (first fdecl))
(core/loop [ret [] fdecls fdecl]
(if fdecls
(recur (conj ret (asig (first fdecls))) (next fdecls))
(seq ret)))
(core/list (asig fdecl))))))
(core/defmacro defonce
"defs name to have the root value of init iff the named var has no root value,
else init is unevaluated"
[x init]
`(when-not (exists? ~x)
(def ~x ~init)))
(core/defn destructure [bindings]
(core/let [bents (partition 2 bindings)
pb (core/fn pb [bvec b v]
(core/let [pvec
(core/fn [bvec b val]
(core/let [gvec (gensym "vec__")
gseq (gensym "seq__")
gfirst (gensym "first__")
has-rest (some #{'&} b)]
(core/loop [ret (core/let [ret (conj bvec gvec val)]
(if has-rest
(conj ret gseq (core/list `seq gvec))
ret))
n 0
bs b
seen-rest? false]
(if (seq bs)
(core/let [firstb (first bs)]
(core/cond
(= firstb '&) (recur (pb ret (second bs) gseq)
n
(nnext bs)
true)
(= firstb :as) (pb ret (second bs) gvec)
:else (if seen-rest?
(throw #?(:clj (new Exception "Unsupported binding form, only :as can follow & parameter")
:cljs (new js/Error "Unsupported binding form, only :as can follow & parameter")))
(recur (pb (if has-rest
(conj ret
gfirst `(first ~gseq)
gseq `(next ~gseq))
ret)
firstb
(if has-rest
gfirst
(core/list `nth gvec n nil)))
(core/inc n)
(next bs)
seen-rest?))))
ret))))
pmap
(core/fn [bvec b v]
(core/let [gmap (gensym "map__")
defaults (:or b)]
(core/loop [ret (core/-> bvec (conj gmap) (conj v)
(conj gmap) (conj `(if (implements? ISeq ~gmap) (apply cljs.core/hash-map ~gmap) ~gmap))
((core/fn [ret]
(if (:as b)
(conj ret (:as b) gmap)
ret))))
bes (core/let [transforms
(reduce
(core/fn [transforms mk]
(if (core/keyword? mk)
(core/let [mkns (namespace mk)
mkn (name mk)]
(core/cond (= mkn "keys") (assoc transforms mk #(keyword (core/or mkns (namespace %)) (name %)))
(= mkn "syms") (assoc transforms mk #(core/list `quote (symbol (core/or mkns (namespace %)) (name %))))
(= mkn "strs") (assoc transforms mk core/str)
:else transforms))
transforms))
{}
(keys b))]
(reduce
(core/fn [bes entry]
(reduce #(assoc %1 %2 ((val entry) %2))
(dissoc bes (key entry))
((key entry) bes)))
(dissoc b :as :or)
transforms))]
(if (seq bes)
(core/let [bb (key (first bes))
bk (val (first bes))
local (if #?(:clj (core/instance? clojure.lang.Named bb)
:cljs (cljs.core/implements? INamed bb))
(with-meta (symbol nil (name bb)) (meta bb))
bb)
bv (if (contains? defaults local)
(core/list 'cljs.core/get gmap bk (defaults local))
(core/list 'cljs.core/get gmap bk))]
(recur
(if (core/or (core/keyword? bb) (core/symbol? bb)) ;(ident? bb)
(core/-> ret (conj local bv))
(pb ret bb bv))
(next bes)))
ret))))]
(core/cond
(core/symbol? b) (core/-> bvec (conj (if (namespace b) (symbol (name b)) b)) (conj v))
(core/keyword? b) (core/-> bvec (conj (symbol (name b))) (conj v))
(vector? b) (pvec bvec b v)
(map? b) (pmap bvec b v)
:else (throw
#?(:clj (new Exception (core/str "Unsupported binding form: " b))
:cljs (new js/Error (core/str "Unsupported binding form: " b)))))))
process-entry (core/fn [bvec b] (pb bvec (first b) (second b)))]
(if (every? core/symbol? (map first bents))
bindings
(core/if-let [kwbs (seq (filter #(core/keyword? (first %)) bents))]
(throw
#?(:clj (new Exception (core/str "Unsupported binding key: " (ffirst kwbs)))
:cljs (new js/Error (core/str "Unsupported binding key: " (ffirst kwbs)))))
(reduce process-entry [] bents)))))
(core/defmacro goog-define
"Defines a var using `goog.define`. Passed default value must be
string, number or boolean.
Default value can be overridden at compile time using the
compiler option `:closure-defines`.
Example:
(ns your-app.core)
(goog-define DEBUG! false)
;; can be overridden with
:closure-defines {\"your_app.core.DEBUG_BANG_\" true}
or
:closure-defines {'your-app.core/DEBUG! true}"
[sym default]
(assert-args goog-define
(core/or (core/string? default)
(core/number? default)
(core/true? default)
(core/false? default)) "a string, number or boolean as default value")
(core/let [defname (comp/munge (core/str *ns* "/" sym))
type (core/cond
(core/string? default) "string"
(core/number? default) "number"
(core/or (core/true? default) (core/false? default)) "boolean")]
`(do
(declare ~(symbol sym))
(~'js* ~(core/str "/** @define {" type "} */"))
(goog/define ~defname ~default))))
(core/defmacro let
"binding => binding-form init-expr
Evaluates the exprs in a lexical context in which the symbols in
the binding-forms are bound to their respective init-exprs or parts
therein."
[bindings & body]
(assert-args let
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
`(let* ~(destructure bindings) ~@body))
(core/defmacro loop
"Evaluates the exprs in a lexical context in which the symbols in
the binding-forms are bound to their respective init-exprs or parts
therein. Acts as a recur target."
[bindings & body]
(assert-args loop
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
(core/let [db (destructure bindings)]
(if (= db bindings)
`(loop* ~bindings ~@body)
(core/let [vs (take-nth 2 (drop 1 bindings))
bs (take-nth 2 bindings)
gs (map (core/fn [b] (if (core/symbol? b) b (gensym))) bs)
bfs (reduce (core/fn [ret [b v g]]
(if (core/symbol? b)
(conj ret g v)
(conj ret g v b g)))
[] (map core/vector bs vs gs))]
`(let ~bfs
(loop* ~(vec (interleave gs gs))
(let ~(vec (interleave bs gs))
~@body)))))))
(def fast-path-protocols
"protocol fqn -> [partition number, bit]"
(zipmap (map #(symbol "cljs.core" (core/str %))
'[IFn ICounted IEmptyableCollection ICollection IIndexed ASeq ISeq INext
ILookup IAssociative IMap IMapEntry ISet IStack IVector IDeref
IDerefWithTimeout IMeta IWithMeta IReduce IKVReduce IEquiv IHash
ISeqable ISequential IList IRecord IReversible ISorted IPrintWithWriter IWriter
IPrintWithWriter IPending IWatchable IEditableCollection ITransientCollection
ITransientAssociative ITransientMap ITransientVector ITransientSet
IMultiFn IChunkedSeq IChunkedNext IComparable INamed ICloneable IAtom
IReset ISwap IIterable])
(iterate (core/fn [[p b]]
(if (core/== 2147483648 b)
[(core/inc p) 1]
[p #?(:clj (core/bit-shift-left b 1)
:cljs (core/* 2 b))]))
[0 1])))
(def fast-path-protocol-partitions-count
"total number of partitions"
(core/let [c (count fast-path-protocols)
m (core/mod c 32)]
(if (core/zero? m)
(core/quot c 32)
(core/inc (core/quot c 32)))))
(core/defmacro str [& xs]
(core/let [interpolate (core/fn [x]
(if (core/string? x)
"~{}"
"cljs.core.str.cljs$core$IFn$_invoke$arity$1(~{})"))
strs (core/->> xs
(map interpolate)
(interpose ",")
(apply core/str))]
(list* 'js* (core/str "[" strs "].join('')") xs)))
(core/defn- bool-expr [e]
(vary-meta e assoc :tag 'boolean))
(core/defn- simple-test-expr? [env ast]
(core/and
(#{:var :invoke :constant :dot :js} (:op ast))
('#{boolean seq} (cljs.analyzer/infer-tag env ast))))
(core/defmacro and
"Evaluates exprs one at a time, from left to right. If a form
returns logical false (nil or false), and returns that value and
doesn't evaluate any of the other expressions, otherwise it returns
the value of the last expr. (and) returns true."
([] true)
([x] x)
([x & next]
(core/let [forms (concat [x] next)]
(if (every? #(simple-test-expr? &env %)
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) forms))
(core/let [and-str (core/->> (repeat (count forms) "(~{})")
(interpose " && ")
(apply core/str))]
(bool-expr `(~'js* ~and-str ~@forms)))
`(let [and# ~x]
(if and# (and ~@next) and#))))))
(core/defmacro or
"Evaluates exprs one at a time, from left to right. If a form
returns a logical true value, or returns that value and doesn't
evaluate any of the other expressions, otherwise it returns the
value of the last expression. (or) returns nil."
([] nil)
([x] x)
([x & next]
(core/let [forms (concat [x] next)]
(if (every? #(simple-test-expr? &env %)
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) forms))
(core/let [or-str (core/->> (repeat (count forms) "(~{})")
(interpose " || ")
(apply core/str))]
(bool-expr `(~'js* ~or-str ~@forms)))
`(let [or# ~x]
(if or# or# (or ~@next)))))))
(core/defmacro nil? [x]
`(coercive-= ~x nil))
(core/defmacro some? [x]
`(not (nil? ~x)))
(core/defmacro coercive-not [x]
(bool-expr (core/list 'js* "(!~{})" x)))
(core/defmacro coercive-not= [x y]
(bool-expr (core/list 'js* "(~{} != ~{})" x y)))
(core/defmacro coercive-= [x y]
(bool-expr (core/list 'js* "(~{} == ~{})" x y)))
(core/defmacro coercive-boolean [x]
(with-meta (core/list 'js* "~{}" x)
{:tag 'boolean}))
;; internal - do not use.
(core/defmacro truth_ [x]
(core/assert (core/symbol? x) "x is substituted twice")
(core/list 'js* "(~{} != null && ~{} !== false)" x x))
(core/defmacro js-arguments []
(core/list 'js* "arguments"))
(core/defmacro js-delete [obj key]
(core/list 'js* "delete ~{}[~{}]" obj key))
(core/defmacro js-in [key obj]
(core/list 'js* "~{} in ~{}" key obj))
(core/defmacro js-debugger
"Emit JavaScript \"debugger;\" statement"
[]
(core/list 'do
(core/list 'js* "debugger")
nil))
(core/defmacro js-comment
"Emit a top-level JavaScript multi-line comment. New lines will create a
new comment line. Comment block will be preceded and followed by a newline"
[comment]
(core/let [[x & ys] (string/split comment #"\n")]
(core/list 'js*
(core/str
"\n/**\n"
(core/str " * " x "\n")
(core/->> ys
(map #(core/str " * " (string/replace % #"^ " "") "\n"))
(reduce core/str ""))
" */\n"))))
(core/defmacro unsafe-cast
"EXPERIMENTAL: Subject to change. Unsafely cast a value to a different type."
[t x]
(core/let [cast-expr (core/str "~{} = /** @type {" t "} */ (~{})")]
(core/list 'js* cast-expr x x)))
(core/defmacro js-inline-comment
"Emit an inline JavaScript comment."
[comment]
(core/list 'js* (core/str "/**" comment "*/")))
(core/defmacro true? [x]
(bool-expr (core/list 'js* "~{} === true" x)))
(core/defmacro false? [x]
(bool-expr (core/list 'js* "~{} === false" x)))
(core/defmacro string? [x]
(bool-expr (core/list 'js* "typeof ~{} === 'string'" x)))
;; TODO: x must be a symbol, not an arbitrary expression
(core/defmacro exists?
"Return true if argument exists, analogous to usage of typeof operator
in JavaScript."
[x]
(bool-expr
(core/list 'js* "typeof ~{} !== 'undefined'"
(vary-meta x assoc :cljs.analyzer/no-resolve true))))
(core/defmacro undefined?
"Return true if argument is identical to the JavaScript undefined value."
[x]
(bool-expr (core/list 'js* "(void 0 === ~{})" x)))
(core/defmacro identical? [a b]
(bool-expr (core/list 'js* "(~{} === ~{})" a b)))
(core/defmacro instance? [c x]
;; Google Closure warns about some references to RegExp, so
;; (instance? RegExp ...) needs to be inlined, but the expansion
;; should preserve the order of argument evaluation.
(bool-expr (if (clojure.core/symbol? c)
(core/list 'js* "(~{} instanceof ~{})" x c)
`(let [c# ~c x# ~x]
(~'js* "(~{} instanceof ~{})" x# c#)))))
(core/defmacro number? [x]
(bool-expr (core/list 'js* "typeof ~{} === 'number'" x)))
(core/defmacro symbol? [x]
(bool-expr `(instance? Symbol ~x)))
(core/defmacro keyword? [x]
(bool-expr `(instance? Keyword ~x)))
(core/defmacro aget
([array idx]
(core/case (ana/checked-arrays)
:warn `(checked-aget ~array ~idx)
:error `(checked-aget' ~array ~idx)
(core/list 'js* "(~{}[~{}])" array idx)))
([array idx & idxs]
(core/case (ana/checked-arrays)
:warn `(checked-aget ~array ~idx ~@idxs)
:error `(checked-aget' ~array ~idx ~@idxs)
(core/let [astr (apply core/str (repeat (count idxs) "[~{}]"))]
`(~'js* ~(core/str "(~{}[~{}]" astr ")") ~array ~idx ~@idxs)))))
(core/defmacro aset
([array idx val]
(core/case (ana/checked-arrays)