mirror of
https://gitlab.com/Syping/luaengineapp.git
synced 2025-09-06 08:52:05 +02:00
add LuaEngine compiler
This commit is contained in:
parent
adb091dfbb
commit
eec5071578
9 changed files with 225 additions and 20 deletions
|
@ -20,7 +20,6 @@
|
|||
#include <QTextStream>
|
||||
#include <QMetaMethod>
|
||||
#include <QtGlobal>
|
||||
#include <QDebug>
|
||||
|
||||
LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
|
||||
{
|
||||
|
@ -53,6 +52,13 @@ void LuaEngine::loadBaseLibraries()
|
|||
luaL_openlibs(L);
|
||||
}
|
||||
|
||||
int LuaEngine::luaEngineWriter_p(lua_State *L_p, const void *buffer, size_t size, void *array)
|
||||
{
|
||||
Q_UNUSED(L_p)
|
||||
((QByteArray*)array)->append(QByteArray(static_cast<const char*>(buffer), (int)size));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LuaEngine::luaEngineVersion_p(lua_State *L_p)
|
||||
{
|
||||
pushVariant(L_p, "0.1");
|
||||
|
@ -83,10 +89,44 @@ int LuaEngine::luaEnginePlatform_p(lua_State *L_p)
|
|||
return 1;
|
||||
}
|
||||
|
||||
bool LuaEngine::executeLuaScript(const QByteArray &data)
|
||||
QByteArray LuaEngine::dumpLuaScript()
|
||||
{
|
||||
QByteArray array;
|
||||
lua_lock(L);
|
||||
lua_dump(L, luaEngineWriter_p, (void*)&array, 1);
|
||||
lua_unlock(L);
|
||||
return array;
|
||||
}
|
||||
|
||||
bool LuaEngine::loadLuaScript(const QByteArray &data)
|
||||
{
|
||||
int result = luaL_loadbuffer(L, data.data(), data.size(), "script");
|
||||
if (result == 0)
|
||||
return (result == 0) ? true : false;
|
||||
}
|
||||
|
||||
bool LuaEngine::loadLuaScript(QIODevice *device, bool closeDevice)
|
||||
{
|
||||
QByteArray data;
|
||||
if (!device->isOpen()) {
|
||||
if (device->open(QIODevice::ReadOnly)) {
|
||||
data = device->readAll();
|
||||
if (closeDevice)
|
||||
device->close();
|
||||
return loadLuaScript(data);
|
||||
}
|
||||
}
|
||||
else {
|
||||
data = device->readAll();
|
||||
if (closeDevice)
|
||||
device->close();
|
||||
return loadLuaScript(data);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LuaEngine::executeLuaScript(const QByteArray &data)
|
||||
{
|
||||
if (loadLuaScript(data))
|
||||
return (lua_pcall(L, 0, LUA_MULTRET, 0) == 0) ? true : false;
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ extern "C" {
|
|||
#include "../lua/lua.h"
|
||||
#include "../lua/lualib.h"
|
||||
#include "../lua/lauxlib.h"
|
||||
#include "../lua/llimits.h"
|
||||
}
|
||||
|
||||
class LUAENGINESHARED_EXPORT LuaEngine : public QObject
|
||||
|
@ -39,6 +40,9 @@ public:
|
|||
~LuaEngine();
|
||||
lua_State* luaState();
|
||||
void loadBaseLibraries();
|
||||
QByteArray dumpLuaScript();
|
||||
bool loadLuaScript(const QByteArray &data);
|
||||
bool loadLuaScript(QIODevice *device, bool closeDevice = true);
|
||||
bool executeLuaScript(const QByteArray &data);
|
||||
bool executeLuaScript(QIODevice *device, bool closeDevice = true);
|
||||
bool executeLuaFunction(const char *name, bool requireReturn = false);
|
||||
|
@ -74,6 +78,7 @@ public:
|
|||
|
||||
private:
|
||||
lua_State *L;
|
||||
static int luaEngineWriter_p(lua_State *L_p, const void *buffer, size_t size, void *array);
|
||||
static int luaEngineVersion_p(lua_State *L_p);
|
||||
static int luaEnginePlatform_p(lua_State *L_p);
|
||||
static int luaObjectDelete_p(lua_State *L_p);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue