Last active
June 8, 2021 18:16
-
-
Save jaawerth/4e6c61f218ce4f3232f6d0b8191dbb15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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