Skip to content

Commit ce46b80

Browse files
mfikesswannodette
authored andcommitted
CLJS-1436: self-host: Doesn't load dep ns when loading own cache
This revison first loads any :require-macros deps followed by any :requires and :imports deps before executing the original code associated with a :lang :js callback. The revision honors the async callback structure, pushing the original code into the innermost callback position.
1 parent 1b8b492 commit ce46b80

File tree

1 file changed

+68
-17
lines changed

1 file changed

+68
-17
lines changed

src/main/cljs/cljs/js.cljs

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,49 @@
158158

159159
(def *loaded* (atom #{}))
160160

161+
(defn- run-async!
162+
"Like cljs.core/run!, but for an async procedure, and with the
163+
ability to break prior to processing the entire collection.
164+
165+
Chains successive calls to the supplied procedure for items in
166+
the collection. The procedure should accept an item from the
167+
collection and a callback of one argument. If the break? predicate,
168+
when applied to the procedure callback value, yields a truthy
169+
result, terminates early calling the supplied cb with the callback
170+
value. Otherwise, when complete, calls cb with nil."
171+
[proc coll break? cb]
172+
(if (seq coll)
173+
(proc (first coll)
174+
(fn [res]
175+
(if (break? res)
176+
(cb res)
177+
(run-async! proc (rest coll) break? cb))))
178+
(cb nil)))
179+
180+
(declare require)
181+
182+
(defn- process-deps
183+
[bound-vars names opts cb]
184+
(run-async! (fn [name cb]
185+
(require bound-vars name nil opts cb))
186+
names
187+
:error
188+
cb))
189+
190+
(defn- process-macros-deps
191+
[bound-vars cache opts cb]
192+
(process-deps bound-vars
193+
(distinct (vals (:require-macros cache)))
194+
(assoc opts :macros-ns true)
195+
cb))
196+
197+
(defn- process-libs-deps
198+
[bound-vars cache opts cb]
199+
(process-deps bound-vars
200+
(distinct (concat (vals (:requires cache)) (vals (:imports cache))))
201+
(dissoc opts :macros-ns)
202+
cb))
203+
161204
(defn require
162205
([name cb]
163206
(require name nil cb))
@@ -201,23 +244,31 @@
201244
(do
202245
(swap! *loaded* conj name)
203246
(cb {:value true})))))
204-
:js (let [res (try
205-
((:*eval-fn* bound-vars) resource)
206-
(when cache
207-
(load-analysis-cache!
208-
(:*compiler* bound-vars) name cache))
209-
(when source-map
210-
(load-source-map!
211-
(:*compiler* bound-vars) name source-map))
212-
(catch :default cause
213-
(wrap-error
214-
(ana/error env
215-
(str "Could not require " name) cause))))]
216-
(if (:error res)
217-
(cb res)
218-
(do
219-
(swap! *loaded* conj name)
220-
(cb {:value true}))))
247+
:js (process-macros-deps bound-vars cache opts
248+
(fn [res]
249+
(if (:error res)
250+
(cb res)
251+
(process-libs-deps bound-vars cache opts
252+
(fn [res]
253+
(if (:error res)
254+
(cb res)
255+
(let [res (try
256+
((:*eval-fn* bound-vars) resource)
257+
(when cache
258+
(load-analysis-cache!
259+
(:*compiler* bound-vars) name cache))
260+
(when source-map
261+
(load-source-map!
262+
(:*compiler* bound-vars) name source-map))
263+
(catch :default cause
264+
(wrap-error
265+
(ana/error env
266+
(str "Could not require " name) cause))))]
267+
(if (:error res)
268+
(cb res)
269+
(do
270+
(swap! *loaded* conj name)
271+
(cb {:value true}))))))))))
221272
(cb (wrap-error
222273
(ana/error env
223274
(str "Invalid :lang specified " lang ", only :clj or :js allowed"))))))

0 commit comments

Comments
 (0)