5
votes

I'd like to construct a ClojureScript macro (executed/compiled via Clojure) that informs it's construction of a return form based on the static, compile-time metadata of a ClojureScript var argument.

I understand it is possible to access compile-time, static var metadata from ClojureScript code (using (meta (var varsym)); see this post). But is this data accessible to the compilation process in such a way that we could access it from a macro?

Here's a rough sketch of what I'd like to do (and the question is really how you'd write get-meta-for-varsym below):

;; executed/compiled in clj, targeting cljs
(defmacro themacro
  [varsym & args]
  (let [var-meta (get-meta-for-varsym varsym)
        return-form (compile-return-form-from-metadata var-meta args)]
    return-form))
1
Unlike the JVM implementation, ClojureScript does not use Vars at runtime, but uses bare Javascript variables instead. - Nathan Davis
@Nathan Davis: Yup; I understand that. But ClojureScript still does create a facade that allows for static var metadata. Which is cool, and good enough for a lot of purposes. - metasoarous

1 Answers

6
votes

For this you have to use the Clojurescript analyzer:

(ns your-macros
  (:require [cljs.analyzer :as cljs]))

(defmacro var-data
  [sym]
  (cljs/resolve-var &env sym))

Then in your clojurescript file:

(ns your-cljs)

(def ^{:foo :bar} xxy {})

(var-data xxy)

The meta data will be in :meta key of the map.