Move Lua wrapper code for Variants out of the Variant class file.

This commit is contained in:
Alex Szpakowski
2020-02-28 21:54:03 -04:00
parent b95788c104
commit e71f27c65d
16 changed files with 237 additions and 241 deletions
-40
View File
@@ -38,46 +38,6 @@ Message::~Message()
{
}
int Message::toLua(lua_State *L)
{
luax_pushstring(L, name);
for (const Variant &v : args)
v.toLua(L);
return (int) args.size() + 1;
}
Message *Message::fromLua(lua_State *L, int n)
{
std::string name = luax_checkstring(L, n);
std::vector<Variant> vargs;
int count = lua_gettop(L) - n;
n++;
Variant varg;
for (int i = 0; i < count; i++)
{
if (lua_isnoneornil(L, n+i))
break;
luax_catchexcept(L, [&]() {
vargs.push_back(Variant::fromLua(L, n+i));
});
if (vargs.back().getType() == Variant::UNKNOWN)
{
vargs.clear();
luaL_error(L, "Argument %d can't be stored safely\nExpected boolean, number, string or userdata.", n+i);
return nullptr;
}
}
return new Message(name, vargs);
}
Event::~Event()
{
}
-3
View File
@@ -46,9 +46,6 @@ public:
Message(const std::string &name, const std::vector<Variant> &vargs = {});
~Message();
int toLua(lua_State *L);
static Message *fromLua(lua_State *L, int n);
const std::string name;
const std::vector<Variant> args;
+34 -11
View File
@@ -38,13 +38,23 @@ namespace event
#define instance() (Module::getInstance<Event>(Module::M_EVENT))
static int luax_pushmessage(lua_State *L, const Message &m)
{
luax_pushstring(L, m.name);
for (const Variant &v : m.args)
luax_pushvariant(L, v);
return (int) m.args.size() + 1;
}
static int w_poll_i(lua_State *L)
{
Message *m = nullptr;
if (instance()->poll(m))
if (instance()->poll(m) && m != nullptr)
{
int args = m->toLua(L);
int args = luax_pushmessage(L, *m);
m->release();
return args;
}
@@ -63,9 +73,9 @@ int w_wait(lua_State *L)
{
Message *m = nullptr;
luax_catchexcept(L, [&]() { m = instance()->wait(); });
if (m)
if (m != nullptr)
{
int args = m->toLua(L);
int args = luax_pushmessage(L, *m);
m->release();
return args;
}
@@ -75,15 +85,28 @@ int w_wait(lua_State *L)
int w_push(lua_State *L)
{
StrongRef<Message> m;
luax_catchexcept(L, [&]() { m.set(Message::fromLua(L, 1), Acquire::NORETAIN); });
std::string name = luax_checkstring(L, 1);
std::vector<Variant> vargs;
luax_pushboolean(L, m.get() != nullptr);
int nargs = lua_gettop(L);
for (int i = 2; i <= nargs; i++)
{
if (lua_isnoneornil(L, i))
break;
if (m.get() == nullptr)
return 1;
luax_catchexcept(L, [&]() { vargs.push_back(luax_checkvariant(L, i)); });
if (vargs.back().getType() == Variant::UNKNOWN)
{
vargs.clear();
return luaL_error(L, "Argument %d can't be stored safely\nExpected boolean, number, string or userdata.", i);
}
}
StrongRef<Message> m(new Message(name, vargs), Acquire::NORETAIN);
instance()->push(m);
luax_pushboolean(L, true);
return 1;
}
@@ -98,7 +121,7 @@ int w_quit(lua_State *L)
luax_catchexcept(L, [&]() {
std::vector<Variant> args;
for (int i = 1; i <= std::max(1, lua_gettop(L)); i++)
args.push_back(Variant::fromLua(L, i));
args.push_back(luax_checkvariant(L, i));
StrongRef<Message> m(new Message("quit", args), Acquire::NORETAIN);
instance()->push(m);
@@ -115,7 +138,7 @@ int w_restart(lua_State *L)
args.emplace_back("restart", strlen("restart"));
for (int i = 1; i <= lua_gettop(L); i++)
args.push_back(Variant::fromLua(L, i));
args.push_back(luax_checkvariant(L, i));
StrongRef<Message> m(new Message("quit", args), Acquire::NORETAIN);
instance()->push(m);
+1
View File
@@ -24,6 +24,7 @@
// LOVE
#include "common/config.h"
#include "Event.h"
#include "common/runtime.h"
namespace love
{
+2 -5
View File
@@ -36,9 +36,6 @@ namespace thread
class Channel : public love::Object
{
// FOR WRAPPER USE ONLY
friend int w_Channel_performAtomic(lua_State *);
public:
static love::Type type;
@@ -57,11 +54,11 @@ public:
bool hasRead(uint64 id) const;
void clear();
private:
void lockMutex();
void unlockMutex();
private:
MutexRef mutex;
ConditionalRef cond;
std::queue<Variant> queue;
+2 -1
View File
@@ -21,6 +21,7 @@
#include "LuaThread.h"
#include "event/Event.h"
#include "common/config.h"
#include "common/runtime.h"
#ifdef LOVE_BUILD_STANDALONE
extern "C" int luaopen_love(lua_State * L);
@@ -76,7 +77,7 @@ void LuaThread::threadFunction()
int pushedargs = (int) args.size();
for (int i = 0; i < pushedargs; i++)
args[i].toLua(L);
luax_pushvariant(L, args[i]);
args.clear();
+5 -5
View File
@@ -34,7 +34,7 @@ int w_Channel_push(lua_State *L)
{
Channel *c = luax_checkchannel(L, 1);
luax_catchexcept(L, [&]() {
Variant var = Variant::fromLua(L, 2);
Variant var = luax_checkvariant(L, 2);
if (var.getType() == Variant::UNKNOWN)
luaL_argerror(L, 2, "boolean, number, string, love type, or table expected");
uint64 id = c->push(var);
@@ -48,7 +48,7 @@ int w_Channel_supply(lua_State *L)
Channel *c = luax_checkchannel(L, 1);
bool result = false;
luax_catchexcept(L, [&]() {
Variant var = Variant::fromLua(L, 2);
Variant var = luax_checkvariant(L, 2);
if (var.getType() == Variant::UNKNOWN)
luaL_argerror(L, 2, "boolean, number, string, love type, or table expected");
if (lua_isnumber(L, 3))
@@ -66,7 +66,7 @@ int w_Channel_pop(lua_State *L)
Channel *c = luax_checkchannel(L, 1);
Variant var;
if (c->pop(&var))
var.toLua(L);
luax_pushvariant(L, var);
else
lua_pushnil(L);
return 1;
@@ -84,7 +84,7 @@ int w_Channel_demand(lua_State *L)
result = c->demand(&var);
if (result)
var.toLua(L);
luax_pushvariant(L, var);
else
lua_pushnil(L);
return 1;
@@ -95,7 +95,7 @@ int w_Channel_peek(lua_State *L)
Channel *c = luax_checkchannel(L, 1);
Variant var;
if (c->peek(&var))
var.toLua(L);
luax_pushvariant(L, var);
else
lua_pushnil(L);
return 1;
+1
View File
@@ -23,6 +23,7 @@
// LOVE
#include "Channel.h"
#include "common/runtime.h"
namespace love
{
+1 -1
View File
@@ -39,7 +39,7 @@ int w_Thread_start(lua_State *L)
for (int i = 0; i < nargs; ++i)
{
luax_catchexcept(L, [&]() {
args.push_back(Variant::fromLua(L, i+2));
args.push_back(luax_checkvariant(L, i+2));
});
if (args.back().getType() == Variant::UNKNOWN)
+1
View File
@@ -23,6 +23,7 @@
// LOVE
#include "ThreadModule.h"
#include "common/runtime.h"
namespace love
{