public
Last active

A name for every CLJS fn

  • Download Gist
better-fn-printing.cljs
Clojure
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
(ns dummy
(:require [clojure.string :as str]))
 
(.log js/console (pr-str name))
 
(defn unmunge [s]
(-> s
(str/replace #"_DOT_" ".")
(str/replace #"_" "-")
))
 
(defn get-func-signature [func]
(let [name (.-name func)]
(when (seq name)
(let [[_ ns fn-name line :as x] (re-find #"fn_(.+)_SLASH_(.+)_(\d+)" name)]
(if x
(str (unmunge ns) "/" (unmunge fn-name) " line:" line)
name)))
))
 
(extend-type js/Function
IPrintWithWriter
(-pr-writer [this writer opts]
(if-let [sig (get-func-signature this)]
(-write writer (str "#<function " sig ">"))
(-write writer (str "#<" this ">"))
)))
 
(.log js/console (pr-str name))
(.log js/console (pr-str js/goog.base))
cljs-better-fn-names.patch
Diff
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
From bbb8c39223130de03c56825c17a7ae9f3730d0e3 Mon Sep 17 00:00:00 2001
From: Thomas Heller <info@zilence.net>
Date: Sun, 17 Feb 2013 17:36:34 +0100
Subject: [PATCH] generate a javascript function <name>() for every fn
 
makes debugging a whole lot easier
---
src/clj/cljs/analyzer.clj | 42 ++++++++++++++++++++++++++++++++++++++++--
1 files changed, 40 insertions(+), 2 deletions(-)
 
diff --git a/src/clj/cljs/analyzer.clj b/src/clj/cljs/analyzer.clj
index f379d8d..59eddcd 100644
--- a/src/clj/cljs/analyzer.clj
+++ b/src/clj/cljs/analyzer.clj
@@ -368,6 +368,42 @@
{:env env :variadic variadic :params params :max-fixed-arity fixed-arity
:type type :form form :recurs @(:flag recur-frame) :expr expr}))
+;; from clojure jvm/clojure/lang/Compiler.java
+;; some of those shouldnt appear ever, better safe than sorry tho
+(def fn-name-replace-chars {\- "_",
+ \. "_DOT_",
+ \: "_COLON_",
+ \+ "_PLUS_",
+ \> "_GT_",
+ \< "_LT_",
+ \= "_EQ_",
+ \~ "_TILDE_",
+ \! "_BANG_",
+ \@ "_CIRCA_",
+ \# "_SHARP_",
+ \', "_SINGLEQUOTE_",
+ \" "_DOUBLEQUOTE_",
+ \% "_PERCENT_",
+ \^ "_CARET_",
+ \& "_AMPERSAND_",
+ \* "_STAR_",
+ \| "_BAR_",
+ \{ "_LBRACE_",
+ \} "_RBRACE_",
+ \[ "_LBRACK_",
+ \] "_RBRACK_",
+ \/ "_SLASH_",
+ \\ "_BSLASH_",
+ \? "_QMARK_"})
+
+(defn make-fn-name [env name]
+ (let [sb (StringBuilder.)]
+ (doseq [c (str "fn-" (get-in env [:ns :name]) "/" (or name (gensym "anon")) "-" (:line env))]
+ (.append sb (fn-name-replace-chars c c)))
+ (symbol (.toString sb))
+ ))
+
+
(defmethod parse 'fn*
[op env [_ & args :as form] name]
(let [[name meths] (if (symbol? (first args))
@@ -376,7 +412,8 @@
;;turn (fn [] ...) into (fn ([]...))
meths (if (vector? (first meths)) (list meths) meths)
locals (:locals env)
- locals (if name (assoc locals name {:name name :shadow (locals name)}) locals)
+ fn-name (make-fn-name env name)
+ locals (if name (assoc locals name {:name fn-name :fn-name fn-name :shadow (locals name)}) locals)
type (-> form meta ::type)
fields (-> form meta ::fields)
protocol-impl (-> form meta :protocol-impl)
@@ -404,13 +441,14 @@
:max-fixed-arity max-fixed-arity
:method-params (map :params methods))
locals)
+
methods (if name
;; a second pass with knowledge of our function-ness/arity
;; lets us optimize self calls
(no-warn (doall (map #(analyze-fn-method menv locals % type) meths)))
methods)]
;;todo - validate unique arities, at most one variadic, variadic takes max required args
- {:env env :op :fn :form form :name name :methods methods :variadic variadic
+ {:env env :op :fn :form form :name fn-name :methods methods :variadic variadic
:recur-frames *recur-frames* :loop-lets *loop-lets*
:jsdoc [(when variadic "@param {...*} var_args")]
:max-fixed-arity max-fixed-arity
--
1.7.6

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.