Skip to content

Instantly share code, notes, and snippets.

@elliotpotts
Created November 15, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotpotts/6887af1bf43d0ca4955f54dede676631 to your computer and use it in GitHub Desktop.
Save elliotpotts/6887af1bf43d0ca4955f54dede676631 to your computer and use it in GitHub Desktop.
// 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