Skip to content

Instantly share code, notes, and snippets.

@disco0
Forked from jaawerth/metadata.fnl
Created June 8, 2021 18:16
Show Gist options
  • Save disco0/90b7a6f8c0b9084d386f4ebc9204be71 to your computer and use it in GitHub Desktop.
Save disco0/90b7a6f8c0b9084d386f4ebc9204be71 to your computer and use it in GitHub Desktop.
; lie and say it's clojure for highlighting
; vim: ft=clojure
; this is a hack that won't be necessary if/when we add `get-option` to the macro environment
(local meta-enabled (-> _SCOPE.specials.doc
(pcall (list (sym :doc) (sym :doc)) _SCOPE _CHUNK)))
(fn meta/when-enabled [...]
"Execute body in an implicit `do` when metadata is enabled. Otherwise,
contents are excluded from compiled output."
(when meta-enabled `(do ,...)))
(λ meta/with [func ...]
"Accepts and always returns func. When metadata is enabled, evaluates body in
an implicit `let` with the global metadata table bound to `$metadata`. If the
body returns a table, it's set as the metadata of func. The $metadata api is:
Get/set entire metadata for a function:
(. $metadata some-func) ; get func's metadata tbl
(tset $metadata somefunc all-metadata-for-somefunc) ; set func's metadata tbl
($metadata:get tgt-func key) ; get meta field for func
($metadata:set tgt-func key value) ; set meta field for func
($metadata:setall tgt-func key1 val1 key2 val2 ...) ; set metadata k/v pairs
The built-in metadata keys are :fnl/docstring and :fnl/arglist."
(if (not meta-enabled) func
(let [meta-sym (sym :$metadata)]
`(let [func# ,func
,meta-sym (. (require :fennel) :metadata)]
(local fn-meta# (do ,...))
(when (= :table (type fn-meta#))
(each [k# v# (pairs fn-meta#)]
(: ,meta-sym :set func# k# v#)))
func#))))
{: meta/when-enabled : meta/with }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment