summaryrefslogtreecommitdiff
path: root/indra/llcommon
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llcommon')
-rw-r--r--indra/llcommon/llstring.h39
-rw-r--r--indra/llcommon/lua_function.cpp98
-rw-r--r--indra/llcommon/lua_function.h8
-rw-r--r--indra/llcommon/stringize.h6
4 files changed, 133 insertions, 18 deletions
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h
index b43093fbfc..dbd60bc9c7 100644
--- a/indra/llcommon/llstring.h
+++ b/indra/llcommon/llstring.h
@@ -516,9 +516,36 @@ struct ll_convert_impl
TO operator()(const FROM& in) const;
};
-// Use a function template to get the nice ll_convert<TO>(from_value) API.
+/**
+ * somefunction(ll_convert(data))
+ * target = ll_convert(data)
+ * totype otherfunc(const fromtype& data)
+ * {
+ * // ...
+ * return ll_convert(data);
+ * }
+ * all infer both the FROM type and the TO type.
+ */
+template <typename FROM>
+class ll_convert
+{
+private:
+ const FROM& mRef;
+
+public:
+ ll_convert(const FROM& ref): mRef(ref) {}
+
+ template <typename TO>
+ inline operator TO() const
+ {
+ return ll_convert_impl<TO, FROM>()(mRef);
+ }
+};
+
+// When the TO type must be explicit, use a function template to get
+// ll_convert_to<TO>(from_value) API.
template<typename TO, typename FROM>
-TO ll_convert(const FROM& in)
+TO ll_convert_to(const FROM& in)
{
return ll_convert_impl<TO, FROM>()(in);
}
@@ -574,8 +601,8 @@ inline size_t ll_convert_length<char> (const char* zstr) { return std::strl
// and longname(const string&, len) so calls written pre-ll_convert() will
// work. Most of these overloads will be unified once we turn on C++17 and can
// use std::string_view.
-// It also uses aliasmacro to ensure that both ll_convert<OUTSTR>(const char*)
-// and ll_convert<OUTSTR>(const string&) will work.
+// It also uses aliasmacro to ensure that both ll_convert(const char*)
+// and ll_convert(const string&) will work.
#define ll_convert_forms(aliasmacro, OUTSTR, INSTR, longname) \
LL_COMMON_API OUTSTR longname(const INSTR::value_type* in, size_t len); \
inline auto longname(const INSTR& in, size_t len) \
@@ -807,7 +834,7 @@ LL_COMMON_API std::string ll_convert_string_to_utf8_string(const std::string& in
template<typename STRING>
STRING windows_message(unsigned long error)
{
- return ll_convert<STRING>(windows_message<std::wstring>(error));
+ return ll_convert(windows_message<std::wstring>(error));
}
/// There's only one real implementation
@@ -1780,7 +1807,7 @@ auto LLStringUtilBase<T>::getoptenv(const std::string& key) -> boost::optional<s
if (found)
{
// return populated boost::optional
- return { ll_convert<string_type>(*found) };
+ return { ll_convert_to<string_type>(*found) };
}
else
{
diff --git a/indra/llcommon/lua_function.cpp b/indra/llcommon/lua_function.cpp
index 07e0c1fac2..467ca04449 100644
--- a/indra/llcommon/lua_function.cpp
+++ b/indra/llcommon/lua_function.cpp
@@ -25,6 +25,9 @@
#include "llsdutil.h"
#include "lualistener.h"
+/*****************************************************************************
+* luau namespace
+*****************************************************************************/
namespace
{
// can't specify free function free() as a unique_ptr deleter
@@ -53,6 +56,9 @@ int lluau::dostring(lua_State* L, const std::string& desc, const std::string& te
return lua_pcall(L, 0, LUA_MULTRET, 0);
}
+/*****************************************************************************
+* Lua <=> C++ conversions
+*****************************************************************************/
std::string lua_tostdstring(lua_State* L, int index)
{
size_t len;
@@ -414,6 +420,9 @@ void lua_pushllsd(lua_State* L, const LLSD& data)
}
}
+/*****************************************************************************
+* LuaState class
+*****************************************************************************/
LuaState::LuaState(script_finished_fn cb):
mCallback(cb),
mState(luaL_newstate())
@@ -499,7 +508,9 @@ std::pair<int, LLSD> LuaState::expr(const std::string& desc, const std::string&
return result;
}
-
+/*****************************************************************************
+* LuaPopper class
+*****************************************************************************/
LuaPopper::~LuaPopper()
{
if (mCount)
@@ -508,15 +519,21 @@ LuaPopper::~LuaPopper()
}
}
+/*****************************************************************************
+* LuaFunction class
+*****************************************************************************/
LuaFunction::LuaFunction(const std::string_view& name, lua_CFunction function,
const std::string_view& helptext)
{
- getRegistry().emplace(name, Registry::mapped_type{ function, helptext });
+ const auto& [registry, lookup] = getState();
+ registry.emplace(name, Registry::mapped_type{ function, helptext });
+ lookup.emplace(function, name);
}
void LuaFunction::init(lua_State* L)
{
- for (const auto& [name, pair]: getRegistry())
+ const auto& [registry, lookup] = getRState();
+ for (const auto& [name, pair]: registry)
{
const auto& [funcptr, helptext] = pair;
lua_register(L, name.c_str(), funcptr);
@@ -527,19 +544,80 @@ lua_CFunction LuaFunction::get(const std::string& key)
{
// use find() instead of subscripting to avoid creating an entry for
// unknown key
- const auto& registry{ getRegistry() };
+ const auto& [registry, lookup] = getState();
auto found{ registry.find(key) };
return (found == registry.end())? nullptr : found->second.first;
}
-LuaFunction::Registry& LuaFunction::getRegistry()
+std::pair<LuaFunction::Registry&, LuaFunction::Lookup&> LuaFunction::getState()
{
- // use a function-local static to ensure it's initialized
+ // use function-local statics to ensure they're initialized
static Registry registry;
- return registry;
+ static Lookup lookup;
+ return { registry, lookup };
}
+/*****************************************************************************
+* help()
+*****************************************************************************/
+lua_function(help,
+ "help(): list viewer's Lua functions\n"
+ "help(function): show help string for specific function")
+{
+ auto& luapump{ LLEventPumps::instance().obtain("lua output") };
+ const auto& [registry, lookup]{ LuaFunction::getRState() };
+ if (! lua_gettop(L))
+ {
+ // no arguments passed: list all lua_functions
+ for (const auto& [name, pair] : registry)
+ {
+ const auto& [fptr, helptext] = pair;
+ luapump.post(helptext);
+ }
+ }
+ else
+ {
+ // arguments passed: list each of the specified lua_functions
+ for (int idx = 1, top = lua_gettop(L); idx <= top; ++idx)
+ {
+ std::string arg{ stringize("<unknown ", lua_typename(L, lua_type(L, idx)), ">") };
+ if (lua_type(L, idx) == LUA_TSTRING)
+ {
+ arg = lua_tostdstring(L, idx);
+ }
+ else if (lua_type(L, idx) == LUA_TFUNCTION)
+ {
+ // Caller passed the actual function instead of its string
+ // name. A Lua function is an anonymous callable object; it
+ // has a name only by assigment. You can't ask Lua for a
+ // function's name, which is why our constructor maintains a
+ // reverse Lookup map.
+ auto function{ lua_tocfunction(L, idx) };
+ if (auto found = lookup.find(function); found != lookup.end())
+ {
+ // okay, pass found name to lookup below
+ arg = found->second;
+ }
+ }
+
+ if (auto found = registry.find(arg); found != registry.end())
+ {
+ luapump.post(found->second.second);
+ }
+ else
+ {
+ luapump.post(arg + ": NOT FOUND");
+ }
+ }
+ // pop all arguments
+ lua_settop(L, 0);
+ }
+ return 0; // void return
+}
+/*****************************************************************************
+* lua_what
+*****************************************************************************/
std::ostream& operator<<(std::ostream& out, const lua_what& self)
{
switch (lua_type(self.L, self.index))
@@ -595,6 +673,9 @@ std::ostream& operator<<(std::ostream& out, const lua_what& self)
return out;
}
+/*****************************************************************************
+* lua_stack
+*****************************************************************************/
std::ostream& operator<<(std::ostream& out, const lua_stack& self)
{
const char* sep = "stack: [";
@@ -607,6 +688,9 @@ std::ostream& operator<<(std::ostream& out, const lua_stack& self)
return out;
}
+/*****************************************************************************
+* DebugExit
+*****************************************************************************/
DebugExit::~DebugExit()
{
LL_DEBUGS("Lua") << "exit " << mName << LL_ENDL;
diff --git a/indra/llcommon/lua_function.h b/indra/llcommon/lua_function.h
index c23bf533ba..b3a0bb0d7e 100644
--- a/indra/llcommon/lua_function.h
+++ b/indra/llcommon/lua_function.h
@@ -131,9 +131,13 @@ public:
static lua_CFunction get(const std::string& key);
-private:
+protected:
using Registry = std::map<std::string, std::pair<lua_CFunction, std::string>>;
- static Registry& getRegistry();
+ using Lookup = std::map<lua_CFunction, std::string>;
+ static std::pair<const Registry&, const Lookup&> getRState() { return getState(); }
+
+private:
+ static std::pair<Registry&, Lookup&> getState();
};
/**
diff --git a/indra/llcommon/stringize.h b/indra/llcommon/stringize.h
index c0b13135f9..63d44a7272 100644
--- a/indra/llcommon/stringize.h
+++ b/indra/llcommon/stringize.h
@@ -88,13 +88,13 @@ struct gstringize_impl
};
// partially specialize for a single STRING argument -
-// note that ll_convert<T>(T) already handles the trivial case
+// note that ll_convert_to<T>(T) already handles the trivial case
template <typename OUTCHAR, typename INCHAR>
struct gstringize_impl<OUTCHAR, std::basic_string<INCHAR>>
{
auto operator()(const std::basic_string<INCHAR>& arg)
{
- return ll_convert<std::basic_string<OUTCHAR>>(arg);
+ return ll_convert_to<std::basic_string<OUTCHAR>>(arg);
}
};
@@ -105,7 +105,7 @@ struct gstringize_impl<OUTCHAR, INCHAR*>
{
auto operator()(const INCHAR* arg)
{
- return ll_convert<std::basic_string<OUTCHAR>>(arg);
+ return ll_convert_to<std::basic_string<OUTCHAR>>(arg);
}
};