mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-09 04:49:39 -10:00
Add Lua API for promises and async coroutines
This adds a Lua API for representing asynchronous operations and running coroutines based on those operations comfortably. This is very similar to features other languages have introduced such as C# or JavaScript. In fact, the whole "promise" thing is derived from how JavaScript represents these operations. This is the initial and very basic system for supporting async coroutines in Lua. There is no native support for running these operations at specific points (they run whenever the awaited promise resolved) or making sure they do not leak through different game states.
This commit is contained in:
1 parent
96bee4a4fa
commit
ebcf686cf7
40 files changed
+1342
-123
No files matched your search
@@ -4,14 +4,62 @@
|
||||
#include "LuaHeaders.h"
|
||||
|
||||
namespace luacpp {
|
||||
LuaFunction LuaFunction::createFromCFunction(lua_State* L, lua_CFunction function, const LuaValueList& upvalues) {
|
||||
namespace {
|
||||
|
||||
int stdFunctionDestructor(lua_State* L)
|
||||
{
|
||||
// The userdata is the function object so we call the destructor here to free up the internal resources
|
||||
const auto luaPtr = lua_touserdata(L, 1);
|
||||
const auto funcPtr = static_cast<LuaFunctionObject*>(luaPtr);
|
||||
|
||||
funcPtr->~LuaFunctionObject();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stdFunctionCaller(lua_State* L)
|
||||
{
|
||||
const auto numArgs = lua_gettop(L);
|
||||
|
||||
// Push the actual function object on the stack
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
|
||||
const auto funcObj = static_cast<LuaFunctionObject*>(lua_touserdata(L, -1));
|
||||
lua_pop(L, 1); // We have the pointer so we can remove this from the stack again
|
||||
|
||||
LuaValueList params;
|
||||
params.reserve(numArgs);
|
||||
|
||||
for (int i = 1; i <= numArgs; ++i) {
|
||||
LuaValue val;
|
||||
val.setReference(UniqueLuaReference::create(L, i));
|
||||
params.push_back(val);
|
||||
}
|
||||
|
||||
const auto ret = (*funcObj)(L, params);
|
||||
|
||||
// Prepare to return values. Clear the stack
|
||||
lua_settop(L, 0);
|
||||
for (const auto& retVal : ret)
|
||||
{
|
||||
retVal.pushValue(L);
|
||||
}
|
||||
|
||||
// Done!
|
||||
return static_cast<int>(ret.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LuaFunction LuaFunction::createFromCFunction(lua_State* L, lua_CFunction function, const LuaValueList& upvalues)
|
||||
{
|
||||
LuaFunction func;
|
||||
|
||||
for (auto& val : upvalues) {
|
||||
val.pushValue();
|
||||
val.pushValue(L);
|
||||
}
|
||||
|
||||
lua_pushcclosure(L, function, (int) upvalues.size());
|
||||
lua_pushcclosure(L, function, (int)upvalues.size());
|
||||
|
||||
func.setReference(UniqueLuaReference::create(L));
|
||||
|
||||
@@ -19,6 +67,23 @@ LuaFunction LuaFunction::createFromCFunction(lua_State* L, lua_CFunction functio
|
||||
|
||||
return func;
|
||||
}
|
||||
LuaFunction LuaFunction::createFromStdFunction(lua_State* L, LuaFunctionObject function) {
|
||||
auto userdataMetatable = LuaTable::create(L);
|
||||
userdataMetatable.addValue("__gc", stdFunctionDestructor);
|
||||
|
||||
// Create storage for the function object in Lua and initialize our data there via placement new
|
||||
auto functionStorage = lua_newuserdata(L, sizeof(function));
|
||||
new (functionStorage) LuaFunctionObject(std::move(function));
|
||||
|
||||
userdataMetatable.pushValue(L);
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
LuaValue funcValue;
|
||||
funcValue.setReference(UniqueLuaReference::create(L));
|
||||
lua_pop(L, 1); // Value is referenced. We can remove it from the stack now
|
||||
|
||||
return LuaFunction::createFromCFunction(L, stdFunctionCaller, { funcValue });
|
||||
}
|
||||
|
||||
LuaFunction LuaFunction::createFromCode(lua_State* L, std::string const& code, std::string const& name) {
|
||||
int load_err = luaL_loadbuffer(L, code.c_str(), code.length(), name.c_str());
|
||||
@@ -48,6 +113,9 @@ LuaFunction::LuaFunction() : LuaValue(), _errorFunction(nullptr) {
|
||||
LuaFunction::LuaFunction(const LuaFunction&) = default;
|
||||
LuaFunction& LuaFunction::operator=(const LuaFunction&) = default;
|
||||
|
||||
LuaFunction::LuaFunction(LuaFunction&&) noexcept = default;
|
||||
LuaFunction& LuaFunction::operator=(LuaFunction&&) noexcept = default;
|
||||
|
||||
LuaFunction::~LuaFunction() = default;
|
||||
|
||||
bool LuaFunction::setEnvironment(const LuaTable& table) {
|
||||
@@ -55,8 +123,8 @@ bool LuaFunction::setEnvironment(const LuaTable& table) {
|
||||
throw LuaException("Table reference is not valid!");
|
||||
}
|
||||
|
||||
this->pushValue();
|
||||
table.pushValue();
|
||||
this->pushValue(_luaState);
|
||||
table.pushValue(_luaState);
|
||||
|
||||
bool ret = lua_setfenv(_luaState, -2) != 0;
|
||||
|
||||
@@ -78,10 +146,10 @@ void LuaFunction::setReference(const LuaReference& ref) {
|
||||
return;
|
||||
}
|
||||
|
||||
ref->pushValue();
|
||||
|
||||
lua_State* L = ref->getState();
|
||||
|
||||
ref->pushValue(L);
|
||||
|
||||
if (lua_type(L, -1) != LUA_TFUNCTION) {
|
||||
lua_pop(L, 1);
|
||||
throw LuaException("Reference does not refere to a function!");
|
||||
@@ -97,7 +165,7 @@ LuaValueList LuaFunction::call(const LuaValueList& args) const {
|
||||
|
||||
if (_errorFunction) {
|
||||
// push the error function
|
||||
_errorFunction->pushValue();
|
||||
_errorFunction->pushValue(_luaState);
|
||||
err_idx = lua_gettop(_luaState);
|
||||
stackTop = err_idx;
|
||||
} else {
|
||||
@@ -105,11 +173,11 @@ LuaValueList LuaFunction::call(const LuaValueList& args) const {
|
||||
}
|
||||
|
||||
// Push the function onto the stack
|
||||
this->pushValue();
|
||||
this->pushValue(_luaState);
|
||||
|
||||
// Push the arguments onto the stack
|
||||
for (const auto& arg : args) {
|
||||
arg.pushValue();
|
||||
arg.pushValue(_luaState);
|
||||
}
|
||||
|
||||
// actually call the function now!
|
||||
|
||||
Reference in new issue
Block a user