summaryrefslogtreecommitdiff
path: root/indra/llcommon/lualistener.cpp
blob: 0fa03ffb3b4b69075579ab46ce537a3b02f26b2a (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
/**
 * @file   lualistener.cpp
 * @author Nat Goodspeed
 * @date   2024-02-06
 * @brief  Implementation for lualistener.
 * 
 * $LicenseInfo:firstyear=2024&license=viewerlgpl$
 * Copyright (c) 2024, Linden Research, Inc.
 * $/LicenseInfo$
 */

// Precompiled header
#include "linden_common.h"
// associated header
#include "lualistener.h"
// STL headers
// std headers
#include <cstdlib>                  // std::rand()
#include <cstring>                  // std::memcpy()
// external library headers
#include "luau/lua.h"
// other Linden headers
#include "llerror.h"
#include "llleaplistener.h"
#include "lua_function.h"

LuaListener::LuaListener(lua_State* L):
    super(getUniqueKey()),
    mListener(
        new LLLeapListener(
            [L](LLEventPump& pump, const std::string& listener)
            { return connect(L, pump, listener); }))
{
    mReplyConnection = connect(L, mReplyPump, "LuaListener");
}

LuaListener::~LuaListener()
{
    LL_DEBUGS("Lua") << "~LuaListener('" << mReplyPump.getName() << "')" << LL_ENDL;
}

int LuaListener::getUniqueKey()
{
    // Find a random key that does NOT already correspond to a LuaListener
    // instance. Passing a duplicate key to LLInstanceTracker would do Bad
    // Things.
    int key;
    do
    {
        key = std::rand();
    } while (LuaListener::getInstance(key));
    // This is theoretically racy, if we were instantiating new
    // LuaListeners on multiple threads. Don't.
    return key;
}

LLBoundListener LuaListener::connect(lua_State* L, LLEventPump& pump, const std::string& listener)
{
    return pump.listen(
        listener,
        [L, pumpname=pump.getName()](const LLSD& data)
        { return call_lua(L, pumpname, data); });
}

bool LuaListener::call_lua(lua_State* L, const std::string& pump, const LLSD& data)
{
    LL_INFOS("Lua") << "LuaListener::call_lua('" << pump << "', " << data << ")" << LL_ENDL;
    if (! lua_checkstack(L, 3))
    {
        LL_WARNS("Lua") << "Cannot extend Lua stack to call listen_events() callback"
                        << LL_ENDL;
        return false;
    }
    // push the registered Lua callback function stored in our registry as
    // "event.function"
    lua_getfield(L, LUA_REGISTRYINDEX, "event.function");
    llassert(lua_isfunction(L, -1));
    // pass pump name
    lua_pushstdstring(L, pump);
    // then the data blob
    lua_pushllsd(L, data);
    // call the registered Lua listener function; allow it to return bool;
    // no message handler
    auto status = lua_pcall(L, 2, 1, 0);
    bool result{ false };
    if (status != LUA_OK)
    {
        LL_WARNS("Lua") << "Error in listen_events() callback: "
                        << lua_tostdstring(L, -1) << LL_ENDL;
    }
    else
    {
        result = lua_toboolean(L, -1);
    }
    // discard either the error message or the bool return value
    lua_pop(L, 1);
    return result;
}

std::string LuaListener::getCommandName() const
{
    return mListener->getPumpName();
}