Fork me on GitHub
#joyride
<
2024-07-03
>
pez10:07:39

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]])

borkdude13:07:40

Some of these things are already there under clojure.repl I think

borkdude13:07:00

$ nbb
Welcome to nbb v1.2.179!
user=> (clojure.repl/apropos "tree")
(clojure.core/tree-seq)

borkdude13:07:37

to make it work for cljs.repl I think ns-aliases can be used

pez11:08:24

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?

borkdude12:08:42

can you show what you tried with ns-aliases instead? Also make sure you’re using the latest Sci

pez09:08:54

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]}

borkdude10:08:20

I'll fix those warnings, but I don't get those with regular CLJS

borkdude10:08:25

perhaps this is a shadow problem

borkdude10:08:08

Try 0.8.43, I fixed the shadow warnings

👀 1
borkdude10:08:02

SCI 0.8.43 that is

pez10:08:11

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.

borkdude10:08:31

can you just try (require '[clojure.repl]) instead, just for starters?

borkdude10:08:14

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

pez10:08:50

That also gives me

; Could not find namespace: cljs.repl.
With the ns-alias change. Without it I get
; pst does not exist

borkdude10:08:26

Ah it should be the reverse:

{:ns-aliases '{cljs.repl clojure.repl}

borkdude10:08:41

so cljs.repl is an alias for clojure.repl which is built into SCI

borkdude10:08:10

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.

pez10:08:54

Ah, thanks!

pez10:08:06

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]])

borkdude10:08:53

pst doesn't exist in CLJS

borkdude10:08:09

sorry, I'm wrong about that

borkdude10:08:03

but maybe that's not implemented yet in SCI

borkdude10:08:54

yes, it's only implemented for the JVM right now

pez10:08:09

I can maybe add some NYIP version of pst to the ns in Joyride.

pez10:08:10

Or, probably I should address the whole problem in Calva instead. Treating all cljs repls equal is not the way to go…

borkdude10:08:31

What does nyip mean?

pez11:08:02

Not Yet ImPlemented. 😃

borkdude11:08:21

or we could just add it to SCI ;)

pez11:08:27

“just” add it 😃 But, yeah, would be awesome.

borkdude11:08:29

you could also add it as a function in the joyride SCI config

borkdude11:08:40

which just throws with NYIP

pez11:08:45

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}}))

borkdude11:08:56

this is inside of a CLJS repl

🙏 1
pez11:08:22

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)

borkdude11:08:03

yes, since the source isn't there, but it'll work on user sources

borkdude11:08:37

it also doesn't work in bb for example

pez11:08:02

Makes sense.

pez12:08:26

I fixed the review feedback. Does it look good to you know?