Skip to content

Commit bb55706

Browse files
committed
Safari stacktrace mapping support in browser REPL
1 parent 8632f04 commit bb55706

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/clj/cljs/repl/browser.clj

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,83 @@
198198
(comp/with-core-cljs nil
199199
(fn [] (server/start repl-env))))
200200

201+
;; =============================================================================
202+
;; Stracktrace parsing
203+
204+
(defmulti parse-stacktrace (fn [repl-env st err opts] (:ua-product err)))
205+
206+
;; -----------------------------------------------------------------------------
207+
;; Safari Stacktrace
208+
209+
(defn safari-st-el->frame
210+
"Parses a stack line into a frame representation, returning nil
211+
if parse failed."
212+
[st-el opts]
213+
(let [[function flc] (if (re-find #"@" st-el)
214+
(string/split st-el #"@")
215+
[nil st-el])
216+
xs (string/split flc #":")
217+
[pre post]
218+
(reduce
219+
(fn [[pre post] [x i]]
220+
(if (<= i 2)
221+
[pre (conj post x)]
222+
[(conj pre x) post]))
223+
[[] []] (map vector xs (range (count xs) 0 -1)))
224+
file (string/join ":" pre)
225+
[line column] (map #(Long/parseLong %) post)]
226+
(if (and file function line column)
227+
{:file (if (re-find #"http://localhost:9000/" file)
228+
(-> file
229+
(string/replace #"http://localhost:9000/" "")
230+
(string/replace (Pattern/compile (str "^" (util/output-directory opts) "/")) ""))
231+
(if-let [asset-root (:asset-root opts)]
232+
(string/replace file asset-root "")
233+
(throw
234+
(ex-info (str "Could not relativize URL " file)
235+
{:type :parse-stacktrace
236+
:reason :relativize-url}))))
237+
:function function
238+
:line line
239+
:column column}
240+
(when-not (string/blank? function)
241+
{:file nil
242+
:function (string/trim function)
243+
:line nil
244+
:column nil}))))
245+
246+
(comment
247+
(safari-st-el->frame
248+
"cljs$core$seq@http://localhost:9000/out/cljs/core.js:4259:17" {})
249+
)
250+
251+
(defmethod parse-stacktrace :safari
252+
[repl-env st err opts]
253+
(->> st
254+
string/split-lines
255+
(take-while #(not (.startsWith % "eval code")))
256+
(remove string/blank?)
257+
(map #(safari-st-el->frame % opts))
258+
(remove nil?)
259+
vec))
260+
261+
(comment
262+
(parse-stacktrace nil
263+
"cljs$core$seq@http://localhost:9000/out/cljs/core.js:4259:17\ncljs$core$first@http://localhost:9000/out/cljs/core.js:4289:22\ncljs$core$ffirst@http://localhost:9000/out/cljs/core.js:5357:39\nhttp://localhost:9000/out/cljs/core.js:16972:92\nhttp://localhost:9000/out/cljs/core.js:16973:3\nhttp://localhost:9000/out/cljs/core.js:10982:133\nsval@http://localhost:9000/out/cljs/core.js:10983:3\ncljs$core$ISeqable$_seq$arity$1@http://localhost:9000/out/cljs/core.js:11074:14\ncljs$core$seq@http://localhost:9000/out/cljs/core.js:4240:44\ncljs$core$pr_sequential_writer@http://localhost:9000/out/cljs/core.js:28707:17\ncljs$core$IPrintWithWriter$_pr_writer$arity$3@http://localhost:9000/out/cljs/core.js:29386:38\ncljs$core$pr_writer_impl@http://localhost:9000/out/cljs/core.js:28912:57\ncljs$core$pr_writer@http://localhost:9000/out/cljs/core.js:29011:32\ncljs$core$pr_seq_writer@http://localhost:9000/out/cljs/core.js:29015:20\ncljs$core$pr_sb_with_opts@http://localhost:9000/out/cljs/core.js:29078:24\ncljs$core$pr_str_with_opts@http://localhost:9000/out/cljs/core.js:29092:48\ncljs$core$pr_str__delegate@http://localhost:9000/out/cljs/core.js:29130:34\ncljs$core$pr_str@http://localhost:9000/out/cljs/core.js:29139:39\n\neval code\neval@[native code]\nhttp://localhost:9000/out/clojure/browser/repl.js:23:271\nclojure$browser$repl$evaluate_javascript@http://localhost:9000/out/clojure/browser/repl.js:26:4\nhttp://localhost:9000/out/clojure/browser/repl.js:121:173\ndeliver@http://localhost:9000/out/goog/messaging/abstractchannel.js:142:21\nxpcDeliver@http://localhost:9000/out/goog/net/xpc/crosspagechannel.js:733:19\nmessageReceived_@http://localhost:9000/out/goog/net/xpc/nativemessagingtransport.js:321:23\nfireListener@http://localhost:9000/out/goog/events/events.js:741:25\nhandleBrowserEvent_@http://localhost:9000/out/goog/events/events.js:862:34\nhttp://localhost:9000/out/goog/events/events.js:276:42"
264+
{:ua-product :safari}
265+
nil)
266+
)
267+
268+
;; =============================================================================
269+
;; BrowserEnv
270+
201271
(defrecord BrowserEnv []
202272
repl/IJavaScriptEnv
203273
(-setup [this opts]
204274
(setup this opts))
275+
repl/IParseStacktrace
276+
(-parse-stacktrace [this st err opts]
277+
(parse-stacktrace this st err opts))
205278
(-evaluate [_ _ _ js] (browser-eval js))
206279
(-load [this provides url]
207280
(load-javascript this provides url))

0 commit comments

Comments
 (0)