honeylisp/util.fnl

41 lines
1 KiB
Plaintext
Raw Normal View History

(local lume (require "lume"))
(fn string.fromhex [str]
(str:gsub ".." (fn [cc] (string.char (tonumber cc 16)))))
(fn string.tohex [str]
(str:gsub "." (fn [c] (string.format "%02X" (string.byte c)))))
(fn lo [v] (bit.band v 0xff))
(fn hi [v] (bit.band (bit.rshift v 8) 0xff))
(fn int8-to-bytes [i]
(string.char (lo i)))
(fn int16-to-bytes [i]
(string.char (lo i) (hi i)))
(fn reload [modname]
(tset package.loaded modname nil)
(require modname))
; lume.hotswap really assumes your module is a table
(fn hotswap [modname]
(if (= (type (. package.loaded modname)) :table)
(lume.hotswap modname)
(reload modname)))
(fn mk-swappable-fn [table k]
(fn [...] ((. table k) ...)))
(fn swappable [table]
(local s {})
(each [k v (pairs table)]
(if (= (type v) :function)
(tset s k (mk-swappable-fn table k))
(tset s k v)))
s)
(fn swappable-require [modname]
(swappable (require modname)))
{: lo : hi : int8-to-bytes : int16-to-bytes : reload : hotswap : swappable :require swappable-require}