Files
fs2open.github.com/code/scripting/lua/LuaUtil.cpp
T
asarium ebcf686cf7 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.
2020-05-24 12:36:01 +02:00

65 lines
1.2 KiB
C++

#include "LuaUtil.h"
namespace {
const char* mainStateRefName = "_lua_mainthread";
}
namespace luacpp {
namespace util {
const char* getValueName(ValueType type)
{
switch (type) {
case ValueType::NONE:
return "none";
case ValueType::NIL:
return "nil";
case ValueType::BOOLEAN:
return "boolean";
case ValueType::LIGHTUSERDATA:
return "light userdata";
case ValueType::STRING:
return "string";
case ValueType::NUMBER:
return "number";
case ValueType::TABLE:
return "table";
case ValueType::FUNCTION:
return "function";
case ValueType::USERDATA:
return "userdata";
case ValueType::THREAD:
return "thread";
default:
return "unknown";
}
}
void initializeLuaSupportLib(lua_State* L)
{
auto mainThread = lua_pushthread(L);
Assertion(mainThread, "Must be called with the main thread as the parameter!");
lua_setfield(L, LUA_REGISTRYINDEX, mainStateRefName);
}
lua_State* getMainThread(lua_State* L)
{
if (lua_pushthread(L)) {
// State is the main thread, just return that
lua_pop(L, 1);
return L;
}
lua_pushstring(L, mainStateRefName);
lua_rawget(L, LUA_REGISTRYINDEX);
auto state = lua_tothread(L, -1);
lua_pop(L, 1);
return state;
}
} // namespace util
} // namespace luacpp