This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-07-03
Channels
- # announcements (13)
- # babashka-sci-dev (8)
- # beginners (214)
- # calva (2)
- # cider (32)
- # clj-on-windows (17)
- # clojure (88)
- # clojure-conj (2)
- # clojure-europe (26)
- # clojure-nl (1)
- # clojure-norway (27)
- # clojure-sweden (12)
- # clojure-uk (5)
- # cursive (16)
- # datalevin (4)
- # emacs (7)
- # events (2)
- # graalvm (8)
- # honeysql (8)
- # humbleui (7)
- # jobs (7)
- # joyride (46)
- # london-clojurians (1)
- # lsp (13)
- # malli (3)
- # off-topic (9)
- # pathom (14)
- # pedestal (11)
- # polylith (4)
- # reitit (2)
- # releases (3)
- # remote-jobs (3)
- # xtdb (65)
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]])
$ nbb
Welcome to nbb v1.2.179!
user=> (clojure.repl/apropos "tree")
(clojure.core/tree-seq)
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
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
Ah, I was trying with https://clojuredocs.org/clojure.core/ns-aliases 😃
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.
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.
nil
That also gives me
; Could not find namespace: cljs.repl.
With the ns-alias change. Without it I get
; pst does not exist
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.
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]])
Or, probably I should address the whole problem in Calva instead. Treating all cljs repls equal is not the way to go…
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}}))
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)Thanks for awesome service, @U04V15CAJ. https://github.com/BetterThanTomorrow/joyride/pull/189