File: LuaScript.cpp

package info (click to toggle)
texworks 0.6.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 24,600 kB
  • sloc: cpp: 32,035; ansic: 8,252; javascript: 972; xml: 344; python: 208; sh: 157; makefile: 24
file content (440 lines) | stat: -rw-r--r-- 12,543 bytes parent folder | download
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
	This is part of TeXworks, an environment for working with TeX documents
	Copyright (C) 2010-2023  Jonathan Kew, Stefan Löffler, Charlie Sharpsteen

	This program 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 2 of the License, or
	(at your option) any later version.

	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

	For links to further information, or to contact the authors,
	see <http://www.tug.org/texworks/>.
*/

#include "LuaScript.h"

#include <lua.hpp>

// Compatibility with lua < 5.3
#ifndef lua_isinteger
#define lua_isinteger(L, idx) lua_isnumber((L), (idx))
#endif // !defined(lua_isinteger)
#ifndef lua_tointeger
#define lua_tointeger(L, idx) static_cast<int>(lua_tonumber((L), (idx)))
#endif // !defined(lua_tointeger)

#include <QMetaObject>
#include <QStringList>
#include <QTextStream>
#include <QtPlugin>

namespace Tw {
namespace Scripting {

bool LuaScript::execute(ScriptAPIInterface * tw) const
{
	lua_State * L = m_LuaPlugin->getLuaState();

	if (!L)
		return false;

	// register the TW interface for use in lua
	if (!LuaScript::pushQObject(L, tw->self(), false)) {
		tw->SetResult(tr("Could not register TW"));
		return false;
	}
	lua_setglobal(L, "TW");

	int status = luaL_loadfile(L, qPrintable(m_Filename));
	if (status != 0) {
		tw->SetResult(getLuaStackValue(L, -1, false).toString());
		lua_pop(L, 1);
		return false;
	}

	// call the script
	status = lua_pcall(L, 0, LUA_MULTRET, 0);
	if (status != 0) {
		tw->SetResult(getLuaStackValue(L, -1, false).toString());
		lua_pop(L, 1);
		return false;
	}

	lua_pushnil(L);
	lua_setglobal(L, "TW");

	return true;
}

/*static*/
int LuaScript::pushQObject(lua_State * L, QObject * obj, const bool throwError /* = true */)
{
	Q_UNUSED(throwError)

	if (!L || !obj)
		return 0;

	lua_newtable(L);

	// register callback for all get/set operations on object properties and
	// all call operations on object methods
	if (lua_getmetatable(L, -1) == 0)
		lua_newtable(L);

	lua_pushlightuserdata(L, obj);
	lua_setfield(L, -2, "__qobject");

	lua_pushlightuserdata(L, obj);
	lua_pushcclosure(L, LuaScript::setProperty, 1);
	lua_setfield(L, -2, "__newindex");

	lua_pushlightuserdata(L, obj);
	lua_pushcclosure(L, LuaScript::getProperty, 1);
	lua_setfield(L, -2, "__index");

	lua_pushlightuserdata(L, obj);
	lua_pushcclosure(L, LuaScript::callMethod, 1);
	lua_setfield(L, -2, "__call");

	lua_setmetatable(L, -2);
	return 1;
}

/*static*/
int LuaScript::pushVariant(lua_State * L, const QVariant & v, const bool throwError /* = true */)
{
	if (!L)
		return 0;
	if (v.isNull()) {
		lua_pushnil(L);
		return 1;
	}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
	switch (static_cast<int>(v.type())) {
#else
	switch (v.metaType().id()) {
#endif
		case QMetaType::Bool:
			lua_pushboolean(L, v.toBool());
			return 1;
		case QMetaType::Double:
		case QMetaType::Int:
		case QMetaType::LongLong:
		case QMetaType::UInt:
		case QMetaType::ULongLong:
			lua_pushnumber(L, v.toDouble());
			return 1;
		case QMetaType::Char:
		case QMetaType::QString:
			lua_pushstring(L, v.toString().toUtf8().constData());
			return 1;
		case QMetaType::QVariantList:
		case QMetaType::QStringList:
		{
			QVariantList list = v.toList();

			lua_newtable(L);
			int i{1};
			for (QVariantList::const_iterator iList = list.cbegin(); iList != list.cend(); ++iList, ++i) {
				lua_pushnumber(L, i);
				LuaScript::pushVariant(L, *iList);
				lua_settable(L, -3);
			}
			return 1;
		}
		case QMetaType::QVariantHash:
		{
			QVariantHash hash = v.toHash();

			lua_newtable(L);
			for (QVariantHash::const_iterator iHash = hash.cbegin(); iHash != hash.cend(); ++iHash) {
				LuaScript::pushVariant(L, iHash.value());
				lua_setfield(L, -2, qPrintable(iHash.key()));
			}
			return 1;
		}
		case QMetaType::QVariantMap:
		{
			QVariantMap map = v.toMap();

			lua_newtable(L);
			for (QVariantMap::const_iterator iMap = map.cbegin(); iMap != map.cend(); ++iMap) {
				LuaScript::pushVariant(L, iMap.value());
				lua_setfield(L, -2, qPrintable(iMap.key()));
			}
			return 1;
		}
		case QMetaType::QObjectStar:
			return LuaScript::pushQObject(L, v.value<QObject*>(), throwError);
		default:
			// Don't throw errors if we are not in protected mode in lua, i.e.
			// if the call to this function originated from C code, not in response
			// to a lua request (e.g. during initialization or finalization) as that
			// would crash Tw
			if (throwError) luaL_error(L, "the type %s is currently not supported", v.typeName());
	}
	return 0;
}

/*static*/
int LuaScript::getProperty(lua_State * L)
{
	QString propName;
	QVariant result;

	// We should have the lua table (=object) we're called from and the property
	// name we should get; if not, something is wrong
	if (lua_gettop(L) != 2) {
		luaL_error(L, qPrintable(tr("__get: invalid call -- expected exactly 2 arguments, got %f")), lua_gettop(L));
		return 0;
	}

	// Get the QObject* we operate on
	QObject * obj = static_cast<QObject*>(lua_touserdata(L, lua_upvalueindex(1)));

	// Get the parameters
	propName = QString::fromUtf8(lua_tostring(L, 2));

	switch (doGetProperty(obj, propName, result)) {
		case Property_DoesNotExist:
			luaL_error(L, qPrintable(tr("__get: object doesn't have property/method %s")), qPrintable(propName));
			return 0;
		case Property_NotReadable:
			luaL_error(L, qPrintable(tr("__get: property %s is not readable")), qPrintable(propName));
			return 0;
		case Property_Method:
			lua_pushlightuserdata(L, obj);
			lua_pushstring(L, qPrintable(propName));
			lua_pushcclosure(L, LuaScript::callMethod, 2);
			return 1;
		case Property_OK:
			return LuaScript::pushVariant(L, result);
		default:
			break;
	}
	// we should never reach this point
	return 0;

}

/*static*/
int LuaScript::callMethod(lua_State * L)
{
	QString methodName;
	QList<QVariant> args;
	QVariant result;

	// Get the QObject* we operate on
	QObject * obj = static_cast<QObject*>(lua_touserdata(L, lua_upvalueindex(1)));

	methodName = QString::fromUtf8(lua_tostring(L, lua_upvalueindex(2)));

	for (int i = 1; i <= lua_gettop(L); ++i) {
		args.append(getLuaStackValue(L, i));
	}

	switch (doCallMethod(obj, methodName, args, result)) {
		case Method_OK:
			return LuaScript::pushVariant(L, result);
		case Method_DoesNotExist:
			luaL_error(L, qPrintable(tr("__call: the method %s doesn't exist")), qPrintable(methodName));
			return 0;
		case Method_WrongArgs:
			luaL_error(L, qPrintable(tr("__call: couldn't call %s with the given arguments")), qPrintable(methodName));
			return 0;
		case Method_Failed:
			luaL_error(L, qPrintable(tr("__call: internal error while executing %s")), qPrintable(methodName));
			return 0;
		default:
			break;
	}

	// we should never reach this point
	return 0;
}

/*static*/
int LuaScript::setProperty(lua_State * L)
{
	QString propName;

	// We should have the lua table (=object) we're called from, the property
	// name we should set, and the new value; if not, something is wrong
	if (lua_gettop(L) != 3) {
		luaL_error(L, qPrintable(tr("__set: invalid call -- expected exactly 3 arguments, got %f")), lua_gettop(L));
		return 0;
	}

	// Get the QObject* we operate on
	QObject * obj = static_cast<QObject*>(lua_touserdata(L, lua_upvalueindex(1)));

	// Get the parameters
	propName = QString::fromUtf8(lua_tostring(L, 2));

	switch (doSetProperty(obj, propName, LuaScript::getLuaStackValue(L, 3))) {
		case Property_DoesNotExist:
			luaL_error(L, qPrintable(tr("__set: object doesn't have property %s")), qPrintable(propName));
			return 0;
		case Property_NotWritable:
			luaL_error(L, qPrintable(tr("__set: property %s is not writable")), qPrintable(propName));
			return 0;
		case Property_OK:
			return 0;
		default:
			break;
	}
	// we should never reach this point
	return 0;
}

/*static*/
QVariant LuaScript::getLuaStackValue(lua_State * L, int idx, const bool throwError /* = true */)
{
	if (!L) return QVariant();

	switch (lua_type(L, idx)) {
		case LUA_TNIL:
			return QVariant();
		case LUA_TNUMBER:
			return QVariant(lua_tonumber(L, idx));
		case LUA_TBOOLEAN:
			return QVariant(lua_toboolean(L, idx) == 1);
		case LUA_TSTRING:
			return QVariant(QString::fromUtf8(lua_tostring(L, idx)));
		case LUA_TTABLE:
		{
			// convert index to an absolute value since we'll be messing with
			// the stack
			if (idx < 0) idx += lua_gettop(L) + 1;

			// Check if we're dealing with a QObject* wrapper
			if (lua_getmetatable(L, idx)) {
				int i = lua_gettop(L);
				lua_pushnil(L);

				// see if the metatable contains the key "__qobject"; if it
				// doesn't, trying to get it later could result in an error
				bool isQObject = false;
				while (lua_next(L, i)) {
					lua_pop(L, 1); // pop the value (we don't need it)
					if (!lua_isstring(L, -1))
						continue;
					lua_pushvalue(L, -1); // duplicate the key so we don't disturb lua_next
					if (QString::fromUtf8(lua_tostring(L, -1)) == QLatin1String("__qobject"))
						isQObject = true;
					lua_pop(L, 1); // pop the duplicate key
				}

				if (isQObject) {
					lua_getfield(L, -1, "__qobject");
					if (lua_islightuserdata(L, -1)) {
						QObject * obj = reinterpret_cast<QObject*>(lua_touserdata(L, -1));
						lua_pop(L, 2);
						return QVariant::fromValue(obj);
					}
					lua_pop(L, 1);
				}
				lua_pop(L, 1); // pop the metatable
			}

			// Special treatment for tables
			// If all keys are in the form 1..n, we can convert it to a QList

			// taken from the lua reference of lua_next()
			lua_pushnil(L);
			int n{0};
			int iMax{0};
			bool isArray{true}, isMap{true};

			while (lua_next(L, idx)) {
				if (isArray) {
					if (!lua_isinteger(L, -2))
						isArray = false;
					else {
						++n;
						if (lua_tointeger(L, -2) > iMax)
							iMax = static_cast<int>(lua_tointeger(L, -2));
					}
				}
				if (isMap) {
					// keys must be convertable to string
					if (!lua_isstring(L, -2))
						isMap = false;
					// some value types are not supported by QVariant
					if (lua_isfunction(L, -1) ||
						lua_islightuserdata(L, -1) ||
						lua_isthread(L, -1) ||
						lua_isuserdata(L, -1) )
						isMap = false;
				}
				lua_pop(L, 1);
			}
			if (n != iMax)
				isArray = false;

			// Lua is picky about the correct type of index for accessing table
			// members. Hence we can't simply retrieve the table items by index
			// because they must only be _convertible_ to numbers, so 1 and "1"
			// should be treated the same (though they are not by Lua). Since
			// they keys need not be ordered when calling lua_next, we have to
			// allocate the complete list first and overwrite the items as we
			// get them.
			if (isArray) {
				QVariantList vl;

				for (int i = 0; i < n; ++i)
					vl.append(QVariant());

				lua_pushnil(L);
				while (lua_next(L, idx)) {
					vl[static_cast<int>(lua_tointeger(L, -2) - 1)] = LuaScript::getLuaStackValue(L, -1);
					lua_pop(L, 1);
				}
				return vl;
			}
			// use QMap here because Lua doesn't support multiple values for one
			// key (those are converted to lists implicitly) and QVariantMap is
			// backwards compatible
			if (isMap) {
				QVariantMap vm;

				lua_pushnil(L);
				while (lua_next(L, idx)) {
					// duplicate the key. If we didn't, lua_tostring could
					// convert it, thereby confusing lua_next later on
					lua_pushvalue(L, -2);
					vm.insert(QString::fromUtf8(lua_tostring(L, -1)), LuaScript::getLuaStackValue(L, -2));
					lua_pop(L, 2);
				}
				return vm;
			}
		}
		// deliberately no break here; if the table could not be converted
		// to QList or QMap, we have to treat it as unsupported
		// fall through
		case LUA_TFUNCTION:
		case LUA_TUSERDATA:
		case LUA_TTHREAD:
		case LUA_TLIGHTUSERDATA:
		default:
			// Don't throw errors if we are not in protected mode in lua, i.e.
			// if the call to this function originated from C code, not in response
			// to a lua request (e.g. during initialization or finalization) as that
			// would crash Tw
			if (throwError)
				luaL_error(L, qPrintable(tr("the lua type %s is currently not supported")), lua_typename(L, lua_type(L, idx)));
	}
	return QVariant();
}

} // namespace Scripting
} // namespace Tw