Fork me on GitHub
#clojurescript
<
2022-05-29
>
armed06:05:54

I’ve got an ^:export-ed function. I can call it directly by (js/my.function_name), but how invoke it if I only have its fully qualified name as a string (“my.function_name”)?

thheller06:05:59

(js/goog.getObjectByName "my.function_name") gives you the thing. then just (thing)

armed06:05:32

Hey, @U05224H0W thanks. I’m not sure it’s going to work on advanced optimizations. Had to mention that in the original question,

thheller06:05:11

it will if its exported

thheller06:05:21

thats what export is kinda for. preserving the name after :advanced

armed06:05:48

You’re right, my auto-export macro doesn’t work properly. Released code has js/my.function_name but js/goog.getObjectByName returns null. When I manually add ^:export to the function body it works both ways.

(println
 js/my.function_name
 (js/goog.getObjectByName "my.function_name"))
it prints only js/… line

thheller06:05:19

what is the auto-export macro doing?

armed06:05:11

It scans edn file for symbols.

(defmacro scan-exports!
  [path]
  (let [graph# (edn/read-string (slurp path))
        vars (gather-syms graph#)]
    (doseq [v vars]
      (println "adding v" v)
      (alter-meta! v assoc :export true))
    `'~graph#))

thheller06:05:05

yeah thats invalid and won't work

thheller06:05:31

alter-meta! on vars is not a thing in CLJS and basically does nothing

armed06:05:12

And why it adds js/my.function_name? If I comment out macro usage, js won’t have my function

thheller06:05:03

(defmacro scan-exports!
  [path]
  (let [graph (edn/read-string (slurp path))
        vars (gather-syms graph)]
    `(do ~@(for [v vars]
             `(js/goog.exportSymbol ~(comp/munge v) v))
         ~graph)))

thheller06:05:15

comp being cljs.compiler

thheller06:05:23

this requires however that the namespace using this MUST have a proper ns require for all the namespaces you are using in the edn file

thheller06:05:32

otherwise no guarantee things will work

armed06:05:24

Thanks a lot, @U05224H0W. Your shadow-cljs work is immeasurable.