summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/SmartAutoReplier/LuaScript.cpp
blob: 5ad1508d9954a700427785d5c7688e1bb730b373 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 *  Smart Auto Replier (SAR) - auto replier plugin for Miranda IM
 *
 *  Copyright (C) 2004 - 2012 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>
 *
 *  This code is inspired by article of RichardS at http://www.codeproject.com/Articles/11508/Integrating-Lua-into-C
 *
 *      This file is part of SAR.
 *
 *  SAR is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SAR is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SAR.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

#include <assert.h>
#include "luainc.h"
#include "luascript.h"

#define LUA_CODE_CHUNK_ENTER(luaBridge) lua_State *state = (lua_State*)luaBridge;

#define LUA_CODE_CHUNK_LEAVE

CLuaScript::CLuaScript(CLuaBridge & luaBridge) : m_luaBridge(luaBridge), m_nMethods(DEFAULT_INDEX_INITIALIZER), m_nThisRef(DEFAULT_INDEX_INITIALIZER), m_nArgs(DEFAULT_INDEX_INITIALIZER)
{
LUA_CODE_CHUNK_ENTER(luaBridge)

	lua_newtable(state);
	m_nThisRef = luaL_ref(state, LUA_REGISTRYINDEX);

	CLuaStack luaStack(luaBridge);
	lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef);
	lua_pushlightuserdata(state, reinterpret_cast<void*>(this));
	lua_rawseti(state, -2, 0);

LUA_CODE_CHUNK_LEAVE
}

CLuaScript::~CLuaScript(void)
{
	CLuaStack luaStack(m_luaBridge);

LUA_CODE_CHUNK_ENTER(m_luaBridge)
	
	lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef);
	lua_pushnil(state);
	lua_rawseti(state, -2, 0);

LUA_CODE_CHUNK_LEAVE
}

int CLuaScript::LuaCallback(lua_State *lua)
{
	int iNumberIdx	 = lua_upvalueindex(1);
	int nRetsOnStack = 0;

	bool bRetVal = false;

	if (lua_istable(lua, 1))
	{
		lua_rawgeti(lua, 1, 0);

		if (lua_islightuserdata(lua, -1))
		{
			CLuaScript *pThis	= reinterpret_cast<CLuaScript*>(lua_touserdata(lua, -1));
			int			nMethod = static_cast<int>(lua_tonumber(lua, iNumberIdx));

			assert(!(nMethod > pThis->MethodsCount()));

			lua_remove(lua, 1); lua_remove(lua, -1);

			nRetsOnStack = pThis->ScriptCalling(pThis->Bridge(), nMethod);

			bRetVal = true;
		}
	}

	if (bRetVal == false)
	{
		lua_error(lua);
	}

	return nRetsOnStack;
}

bool CLuaScript::CompileScript(const char *szScript, size_t nLength)
{  
	CLuaTableThis luaThisTable(m_luaBridge, m_nThisRef);

	return m_luaBridge.RunScript(szScript, nLength);
}

int CLuaScript::RegisterFunction(const char *szFuncName)
{
	int nMethod = -1;

	CLuaStack luaStack(m_luaBridge);

LUA_CODE_CHUNK_ENTER(m_luaBridge)

	nMethod = ++m_nMethods;
	
	lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef);
	lua_pushstring(state, szFuncName);
	lua_pushnumber(state, (lua_Number)nMethod);
	lua_pushcclosure(state, CLuaScript::LuaCallback, 1);
	lua_settable(state, -3);

LUA_CODE_CHUNK_LEAVE

	return nMethod;
}

bool CLuaScript::SelectScriptFunction(const char *szFunction)
{
	bool bRetVal = true;

LUA_CODE_CHUNK_ENTER(m_luaBridge)

	lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef);
	lua_pushstring(state, szFunction);
	lua_rawget(state, -2);
	lua_remove(state, -2);

	lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef);

	if (!lua_isfunction(state, -2))
	{
		bRetVal = false;
		lua_pop(state, 2);
	}
	else
	{
		m_nArgs = 0;
		m_szFunction = szFunction;
	}

LUA_CODE_CHUNK_LEAVE
   
	return bRetVal;
}

bool CLuaScript::ScriptHasFunction(const char * szFunction)
{
	CLuaStack luaStack(m_luaBridge);

	bool bRetVal = false;

LUA_CODE_CHUNK_ENTER(m_luaBridge)

	lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef);
	lua_pushstring(state, szFunction);
	lua_rawget(state, -2);
	lua_remove(state, -2);

	bRetVal = (lua_isfunction(state, -1) != FALSE);

LUA_CODE_CHUNK_LEAVE

	return bRetVal;
}

void CLuaScript::AddParam(char * szValue)
{
LUA_CODE_CHUNK_ENTER(m_luaBridge)
	lua_pushstring(state, szValue); ++m_nArgs;
LUA_CODE_CHUNK_LEAVE
}

void CLuaScript::AddParam(int nValue)
{
LUA_CODE_CHUNK_ENTER(m_luaBridge)
	lua_pushnumber(state, (lua_Number)nValue); ++m_nArgs;
LUA_CODE_CHUNK_LEAVE
}

void CLuaScript::AddParam(float fValue)
{
LUA_CODE_CHUNK_ENTER(m_luaBridge)
	lua_pushnumber(state, (lua_Number)fValue); ++m_nArgs;
LUA_CODE_CHUNK_LEAVE
}

bool CLuaScript::Run(int nRetValues)
{
	bool bRetVal = m_luaBridge.ExecuteFunction((m_nArgs + 1), nRetValues);

	if (bRetVal == true && nRetValues > 0)
	{
		HandleReturns(m_luaBridge, m_szFunction);
		lua_pop(static_cast<lua_State*>(m_luaBridge), nRetValues);
	}

	return bRetVal;
}

CLuaBridge & CLuaScript::Bridge()
{ 
	return m_luaBridge; 
}

int CLuaScript::MethodsCount()
{
	return m_nMethods;
}