Files
fs2open.github.com/code/scripting/api/objs/promise.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

94 lines
2.4 KiB
C++

//
// Created by marius on 14.05.20.
//
#include "promise.h"
namespace scripting {
namespace api {
ADE_OBJ(l_Promise,
LuaPromise,
"promise",
"A promise that represents an operation that will return a value at some point in the future");
// Can't call this "then" since that is a Lua keyword
ADE_FUNC(continueWith,
l_Promise,
"function(args...)",
"When the called on promise resolves, this function will be called with the resolved value of the promise.",
"promise",
"A promise that will resolve with the return value of the passed function.")
{
LuaPromise* promise = nullptr;
luacpp::LuaFunction thenFunc;
if (!ade_get_args(L, "ou", l_Promise.GetPtr(&promise), &thenFunc)) {
return ADE_RETURN_NIL;
}
if (!thenFunc.isValid()) {
LuaError(L, "'continueWith' function is invalid!");
return ADE_RETURN_NIL;
}
if (promise == nullptr || !promise->isValid()) {
LuaError(L, "Invalid promise detected. This should not happen. Please contact a developer.");
return ADE_RETURN_NIL;
}
return ade_set_args(L, "o", l_Promise.Set(promise->then(thenFunc)));
}
ADE_FUNC(isResolved,
l_Promise,
nullptr,
"Checks if the promise is already resolved.",
"boolean",
"true if resolved, false if result is still pending.")
{
LuaPromise* promise = nullptr;
if (!ade_get_args(L, "o", l_Promise.GetPtr(&promise))) {
return ADE_RETURN_NIL;
}
if (promise == nullptr || !promise->isValid()) {
LuaError(L, "Invalid promise detected. This should not happen. Please contact a developer.");
return ADE_RETURN_NIL;
}
return ade_set_args(L, "b", promise->isResolved());
}
ADE_FUNC(getValue,
l_Promise,
nullptr,
"Gets the resolved value of this promise. Causes an error when used on an unresolved promise!",
"unknown",
"The resolved values.")
{
LuaPromise* promise = nullptr;
if (!ade_get_args(L, "o", l_Promise.GetPtr(&promise))) {
return ADE_RETURN_NIL;
}
if (promise == nullptr || !promise->isValid()) {
LuaError(L, "Invalid promise detected. This should not happen. Please contact a developer.");
return ADE_RETURN_NIL;
}
if (!promise->isResolved()) {
LuaError(L, "Tried to get the value of a promise that was not resolved!");
return ADE_RETURN_NIL;
}
// We can't use our usual functions for returning here since we return a variable amount of values
const auto& retVals = promise->resolveValue();
for (const auto& retVal : retVals) {
retVal.pushValue(L);
}
return static_cast<int>(retVals.size());
}
} // namespace api
} // namespace scripting