Skip to content

Commit

Permalink
Fixes mfikes#14 and mfikes#3, moves CIDER fix to client side (big per…
Browse files Browse the repository at this point in the history
…formance boost)
  • Loading branch information
kiranshila committed Aug 17, 2020
1 parent a0a9e64 commit 37f9c3b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
3 changes: 3 additions & 0 deletions src/esprit/make_rom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
[clojure.java.io :as io]
[clojure.string :as string]))

(def argument-global "arguments2462148dcdc") ; Random so we don't get a name collision https://xkcd.com/221/

(defn -main []
(let [main-js (-> (slurp "out/main.js")
(string/replace "arguments" (str "[" argument-global "=arguments," argument-global "][1]"))
(string/replace "/[\\\\\"\\b\\f\\n\\r\\t]/g" "/[\\\\\"\\f\\n\\r\\t]/g")
(string/replace "goog.NONCE_PATTERN_=/^[\\w+/_-]+[=]{0,2}$/" "goog.NONCE_PATTERN_=null")
(string/replace "/^((https:)?\\/\\/[0-9a-z.:[\\]-]+\\/|\\/[^/\\\\]|[^:/\\\\%]+\\/|[^:/\\\\%]*[?#]|about:blank#)/i" "null")
Expand Down
45 changes: 33 additions & 12 deletions src/esprit/repl.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
(ns esprit.repl
(:require
[clojure.string :as string]
[clojure.java.io :as io]
[cljs.compiler :as comp]
[cljs.repl :as repl]
[clojure.data.json :as json]
[config.core :refer [env]])
[clojure.java.io :as io]
[cljs.compiler :as comp]
[cljs.repl :as repl]
[clojure.data.json :as json]
[config.core :refer [env]]
[clojure.string :as str])
(:import
(java.net Socket)
(java.lang StringBuilder)
(java.io BufferedReader BufferedWriter IOException)
(javax.jmdns JmDNS ServiceListener)
(java.net URI)))
(java.net Socket)
(java.lang StringBuilder)
(java.io BufferedReader BufferedWriter IOException)
(javax.jmdns JmDNS ServiceListener)
(java.net URI)))

(def argument-global "arguments2462148dcdc") ; Random so we don't get a name collision

(defn set-logging-level [logger-name level]
"Sets the logging level for a logger to a level."
Expand Down Expand Up @@ -134,10 +136,29 @@
{:pre [(map? s)]}
(.close (:socket s)))

(defn fn-ify
"Wraps bare try-catch into a fn as to properly return pr_str"
[js]
(str "(function (){try{return " (subs js 4 (dec (count js))) "}})()"))

(defn str-replace-js
"Performs str replacements to fix various espruino bugs we find"
[js]
(-> js
(str/replace "arguments" (str "[" argument-global "=arguments," argument-global "][1]"))))

(defn process
"Process outgoing JS to make compatible with Espruino"
[js]
(if (str/starts-with? js "try{cljs.core.pr_str.call")
(do
(str-replace-js (fn-ify js)))
(str-replace-js js)))

(defn write
[out js]
(:pre [(instance? BufferedWriter out) (string? js)])
(.write out js)
(.write out (process js))
(.write out (int 0)) ;; terminator
(.flush out))

Expand Down
32 changes: 12 additions & 20 deletions src/esprit/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@
(defn- write [c o]
(ind/indicate-eval false)
(doto c
(.write (.stringify js/JSON o))
(.write "\0"))
(.write (.stringify js/JSON o))
(.write "\0"))
(ind/indicate-print))

(defn fn-ify
"Wraps bare try-catch into a fn as to properly return pr_str"
[js]
(str "(function(){try{return " (subs js 4 (dec (count js))) "})()"))

(defn eval-data [data]
(let [response (try
(ind/indicate-eval true)
#js {:status "success"
:value (js/eval data)}
(catch :default ex
#js {:status "exception"
:value (str ex)
:stacktrace (.-stack ex)}))]
(write c response)))
(println data)
(try
(ind/indicate-eval true)
#js {:status "success"
:value (js/eval data)}
(catch :default ex
#js {:status "exception"
:value (str ex)
:stacktrace (.-stack ex)})))

(defn- handle-repl-connection [c]
(.log js/console "New REPL Connection")
Expand All @@ -51,10 +46,7 @@
(reset! buffer "")
(cond
(string/starts-with? data "(function (){try{return cljs.core.pr_str")
(eval-data data)

(string/starts-with? data "try{cljs.core.pr_str.call")
(eval-data data)
(write c (eval-data data))

(= data ":cljs/quit")
(.end c)
Expand Down

0 comments on commit 37f9c3b

Please sign in to comment.