-
Notifications
You must be signed in to change notification settings - Fork 13
/
core.clj
183 lines (137 loc) · 5.45 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
(ns oops.core
"Public macros to be consumed via core.cljs."
(:require [clojure.spec.alpha :as s]
[oops.codegen :refer [gen-oget gen-oset gen-ocall gen-oapply]]
[oops.compiler :refer [with-compiler-context! with-suppressed-reporting!]]))
; -- core macros ------------------------------------------------------------------------------------------------------------
(defmacro oget [obj & selector]
(with-compiler-context! &form &env
(gen-oget obj selector)))
(defmacro oget+ [obj & selector]
(with-compiler-context! &form &env
(with-suppressed-reporting! :dynamic-selector-usage
(gen-oget obj selector))))
(defmacro oset! [obj & selector+val]
(with-compiler-context! &form &env
(gen-oset obj selector+val)))
(defmacro oset!+ [obj & selector+val]
(with-compiler-context! &form &env
(with-suppressed-reporting! :dynamic-selector-usage
(gen-oset obj selector+val))))
(defmacro ocall [obj selector & args]
(with-compiler-context! &form &env
(gen-ocall obj selector args)))
(defmacro ocall+ [obj selector & args]
(with-compiler-context! &form &env
(with-suppressed-reporting! :dynamic-selector-usage
(gen-ocall obj selector args))))
(defmacro oapply [obj & selector+args]
(with-compiler-context! &form &env
(gen-oapply obj selector+args)))
(defmacro oapply+ [obj & selector+args]
(with-compiler-context! &form &env
(with-suppressed-reporting! :dynamic-selector-usage
(gen-oapply obj selector+args))))
; -- convenience macros -----------------------------------------------------------------------------------------------------
(defmacro ocall!
"This macro is identical to ocall, use it if you want to express a side-effecting call."
[obj selector & args]
(with-compiler-context! &form &env
(gen-ocall obj selector args)))
(defmacro ocall!+
"This macro is identical to ocall, use it if you want to express a side-effecting call."
[obj selector & args]
(with-compiler-context! &form &env
(with-suppressed-reporting! :dynamic-selector-usage
(gen-ocall obj selector args))))
(defmacro oapply!
"This macro is identical to oapply, use it if you want to express a side-effecting call."
[obj & selector+args]
(with-compiler-context! &form &env
(gen-oapply obj selector+args)))
(defmacro oapply!+
"This macro is identical to oapply, use it if you want to express a side-effecting call."
[obj & selector+args]
(with-compiler-context! &form &env
(with-suppressed-reporting! :dynamic-selector-usage
(gen-oapply obj selector+args))))
; -- convenience g-macros ---------------------------------------------------------------------------------------------------
(defmacro gget [& args]
`(oops.core/oget goog/global ~@args))
(defmacro gget+ [& args]
`(oops.core/oget+ goog/global ~@args))
(defmacro gset! [& args]
`(oops.core/oset! goog/global ~@args))
(defmacro gset!+ [& args]
`(oops.core/oset!+ goog/global ~@args))
(defmacro gcall [& args]
`(oops.core/ocall goog/global ~@args))
(defmacro gcall+ [& args]
`(oops.core/ocall+ goog/global ~@args))
(defmacro gapply [& args]
`(oops.core/oapply goog/global ~@args))
(defmacro gapply+ [& args]
`(oops.core/oapply+ goog/global ~@args))
(defmacro gcall! [& args]
`(oops.core/ocall! goog/global ~@args))
(defmacro gcall!+ [& args]
`(oops.core/ocall!+ goog/global ~@args))
(defmacro gapply! [& args]
`(oops.core/oapply! goog/global ~@args))
(defmacro gapply!+ [& args]
`(oops.core/oapply!+ goog/global ~@args))
; -- specs for our macro apis -----------------------------------------------------------------------------------------------
;
; This is not much useful because we cannot reason about macro args much,
; but I include it because it is catching some edge cases
; and there is a room for possible further refinements.
; Additionally we do ad-hoc validations inside our macros.
(defn anything? [_] true) ; TODO: use any? when we drop Clojure 1.8 support
(defmacro api-spec [obj? args]
`(s/fspec :args (s/cat ~@(if obj? [:obj anything?])
~@(if (symbol? args)
(var-get (resolve args))
args))
:ret anything?))
(defmacro o-api [args]
`(api-spec true ~args))
(defmacro g-api [args]
`(api-spec false ~args))
(def oget-args [:selector (s/* anything?)])
(def oget-api (o-api oget-args))
(def gget-api (g-api oget-args))
(def oset-args [:selector (s/+ anything?) :val anything?])
(def oset-api (o-api oset-args))
(def gset-api (g-api oset-args))
(def ocall-args [:selector anything? :args (s/* anything?)])
(def ocall-api (o-api ocall-args))
(def gcall-api (g-api ocall-args))
(def oapply-args [:selector (s/+ anything?) :args sequential?])
(def oapply-api (o-api oapply-args))
(def gapply-api (g-api oapply-args))
; --- o-api
(s/def oget oget-api)
(s/def oget+ oget-api)
(s/def oset! oset-api)
(s/def oset!+ oset-api)
(s/def ocall ocall-api)
(s/def ocall+ ocall-api)
(s/def ocall! ocall-api)
(s/def ocall!+ ocall-api)
(s/def oapply oapply-api)
(s/def oapply+ oapply-api)
(s/def oapply! oapply-api)
(s/def oapply!+ oapply-api)
; --- g-api
(s/def gget gget-api)
(s/def gget+ gget-api)
(s/def gset! gset-api)
(s/def gset!+ gset-api)
(s/def gcall gcall-api)
(s/def gcall+ gcall-api)
(s/def gcall! gcall-api)
(s/def gcall!+ gcall-api)
(s/def gapply gapply-api)
(s/def gapply+ gapply-api)
(s/def gapply! gapply-api)
(s/def gapply!+ gapply-api)