This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
/
repl.cljs
1477 lines (1314 loc) · 48.3 KB
/
repl.cljs
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
(ns lumo.repl
(:refer-clojure :exclude [load-file*])
(:require-macros [cljs.env.macros :as env]
[cljs.analyzer.macros :refer [no-warn]]
[lumo.repl :refer [with-err-str]])
(:require [cljs.analyzer :as ana]
[cljs.compiler :as comp]
[cljs.env :as env]
[cljs.js :as cljs]
[cljs.reader :as reader]
[cljs.repl]
[cljs.stacktrace :as st]
[cljs.tagged-literals :as tags]
[cljs.tools.reader :as r]
[cljs.tools.reader.reader-types :as rt]
[clojure.string :as string]
[cognitect.transit :as transit]
[goog.object :as gobj]
[lumo.js-deps :as deps]
[lumo.common :as common]
[lumo.pprint.data :as pprint]
[lumo.repl-resources :refer [special-doc-map repl-special-doc-map]])
(:import [goog.string StringBuffer]))
;; =============================================================================
;; Globals
(def ^{:dynamic true
:doc "*pprint-results* controls whether Lumo REPL results are
pretty printed. If it is bound to logical false, results
are printed in a plain fashion. Otherwise, results are
pretty printed."}
*pprint-results* true)
(def ^:private ^:dynamic *loading-foreign* false)
(def ^:private ^:dynamic *executing-path* nil)
(defonce ^:private st (cljs/empty-state))
(defonce ^:private current-ns (volatile! 'cljs.user))
(defonce ^:private app-opts (volatile! nil))
(def ^:private ^:const could-not-eval-regex #"Could not eval")
(def ^:private ^:const MACROS_SUFFIX "$macros")
(def ^:private ^:const JS_EXT ".js")
(def ^:private ^:const CACHE_SUFFIX ".cache.json")
;; =============================================================================
;; Dependency loading
(defn- filename->lang
"Converts a filename to a lang keyword by inspecting the file
extension."
[filename]
(if (string/ends-with? filename JS_EXT)
:js
:clj))
(defn- replace-extension
"Replaces the extension on a file."
[filename new-extension]
(string/replace filename #".clj[sc]?$" new-extension))
(defn- parse-edn
"Parses edn source to Clojure data."
[edn-source]
(reader/read-string edn-source))
(defn- filenames-to-try
"Produces a sequence of filenames to try reading, in the
order they should be tried."
[macros path]
(let [extensions (if macros
[".clj" ".cljc"]
[".cljs" ".cljc" ".js"])]
(for [extension extensions]
(str path extension))))
(defn- closure-index* []
(let [paths-to-deps
(map (fn [[_ path provides requires]]
[path
(map second
(re-seq #"'(.*?)'" provides))
(map second
(re-seq #"'(.*?)'" requires))])
(re-seq #"\ngoog\.addDependency\('(.*)', \[(.*?)\], \[(.*?)\].*"
(js/$$LUMO_GLOBALS.load "goog/deps.js")))]
(into {}
(for [[path provides requires] paths-to-deps
provide provides]
[(symbol provide) {:path (str "goog/" (second (re-find #"(.*)\.js$" path)))
:requires requires}]))))
(def ^:private closure-index (memoize closure-index*))
(defonce goog-loaded
(volatile! '#{goog.object
goog.string
goog.string.StringBuffer
goog.array
goog.crypt.base64
goog.math.Long}))
(defn- goog-dep-source [name]
(let [index (closure-index)]
(when-let [{:keys [path]} (get index name)]
(let [sorted-deps (remove @goog-loaded (deps/topo-sort index name))]
(vswap! goog-loaded into sorted-deps)
(reduce str
(map (fn [dep-name]
(let [{:keys [path]} (get index dep-name)]
(js/$$LUMO_GLOBALS.load (str path JS_EXT)))) sorted-deps))))))
(defn- load-goog
"Loads a Google Closure implementation source file. `goog` namespaces are
actually already included in the bundle because we compile with simple
optimizations."
[name cb]
(if-let [source (goog-dep-source name)]
(cb {:source source
:lang :js})
(cb nil)))
(defn- skip-load-js?
"Indicates namespaces for which JS code is already loaded, but for which
we might need to load the corresponding analysis cache."
[name macros]
(and (not macros)
(contains?
'#{cljs.analyzer
cljs.compiler
cljs.env
cljs.js
cljs.reader
cljs.repl
cljs.source-map
cljs.source-map.base64
cljs.source-map.base64-vlq
cljs.spec.alpha
cljs.spec.gen.alpha
cljs.tagged-literals
cljs.tools.reader
cljs.tools.reader.reader-types
cljs.tools.reader.impl.commons
cljs.tools.reader.impl.utils
clojure.core.rrb-vector
clojure.core.rrb-vector.interop
clojure.core.rrb-vector.nodes
clojure.core.rrb-vector.protocols
clojure.core.rrb-vector.rrbt
clojure.core.rrb-vector.transients
clojure.core.rrb-vector.trees
clojure.string
clojure.set
clojure.walk
cognitect.transit
fipp.visit
fipp.engine
fipp.deque
lazy-map.core
lumo.core
lumo.pprint.data
lumo.repl
lumo.repl-resources
lumo.js-deps
lumo.common}
name)))
(defn- skip-load?
[name macros?]
(if macros?
(contains?
'#{cljs.core
cljs.js
cljs.repl
lazy-map.core
clojure.core.rrb-vector.macros}
name)
(or
(contains?
'#{goog
goog.object
goog.string
goog.string.StringBuffer
goog.array
goog.crypt.base64
goog.math.Long
cljs.core
com.cognitect.transit
com.cognitect.transit.delimiters
com.cognitect.transit.handlers
com.cognitect.transit.util
com.cognitect.transit.caching
com.cognitect.transit.types
com.cognitect.transit.eq
com.cognitect.transit.impl.decoder
com.cognitect.transit.impl.reader
com.cognitect.transit.impl.writer}
name)
(ana/node-module-dep? name))))
(declare inject-lumo-eval)
(defn- run-sync!
"Like cljs.js/run-async!, but with the expectation that cb will be called
synchronously within proc. When callbacks are done synchronously, run-async!
ends up growing the stack as coll is processed, while this implementation
employs recur."
[proc coll break? cb]
(loop [coll coll]
(if (seq coll)
(let [cb-val (volatile! nil)]
(proc (first coll) #(vreset! cb-val %))
(if (break? @cb-val)
(cb @cb-val)
(recur (rest coll))))
(doto nil cb))))
;; Monkey-patch cljs.js/run-async! to instead be our more stack-efficient run-sync!
(set! cljs/run-async! run-sync!)
(defn- load-bundled [name path file-path source cb]
(when-let [cache-json (or (js/$$LUMO_GLOBALS.load (str file-path ".cache.json"))
(js/$$LUMO_GLOBALS.load (str path ".cache.json")))]
(cb {:source source
:lang :js
:cache (common/transit-json->cljs cache-json)})
(when (symbol-identical? name 'cljs.spec.test.alpha$macros)
(inject-lumo-eval 'cljs.spec.test.alpha$macros))
:loaded))
;; TODO: we could be smarter and only load the libs that we haven't already loaded
(defn- load-js-lib
[name cb]
(let [sources (mapcat (fn [{:keys [file requires]}]
(concat (->> requires
(filter #(.startsWith % "goog."))
(map (comp goog-dep-source symbol)))
[(.-source (js/$$LUMO_GLOBALS.readSource file))]))
(deps/js-libs-to-load name))]
(binding [*loading-foreign* true]
(cb {:lang :js
:source (string/join "\n" sources)})
:loaded)))
;; TODO: build options (e.g.: static-fns) also affect cache
(defn- cached-callback-data
[cache-dir cache-prefix source-data]
(when-not (nil? cache-dir)
(let [cache-filename (str cache-prefix JS_EXT)
cached-data (js/$$LUMO_GLOBALS.readCache cache-filename)]
(when (and cached-data
(> (.-modified cached-data) (.-modified source-data)))
(when-let [cache-json (js/$$LUMO_GLOBALS.readCache (str cache-prefix CACHE_SUFFIX))]
{:lang :js
:source (.-source cached-data)
:filename cache-filename
:cache (common/transit-json->cljs (.-source cache-json))})))))
;; TODO: can be optimized e.g. to just analyze CLJ sources if JS present
;; but no analysis cache
(defn- load-external
[path filename macros? cb]
(when-let [source-data (or (js/$$LUMO_GLOBALS.readSource filename)
(js/$$LUMO_GLOBALS.readFile filename))]
(let [cache-dir (:cache-path @app-opts)
cache-prefix (str cache-dir "/" (munge path) (when macros? MACROS_SUFFIX))]
(if-let [cached-callback-data (cached-callback-data cache-dir cache-prefix source-data)]
(cb cached-callback-data)
(let [ret {:lang (filename->lang filename)
:file filename
:source (.-source source-data)}]
(if (or (string/ends-with? filename ".cljs")
(string/ends-with? filename ".cljc"))
(if-let [javascript-source (js/$$LUMO_GLOBALS.readSource (replace-extension filename JS_EXT))]
(if-let [cache-edn (js/$$LUMO_GLOBALS.readSource (str filename ".cache.edn"))]
(cb {:lang :js
:file filename
:source (.-source javascript-source)
:cache (parse-edn cache-edn)})
;; one last attempt to read analysis cache
(if-let [cache-json (js/$$LUMO_GLOBALS.readSource (str filename CACHE_SUFFIX))]
(cb {:lang :js
:file filename
:source (.-source javascript-source)
:cache (common/transit-json->cljs cache-json)})
(cb ret)))
(cb ret))
(cb ret))))
:loaded)))
(defn- load-and-cb!
[name path file-path macros? cb]
(let [name (if macros?
(symbol (str name MACROS_SUFFIX))
name)
bundled-src-prefix (cond-> path
macros? (str MACROS_SUFFIX))
bundled-source (js/$$LUMO_GLOBALS.load (str bundled-src-prefix JS_EXT))]
(cond
(skip-load-js? name macros?)
(load-bundled name bundled-src-prefix file-path "" cb)
(some? bundled-source)
;; bundled source are AOTed macros which don't have the `.clj[sc]*` extension
(load-bundled name bundled-src-prefix file-path bundled-source cb)
(deps/js-lib? name)
(load-js-lib name cb)
:else
(load-external path file-path macros? cb))))
(defn- load-other [{:keys [name path macros file]} cb]
(loop [paths (filenames-to-try macros path)]
(if-let [file-path (first paths)]
(when-not (load-and-cb! name path file-path macros cb)
(recur (next paths)))
(cb nil))))
(defn- load-file* [filename cb]
(let [path (string/replace filename #"\.[^/.]+$" "")]
(when-not (load-and-cb! nil path filename false cb)
(cb nil))))
(defn- load [{:keys [name macros path file] :as m} cb]
(cond
file
(load-file* file cb)
(skip-load? name macros)
(cb {:source ""
:lang :js})
(re-matches #"^goog/.*" path)
(load-goog name cb)
:else (load-other m cb)))
(defn- macros-cache? [cache]
(.endsWith (str (:name cache)) MACROS_SUFFIX))
(defn- handle-caching-error
[error]
(throw (ex-info (str "Failed writing cache to " (.-path error))
{:js-error error})))
(defn- write-cache
[name path source cache prefix-path]
(letfn [(wrap-error [err]
(when err
(handle-caching-error err)))]
(let [macros? (macros-cache? cache)
filename-prefix (str prefix-path "/" (munge path) (when macros? MACROS_SUFFIX))
cache-json (common/cljs->transit-json cache)]
(wrap-error (js/$$LUMO_GLOBALS.writeCache (str filename-prefix JS_EXT) source))
(wrap-error (js/$$LUMO_GLOBALS.writeCache (str filename-prefix CACHE_SUFFIX) cache-json)))))
(defn- caching-node-eval
"Evaluates JavaScript in node, writing source and analysis cache to disk
when desired."
[{:keys [name source cache path]}]
(when-let [cache-path (and source cache path (:cache-path @app-opts))]
(write-cache name path source cache cache-path))
(let [foreign? *loading-foreign*
exec-path *executing-path*]
(set! *loading-foreign* false)
(set! *executing-path* nil)
(js/$$LUMO_GLOBALS.eval source foreign? exec-path)))
;; =============================================================================
;; REPL plumbing
;; --------------------
;; Brace highlighting
(declare repl-read-string)
(defn- is-completely-readable?
[source]
(try
(let [[_ chars] (repl-read-string source)]
(string/blank? chars))
(catch :default _
false)))
(def ^:private matches
{")" "("
"}" "{"
"]" "["})
(defn- form-start
[source pos]
(let [source (subs source 0 (inc pos))
match (get matches (aget source pos))]
(loop [searchable-source source]
(let [match-idx (.lastIndexOf searchable-source match)
idx (cond-> match-idx
(identical? (aget source (dec match-idx)) "#")
dec)
candidate-form (subs source idx (inc pos))]
(when-not (neg? idx)
(if (is-completely-readable? candidate-form)
match-idx
(recur (subs source 0 match-idx))))))))
(defn- calc-highlight-coords
[lines start-idx pos]
(if start-idx
(let [line-count (.-length lines)]
(if (== line-count 1)
#js [start-idx 0]
(loop [start-idx start-idx
line-ndx line-count
lines lines]
(let [line-char-count (count (first lines))]
(if (< start-idx line-char-count)
#js [start-idx (dec line-ndx)]
(do
(.shift lines)
(recur (- start-idx (inc line-char-count)) (dec line-ndx) lines)))))))
[-1 -1]))
(defn- ^:export get-highlight-coordinates
"Returns the highlight coordinates [cursorX dy] for the matching brace of the
one at pos. cursorX is the horizontal position of the cursor starting from
the beginning of the line. dy is the number of lines above the current
cursor where the matching brace is located."
[lines pos]
(let [source (string/join "\n" lines)
pos (+ pos (inc (.lastIndexOf source "\n")))
start-idx (form-start source pos)]
(calc-highlight-coords lines start-idx pos)))
;; --------------------
;; Error handling
(defn- ^:boolean could-not-eval? [msg]
(and (not (nil? msg)) (boolean (re-find could-not-eval-regex msg))))
(defn- form-indicator-str
[column current-ns]
(str
(apply str
(repeat (+ (count (name current-ns)) 2 column) " "))
"⬆"))
(defn- print-error-column-indicator
[error]
(when-let [cause (and (could-not-eval? (ex-message error)) (ex-cause error))]
(let [{:keys [column]} (ex-data cause)
column-indicator-str (form-indicator-str column @current-ns)]
(println column-indicator-str))))
(defn- extract-cljs-js-error [error]
(let [message (ex-message error)]
(cond-> error
(and (instance? ExceptionInfo error)
(= :cljs/analysis-error (:tag (ex-data error)))
(or (identical? "ERROR" message)
(could-not-eval? message))
(ex-cause error))
ex-cause)))
(defn- ^:boolean reader-error?
[e]
(keyword-identical? :reader-exception (:type (ex-data e))))
(defn- location-info
[error]
(let [data (ex-data error)]
(when (and (:line data)
(:file data))
(str " at line " (:line data) " " (:file data)#_(file-path (:file data))))))
(declare all-ns ns-syms)
(defn- print-error
([error stacktrace?]
(print-error error stacktrace? nil))
([error stacktrace? printed-message]
(binding [*print-fn* *print-err-fn*]
(print-error-column-indicator error)
(let [error (extract-cljs-js-error error)
message (ex-message error)]
(when (or (not ((fnil string/starts-with? "") printed-message message))
stacktrace?)
(println (str message (when (reader-error? error)
(location-info error)))))
#_(when-let [data (and print-ex-data? (ex-data error))]
(print-value data {::as-code? false}))
(when stacktrace?
(let [canonical-stacktrace (st/parse-stacktrace
{}
(.-stack error)
{:ua-product :nodejs}
{:output-dir "file://(/goog/..)?"})]
(println
(st/mapped-stacktrace-str
canonical-stacktrace
{}
nil))))
(when-let [cause (.-cause error)]
(recur cause stacktrace? message))))))
(defn- handle-error [error stacktrace?]
(print-error error stacktrace?)
(if-not (:repl? @app-opts)
(set! (. js/process -exitCode) 1)
(set! *e (extract-cljs-js-error error))))
;; --------------------
;; REPL specials
(defn- drop-macros-suffix
[ns-name]
(if (string/ends-with? ns-name MACROS_SUFFIX)
(apply str (drop-last 7 ns-name))
ns-name))
(defn- maybe-eliminate-macros-suffix
[x]
(if (symbol? x)
(symbol (drop-macros-suffix (namespace x)) (name x))
x))
(defn- add-macros-suffix
[sym]
(symbol (str (name sym) MACROS_SUFFIX)))
(defn- all-ns
"Returns a sequence of all namespaces."
[]
(keys (::ana/namespaces @st)))
(defn- get-namespace
"Gets the AST for a given namespace."
[ns]
(get-in @st [::ana/namespaces ns]))
(defn- resolve-var
"Given an analysis environment resolve a var. Analogous to
clojure.core/resolve"
[env sym]
{:pre [(map? env) (symbol? sym)]}
(try
(ana/resolve-var env sym
(ana/confirm-var-exists-throw))
(catch :default _
(ana/resolve-macro-var env sym))))
(defn- get-macro-var
[env sym macros-ns]
{:pre [(symbol? macros-ns)]}
(when-let [macro-var (env/with-compiler-env st
(resolve-var env (symbol macros-ns (name sym))))]
(assoc macro-var :ns macros-ns)))
(defn- get-var
[env sym]
(binding [ana/*cljs-warning-handlers* nil]
(let [var (or (env/with-compiler-env st (resolve-var env sym))
(some #(get-macro-var env sym %)
(vals (get-in @st [::ana/namespaces @current-ns :use-macros]))))]
(when var
(-> (cond-> var
(not (:ns var))
(assoc :ns (symbol (namespace (:name var))))
(identical? (namespace (:name var)) (str (:ns var)))
(update :name #(symbol (name %))))
(update :ns (comp symbol drop-macros-suffix str)))))))
(defn- get-aenv []
(assoc (ana/empty-env)
:ns (get-namespace @current-ns)
:context :expr))
(defn- ns-syms
[nsname pred]
(into []
(comp (filter pred) (map key))
(:defs (get-namespace nsname))))
(defn- public-syms
"Returns a sequence of the public symbols in a namespace."
[nsname]
(ns-syms nsname
(fn [[_ attrs]]
(and (not (:private attrs))
(not (:anonymous attrs))))))
(defn- compiler-state-backup []
{:st @st
:loaded @cljs/*loaded*})
(defn- restore-compiler-state! [backup]
(reset! st (:st backup))
(reset! cljs/*loaded* (:loaded backup)))
(defn- wrap-self
"Takes a self-ish fn and returns it wrapped with exception handling.
Compiler state is restored if self-ish fn fails."
[f]
(fn g
([a b]
(let [backup (compiler-state-backup)]
(try
(f a b)
(catch :default e
(restore-compiler-state! backup)
(throw e)))))))
(defn- root-resource
"Returns the root directory path for a lib"
[lib]
(str \/
(.. (name lib)
(split \-) (join \_)
(split \.) (join \/))))
(defn- root-directory
"Returns the root resource path for a lib"
[lib]
(let [d (root-resource lib)]
(subs d 0 (.lastIndexOf d "/"))))
(defn- load-path->cp-path
[path]
(let [src (if (identical? "/" (first path))
path
(str (root-directory @current-ns) \/ path))
src (.substring src 1)]
(or (and (js/$$LUMO_GLOBALS.resource (str src ".cljs")) (str src ".cljs"))
(str src ".cljc"))))
(defn- wrap-special-fns
[wfn fns]
"Wrap wfn around all (fn) values in fns hashmap."
(into {} (for [[k v] fns] [k (wfn v)])))
(declare execute-path)
(def ^:private repl-special-fns
(let [load-file-fn
(fn self
[[_ file :as form] opts]
(execute-path file opts))
in-ns-fn
(fn self
[[_ maybe-quoted :as form] opts]
(cljs/eval st maybe-quoted opts
(fn [{:keys [error value]}]
(if error
(handle-error error true)
(let [ns-name value]
(if-not (symbol? ns-name)
(binding [*print-fn* *print-err-fn*]
(println "Argument to in-ns must be a symbol."))
(if (ana/get-namespace ns-name)
(vreset! current-ns ns-name)
(let [ns-form `(~'ns ~ns-name)]
(cljs/eval st ns-form opts
(fn [{:keys [error]}]
(if error
(handle-error error true)
(vreset! current-ns ns-name))))))))))))]
(wrap-special-fns wrap-self
{'in-ns in-ns-fn
'clojure.core/in-ns in-ns-fn
'load-file load-file-fn
'clojure.core/load-file load-file-fn
'load
(fn self
[[_ & paths :as form] opts]
(let [cp-paths (map load-path->cp-path paths)]
(run! #(execute-path % opts) cp-paths)))})))
(defn- repl-special? [form]
(and (seq? form) (contains? repl-special-fns (first form))))
(defn- dir* [nsname]
(run! prn
(into
(apply sorted-set (public-syms nsname))
(public-syms (add-macros-suffix nsname)))))
(defn- special-doc [name-symbol]
(assoc (special-doc-map name-symbol)
:name name-symbol
:special-form true))
(defn- repl-special-doc [name-symbol]
(assoc (repl-special-doc-map name-symbol)
:name name-symbol
:repl-special-function true))
(defn- undo-reader-conditional-spacing
"Undoes the effect that wrapping a reader conditional around
a defn has on a docstring."
[s]
;; We look for five spaces (or six, in case that the docstring
;; is not aligned under the first quote) after the first newline
;; (or two, in case the doctring has an unpadded blank line
;; after the first), and then replace all five (or six) spaces
;; after newlines with two.
(when-not (nil? s)
(if (re-find #"[^\n]*\n\n?\s{5,6}\S.*" s)
(string/replace-all s #"\n ?" "\n ")
s)))
;; TODO: proper spec support (due to $macros), need to write own print-doc fn
(defn- doc* [name]
(if-let [special-name ('{& fn catch try finally try} name)]
(doc* special-name)
(cond
(special-doc-map name)
(cljs.repl/print-doc (merge (special-doc-map name)
{:special-form true
:name name}))
(repl-special-doc-map name)
(cljs.repl/print-doc (repl-special-doc name))
(get-namespace name)
(cljs.repl/print-doc (select-keys (get-namespace name) [:name :doc]))
(get-var (get-aenv) name)
(cljs.repl/print-doc
(let [aenv (get-aenv)
var (get-var aenv name)
m (-> (select-keys var
[:ns :name :doc :forms :arglists :macro :url])
(update-in [:doc] undo-reader-conditional-spacing)
(merge
{:forms (-> var :meta :forms second)
:arglists (-> var :meta :arglists second)}))]
(cond-> (update-in m [:name] clojure.core/name)
(:protocol-symbol var)
(assoc :protocol true
:methods
(->> (get-in var [:protocol-info :methods])
(map (fn [[fname sigs]]
[fname {:doc (:doc
(get-var aenv
(symbol (str (:ns var)) (str fname))))
:arglists (seq sigs)}]))
(into {})))))))))
(defn- get-file-source
[filepath]
(if (symbol? filepath)
(let [without-extension (string/replace
(string/replace (name filepath) #"\." "/")
#"-" "_")]
(or
(js/$$LUMO_GLOBALS.load (str without-extension ".clj"))
(js/$$LUMO_GLOBALS.load (str without-extension ".cljc"))
(js/$$LUMO_GLOBALS.load (str without-extension ".cljs"))))
(or
(js/$$LUMO_GLOBALS.load filepath)
(some-> (js/$$LUMO_GLOBALS.readSource filepath) .-source)
(js/$$LUMO_GLOBALS.load (string/replace filepath #"^/.*/main.out/" "")))))
(defn- fetch-source
[var]
(or (::repl-entered-source var)
(when-let [filepath (or (:file var) (:file (:meta var)))]
(when-let [file-source (get-file-source filepath)]
(let [rdr (rt/source-logging-push-back-reader file-source)]
(dotimes [_ (dec (:line var))] (rt/read-line rdr))
(-> (r/read {:read-cond :allow :features #{:cljs}} rdr)
meta :source))))))
(defn- source* [sym]
(println (or (fetch-source (get-var (get-aenv) sym))
"Source not found")))
(defn- namespace-doc [nspace]
(select-keys (get-in @st [::ana/namespaces nspace]) [:name :doc]))
(defn find-doc
"Prints documentation for any var whose documentation or name
contains a match for re-string-or-pattern"
[re-string-or-pattern]
(let [re (re-pattern re-string-or-pattern)
ms (concat (mapcat #(sort-by :name
(map (fn [[k v]]
(assoc (:meta v) :name (symbol % k)))
(get-in @st [::ana/namespaces % :defs])))
(all-ns))
(map namespace-doc (all-ns))
(map special-doc (keys special-doc-map)))]
(doseq [m ms
:when (and (:doc m)
(or (re-find re (:doc m))
(re-find re (str (:name m)))))]
(doc* (:name m)))))
(defn apropos
"Given a regular expression or stringable thing, return a seq of all
public definitions in all currently-loaded namespaces that match the
str-or-pattern."
[str-or-pattern]
(let [matches? (if (instance? js/RegExp str-or-pattern)
#(re-find str-or-pattern (str %))
#(.includes (str %) (str str-or-pattern)))]
(sort
(into []
(comp
(mapcat (fn [ns]
(let [ns-name (drop-macros-suffix (str ns))]
(map #(symbol ns-name (str %))
(filter matches? (public-syms ns))))))
;; we need to call distinct here because in ClojureScript there
;; can exist macros & functions with the same name
(distinct))
(all-ns)))))
;; Taken from planck eval implementation
;; The following atoms and fns set up a scheme to
;; emit function values into JavaScript as numeric
;; references that are looked up.
(defonce ^:private fn-index (volatile! 0))
(defonce ^:private fn-refs (volatile! {}))
(defn- clear-fns!
"Clears saved functions."
[]
(vreset! fn-refs {}))
(defn- put-fn
"Saves a function, returning a numeric representation."
[f]
(let [n (vswap! fn-index inc)]
(vswap! fn-refs assoc n f)
n))
(defn- get-fn
"Gets a function, given its numeric representation."
[n]
(get @fn-refs n))
(defn- emit-fn [f]
(print "lumo.repl.get_fn(" (put-fn f) ")"))
(defmethod comp/emit-constant js/Function
[f]
(emit-fn f))
(defmethod comp/emit-constant cljs.core/Var
[f]
(emit-fn f))
(defn eval
([form]
(eval form (.-name *ns*)))
([form ns]
(eval form ns env/*compiler*))
([form ns compiler]
(let [result (volatile! nil)]
(cljs/eval compiler form
{:ns ns
:context :expr
:def-emits-var true}
(fn [{:keys [value error]}]
(if error
(handle-error error true)
(vreset! result value))))
@result)))
(defn- intern
([ns name]
(intern ns name nil))
([ns name val]
(when-let [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(eval `(def ~name ~val) (ns-name the-ns)))))
(defn- inject-lumo-eval
[target-ns]
(intern target-ns 'eval eval))
;; --------------------
;; Code evaluation
(defn- make-eval-opts []
(merge
{:ns @current-ns
:target :nodejs}
(select-keys @app-opts [:verbose :static-fns :fn-invoke-direct :checked-arrays])))
(defn- current-alias-map []
(let [cur-ns @current-ns]
(into {}
(merge (get-in @st [::ana/namespaces cur-ns :requires])
(get-in @st [::ana/namespaces cur-ns :require-macros])))))
(defn- current-alias?
[ns]
(contains? (set (vals (current-alias-map))) ns))
(defn- reader-eof? [e]
(let [{:keys [ex-kind]} (ex-data e)]
(keyword-identical? ex-kind :eof)))
(defn- read-chars
[reader]
(let [sb (StringBuffer.)]
(loop [c (rt/read-char reader)]
(if-not (nil? c)
(do
(.append sb c)
(recur (rt/read-char reader)))
(str sb)))))
(defn- get-data-readers*
"Returns the merged data reader mappings."
[]
(reduce (fn [data-readers url+source]
(let [url (.-url url+source)
source (.-source url+source)
mappings (r/read-string source)]
(when-not (map? mappings)
(throw (ex-info (str "Not a valid data-reader map")
{:url url})))
(reduce-kv (fn [data-readers tag fn-sym]
(when-not (symbol? tag)
(throw (ex-info (str "Invalid form in data-reader file")
{:url url
:form tag})))
(when (and (get data-readers tag)
(not= (get mappings tag) fn-sym))
(throw (ex-info "Conflicting data-reader mapping"
{:url url
:conflict tag
:mappings data-readers})))
(assoc data-readers tag fn-sym))
data-readers
mappings)))
{}
(js/$$LUMO_GLOBALS.loadUpstreamDataReaders)))
(def ^:private get-data-readers (memoize get-data-readers*))
(defn- load-data-readers!* [compiler]
(let [data-readers (get-data-readers)
nses (map (comp symbol namespace) (vals data-readers))]
(doseq [ns nses]
(try
(eval `(require '~ns))
(catch js/Error _)))
(swap! compiler update-in [::ana/data-readers]
merge (into {} (map (fn [[k v]]
[k (eval v)]))
data-readers))
(::ana/data-readers @compiler)))
(def load-data-readers! (memoize load-data-readers!*))
(defn- repl-read-string
"Returns a vector of the first read form, and any balance text."
[source]
(let [reader (rt/string-push-back-reader source)
cur-ns @current-ns]
(binding [ana/*cljs-ns* cur-ns
*ns* (create-ns cur-ns)
env/*compiler* st
r/*data-readers* (merge tags/*cljs-data-readers* (load-data-readers! env/*compiler*))
r/resolve-symbol ana/resolve-symbol
r/*alias-map* (current-alias-map)]
[(r/read {:read-cond :allow :features #{:cljs}} reader) (read-chars reader)])))
(defn- ns-for-source [source]
(let [[ns-form] (repl-read-string source)
{:keys [op name]} (no-warn (ana/analyze (ana/empty-env) ns-form))]
(when (or (keyword-identical? op :ns)
(keyword-identical? op :ns*))
name)))
(defn- print-value [value]
(if *pprint-results*
(pprint/pprint value)
(prn value)))
(defn- capture-session-state
"Captures all of the commonly set global vars as a session state map."
[]
{:*print-meta* *print-meta*
:*print-length* *print-length*
:*print-level* *print-level*
:*print-namespace-maps* *print-namespace-maps*
:*unchecked-if* *unchecked-if*
:*assert* *assert*
:*1 *1
:*2 *2
:*3 *3
:*e *e
:ns @current-ns})