I wonder what would be involved to support the cljs.repl utilities. So that this would work:
(require '[cljs.repl :refer [apropos dir doc find-doc print-doc pst source]])
I actually didn’t try much with ns-aliases. I didn’t understand how to leverage it for solvning my problem. And when I tried it it returned an empty map. But that was some older version or sci. Nu tried bumping Joyride to https://github.com/babashka/sci/commit/7fb09a7826f7451ed73587888bc909076394b1ba, but that needs some work somewhere. I get build warnings:
------ WARNING #1 - :undeclared-var --------------------------------------------
File: /Users/pez/.gitlibs/libs/org.babashka/sci/7fb09a7826f7451ed73587888bc909076394b1ba/src/sci/impl/analyzer.cljc:1703:16
--------------------------------------------------------------------------------
1700 | (if (<= children-count 16)
1701 | #?(:clj #(let [^objects arr (into-array Object %&)]
1702 | (clojure.lang.PersistentArrayMap/createWithCheck arr))
1703 | :cljs #(PersistentArrayMap.createWithCheck (into-array %&))
----------------------^---------------------------------------------------------
Use of undeclared Var sci.impl.analyzer/PersistentArrayMap
--------------------------------------------------------------------------------
1704 | :default array-map)
1705 | #?(:clj #(let [^clojure.lang.ISeq s %&]
1706 | (clojure.lang.PersistentHashMap/createWithCheck s))
1707 | :cljs #(PersistentHashMap.createWithCheck (into-array %&))
--------------------------------------------------------------------------------
------ WARNING #2 - :undeclared-var --------------------------------------------
File: /Users/pez/.gitlibs/libs/org.babashka/sci/7fb09a7826f7451ed73587888bc909076394b1ba/src/sci/impl/analyzer.cljc:1707:16
--------------------------------------------------------------------------------
1704 | :default array-map)
1705 | #?(:clj #(let [^clojure.lang.ISeq s %&]
1706 | (clojure.lang.PersistentHashMap/createWithCheck s))
1707 | :cljs #(PersistentHashMap.createWithCheck (into-array %&))
----------------------^---------------------------------------------------------
Use of undeclared Var sci.impl.analyzer/PersistentHashMap
--------------------------------------------------------------------------------
1708 | :default hash-map)))
1709 |
1710 | (defn return-map [ctx the-map analyzed-children]
1711 | (let [mf (map-fn (count analyzed-children))]
--------------------------------------------------------------------------------
------ WARNING #3 - :undeclared-var --------------------------------------------
File: /Users/pez/.gitlibs/libs/org.babashka/sci/7fb09a7826f7451ed73587888bc909076394b1ba/src/sci/impl/analyzer.cljc:1844:51
--------------------------------------------------------------------------------
1841 | vector expr m)
1842 | (set? expr) (analyze-vec-or-set ctx set
1843 | #?(:clj #(clojure.lang.PersistentHashSet/createWithCheck ^clojure.lang.ISeq %&)
1844 | :cljs #(PersistentHashSet.createWithCheck (into-array %&))
---------------------------------------------------------^----------------------
Use of undeclared Var sci.impl.analyzer/PersistentHashSet
--------------------------------------------------------------------------------
1845 | :default vector)
1846 | expr m)
1847 | (seq? expr) (if (seq expr)
1848 | (analyze-call ctx expr m top-level?)
--------------------------------------------------------------------------------
and when using the built Joyride I get:
PersistentArrayMap is not defined
Evaluation of file js_repl.cljs failed: #error {:message "PersistentArrayMap is not defined", :data {:type :sci/error, :line 41, :column 1, :message "PersistentArrayMap is not defined", :sci.impl/callstack #object[cljs.core.Volatile {:val ({:line 41, :column 1, :ns #object[sci.lang.Namespace], :file "/Users/pez/Projects/joyride/examples/.joyride/scripts/js_repl.cljs"} {:line 41, :column 1, :ns #object[sci.lang.Namespace], :file "/Users/pez/Projects/joyride/examples/.joyride/scripts/js_repl.cljs", :sci.impl/f-meta {:ns #object[sci.lang.Namespace], :macro true, :sci/built-in true, :name defn, :arglists ([name doc-string? attr-map? [params*] prepost-map? body] [name doc-string? attr-map? ([params*] prepost-map? body) + attr-map?]), :doc "Same as (def name (core/fn [params* ] exprs*)) or (def\n name (core/fn ([params* ] exprs*)+)) with any doc-string or attrs added\n to the var metadata. prepost-map defines a map with optional keys\n :pre and :post that contain collections of pre or post conditions."}} {:line 41, :column 1, :ns #object[sci.lang.Namespace], :file "/Users/pez/Projects/joyride/examples/.joyride/scripts/js_repl.cljs"})}], :file "/Users/pez/Projects/joyride/examples/.joyride/scripts/js_repl.cljs"}, :cause #object[ReferenceError ReferenceError: PersistentArrayMap is not defined]}
all you need to do is this: https://github.com/nextjournal/clerk/blob/35f5661bc04e684b4a007194af4ca715334573d9/src/nextjournal/clerk/sci_env.cljs#L167
I'll fix those warnings, but I don't get those with regular CLJS
perhaps this is a shadow problem
Ah, I was trying with https://clojuredocs.org/clojure.core/ns-aliases 😃
Try 0.8.43, I fixed the shadow warnings
SCI 0.8.43 that is
Now doing this. https://github.com/BetterThanTomorrow/joyride/blob/cljs-repl/src/joyride/sci.cljs#L128 But still get:
(require '[cljs.repl :refer [apropos dir doc find-doc print-doc pst source]])
; Could not find namespace: cljs.repl.can you just try (require '[clojure.repl]) instead, just for starters?
This works out of the box with SCI 🤷
cljs.user=> (sci/binding [sci/print-fn *print-fn*] (sci/eval-string "(require '[clojure.repl :as r]) (r/doc inc)"))
-------------------------
clojure.core/inc
([x])
Returns a number one greater than num.
nilThat also gives me
; Could not find namespace: cljs.repl.
With the ns-alias change. Without it I get
; pst does not existAh it should be the reverse:
{:ns-aliases '{cljs.repl clojure.repl}so cljs.repl is an alias for clojure.repl which is built into SCI
cljs.user=> (sci/binding [sci/print-fn *print-fn*] (sci/eval-string "(require '[cljs.repl :as r]) (r/doc inc)" {:ns-aliases '{cljs.repl clojure.repl}}))
-------------------------
clojure.core/inc
([x])
Returns a number one greater than num.Ah, thanks!
So now I at how to make it not croak on pst . This gives no errors:
(require '[cljs.repl :refer [apropos dir doc find-doc print-doc source]])
pst doesn't exist in CLJS
sorry, I'm wrong about that
but maybe that's not implemented yet in SCI
yes, it's only implemented for the JVM right now
I can maybe add some NYIP version of pst to the ns in Joyride.
Or, probably I should address the whole problem in Calva instead. Treating all cljs repls equal is not the way to go…
What does nyip mean?
Not Yet ImPlemented. 😃
or we could just add it to SCI ;)
“just” add it 😃 But, yeah, would be awesome.
you could also add it as a function in the joyride SCI config
which just throws with NYIP
How do I run that
cljs.user=> (sci/binding [sci/print-fn *print-fn*] (sci/eval-string "(require '[cljs.repl :as r]) (r/doc inc)" {:ns-aliases '{cljs.repl clojure.repl}}))this is inside of a CLJS repl
source fails on clojure.core things, btw:
user=> (require '[clojure.repl :refer [apropos dir doc find-doc print-doc source]])
nil
user=> (source map)
Source not found
nil
(Same with Joyride or nbb)yes, since the source isn't there, but it'll work on user sources
it also doesn't work in bb for example
Makes sense.
Thanks for awesome service, @borkdude. https://github.com/BetterThanTomorrow/joyride/pull/189
reviewed
I fixed the review feedback. Does it look good to you know?
yezzz
I’m not sure how to employ ns-aliases for this. I am now trying another way. Mostly as a way to educate myself.
For context: it is Calva that makes that require when it connects to a ClojureScript repl.
What I am now trying is to add a sci-config for cljs.repl. Like so:
(ns sci.configs.cljs.repl
(:require [cljs.repl :as repl]
[sci.core :as sci]))
(def repl-ns (sci/create-ns 'cljs.repl))
(defn ^:macro apropos [query]
`(cljs.repl/apropos (str ~query)))
(defn ^:macro dir [ns]
`(cljs.repl/dir ~ns))
(def cljs-repl-namespace (merge (sci/copy-ns cljs.repl repl-ns)
{'apropos (sci/copy-var apropos repl-ns)
'dir (sci/copy-var dir repl-ns)}))
(def namespaces {'cljs.repl cljs-repl-namespace})
(def config {:namespaces namespaces})
Then using sci.configs.cljs.repl/cljs-repl-namespace in the joyride code to add the cljs.repl namespace. Obviously I do not know what I am doing, and I get Maximum call stack size exceeded when trying to use apropos or dir from my Joyride script.
Am I even close?can you show what you tried with ns-aliases instead? Also make sure you’re using the latest Sci
Some of these things are already there under clojure.repl I think
$ nbb
Welcome to nbb v1.2.179!
user=> (clojure.repl/apropos "tree")
(clojure.core/tree-seq)to make it work for cljs.repl I think ns-aliases can be used
🙏