aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/je_lua.cpp
blob: 020599da5b77aed4575a1b56f54a26d1f52c5f9f (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "libjin/jin.h"

#include "je_lua.h"

using namespace JinEngine::Math;

namespace JinEngine
{
    namespace Lua
    {

        static const char* Jin_Lua_Objects_Table = "Jin_Objects_Table";

        static const char* Jin_Lua_Modules_Table = "Jin_Modules_Table";

        static const char* Jin_Lua_Reference_Table = "Jin_Reference_Table";

        Proxy* luax_newinstance(lua_State* L, const char* type, SharedBase* shared)
        {
            Proxy* proxy = static_cast<Proxy*>(luax_newinstance(L, type, sizeof(Proxy)));
            if (shared) proxy->bind(shared);
            luax_getobjectstable(L);
            // Add to objects_table, like objects_table[shared] = proxy
            luax_pushlightuserdata(L, shared);
            luax_pushvalue(L, -3);
            luax_settable(L, -3);
            luax_pop(L, 1); // Pop objects table.
            return proxy;
        }

        int luax_getobject(lua_State* L, SharedBase* shared)
        {
            luax_getobjectstable(L);
            luax_pushlightuserdata(L, shared);
            luax_gettable(L, -2);
            luax_remove(L, -2); // Remove objects table on stack.
            return 1;
        }

        void luax_removeobject(lua_State* L, SharedBase* shared)
        {
            luax_getobjectstable(L);
            luax_pushlightuserdata(L, shared);
            luax_pushnil(L);
            luax_settable(L, -3);
            luax_pop(L, 1);
        }

        int luax_getobjectstable(lua_State* L)
        {
            luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Objects_Table);
            // If no such table, add one.
            if (luax_isnil(L, -1) || !luax_istable(L, -1))
            {
                luax_pop(L, 1);
                luax_newtable(L);

                // metatable 
                luax_newtable(L);
                // weak table
                luax_pushliteral(L, "v");
                luax_setfield(L, -2, "__mode");
                // setmetatable(newtable, metatable)
                luax_setmetatable(L, -2);

                luax_pushvalue(L, -1);
                luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Objects_Table);
            }
            return 1;
        }

        int luax_getmodulestable(lua_State* L)
        {
            luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Modules_Table);
            // If no such table, add one.
            if (luax_isnil(L, -1) || !luax_istable(L, -1))
            {
                luax_pop(L, 1);
                luax_newtable(L);

                // metatable 
                luax_newtable(L);
                // weak table
                luax_pushliteral(L, "v");
                luax_setfield(L, -2, "__mode");
                // setmetatable(newtable, metatable)
                luax_setmetatable(L, -2);

                luax_pushvalue(L, -1);
                luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Modules_Table);
            }
            return 1;
        }

        int luax_getreferencestable(lua_State* L)
        {
            luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Reference_Table);
            // If no such table, add one.
            if (luax_isnil(L, -1) || !luax_istable(L, -1))
            {
                luax_pop(L, 1);
                luax_newtable(L);
                luax_pushvalue(L, -1);
                luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Reference_Table);
            }
            return 1;
        }

        void luax_getreference(lua_State* L, SharedBase* shared)
        {
            luax_getreferencestable(L);
            luax_pushlightuserdata(L, shared);
            luax_gettable(L, -2);
            luax_remove(L, -2);
        }

        bool luax_addreference(lua_State* L, SharedBase* shared, SharedBase* dep)
        {
            luax_getreference(L, shared);
            // If no dependencies table, add one.
            if (luax_isnil(L, -1))
            {
                luax_pop(L, 1);
                luax_getreferencestable(L);
                luax_newtable(L);
                luax_pushlightuserdata(L, shared);
                luax_pushvalue(L, -2);
                luax_settable(L, -4);
                luax_remove(L, -2); // Remove references table.
            }
            luax_pushlightuserdata(L, dep);
            luax_getobject(L, dep);
            if (luax_isnil(L, -1))
            {
                luax_pop(L, 3); // Pop nil, dep, reftbl.
                return false;
            }
            luax_settable(L, -3);
            luax_pop(L, 1);
            return true;
        }

        void luax_removereference(lua_State* L, SharedBase* shared)
        {
            luax_getreferencestable(L);
            luax_pushlightuserdata(L, shared);
            luax_pushnil(L);
            luax_settable(L, -3);
            luax_pop(L, 1);
        }

        void luax_removereference(lua_State* L, SharedBase* shared, SharedBase* dep)
        {
            luax_getreference(L, shared);
            if (luax_isnil(L, -1))
            {
                luax_pop(L, 1);
                return;
            }
            luax_pushlightuserdata(L, dep);
            luax_pushnil(L);
            luax_settable(L, -3);
            luax_pop(L, 1);
        }

    }
}