/
core.clj
413 lines (339 loc) · 14.4 KB
/
core.clj
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
;; Copyright (c) Alan Dipert and Micha Niskin. 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 javelin.core
(:refer-clojure :exclude [dosync])
(:require
[cljs.core]
[clojure.walk :refer [prewalk]]
[clojure.pprint :as p]
[cljs.analyzer :as a]
[clojure.java.io :as io]
[clojure.string :as s]))
(declare walk)
;; util ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro ^:private with-let*
"Binds resource to binding and evaluates body. Then, returns
resource. It's a cross between doto and with-open."
[[binding resource] & body]
`(let [~binding ~resource] ~@body ~binding))
(def destructure*
"Select a version of #'destructure that works with the version of the CLJS
compiler that is provided. Older versions of CLJS do not provide destructure
so we fall back to using Clojure's destructure function in that case."
(if-not (resolve 'cljs.core/destructure)
destructure
cljs.core/destructure))
(defn extract-syms
"Extract symbols that will be bound by bindings, including autogenerated
symbols produced for destructuring."
[bindings]
(map first (partition 2 (destructure* bindings))))
(defn extract-syms-without-autogen
"Extract only the symbols that the user is binding from bindings, omitting
any intermediate autogenerated bindings used for destructuring. A trick is
used here taking advantage of the fact that gensym names are produced as a
side effect -- successive calls to extract-syms are not redundant."
[bindings]
(let [syms1 (set (extract-syms bindings))
syms2 (set (extract-syms bindings))]
(seq (clojure.set/intersection syms1 syms2))))
(defn bind-syms
"Given a binding form, returns a seq of the symbols that will be bound.
(bind-syms '[{:keys [foo some.ns/bar] :as baz} baf & quux])
;=> (foo bar baz baf quux)"
[form]
(extract-syms-without-autogen [form nil]))
(defn macroexpand*
"Expand form if it is a CLJS macro, otherwise just return form."
[env form]
(if (seq? form)
(let [ex (a/macroexpand-1 env form)]
(if (identical? ex form)
form
(macroexpand* env ex)))
form))
(defn macroexpand-all*
"Fully expand all CLJS macros contained in form."
[env form]
(prewalk (partial macroexpand* env) form))
(defmacro macroexpand-all
"Fully expand all CLJS macros contained in form."
[form]
(macroexpand-all* &env form))
(defmacro mx
"Expand all macros in form and pretty-print them (as code)."
[form]
`(println
~(with-out-str
(p/write (macroexpand-all* &env form) :dispatch p/code-dispatch))))
(defmacro mx2
"Expand all macros in form and pretty-print them (as data)."
[form]
`(println
~(with-out-str
(p/write (macroexpand-all* &env form)))))
;; javelin cells ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:dynamic *env* nil)
(def ^:dynamic *hoist* nil)
(def ^:dynamic *pass* nil)
(create-ns 'js)
(let [to-list #(into '() (reverse %))
special a/specials
special? #(contains? special %)
unsupp?* #(contains? '#{def ns deftype* defrecord*} %)
core? #(contains? #{"clojure.core" "cljs.core" "js"} (namespace %))
empty?* #(= 0 (count %))
dot? #(= '. (first %))
try? #(= 'try (first %))
catch? #(= 'catch (first %))
finally? #(= 'finally (first %))
binding1? #(contains? '#{let* loop*} (first %))
binding2? #(= 'letfn* (first %))
binding3? #(= 'fn* (first %))
catch? #(= 'catch (first %))
quoted? #(= 'quote (first %))
unwrap1? #(= 'clojure.core/unquote (first %))
unwrap2? #(= 'clojure.core/unquote-splicing (first %))
err1 #(format "formula expansion contains unsupported %s form" %)]
(defn unsupp? [x local]
(let [op (first x)]
(and (not (*env* op)) (not (local op)) (unsupp?* op))))
(defn hoist? [x local]
(and (not (or (local x) (core? x))) (or (*env* x) (not (special? x)))))
(defn walk-sym [x local]
(if-not (hoist? x local)
x
(let [h (@*hoist* x)]
(when-not h (swap! *hoist* conj (with-meta x {::h (gensym)})))
(::h (meta (@*hoist* x))))))
(defn walk-map [x local]
(into (empty x) (map #(mapv (fn [x] (walk x local)) %) x)))
(defn walk-seq [x local]
(into (empty x) (map #(walk % local) x)))
(defn walk-bind1 [[sym bindings & body] local]
(let [local (atom local)
bind1 (fn [[k v]]
(with-let* [x [k (walk v @local)]]
(swap! local conj k)))
bindings (mapcat bind1 (partition 2 bindings))]
(to-list `(~sym [~@bindings] ~@(map #(walk % @local) body)))))
(defn walk-catch [[sym etype bind & body] local]
(to-list `(~sym ~etype ~bind ~@(map #(walk % (conj local bind)) body))))
(defn walk-finally [[sym & body] local]
(to-list `(~sym ~@(map #(walk % local) body))))
(defn walk-try [[sym & body] local]
(to-list `(~sym ~@(map #((cond (not (seq? %)) walk
(catch? %) walk-catch
(finally? %) walk-finally
:else walk)
% local)
body))))
(defn walk-bind2 [[sym bindings & body] local]
(let [local (reduce conj local (map first (partition 2 bindings)))
bindings (map #(%1 %2) (cycle [identity #(walk % local)]) bindings)]
(to-list `(~sym [~@bindings] ~@(map #(walk % local) body)))))
(defn walk-bind3 [[sym & arities] local]
(let [fname (when (symbol? (first arities)) [(first arities)])
arities (if fname (rest arities) arities)
arities (if (vector? (first arities)) [arities] arities)
local (if fname (conj local (first fname)) local)]
(let [mkarity (fn [[bindings & body]]
(let [local (into local (remove #(= '& %) bindings))]
(to-list `([~@bindings] ~@(map #(walk % local) body)))))
arities (map mkarity arities)]
(to-list `(~sym ~@fname ~@arities)))))
(defn walk-passthru [x local]
(with-let* [s (gensym)] (swap! *pass* assoc s x)))
(defn walk-dot [[sym obj meth & more] local]
(let [obj (walk obj local)
more (map #(walk % local) more)
walk-meth (fn [m] (list (first m) (map #(walk % local) (rest m))))]
(to-list `(~sym ~obj ~@(if-not (seq? meth) `[~meth ~@more] [(walk-meth meth)])))))
(defn walk-list [x local]
(let [unsupp? #(unsupp? % local)]
(cond (empty?* x) x
(dot? x) (walk-dot x local)
(try? x) (walk-try x local)
(binding1? x) (walk-bind1 x local)
(binding2? x) (walk-bind2 x local)
(binding3? x) (walk-bind3 x local)
(quoted? x) (walk-passthru x local)
(unwrap1? x) (walk-passthru (second x) local)
(unwrap2? x) (walk-passthru (list 'deref (second x)) local)
(unsupp? x) (throw (Exception. (err1 (first x))))
:else (to-list (map #(walk % local) x)))))
(defn walk [x local]
(cond (symbol? x) (walk-sym x local)
(map? x) (walk-map x local)
(set? x) (walk-seq x local)
(vector? x) (walk-seq x local)
(seq? x) (walk-list x local)
:else x))
(defn hoist [x env]
(binding [*env* env, *hoist* (atom #{}), *pass* (atom {})]
(let [body (walk (macroexpand-all* env x) #{})
[params args] (if (empty? @*pass*) [[] []] (apply map vector @*pass*))
params (into params (map #(::h (meta %)) @*hoist*))
args (into args @*hoist*)]
[(list 'fn params body) args])))
(defn cell* [x env]
(let [[f args] (hoist x env)]
(to-list `((formula ~f) ~@args)))))
;; javelin CLJS macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro with-let
"Binds resource to binding and evaluates body. Then, returns
resource. It's a cross between doto and with-open."
[[binding resource] & body]
`(let [~binding ~resource] ~@body ~binding))
(defmacro cell=
([expr] (cell* expr &env))
([expr f]
`(with-let [c# (cell= ~expr)]
(set! (.-update c#) ~f))))
(defmacro set-cell!=
([c expr]
`(set-cell!= ~c ~expr nil))
([c expr updatefn]
(let [[f args] (hoist expr &env)]
`(set-formula! ~c ~f [~@args] ~updatefn))))
(defmacro defc
([sym expr] `(def ~sym (cell ~expr)))
([sym doc expr] `(def ~sym ~doc (cell ~expr))))
(defmacro defc=
([sym expr] `(def ~sym (cell= ~expr)))
([sym doc & [expr f]]
(let [doc? (string? doc)
f (when-let [f' (if doc? f expr)] [f'])
expr (if doc? expr doc)
doc (when doc? [doc])]
`(def ~sym ~@doc (cell= ~expr ~@f)))))
(defmacro formula-of
"ALPHA: this macro may change.
Given a vector of dependencies and one or more body expressions, emits a
form that will produce a formula cell. The dependencies must be names that
will be re-bound to their values within the body. No code walking is done.
The value of the formula cell is computed by evaluating the body expressions
whenever any of the dependencies change.
Note: the dependencies need not be cells.
E.g.
(def x 100)
(def y (cell 200))
(def z (cell= (inc y)))
(def c (formula-of [x y z] (+ x y z)))
(deref c) ;=> 501
(swap! y inc)
(deref c) ;=> 503
"
[deps & body]
(assert (and (vector? deps) (every? symbol? deps))
"first argument must be a vector of symbols")
`((formula (fn [~@deps] ~@body)) ~@deps))
(defmacro formulet
"ALPHA: this macro may change.
Given a vector of binding-form/dependency pairs and one or more body
expressions, emits a form that will produce a formula cell. Each binding
form is bound to the value of the corresponding dependency within the body.
No code walking is done. The value of the formula cell is computed by
evaluating the body expressions whenever any of the dependencies change.
Note: the depdendency expressions are evaluated only once, when the formula
cell is created, and they need not evaluate to javelin cells.
E.g.
(def a (cell 42))
(def b (cell {:x 100 :y 200}))
(def c (formulet [v (cell= (inc a))
w (+ 1 2)
{:keys [x y]} b]
(+ v w x y)))
(deref c) ;=> 346
(swap! a inc)
(deref c) ;=> 347
"
[bindings & body]
(let [binding-pairs (partition 2 bindings)]
(assert (and (vector? bindings) (even? (count binding-pairs)))
"first argument must be a vector of binding pairs")
`((formula (fn [~@(map first binding-pairs)] ~@body))
~@(map second binding-pairs))))
(defmacro ^:private cell-let-1 [[bindings c] & body]
(let [syms (bind-syms bindings)
dcell `((formula (fn [~bindings] [~@syms])) ~c)]
`(let [[~@syms] (cell-map identity ~dcell)] ~@body)))
(defmacro cell-let
[[bindings c & more] & body]
(if-not (seq more)
`(cell-let-1 [~bindings ~c] ~@body)
`(cell-let-1 [~bindings ~c]
(cell-let ~(vec more) ~@body))))
(defmacro dosync
"Evaluates the body within a Javelin transaction. Propagation of updates
to formula cells is deferred until the transaction is complete. Input
cells *will* update during the transaction. Transactions may be nested."
[& body]
`(dosync* (fn [] ~@body)))
(defmacro cell-doseq
"Takes a vector of binding-form/collection-cell pairs and one or more body
expressions, similar to clojure.core/doseq. Iterating over the collection
cells produces a sequence of items that may grow, shrink, or update over
time. Whenever this sequence grows the body expressions are evaluated (for
side effects) exactly once for each new location in the sequence. Bindings
are bound to cells that refer to the item at that location.
Consider:
(def things (cell [{:x :a} {:x :b} {:x :c}]))
(cell-doseq [{:keys [x]} things]
(prn :creating @x)
(add-watch x nil #(prn :updating %3 %4)))
;; the following is printed -- note that x is a cell:
:creating :a
:creating :b
:creating :c
Shrink things by removing the last item:
(swap! things pop)
;; the following is printed (because the 3rd item in things is now nil,
;; since things only contains 2 items) -- note that the doit function is
;; not called (or we would see a :creating message):
:updating :c nil
Grow things such that it is one item larger than it ever was:
(swap! things into [{:x :u} {:x :v}])
;; the following is printed (because things now has 4 items, so the 3rd
;; item is now {:x :u} and the max size increases by one with the new
;; item {:x :v}):
:updating nil :u
:creating :v
A weird imagination is most useful to gain full advantage of all the features."
[bindings & body]
(if (= 2 (count bindings))
`(cell-doseq*
((formula seq) ~(second bindings))
(fn [item#] (cell-let [~(first bindings) item#] ~@body)))
(let [pairs (partition 2 bindings)
lets (->> pairs (filter (comp (partial = :let) first)) (mapcat second))
binds* (->> pairs (take-while (complement (comp keyword? first))))
mods* (->> pairs (drop-while (complement (comp keyword? first))) (mapcat identity))
syms (->> binds* (mapcat (comp bind-syms first)))
exprs (->> binds* (map second))
gens (take (count exprs) (repeatedly gensym))
fors (-> (->> binds* (map first)) (interleave gens) (concat mods*))]
`(cell-doseq*
((formula (fn [~@gens] (for [~@fors] [~@syms]))) ~@exprs)
(fn [item#] (cell-let [[~@syms] item#, ~@lets] ~@body))))))
;; FIXME: this macro doesn't work correctly, maybe mutation observers?
(defmacro prop-cell
([prop]
`(let [ret# (cell ~prop)]
(js/setInterval #(reset! ret# ~prop) 100)
(cell= ret#)))
([prop setter & [callback]]
`(let [setter# ~setter
callback# (or ~callback identity)]
(cell= (set! ~prop setter#))
(js/setInterval
#(when (not= @setter# ~prop)
(callback# ~prop)
(set! ~prop @setter#))
100)
setter#)))