Fork me on GitHub
#sci
<
2023-03-07
>
Matthew Downey01:03:47

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!

🎉 2
sci 2
respatialized14:03:09

"view source" but returns the react/reagent components in the page as data structures

respatialized14:03:14

(e.g. I'm imagining how it might be possible to mix your in-page evaluations with data already provided by the page)

Matthew Downey14:03:05

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.

respatialized15:03:20

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

mhuebert11:03:45

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

mhuebert11:03:36

with this, there’s no need to pull out the implementation at all (except when that’s desired for other reasons)

borkdude11:03:50

@mhuebert that's very exciting! finally!

borkdude11:03:48

@mhuebert but when compiling for CLJS, it doesn't ever need to be defmacro for the JVM CLJS compiler? I'm a bit confused :)

mhuebert11:03:22

correct, the inner defmacro is removed (becomes a defn) when compiling for cljs

mhuebert11:03:43

but it has the :sci/macro metadata added, so that when sci copies the namespace it becomes a macro

mhuebert11:03:52

the (:ns &env) is true when the target is cljs. normally when the target is cljs the macro disappears

mhuebert11:03:26

maybe I am mistaken after all

mhuebert11:03:55

i think: when sci-macro expands for the JVM cljs compiler it will still emit defmacro

mhuebert11:03:21

this will break in selfhost but I am currently not trying to target that

borkdude11:03:20

as long as it works for you :)

borkdude11:03:45

with another layer of macrovich it'd probably work for all ;)