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
|
/**
* @file llluamanager.cpp
* @brief classes and functions for interfacing with LUA.
*
* $LicenseInfo:firstyear=2023&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2023, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*
*/
#include "llviewerprecompiledheaders.h"
#include "llluamanager.h"
#include "llerror.h"
#include "lleventcoro.h"
#include "lua_function.h"
#include "lualistener.h"
#include "stringize.h"
// skip all these link dependencies for integration testing
#ifndef LL_TEST
#include "lluilistener.h"
#include "llviewercontrol.h"
// FIXME extremely hacky way to get to the UI Listener framework. There's
// a cleaner way.
extern LLUIListener sUIListener;
#endif // ! LL_TEST
#include <boost/algorithm/string/replace.hpp>
#include "luau/luacode.h"
#include "luau/lua.h"
#include "luau/luaconf.h"
#include "luau/lualib.h"
#include <sstream>
#include <string_view>
#include <vector>
/*
// This function consumes ALL Lua stack arguments and returns concatenated
// message string
std::string lua_print_msg_args(lua_State* L, const std::string_view& level)
{
// On top of existing Lua arguments, push 'where' info
luaL_checkstack(L, 1, nullptr);
luaL_where(L, 1);
// start with the 'where' info at the top of the stack
std::ostringstream out;
out << lua_tostring(L, -1);
lua_pop(L, 1);
const char* sep = ""; // 'where' info ends with ": "
// now iterate over arbitrary args, calling Lua tostring() on each and
// concatenating with separators
for (int p = 1; p <= lua_gettop(L); ++p)
{
out << sep;
sep = " ";
// push Lua tostring() function -- note, semantically different from
// lua_tostring()!
lua_getglobal(L, "tostring");
// Now the stack is arguments 1 .. N, plus tostring().
// Rotate downwards, producing stack args 2 .. N, tostring(), arg1.
lua_rotate(L, 1, -1);
// pop tostring() and arg1, pushing tostring(arg1)
// (ignore potential error code from lua_pcall() because, if there was
// an error, we expect the stack top to be an error message -- which
// we'll print)
lua_pcall(L, 1, 1, 0);
// stack now holds args 2 .. N, tostring(arg1)
out << lua_tostring(L, -1);
}
// pop everything
lua_settop(L, 0);
// capture message string
std::string msg{ out.str() };
// put message out there for any interested party (*koff* LLFloaterLUADebug *koff*)
LLEventPumps::instance().obtain("lua output").post(stringize(level, ": ", msg));
return msg;
}
*/
std::string lua_print_msg(lua_State *L, const std::string_view &level)
{
lua_getglobal(L, "tostring");
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, 1); /* value to print */
lua_call(L, 1, 1);
std::string msg = lua_tostring(L, -1);
LLEventPumps::instance().obtain("lua output").post(stringize(level, ": ", msg));
return msg;
}
lua_function(print_debug)
{
LL_DEBUGS("Lua") << lua_print_msg(L, "DEBUG") << LL_ENDL;
return 0;
}
// also used for print(); see LuaState constructor
lua_function(print_info)
{
LL_INFOS("Lua") << lua_print_msg(L, "INFO") << LL_ENDL;
return 0;
}
lua_function(print_warning)
{
LL_WARNS("Lua") << lua_print_msg(L, "WARN") << LL_ENDL;
return 0;
}
#ifndef LL_TEST
lua_function(run_ui_command)
{
int top = lua_gettop(L);
std::string func_name;
if (top >= 1)
{
func_name = lua_tostring(L,1);
}
std::string parameter;
if (top >= 2)
{
parameter = lua_tostring(L,2);
}
LL_WARNS("LUA") << "running ui func " << func_name << " parameter " << parameter << LL_ENDL;
LLSD event;
event["function"] = func_name;
if (!parameter.empty())
{
event["parameter"] = parameter;
}
sUIListener.call(event);
lua_settop(L, 0);
return 0;
}
#endif // ! LL_TEST
lua_function(post_on)
{
std::string pumpname{ lua_tostdstring(L, 1) };
LLSD data{ lua_tollsd(L, 2) };
lua_pop(L, 2);
LL_INFOS("Lua") << "post_on('" << pumpname << "', " << data << ")" << LL_ENDL;
LLEventPumps::instance().obtain(pumpname).post(data);
return 0;
}
lua_function(listen_events)
{
if (! lua_isfunction(L, 1))
{
luaL_typeerror(L, 1, "function");
return 0;
}
luaL_checkstack(L, 2, nullptr);
// Get the lua_State* for the main thread of this state, in case we were
// called from a coroutine thread. We're going to make callbacks into Lua
// code, and we want to do it on the main thread rather than a (possibly
// suspended) coroutine thread.
// Registry table is at pseudo-index LUA_REGISTRYINDEX
// Main thread is at registry key LUA_RIDX_MAINTHREAD
auto regtype {lua_rawgeti(L, LUA_REGISTRYINDEX, 1 /*LUA_RIDX_MAINTHREAD*/)};
// Not finding the main thread at the documented place isn't a user error,
// it's a Problem
llassert_always(regtype == LUA_TTHREAD);
lua_State* mainthread{ lua_tothread(L, -1) };
// pop the main thread
lua_pop(L, 1);
luaL_checkstack(mainthread, 1, nullptr);
LuaListener::ptr_t listener;
// Does the main thread already have a LuaListener stored in the registry?
// That is, has this Lua chunk already called listen_events()?
auto keytype{ lua_getfield(mainthread, LUA_REGISTRYINDEX, "event.listener") };
llassert(keytype == LUA_TNIL || keytype == LUA_TNUMBER);
if (keytype == LUA_TNUMBER)
{
// We do already have a LuaListener. Retrieve it.
int isint;
listener = LuaListener::getInstance(lua_tointegerx(mainthread, -1, &isint));
// pop the int "event.listener" key
lua_pop(mainthread, 1);
// Nobody should have destroyed this LuaListener instance!
llassert(isint && listener);
}
else
{
// pop the nil "event.listener" key
lua_pop(mainthread, 1);
// instantiate a new LuaListener, binding the mainthread state -- but
// use a no-op deleter: we do NOT want to delete this new LuaListener
// on return from listen_events()!
listener.reset(new LuaListener(mainthread), [](LuaListener*){});
// set its key in the field where we'll look for it later
lua_pushinteger(mainthread, listener->getKey());
lua_setfield(mainthread, LUA_REGISTRYINDEX, "event.listener");
}
// Now that we've found or created our LuaListener, store the passed Lua
// function as the callback. Beware: our caller passed the function on L's
// stack, but we want to store it on the mainthread registry.
if (L != mainthread)
{
// push 1 value (the Lua function) from L's stack to mainthread's
lua_xmove(L, mainthread, 1);
}
lua_setfield(mainthread, LUA_REGISTRYINDEX, "event.function");
// return the reply pump name and the command pump name on caller's lua_State
lua_pushstdstring(L, listener->getReplyName());
lua_pushstdstring(L, listener->getCommandName());
return 2;
}
lua_function(await_event)
{
// await_event(pumpname [, timeout [, value to return if timeout (default nil)]])
auto pumpname{ lua_tostdstring(L, 1) };
LLSD result;
if (lua_gettop(L) > 1)
{
auto timeout{ lua_tonumber(L, 2) };
// with no 3rd argument, should be LLSD()
auto dftval{ lua_tollsd(L, 3) };
lua_settop(L, 0);
result = llcoro::suspendUntilEventOnWithTimeout(pumpname, timeout, dftval);
}
else
{
// no timeout
lua_pop(L, 1);
result = llcoro::suspendUntilEventOn(pumpname);
}
lua_pushllsd(L, result);
return 1;
}
void LLLUAmanager::runScriptFile(const std::string& filename, script_finished_fn cb)
{
std::string desc{ stringize("runScriptFile('", filename, "')") };
LLCoros::instance().launch(desc, [desc, filename, cb]()
{
LuaState L(desc, cb);
auto LUA_sleep_func = [](lua_State *L)
{
F32 seconds = lua_tonumber(L, -1);
lua_pop(L, 1);
llcoro::suspendUntilTimeout(seconds);
return 0;
};
lua_register(L, "sleep", LUA_sleep_func);
llifstream in_file;
in_file.open(filename.c_str());
if (in_file.is_open())
{
std::string text{std::istreambuf_iterator<char>(in_file),
std::istreambuf_iterator<char>()};
L.checkLua(lluau::dostring(L, desc, text));
}
else
{
LL_WARNS("Lua") << "unable to open script file '" << filename << "'" << LL_ENDL;
}
});
}
void LLLUAmanager::runScriptLine(const std::string& cmd, script_finished_fn cb)
{
// find a suitable abbreviation for the cmd string
std::string_view shortcmd{ cmd };
const size_t shortlen = 40;
std::string::size_type eol = shortcmd.find_first_of("\r\n");
if (eol != std::string::npos)
shortcmd = shortcmd.substr(0, eol);
if (shortcmd.length() > shortlen)
shortcmd = stringize(shortcmd.substr(0, shortlen), "...");
std::string desc{ stringize("runScriptLine('", shortcmd, "')") };
LLCoros::instance().launch(desc, [desc, cmd, cb]()
{
LuaState L(desc, cb);
L.checkLua(lluau::dostring(L, desc, cmd));
});
}
void LLLUAmanager::runScriptOnLogin()
{
#ifndef LL_TEST
std::string filename = gSavedSettings.getString("AutorunLuaScriptName");
if (filename.empty())
{
LL_INFOS() << "Script name wasn't set." << LL_ENDL;
return;
}
filename = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, filename);
if (!gDirUtilp->fileExists(filename))
{
LL_INFOS() << filename << " was not found." << LL_ENDL;
return;
}
runScriptFile(filename);
#endif // ! LL_TEST
}
|