summaryrefslogtreecommitdiff
path: root/src/config/lua/bindings/LuaBindingsDispatcherUtils.cpp
blob: a55dfc65506f0e9efa4b2173b9ecfa5d82095125 (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
#include "LuaBindingsInternal.hpp"

using namespace Config::Lua::Bindings;

static constexpr const char* DISPATCHER_MT = "HL.Dispatcher";
static char                  DISPATCHER_TABLES_REGISTRY_KEY;

namespace {
    struct SDispatcherRef {
        int ref = LUA_NOREF;
    };
}

static int dispatcherGc(lua_State* L) {
    auto* dispatcher = sc<SDispatcherRef*>(luaL_checkudata(L, 1, DISPATCHER_MT));
    if (dispatcher->ref != LUA_NOREF) {
        luaL_unref(L, LUA_REGISTRYINDEX, dispatcher->ref);
        dispatcher->ref = LUA_NOREF;
    }

    return 0;
}

static int dispatcherCall(lua_State* L) {
    return Internal::configError(L, "dispatcher objects cannot be called directly; use hl.dispatch(dispatcher)");
}

static int dispatcherToString(lua_State* L) {
    lua_pushstring(L, "HL.Dispatcher");
    return 1;
}

static void ensureDispatcherMetatable(lua_State* L) {
    if (luaL_newmetatable(L, DISPATCHER_MT)) {
        lua_pushcfunction(L, dispatcherGc);
        lua_setfield(L, -2, "__gc");
        lua_pushcfunction(L, dispatcherCall);
        lua_setfield(L, -2, "__call");
        lua_pushcfunction(L, dispatcherToString);
        lua_setfield(L, -2, "__tostring");

        lua_pushstring(L, DISPATCHER_MT);
        lua_setfield(L, -2, "__metatable");
    }

    lua_pop(L, 1);
}

static bool isDispatcherTable(lua_State* L, int idx) {
    if (!lua_istable(L, idx))
        return false;

    idx = lua_absindex(L, idx);
    lua_pushlightuserdata(L, &DISPATCHER_TABLES_REGISTRY_KEY);
    lua_rawget(L, LUA_REGISTRYINDEX);

    if (!lua_istable(L, -1)) {
        lua_pop(L, 1);
        return false;
    }

    lua_pushvalue(L, idx);
    lua_rawget(L, -2);
    const bool result = lua_toboolean(L, -1);
    lua_pop(L, 2);
    return result;
}

static int dispatcherFactory(lua_State* L) {
    const int nargs = lua_gettop(L);

    lua_pushvalue(L, lua_upvalueindex(1));
    lua_insert(L, 1);
    lua_call(L, nargs, LUA_MULTRET);

    const int nresults = lua_gettop(L);
    if (nresults == 1 && lua_isfunction(L, -1))
        return Internal::wrapDispatcher(L);

    return nresults;
}

void Internal::setFn(lua_State* L, const char* name, lua_CFunction fn) {
    if (isDispatcherTable(L, -1)) {
        lua_pushcfunction(L, fn);
        lua_pushcclosure(L, dispatcherFactory, 1);
    } else
        lua_pushcfunction(L, fn);

    lua_setfield(L, -2, name);
}

void Internal::markDispatcherTable(lua_State* L) {
    if (!lua_istable(L, -1))
        return;

    lua_pushlightuserdata(L, &DISPATCHER_TABLES_REGISTRY_KEY);
    lua_rawget(L, LUA_REGISTRYINDEX);

    if (!lua_istable(L, -1)) {
        lua_pop(L, 1);
        lua_newtable(L);
        lua_pushlightuserdata(L, &DISPATCHER_TABLES_REGISTRY_KEY);
        lua_pushvalue(L, -2);
        lua_rawset(L, LUA_REGISTRYINDEX);
    }

    lua_pushvalue(L, -2);
    lua_pushboolean(L, true);
    lua_rawset(L, -3);
    lua_pop(L, 1);
}

int Internal::wrapDispatcher(lua_State* L) {
    luaL_checktype(L, -1, LUA_TFUNCTION);

    const int ref = luaL_ref(L, LUA_REGISTRYINDEX);

    new (lua_newuserdata(L, sizeof(SDispatcherRef))) SDispatcherRef{.ref = ref};

    ensureDispatcherMetatable(L);
    luaL_getmetatable(L, DISPATCHER_MT);
    lua_setmetatable(L, -2);

    return 1;
}

bool Internal::pushDispatcherFunction(lua_State* L, int idx) {
    if (lua_isfunction(L, idx)) {
        lua_pushvalue(L, idx);
        return true;
    }

    auto* dispatcher = sc<SDispatcherRef*>(luaL_testudata(L, idx, DISPATCHER_MT));
    if (!dispatcher || dispatcher->ref == LUA_NOREF)
        return false;

    lua_rawgeti(L, LUA_REGISTRYINDEX, dispatcher->ref);
    if (lua_isfunction(L, -1))
        return true;

    lua_pop(L, 1);
    return false;
}