1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
//
// 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(any... args) => any...",
"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([L, thenFunc](const luacpp::LuaValueList& val) { return thenFunc(L, val); })));
}
ADE_FUNC(catch,
l_Promise,
"function(any... args) => any...",
"When the called on promise produces an error, this function will be called with the error 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->catchError([L, thenFunc](const luacpp::LuaValueList& val) {
return thenFunc(L, val);
})));
}
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(isErrored,
l_Promise,
nullptr,
"Checks if the promise is already in an error state.",
"boolean",
"true if errored, 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->isErrored());
}
ADE_FUNC(getValue,
l_Promise,
nullptr,
"Gets the resolved value of this promise. Causes an error when used on an unresolved or errored promise!",
"any",
"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());
}
ADE_FUNC(getErrorValue,
l_Promise,
nullptr,
"Gets the error value of this promise. Causes an error when used on an unresolved or resolved promise!",
"any",
"The error 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->isErrored()) {
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->errorValue();
for (const auto& retVal : retVals) {
retVal.pushValue(L);
}
return static_cast<int>(retVals.size());
}
} // namespace api
} // namespace scripting
|