Files
fs2open.github.com/code/scripting/api/objs/executor.cpp
T
Goober5000andClaude Fable 5.1 37b0b6ea82 Standardize isValid across the Lua API by making the types auto-generate it (#7761)
* Automatically generate the isValid() Lua API function for handle types

If an ADE object's stored type has an isValid() member, ade_obj now registers an isValid function for it automatically.  An explicit ADE_FUNC(isValid, ...) declared after the ADE_OBJ replaces the generated one, so types with special semantics (e.g. sound) keep their own.  The 67 hand-written functions that merely delegated to the member are removed, and 26 types that previously exposed no isValid() gain one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* Add ADE_OBJ_VALIDATOR for index-based Lua handles

Int-index handles (shipclass, weaponclass, team, etc.) can now declare their validity predicate with a single ADE_OBJ_VALIDATOR or ADE_OBJ_VALIDATOR_RANGE line after the ADE_OBJ.  The registered validator drives the generated isValid() function and is also honored by ade_set_args when the mod table asks for nil instead of invalid objects, which previously never applied to these handles.  The 19 hand-written range-check functions are replaced, and loadout_ship, default_primary, default_secondary, and support_rearm_pool_team gain isValid().  Add a validator to HudGauge while we're at it.

Also fully qualify the scripting namespace in LuaConvert.h so its templates can be instantiated from the unit tests.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
2026-09-08 10:50:53 -04:00

65 lines
1.9 KiB
C++

#include "executor.h"
#include <utility>
namespace scripting {
namespace api {
executor_h::executor_h() = default;
executor_h::executor_h(std::shared_ptr<executor::Executor> executor) : m_executor(std::move(executor)) {}
bool executor_h::isValid() const { return m_executor != nullptr; }
const std::shared_ptr<executor::Executor>& executor_h::getExecutor() const { return m_executor; }
//**********HANDLE: executor
ADE_OBJ(l_Executor, executor_h, "executor", "An executor that can be used for scheduling the execution of a function.");
ADE_FUNC(schedule,
l_Executor,
"function() => boolean",
"Takes a function that returns a boolean and schedules that for execution on this executor. If the function "
"returns true it will be run again the. If it returns false it will be removed from the executor.<br>Note: Use "
"this with care since using this without proper care can lead to the function being run in states that are not "
"desired. Consider using async.run.",
"boolean",
"true if function was scheduled, false otherwise.")
{
executor_h* executor = nullptr;
luacpp::LuaFunction func;
if (!ade_get_args(L, "ou", l_Executor.GetPtr(&executor), &func)) {
return ADE_RETURN_FALSE;
}
if (executor == nullptr || !executor->isValid()) {
return ADE_RETURN_FALSE;
}
if (!func.isValid()) {
LuaError(L, "Invalid function handle specified!");
return ADE_RETURN_FALSE;
}
// Post the function onto the executor with a wrapper to convert the Lua value to the proper enum
executor->getExecutor()->post([L, func = std::move(func)]() {
const auto ret = func(L);
if (ret.empty()) {
return executor::Executor::CallbackResult::Done;
}
if (!ret[0].is(luacpp::ValueType::BOOLEAN)) {
return executor::Executor::CallbackResult::Done;
}
if (ret[0].getValue<bool>()) {
return executor::Executor::CallbackResult::Reschedule;
}
return executor::Executor::CallbackResult::Done;
});
return ADE_RETURN_TRUE;
}
} // namespace api
} // namespace scripting