From a3a408f923988b8abb3ac7d923ab73ba225746bc Mon Sep 17 00:00:00 2001 From: Luis Michaelis Date: Sat, 9 Mar 2024 11:26:19 +0100 Subject: [PATCH] feat(DaedalusVm): allow default externals to take a `DaedalusSymbol` reference --- include/zenkit/DaedalusVm.hh | 13 ++++++++++++- src/DaedalusVm.cc | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/zenkit/DaedalusVm.hh b/include/zenkit/DaedalusVm.hh index 0513e104..ede9dd66 100644 --- a/include/zenkit/DaedalusVm.hh +++ b/include/zenkit/DaedalusVm.hh @@ -605,9 +605,20 @@ namespace zenkit { /// underflows. /// /// \param callback The function to call. The one parameter of the function is the name of the unresolved - /// external. + /// external. ZKAPI void register_default_external(std::function const& callback); + /// \brief Registers a function to be called when the script tries to call an external which has not been + /// registered. + /// + /// Before the callback is invoked, the VM makes sure that all parameters for the external are popped off + /// the stack and a default return value (if needed) is pushed onto the stack. This prevents stack + /// underflows. + /// + /// \param callback The function to call. The one parameter of the function is the symbol of the unresolved + /// external. + ZKAPI void register_default_external(std::function const& callback); + ZKAPI void register_default_external_custom(std::function const& callback); ZKAPI void register_access_trap(std::function const& callback); diff --git a/src/DaedalusVm.cc b/src/DaedalusVm.cc index 260cd56c..8814c37d 100644 --- a/src/DaedalusVm.cc +++ b/src/DaedalusVm.cc @@ -600,6 +600,10 @@ namespace zenkit { } void DaedalusVm::register_default_external(std::function const& callback) { + this->register_default_external([&callback](DaedalusSymbol const& sym) { callback(sym.name()); }); + } + + void DaedalusVm::register_default_external(std::function const& callback) { _m_default_external = [this, callback](DaedalusVm& v, DaedalusSymbol const& sym) { // pop all parameters from the stack auto params = find_parameters_for_function(&sym); @@ -624,7 +628,7 @@ namespace zenkit { v.push_instance(nullptr); } - callback(sym.name()); + callback(sym); }; }