A performance-oriented Lisp-like language where I can have my cake, and eat it (too)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.1 KiB

(import &comptime-only "Macros.cake")
(c-import "<unordered_map>" "<vector>")
(c-import "DynamicLoader.hpp")
(def-type-alias FunctionReferenceArray (<> std::vector (* (* void))))
(def-type-alias FunctionReferenceMap (<> std::unordered_map std::string FunctionReferenceArray))
(def-type-alias FunctionReferenceMapIterator (in FunctionReferenceMap iterator))
(def-type-alias FunctionReferenceMapPair (<> std::pair (const std::string) FunctionReferenceArray))
(var registered-functions FunctionReferenceMap)
(var current-lib DynamicLibHandle nullptr)
(var hot-reload-lib-path (* (const char)) "libGeneratedCakelisp.so")
(defun register-function-pointer (function-pointer (* (* void))
function-name (* (const char)))
(var findIt FunctionReferenceMapIterator
(on-call registered-functions find function-name))
(if (= findIt (on-call registered-functions end))
(var new-function-pointer-array FunctionReferenceArray)
(on-call new-function-pointer-array push_back function-pointer)
(set (at function-name registered-functions)
;; This could also be written as (std::move new-function-pointer-array)
;; I'm not sure how I want it to work
(call (in std move) new-function-pointer-array)))
(on-call (path findIt > second) push_back function-pointer)))
(defun do-hot-reload (&return bool)
(when current-lib
(closeDynamicLibrary current-lib))
(set current-lib (loadDynamicLibrary hot-reload-lib-path))
(unless current-lib
(return false))
(for-in function-referent-it (& FunctionReferenceMapPair) registered-functions
(var loaded-symbol (* void)
(getSymbolFromDynamicLibrary current-lib
(on-call (path function-referent-it . first) c_str)))
(unless loaded-symbol
(return false))
;; TODO: What will happen once modules are unloaded? We can't store pointers to their static memory
(for-in function-pointer (* (* void)) (path function-referent-it . second)
(set (deref function-pointer) loaded-symbol)))
(return true))