File: LuaSyncedTable.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (213 lines) | stat: -rw-r--r-- 4,617 bytes parent folder | download
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
#include "StdAfx.h"
// LuaSyncedTable.cpp: implementation of the LuaSyncedTable class.
//
//////////////////////////////////////////////////////////////////////

//  FIXME: it'd probably be faster overall to have a Script.NewSyncTable()
//         available to the synced side, that checks data assignments in a
//         __newindex call. This could be used in conjunction with the
//         current setup to avoid table creation in WrapTable().

#include "mmgr.h"

#include "LuaSyncedTable.h"

#include "LuaInclude.h"

#include "LuaHashString.h"
#include "LuaUtils.h"


// replace a normal table with a read-only proxy
static bool WrapTable(lua_State* L);

// iteration routines
static int Next(lua_State* L);
static int Pairs(lua_State* L);
static int IPairs(lua_State* L);

// meta-table calls
static int SyncTableIndex(lua_State* L);
static int SyncTableNewIndex(lua_State* L);
static int SyncTableMetatable(lua_State* L);


/******************************************************************************/
/******************************************************************************/


bool LuaSyncedTable::PushEntries(lua_State* L)
{
	HSTR_PUSH(L, "SYNCED");
	lua_pushvalue(L, LUA_GLOBALSINDEX);
	WrapTable(L); // replace the GLOBAL table with a proxy
	lua_rawset(L, -3);

	LuaPushNamedCFunc(L, "snext", Next);
	LuaPushNamedCFunc(L, "spairs", Pairs);
	LuaPushNamedCFunc(L, "sipairs", IPairs);
	
	return true;
}


static bool WrapTable(lua_State* L)
{
	const int realTable = lua_gettop(L);

	lua_newtable(L); { // the proxy table

		lua_newtable(L); { // the metatable

			HSTR_PUSH(L, "__index");
			lua_pushvalue(L, realTable);
			lua_pushcclosure(L, SyncTableIndex, 1);
			lua_rawset(L, -3); // closure 

			HSTR_PUSH(L, "__newindex");
			lua_pushcfunction(L, SyncTableNewIndex);
			lua_rawset(L, -3);

			HSTR_PUSH(L, "__metatable");
			lua_pushcfunction(L, SyncTableMetatable);
			lua_rawset(L, -3);

			HSTR_PUSH(L, "realTable");
			lua_pushvalue(L, realTable);
			lua_rawset(L, -3);
		}

		lua_setmetatable(L, -2);
	}

	lua_remove(L, realTable);

	return true;
}


/******************************************************************************/

static int SyncTableIndex(lua_State* L)
{
	lua_gettable(L, lua_upvalueindex(1));

	if (lua_isstring(L, -1) ||
	    lua_isnumber(L, -1) ||
	    lua_isboolean(L, -1)) {
		return 1;
	}
	
	if (lua_istable(L, -1)) {
		if (WrapTable(L)) {
			return 1;
		} else {
			return 0;
		}
	}
	
	return 0; // functions, userdata, etc...
}


static int SyncTableNewIndex(lua_State* L)
{
	luaL_error(L, "Attempt to write to SYNCED table");
	return 0;
}


static int SyncTableMetatable(lua_State* L)
{
	luaL_error(L, "Attempt to access SYNCED metatable");
	return 0;
}


/******************************************************************************/

static inline void PushRealTable(lua_State* L, int index, const char* name)
{
	if (lua_getmetatable(L, 1) == 0) {
		luaL_error(L, "Error: using %s() with an invalid table", name);
	}
	HSTR_PUSH(L, "realTable");
	lua_rawget(L, -2);
	if (!lua_istable(L, -1)) {
		luaL_error(L, "Error: using %s() with an invalid table", name);
	}
	return;
}


static int Next(lua_State* L)
{
	luaL_checktype(L, 1, LUA_TTABLE);
	lua_settop(L, 2); // create a 2nd argument if there isn't one

	PushRealTable(L, 1, "snext");
	const int realTable = lua_gettop(L);

	lua_pushvalue(L, 2); // push the key to the top
	if (!lua_next(L, realTable)) {
		lua_pushnil(L);
		return 1;
	}

	// can not use tables as keys
	while (lua_istable(L, -2)) {
		lua_pop(L, 1); // remove the value
		if (!lua_next(L, realTable)) {
			lua_pushnil(L);
			return 1;
		}
	}

	if (lua_istable(L, -1)) {
		WrapTable(L);
	}
	
	return 2;
}


static int Pairs(lua_State* L)
{
	luaL_checktype(L, 1, LUA_TTABLE);
	lua_pushcfunction(L, Next);	// iterator
	lua_pushvalue(L, 1);        // state (table)
	lua_pushnil(L);             // initial value
	return 3;
}


static int IPairs(lua_State* L)
{
	lua_Number i = lua_tonumber(L, 2);
	luaL_checktype(L, 1, LUA_TTABLE);

	// initializing
	if ((i == 0) && lua_isnone(L, 2)) {
		PushRealTable(L, 1, "sipairs");
		lua_pushcclosure(L, IPairs, 1); // iterator
		lua_pushvalue(L, 1);            // state (table)
		lua_pushnumber(L, 0);           // initial value
		return 3;
	}

	// incrementing
	i++;
	lua_pushnumber(L, i);
	lua_rawgeti(L, lua_upvalueindex(1), (int)i);
	if (lua_isnil(L, -1)) {
		return 0;
	}
	if (lua_istable(L, -1)) {
		WrapTable(L);
	}
	return 2;
}


/******************************************************************************/
/******************************************************************************/