@borkdude I now have a macro like this working:
(defmacro sci-macro [form]
(if (:ns &env)
(let [[_defmacro name & body] form
[doc body] (if (string? (first body))
[(first body) (rest body)]
[nil body])
arities (if (vector? (first body)) (list body) body)
arities (map (fn [[argv & body]] (list* (into '[&form &env] argv) body)) arities)]
`(defn ~(vary-meta name assoc :sci/macro true)
~@(when doc [doc])
~@arities))
form))
usage is
(sci-macro
(defmacro foo [a] ...))
when compiling for cljs, it emits a defn with :sci/macro metadata and &form &env prepended to all the argument vectors. so I can then do a straightforward sci/copy-ns on that namespace. At least I have tested it with Maria (so, cljs sci target)with this, there’s no need to pull out the implementation at all (except when that’s desired for other reasons)
@mhuebert that's very exciting! finally!
@mhuebert but when compiling for CLJS, it doesn't ever need to be defmacro for the JVM CLJS compiler? I'm a bit confused :)
correct, the inner defmacro is removed (becomes a defn) when compiling for cljs
but it has the :sci/macro metadata added, so that when sci copies the namespace it becomes a macro
the (:ns &env) is true when the target is cljs. normally when the target is cljs the macro disappears
oh
maybe I am mistaken after all
hmm
i think: when sci-macro expands for the JVM cljs compiler it will still emit defmacro
this will break in selfhost but I am currently not trying to target that
as long as it works for you :)
with another layer of macrovich it'd probably work for all ;)
I'm writing a chrome extension to eval text with SCI and show the results in a little popup. I'm hoping to render return results as react components, and then have a user.clj for the extension to define helpers to load whatever is useful into the browser, e.g. a clock or a real-time view of some data source. Not pragmatic, but fun, and I'm enjoying SCI so far!
Awesome :)
"view source" but returns the react/reagent components in the page as data structures
(e.g. I'm imagining how it might be possible to mix your in-page evaluations with data already provided by the page)
That'd be cool! I wonder if there's enough information in the minified source for something like ChatGPT to work backwards to a ClojureScript + Reagent + Hiccup representation of a "normal" JS react component from out in the wild.
I forgot about the “compiled” nature of JSX, shame that we would have to resort to such shaky methods just to understand the code that our browsers are executing