Created
November 15, 2020 12:33
-
-
Save elliotpotts/6887af1bf43d0ca4955f54dede676631 to your computer and use it in GitHub Desktop.
This file contains 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
// Execute the given c++ code, catching scheme exceptions thrown | |
// NOTE: c++ exceptions cannot be caught in this environment! | |
template <typename F> | |
SCM in_try(F&& f) { | |
return scm_c_catch ( | |
SCM_BOOL_T, | |
&in_try_body<F>, reinterpret_cast<void*>(&f), | |
&in_try_handler<F>, reinterpret_cast<void*>(&f), | |
&in_try_preunwind_handler<F>, reinterpret_cast<void*>(&f) | |
); | |
} | |
//TODO: function traits | |
//TODO: return types | |
template<typename F> | |
void define(const char* name, F* fun) { | |
std::string dispatcher_name = fmt::format("__dispatch_{}", name); | |
SCM dispatcher = scm_c_define_gsubr(dispatcher_name.c_str(), 2, 0, 0, reinterpret_cast<void*>(&dispatch<F>)); | |
auto src = fmt::format ( | |
"(use-modules (system foreign)) (define {} (lambda args ({} (make-pointer {}) args)))", | |
name, dispatcher_name, reinterpret_cast<uintptr_t>(fun) | |
); | |
scm_c_eval_string(src.c_str()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment