Skip to content

Commit

Permalink
feat(DaedalusVm): allow default externals to take a DaedalusSymbol
Browse files Browse the repository at this point in the history
…reference
  • Loading branch information
lmichaelis committed Mar 9, 2024
1 parent 2b5aa9e commit a3a408f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion include/zenkit/DaedalusVm.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(std::string_view)> 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<void(DaedalusSymbol const&)> const& callback);

ZKAPI void register_default_external_custom(std::function<void(DaedalusVm&, DaedalusSymbol&)> const& callback);

ZKAPI void register_access_trap(std::function<void(DaedalusSymbol&)> const& callback);
Expand Down
6 changes: 5 additions & 1 deletion src/DaedalusVm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ namespace zenkit {
}

void DaedalusVm::register_default_external(std::function<void(std::string_view)> const& callback) {
this->register_default_external([&callback](DaedalusSymbol const& sym) { callback(sym.name()); });
}

void DaedalusVm::register_default_external(std::function<void(DaedalusSymbol const&)> 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);
Expand All @@ -624,7 +628,7 @@ namespace zenkit {
v.push_instance(nullptr);
}

callback(sym.name());
callback(sym);
};
}

Expand Down

0 comments on commit a3a408f

Please sign in to comment.