summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/environment.cpp
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2018-07-13 23:57:25 +0300
committeraunsane <aunsane@gmail.com>2018-07-15 15:03:47 +0300
commitce719a1725988bded5566a5d7f596b1ba42cfa91 (patch)
treeee5a96fa51444ed509edb111a0ed49eabda834f2 /plugins/MirLua/src/environment.cpp
parent40198c29b0f5e7f14f73a3e0a62541ac918612a8 (diff)
MirLua: add Evaluate option page to test scripts
Diffstat (limited to 'plugins/MirLua/src/environment.cpp')
-rw-r--r--plugins/MirLua/src/environment.cpp47
1 files changed, 39 insertions, 8 deletions
diff --git a/plugins/MirLua/src/environment.cpp b/plugins/MirLua/src/environment.cpp
index 88e7bff62b..1816483a17 100644
--- a/plugins/MirLua/src/environment.cpp
+++ b/plugins/MirLua/src/environment.cpp
@@ -4,13 +4,12 @@
extern PLUGININFOEX pluginInfoEx;
-CMLuaEnvironment::CMLuaEnvironment(lua_State *_l) :
- CMPluginBase(nullptr, pluginInfoEx),
- L(_l)
+CMLuaEnvironment::CMLuaEnvironment(lua_State *L) :
+ CMPluginBase(nullptr, pluginInfoEx), L(L)
{
}
-CMLuaEnvironment::~CMLuaEnvironment()
+int CMLuaEnvironment::Unload()
{
KillModuleIcons(this);
KillModuleSounds(this);
@@ -25,6 +24,8 @@ CMLuaEnvironment::~CMLuaEnvironment()
for (auto &it : m_serviceRefs)
luaL_unref(L, LUA_REGISTRYINDEX, it.second);
+
+ return 0;
}
CMLuaEnvironment* CMLuaEnvironment::GetEnvironment(lua_State *L)
@@ -121,6 +122,9 @@ void CMLuaEnvironment::DestroyServiceFunction(HANDLE hService)
void CMLuaEnvironment::CreateEnvironmentTable()
{
+ if (!lua_isfunction(L, -1))
+ return;
+
lua_createtable(L, 1, 1);
lua_pushlightuserdata(L, this);
lua_rawseti(L, -2, NULL);
@@ -132,14 +136,41 @@ void CMLuaEnvironment::CreateEnvironmentTable()
lua_getglobal(L, "_G");
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
+
+ lua_setupvalue(L, -2, 1);
}
-int CMLuaEnvironment::Load()
+/***********************************************/
+
+wchar_t* CMLuaEnvironment::Error()
{
- luaL_checktype(L, -1, LUA_TFUNCTION);
+ const char *message = lua_tostring(L, -1);
+ wchar_t *error = mir_utf8decodeW(message);
+ lua_pop(L, 1);
+ return error;
+}
+wchar_t* CMLuaEnvironment::Call()
+{
CreateEnvironmentTable();
- lua_setupvalue(L, -2, 1);
+ luaM_pcall(L, 0, 1);
- return lua_pcall(L, 0, 1, 0) == LUA_OK;
+ wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1));
+ lua_pop(L, 1);
+
+ return result;
+}
+
+wchar_t* CMLuaEnvironment::Eval(const wchar_t *script)
+{
+ if (luaL_loadstring(L, T2Utf(script)))
+ return Error();
+ return Call();
+}
+
+wchar_t* CMLuaEnvironment::Exec(const wchar_t *path)
+{
+ if (luaL_loadfile(L, T2Utf(path)))
+ return Error();
+ return Call();
}