| 1 |
/* |
| 2 |
Copyright (c) 2010 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <cstring> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "lua.hpp" |
| 29 |
#include "persist_lua.h" |
| 30 |
#include "th_lua.h" |
| 31 |
#include "th_lua_internal.h" |
| 32 |
|
| 33 |
/* |
| 34 |
This file implements a string proxy system. A string proxy is a userdata |
| 35 |
which behaves like a string or a table, but records how it was made, and |
| 36 |
is serialised as this list of instructions, rather than as the resulting |
| 37 |
value. The application for this lies in persisting localised strings, as |
| 38 |
the language in use at depersist-time may be different to the one in use |
| 39 |
at persist-time. |
| 40 |
|
| 41 |
First note that such a proxy must be a userdata (in order to have custom |
| 42 |
persistance behaviour and a full complement of metamethods). At the same |
| 43 |
time, there is no C data which the userdata needs to hold, so the C part |
| 44 |
of the userdata can be empty. The Lua part of the userdata needs to hold |
| 45 |
the current proxied value and the instructions for recreating the value. |
| 46 |
When the proxied value is a table, then in addition, the Lua part of the |
| 47 |
userdata needs to store a cache of proxies for its children to avoid the |
| 48 |
situation of regularly creating new proxies for children. Due to how the |
| 49 |
persistance library deals with userdata, the Lua parts of these userdata |
| 50 |
cannot be held in the userdata's environment table, so instead the idiom |
| 51 |
of inside-out objects needs to be used. The principle idea of inside-out |
| 52 |
objects is that environment(obj).field becomes metatable(obj).field[obj] |
| 53 |
with the field table having weak values. Unfortunately, this causes weak |
| 54 |
table cycles, which are not handled well in Lua 5.1 (L5.2 uses ephemeron |
| 55 |
tables which fix this). Hence the Value and Cache weak tables are stored |
| 56 |
in the registry table, and the ReconstructInfo is stored as the userdata |
| 57 |
environment. |
| 58 |
*/ |
| 59 |
|
| 60 |
class string_proxy {}; |
| 61 |
|
| 62 |
namespace { |
| 63 |
|
| 64 |
// We need 2 lightuserdata keys for naming the weak tables in the registry, |
| 65 |
// which we get by having 2 bytes of dummy global variables. |
| 66 |
uint8_t weak_table_keys[2] = {0}; |
| 67 |
|
| 68 |
inline void aux_push_weak_table(lua_State* L, int iIndex) { |
| 69 |
lua_pushlightuserdata(L, &weak_table_keys[iIndex]); |
| 70 |
lua_rawget(L, LUA_REGISTRYINDEX); |
| 71 |
} |
| 72 |
|
| 73 |
// Replace the value at the top of the stack with a userdata proxy |
| 74 |
int l_str_new_aux(lua_State* L) { |
| 75 |
luaT_stdnew<string_proxy>(L); |
| 76 |
aux_push_weak_table(L, 0); |
| 77 |
lua_pushvalue(L, -2); |
| 78 |
lua_pushvalue(L, -4); |
| 79 |
lua_rawset(L, -3); |
| 80 |
lua_pop(L, 1); |
| 81 |
lua_replace(L, -2); |
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
// Create a new root-level userdata proxy |
| 86 |
int l_str_new(lua_State* L) { |
| 87 |
// Pack extra arguments into a table |
| 88 |
int iNArgs = lua_gettop(L); |
| 89 |
lua_createtable(L, iNArgs - 2, 0); |
| 90 |
lua_replace(L, 1); // Value inserted by __call |
| 91 |
for (int i = iNArgs; i >= 3; --i) lua_rawseti(L, 1, i - 2); |
| 92 |
|
| 93 |
// Make proxy |
| 94 |
luaL_checkany(L, 2); |
| 95 |
l_str_new_aux(L); |
| 96 |
|
| 97 |
// Save extra arguments as reconstruction information |
| 98 |
lua_insert(L, 1); |
| 99 |
lua_setfenv(L, 1); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
// Helper function to make an array in Lua |
| 104 |
void aux_mk_table(lua_State* L, std::vector<const char*> literals, |
| 105 |
std::vector<int> values) { |
| 106 |
// .size is a size_t, but lua only handles int indexes |
| 107 |
int literal_count = static_cast<int>(literals.size()); |
| 108 |
int value_count = static_cast<int>(values.size()); |
| 109 |
|
| 110 |
lua_createtable(L, literal_count + value_count, 0); |
| 111 |
for (int i = 0; i < literal_count; ++i) { |
| 112 |
const char* sStr = literals.at(i); |
| 113 |
lua_pushstring(L, sStr); |
| 114 |
lua_rawseti(L, -2, i + 1); |
| 115 |
} |
| 116 |
for (int i = 0; i < value_count; ++i) { |
| 117 |
int iValue = values.at(i); |
| 118 |
if (0 > iValue && iValue > LUA_REGISTRYINDEX) --iValue; |
| 119 |
lua_pushvalue(L, iValue); |
| 120 |
lua_rawseti(L, -2, i + literal_count + 1); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Helper function which pushes onto the stack a random key from the table |
| 125 |
// (previously) on the top of the stack. |
| 126 |
void aux_push_random_key(lua_State* L) { |
| 127 |
int iNKeys = 0; |
| 128 |
lua_newtable(L); |
| 129 |
lua_getglobal(L, "pairs"); |
| 130 |
lua_pushvalue(L, -3); |
| 131 |
lua_call(L, 1, 3); |
| 132 |
while (true) { |
| 133 |
lua_pushvalue(L, -3); |
| 134 |
lua_pushvalue(L, -3); |
| 135 |
lua_pushvalue(L, -3); |
| 136 |
lua_remove(L, -4); |
| 137 |
lua_call(L, 2, 1); |
| 138 |
if (lua_isnil(L, -1)) break; |
| 139 |
++iNKeys; |
| 140 |
lua_pushvalue(L, -1); |
| 141 |
lua_rawseti(L, -5, iNKeys); |
| 142 |
} |
| 143 |
lua_pop(L, 3); |
| 144 |
lua_getglobal(L, "math"); |
| 145 |
lua_getfield(L, -1, "random"); |
| 146 |
lua_pushinteger(L, 1); |
| 147 |
lua_pushinteger(L, iNKeys); |
| 148 |
lua_call(L, 2, 1); |
| 149 |
lua_gettable(L, -3); |
| 150 |
lua_replace(L, -3); |
| 151 |
lua_pop(L, 1); |
| 152 |
} |
| 153 |
|
| 154 |
// __index metamethod handler. |
| 155 |
// For proxied tables, return proxies of children, preferably cached |
| 156 |
// For proxied strings, return methods |
| 157 |
int l_str_index(lua_State* L) { |
| 158 |
// Look up cached value, and return it if present |
| 159 |
aux_push_weak_table(L, 1); |
| 160 |
lua_pushvalue(L, 1); |
| 161 |
lua_gettable(L, 3); |
| 162 |
lua_replace(L, 3); |
| 163 |
lua_pushvalue(L, 2); |
| 164 |
lua_rawget(L, 3); |
| 165 |
if (!lua_isnil(L, 4)) return 1; |
| 166 |
lua_pop(L, 1); |
| 167 |
|
| 168 |
// Fetch the proxied value |
| 169 |
aux_push_weak_table(L, 0); |
| 170 |
lua_pushvalue(L, 1); |
| 171 |
lua_rawget(L, 4); |
| 172 |
lua_replace(L, 4); |
| 173 |
|
| 174 |
// Handle string methods |
| 175 |
if (lua_type(L, 4) == LUA_TSTRING) { |
| 176 |
lua_rawgeti(L, luaT_environindex, 4); |
| 177 |
lua_pushvalue(L, 2); |
| 178 |
lua_gettable(L, 5); |
| 179 |
return 1; |
| 180 |
} |
| 181 |
|
| 182 |
// Handle __random, as it shouldn't be cached |
| 183 |
if (lua_type(L, 2) == LUA_TSTRING) { |
| 184 |
size_t iLen; |
| 185 |
const char* sKey = lua_tolstring(L, 2, &iLen); |
| 186 |
if (iLen == 8 && std::strcmp(sKey, "__random") == 0) { |
| 187 |
aux_push_random_key(L); |
| 188 |
lua_replace(L, 2); |
| 189 |
lua_settop(L, 2); |
| 190 |
return l_str_index(L); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// Fetch desired value |
| 195 |
lua_pushvalue(L, 2); |
| 196 |
lua_gettable(L, 4); |
| 197 |
lua_replace(L, 4); |
| 198 |
|
| 199 |
// Create new userdata proxy |
| 200 |
l_str_new_aux(L); |
| 201 |
aux_mk_table(L, std::vector<const char*>{}, std::vector<int>{1, 2}); |
| 202 |
lua_setfenv(L, 4); |
| 203 |
|
| 204 |
// Save to cache and return |
| 205 |
lua_pushvalue(L, 2); |
| 206 |
lua_pushvalue(L, 4); |
| 207 |
lua_rawset(L, 3); |
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
// __newindex metamethod handler |
| 212 |
int l_str_newindex(lua_State* L) { |
| 213 |
return luaL_error(L, "String tables are read-only"); |
| 214 |
} |
| 215 |
|
| 216 |
// Generic string method handler |
| 217 |
// The name of the method is stored at upvalue 1 |
| 218 |
int l_str_func(lua_State* L) { |
| 219 |
int iArgCount = lua_gettop(L); |
| 220 |
lua_checkstack(L, iArgCount + 10); |
| 221 |
|
| 222 |
int iUserdataCount = 0; |
| 223 |
|
| 224 |
// Construct the resulting value |
| 225 |
aux_push_weak_table(L, 0); |
| 226 |
for (int i = 1; i <= iArgCount; ++i) { |
| 227 |
lua_pushvalue(L, i); |
| 228 |
if (lua_type(L, i) == LUA_TUSERDATA) { |
| 229 |
lua_rawget(L, iArgCount + 1); |
| 230 |
++iUserdataCount; |
| 231 |
} |
| 232 |
} |
| 233 |
lua_pushvalue(L, luaT_upvalueindex(1)); |
| 234 |
lua_gettable(L, iArgCount + 2); |
| 235 |
lua_replace(L, iArgCount + 1); |
| 236 |
lua_call(L, iArgCount, 1); |
| 237 |
|
| 238 |
// Trivial case of result not depending upon any proxies |
| 239 |
if (iUserdataCount == 0) return 1; |
| 240 |
|
| 241 |
// Wrap result in a proxy |
| 242 |
l_str_new_aux(L); |
| 243 |
|
| 244 |
// Create and save reconstruction information |
| 245 |
lua_createtable(L, iArgCount + 1, 0); |
| 246 |
lua_pushvalue(L, luaT_upvalueindex(1)); |
| 247 |
lua_rawseti(L, -2, 1); |
| 248 |
for (int i = 1; i <= iArgCount; ++i) { |
| 249 |
lua_pushvalue(L, i); |
| 250 |
lua_rawseti(L, -2, i + 1); |
| 251 |
} |
| 252 |
lua_setfenv(L, -2); |
| 253 |
|
| 254 |
return 1; |
| 255 |
} |
| 256 |
|
| 257 |
// __concat metamethod handler |
| 258 |
// Simple (but inefficient) handling by converting concat into format |
| 259 |
int l_str_concat(lua_State* L) { |
| 260 |
int iParent = (lua_type(L, 1) == LUA_TUSERDATA) ? 1 : 2; |
| 261 |
lua_getfield(L, iParent, "format"); |
| 262 |
lua_insert(L, 1); |
| 263 |
lua_pushliteral(L, "%s%s"); |
| 264 |
lua_insert(L, 2); |
| 265 |
lua_call(L, 3, 1); |
| 266 |
return 1; |
| 267 |
} |
| 268 |
|
| 269 |
// pairs() metamethod handler |
| 270 |
int l_str_pairs(lua_State* L) { |
| 271 |
lua_settop(L, 1); |
| 272 |
lua_getfield(L, luaT_environindex, "__next"); |
| 273 |
lua_pushvalue(L, 1); |
| 274 |
lua_pushnil(L); |
| 275 |
return 3; |
| 276 |
} |
| 277 |
|
| 278 |
// ipairs() metamethod handler |
| 279 |
int l_str_ipairs(lua_State* L) { |
| 280 |
lua_settop(L, 1); |
| 281 |
lua_getfield(L, luaT_environindex, "__inext"); |
| 282 |
lua_pushvalue(L, 1); |
| 283 |
lua_pushinteger(L, 0); |
| 284 |
return 3; |
| 285 |
} |
| 286 |
|
| 287 |
// pairs() iterator function |
| 288 |
int l_str_next(lua_State* L) { |
| 289 |
luaL_checktype(L, 1, LUA_TUSERDATA); |
| 290 |
lua_settop(L, 2); |
| 291 |
|
| 292 |
// Fetch proxied value |
| 293 |
aux_push_weak_table(L, 0); |
| 294 |
lua_pushvalue(L, 1); |
| 295 |
lua_rawget(L, 3); |
| 296 |
|
| 297 |
// Remove layer of proxying done in Lua |
| 298 |
// NB: Assumes that pairs(t) returns 3 values: next, t, nil |
| 299 |
// In our case, the returned t should be the raw (unproxied) one |
| 300 |
lua_getglobal(L, "pairs"); |
| 301 |
lua_replace(L, 3); |
| 302 |
lua_call(L, 1, 2); |
| 303 |
|
| 304 |
// Get the next key |
| 305 |
lua_pushvalue(L, 2); |
| 306 |
lua_call(L, 2, 1); |
| 307 |
if (lua_isnil(L, -1)) return 0; |
| 308 |
|
| 309 |
// Get the (proxied) value which goes with the key |
| 310 |
lua_pushvalue(L, -1); |
| 311 |
lua_gettable(L, 1); |
| 312 |
return 2; |
| 313 |
} |
| 314 |
|
| 315 |
// __len metamethod handler |
| 316 |
int l_str_len(lua_State* L) { |
| 317 |
luaL_checktype(L, 1, LUA_TUSERDATA); |
| 318 |
|
| 319 |
// Fetch proxied value |
| 320 |
aux_push_weak_table(L, 0); |
| 321 |
lua_pushvalue(L, 1); |
| 322 |
lua_gettable(L, -2); |
| 323 |
|
| 324 |
// String tables are proxied in Lua, and Lua tables do not honour __len |
| 325 |
// so use ipairs to get the unproxied table to call __len on. |
| 326 |
if (lua_type(L, -1) == LUA_TTABLE) { |
| 327 |
lua_getglobal(L, "ipairs"); |
| 328 |
lua_insert(L, -2); |
| 329 |
lua_call(L, 1, 2); |
| 330 |
lua_replace(L, -2); |
| 331 |
} |
| 332 |
|
| 333 |
lua_pushinteger(L, (lua_Integer)lua_objlen(L, -1)); |
| 334 |
return 1; |
| 335 |
} |
| 336 |
|
| 337 |
// ipairs() iterator function |
| 338 |
int l_str_inext(lua_State* L) { |
| 339 |
lua_Integer n = luaL_checkinteger(L, 2) + 1; |
| 340 |
lua_settop(L, 1); |
| 341 |
l_str_len(L); |
| 342 |
lua_Integer len = lua_tointeger(L, -1); |
| 343 |
|
| 344 |
if (n > len) return 0; |
| 345 |
|
| 346 |
// Fetch proxied value |
| 347 |
lua_settop(L, 2); |
| 348 |
lua_pushvalue(L, 1); |
| 349 |
lua_gettable(L, 2); |
| 350 |
|
| 351 |
// Return new N and the proxied value which goes it |
| 352 |
lua_pushinteger(L, n); |
| 353 |
lua_pushinteger(L, n); |
| 354 |
lua_gettable(L, 1); |
| 355 |
return 2; |
| 356 |
} |
| 357 |
|
| 358 |
// tostring() metamethod handler for debugging / diagnostics |
| 359 |
int l_str_tostring(lua_State* L) { |
| 360 |
// Convert the proxy to a string, recursively calling tostring() |
| 361 |
lua_settop(L, 1); |
| 362 |
aux_push_weak_table(L, 0); |
| 363 |
lua_pushvalue(L, 1); |
| 364 |
lua_rawget(L, 2); |
| 365 |
if (lua_isnil(L, 3)) |
| 366 |
lua_pop(L, 2); |
| 367 |
else { |
| 368 |
lua_replace(L, 1); |
| 369 |
lua_pop(L, 1); |
| 370 |
} |
| 371 |
lua_getglobal(L, "tostring"); |
| 372 |
lua_insert(L, 1); |
| 373 |
lua_call(L, 1, 1); |
| 374 |
|
| 375 |
// Prepend a nice message indicating that proxying is being done |
| 376 |
lua_pushliteral(L, "<LocalisedString> Current value:"); |
| 377 |
lua_insert(L, 1); |
| 378 |
lua_concat(L, 2); |
| 379 |
return 1; |
| 380 |
} |
| 381 |
|
| 382 |
// __call metamethod handler |
| 383 |
// Required to support the compatibility hack for calling _S |
| 384 |
int l_str_call(lua_State* L) { |
| 385 |
luaL_checkany(L, 1); |
| 386 |
|
| 387 |
// Fetch the proxied value |
| 388 |
aux_push_weak_table(L, 0); |
| 389 |
lua_pushvalue(L, 1); |
| 390 |
lua_rawget(L, -2); |
| 391 |
|
| 392 |
// Forward the call onto the proxied value |
| 393 |
lua_replace(L, 1); |
| 394 |
lua_pop(L, 1); |
| 395 |
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET); |
| 396 |
return lua_gettop(L); |
| 397 |
} |
| 398 |
|
| 399 |
// __lt (less-than) metamethod handler |
| 400 |
// Provided so that a list of localised strings can be sorted, which is used |
| 401 |
// to create nice user-interface listing of strings. Note that this will mean |
| 402 |
// that persist->change language->depersist will result in a "random" ordering |
| 403 |
// of the resulting list, but this is generally acceptable. |
| 404 |
int l_str_lt(lua_State* L) { |
| 405 |
luaL_checkany(L, 1); |
| 406 |
luaL_checkany(L, 2); |
| 407 |
lua_settop(L, 2); |
| 408 |
aux_push_weak_table(L, 0); |
| 409 |
lua_pushvalue(L, 1); |
| 410 |
lua_rawget(L, 3); |
| 411 |
lua_pushvalue(L, 2); |
| 412 |
lua_rawget(L, 3); |
| 413 |
lua_pushboolean(L, lua_lessthan(L, 4, 5)); |
| 414 |
return 1; |
| 415 |
} |
| 416 |
|
| 417 |
// __persist metamethod handler |
| 418 |
int l_str_persist(lua_State* L) { |
| 419 |
luaT_rotate(L, 1, -1); |
| 420 |
lua_persist_writer* pWriter = (lua_persist_writer*)lua_touserdata(L, 1); |
| 421 |
int self_index = 3; |
| 422 |
|
| 423 |
// Recreation instructions are stored in the environment, which is written |
| 424 |
// automatically. For compatibility, we write a simple boolean. |
| 425 |
lua_pushboolean(L, 1); |
| 426 |
pWriter->write_stack_object(4); |
| 427 |
lua_getfenv(L, self_index); |
| 428 |
|
| 429 |
// If there were no instructions (i.e. for the root object) then write the |
| 430 |
// value as well. |
| 431 |
if (lua_objlen(L, -1) == 0) { |
| 432 |
lua_pop(L, 2); |
| 433 |
aux_push_weak_table(L, 0); |
| 434 |
lua_pushvalue(L, self_index); |
| 435 |
lua_rawget(L, 4); // lookup self in the weak table |
| 436 |
pWriter->write_stack_object(5); // write that value |
| 437 |
} |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
// __depersist metamethod handler |
| 442 |
int l_str_depersist(lua_State* L) { |
| 443 |
lua_settop(L, 2); |
| 444 |
lua_insert(L, 1); |
| 445 |
lua_persist_reader* pReader = (lua_persist_reader*)lua_touserdata(L, 1); |
| 446 |
|
| 447 |
// Read the instructions for re-creating the value |
| 448 |
if (!pReader->read_stack_object()) return 0; |
| 449 |
if (lua_type(L, 3) == LUA_TBOOLEAN && lua_toboolean(L, 3) == 1) { |
| 450 |
// The current code uses a boolean marker to indicate that the |
| 451 |
// instructions were stored in the environment. Replace the marker |
| 452 |
// with them. |
| 453 |
lua_getfenv(L, 2); |
| 454 |
lua_replace(L, 3); |
| 455 |
} else { |
| 456 |
// Older versions of the code wrote the instructions here, or nil for |
| 457 |
// no instructions. Convert nil to the empty table, and store the |
| 458 |
// instructions as the userdata's environment. |
| 459 |
if (lua_type(L, 3) == LUA_TNIL) { |
| 460 |
lua_newtable(L); |
| 461 |
lua_replace(L, 3); |
| 462 |
} |
| 463 |
lua_pushvalue(L, 3); |
| 464 |
lua_setfenv(L, 2); |
| 465 |
} |
| 466 |
|
| 467 |
// Prepare t, k for saving the value |
| 468 |
aux_push_weak_table(L, 0); |
| 469 |
lua_pushvalue(L, 2); |
| 470 |
|
| 471 |
if (lua_objlen(L, 3) == 0) { |
| 472 |
// No instructions provided, so read the value itself |
| 473 |
if (!pReader->read_stack_object()) return 0; |
| 474 |
} else { |
| 475 |
// The instructions are a table of values; unpack them and replace |
| 476 |
// proxies with their values. |
| 477 |
bool bIsIndexOperation = false; |
| 478 |
int iCount = (int)lua_objlen(L, 3); |
| 479 |
lua_checkstack(L, iCount + 1); |
| 480 |
for (int i = 1; i <= iCount; ++i) { |
| 481 |
lua_rawgeti(L, 3, i); |
| 482 |
if (lua_type(L, -1) == LUA_TUSERDATA) { |
| 483 |
if (i == 1) bIsIndexOperation = true; |
| 484 |
lua_rawget(L, 4); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
if (iCount == 2 && bIsIndexOperation) { |
| 489 |
// If there were two values, and the first was a proxy, then the |
| 490 |
// instruction is to perform a table lookup. |
| 491 |
lua_gettable(L, -2); |
| 492 |
lua_replace(L, -2); |
| 493 |
} else { |
| 494 |
// Otherwise, the first value was a method or method name. |
| 495 |
if (lua_type(L, 6) != LUA_TFUNCTION) { |
| 496 |
lua_pushvalue(L, 6); |
| 497 |
lua_gettable(L, 7); |
| 498 |
lua_replace(L, 6); |
| 499 |
} |
| 500 |
lua_call(L, iCount - 1, 1); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
// Save the value |
| 505 |
lua_rawset(L, 4); |
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
int l_str_reload_actual(lua_State* L) { |
| 510 |
// Reload a single string proxy |
| 511 |
// Stack: reload_cache proxy_to_reload <top |
| 512 |
|
| 513 |
// Mark string as reloaded |
| 514 |
lua_pushvalue(L, 2); |
| 515 |
lua_pushvalue(L, 2); |
| 516 |
lua_settable(L, 1); |
| 517 |
|
| 518 |
// Pull instructions out of the environment and remake value |
| 519 |
lua_getfenv(L, 2); |
| 520 |
bool bIsIndexOperation = false; |
| 521 |
int iCount = (int)lua_objlen(L, 3); |
| 522 |
aux_push_weak_table(L, 0); |
| 523 |
lua_pushvalue(L, 2); |
| 524 |
if (iCount != 0) { |
| 525 |
// Fetch reconstruction information, reloading any de-proxying any |
| 526 |
// string proxies which we come across. Also replace any references |
| 527 |
// to the root with the new root. |
| 528 |
lua_checkstack(L, iCount + 1); |
| 529 |
for (int i = 1; i <= iCount; ++i) { |
| 530 |
lua_rawgeti(L, 3, i); |
| 531 |
if (lua_type(L, -1) == LUA_TUSERDATA) { |
| 532 |
if (i == 1) bIsIndexOperation = true; |
| 533 |
lua_gettable(L, 1); // reload / change root |
| 534 |
lua_pushvalue(L, -1); |
| 535 |
lua_rawseti(L, 3, i); |
| 536 |
lua_rawget(L, 4); // de-proxy |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
if (iCount == 2 && bIsIndexOperation) { |
| 541 |
// If there were two values, and the first was a proxy, then the |
| 542 |
// instruction is to perform a table lookup. |
| 543 |
lua_gettable(L, -2); |
| 544 |
lua_replace(L, -2); |
| 545 |
} else { |
| 546 |
// Otherwise, the first value was a method or method name. |
| 547 |
if (lua_type(L, 6) != LUA_TFUNCTION) { |
| 548 |
lua_pushvalue(L, 6); |
| 549 |
lua_gettable(L, 7); |
| 550 |
lua_replace(L, 6); |
| 551 |
} |
| 552 |
lua_call(L, iCount - 1, 1); |
| 553 |
} |
| 554 |
} else { |
| 555 |
// Root object |
| 556 |
lua_settop(L, 2); |
| 557 |
return 1; |
| 558 |
} |
| 559 |
// Update value |
| 560 |
lua_rawset(L, -3); |
| 561 |
lua_pop(L, 2); |
| 562 |
return 1; |
| 563 |
} |
| 564 |
|
| 565 |
int l_str_unwrap(lua_State* L) { |
| 566 |
luaL_checkany(L, 1); |
| 567 |
lua_settop(L, 1); |
| 568 |
aux_push_weak_table(L, 0); |
| 569 |
lua_pushvalue(L, 1); |
| 570 |
lua_rawget(L, 2); |
| 571 |
if (lua_isnil(L, 3)) { |
| 572 |
lua_settop(L, 1); |
| 573 |
lua_pushboolean(L, 0); |
| 574 |
} else { |
| 575 |
lua_pushboolean(L, 1); |
| 576 |
} |
| 577 |
return 2; |
| 578 |
} |
| 579 |
|
| 580 |
int l_str_reload(lua_State* L) { |
| 581 |
// The first argument should be the old root object, second argument the |
| 582 |
// new one. |
| 583 |
luaL_checktype(L, 1, LUA_TUSERDATA); |
| 584 |
luaL_checktype(L, 2, LUA_TUSERDATA); |
| 585 |
|
| 586 |
// Create caching table to track what has been reloaded |
| 587 |
// (i.e. things are added to this table as they are reloaded, and |
| 588 |
// __index will perform the reloading as required). |
| 589 |
lua_newtable(L); |
| 590 |
lua_insert(L, 1); |
| 591 |
lua_settop(L, 3); |
| 592 |
lua_rawset(L, 1); |
| 593 |
lua_createtable(L, 0, 1); |
| 594 |
lua_pushcfunction(L, l_str_reload_actual); |
| 595 |
lua_setfield(L, -2, "__index"); |
| 596 |
lua_setmetatable(L, -2); |
| 597 |
|
| 598 |
aux_push_weak_table(L, 0); |
| 599 |
luaL_loadstring(L, |
| 600 |
"local reload, all_proxies, _ = ...\n" |
| 601 |
// Make a copy of all_proxies which isn't a weak table |
| 602 |
"local proxies_copy = {}\n" |
| 603 |
"for k, v in pairs(all_proxies) do proxies_copy[k] = v end\n" |
| 604 |
// Do the reloading |
| 605 |
"for k in pairs(proxies_copy) do _ = reload[k] end\n"); |
| 606 |
lua_insert(L, 1); |
| 607 |
lua_call(L, 2, 0); |
| 608 |
|
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
int l_mk_cache(lua_State* L) { |
| 613 |
lua_newtable(L); |
| 614 |
lua_pushvalue(L, luaT_upvalueindex(1)); |
| 615 |
lua_setmetatable(L, -2); |
| 616 |
lua_pushvalue(L, 2); |
| 617 |
lua_pushvalue(L, 3); |
| 618 |
lua_settable(L, 1); |
| 619 |
return 1; |
| 620 |
} |
| 621 |
|
| 622 |
} // namespace |
| 623 |
|
| 624 |
const char* luaT_checkstring(lua_State* L, int idx, size_t* pLength) { |
| 625 |
if (lua_isuserdata(L, idx)) { |
| 626 |
aux_push_weak_table(L, 0); |
| 627 |
bool bRel = (0 > idx && idx > LUA_REGISTRYINDEX); |
| 628 |
lua_pushvalue(L, bRel ? (idx - 1) : idx); |
| 629 |
lua_rawget(L, -2); |
| 630 |
lua_replace(L, bRel ? (idx - 2) : idx); |
| 631 |
lua_pop(L, 1); |
| 632 |
} |
| 633 |
return luaL_checklstring(L, idx, pLength); |
| 634 |
} |
| 635 |
|
| 636 |
void lua_register_strings(const lua_register_state* pState) { |
| 637 |
lua_State* L = pState->L; |
| 638 |
|
| 639 |
// Create Value, and Cache weak tables for inside-out objects. |
| 640 |
for (int i = 0; i <= 1; ++i) { |
| 641 |
lua_pushlightuserdata(L, &weak_table_keys[i]); |
| 642 |
lua_newtable(L); |
| 643 |
lua_createtable(L, 0, 1); |
| 644 |
lua_pushliteral(L, "__mode"); |
| 645 |
lua_pushliteral(L, "k"); |
| 646 |
lua_rawset(L, -3); |
| 647 |
if (i == 1) { |
| 648 |
// Have the cache weak table automatically create caches on demand |
| 649 |
lua_pushliteral(L, "__index"); |
| 650 |
lua_createtable(L, 0, 1); |
| 651 |
lua_pushliteral(L, "__mode"); |
| 652 |
lua_pushliteral(L, "kv"); |
| 653 |
lua_rawset(L, -3); |
| 654 |
luaT_pushcclosure(L, l_mk_cache, 1); |
| 655 |
lua_rawset(L, -3); |
| 656 |
} |
| 657 |
lua_setmetatable(L, -2); |
| 658 |
lua_rawset(L, LUA_REGISTRYINDEX); |
| 659 |
} |
| 660 |
// Give the Value weak table a friendly name for Lua code to use |
| 661 |
lua_pushliteral(L, "StringProxyValues"); |
| 662 |
lua_pushlightuserdata(L, &weak_table_keys[0]); |
| 663 |
lua_rawget(L, LUA_REGISTRYINDEX); |
| 664 |
lua_rawset(L, LUA_REGISTRYINDEX); |
| 665 |
|
| 666 |
lua_class_binding<string_proxy> lcb(pState, "stringProxy", l_str_new, |
| 667 |
lua_metatable::string_proxy); |
| 668 |
// As we overwrite __index, move methods to lua_metatable::string_proxy[4] |
| 669 |
lua_getfield( |
| 670 |
L, pState->metatables[static_cast<size_t>(lua_metatable::string_proxy)], |
| 671 |
"__index"); |
| 672 |
lua_rawseti( |
| 673 |
L, pState->metatables[static_cast<size_t>(lua_metatable::string_proxy)], |
| 674 |
4); |
| 675 |
lcb.add_metamethod(l_str_index, "index"); |
| 676 |
lcb.add_metamethod(l_str_newindex, "newindex"); |
| 677 |
lcb.add_metamethod(l_str_concat, "concat"); |
| 678 |
lcb.add_metamethod(l_str_len, "len"); |
| 679 |
lcb.add_metamethod(l_str_tostring, "tostring"); |
| 680 |
lcb.add_metamethod(l_str_persist, "persist"); |
| 681 |
lcb.add_metamethod(l_str_depersist, "depersist"); |
| 682 |
lcb.add_metamethod(l_str_call, "call"); |
| 683 |
lcb.add_metamethod(l_str_lt, "lt"); |
| 684 |
lcb.add_metamethod(l_str_pairs, "pairs"); |
| 685 |
lcb.add_metamethod(l_str_ipairs, "ipairs"); |
| 686 |
lcb.add_metamethod(l_str_next, "next"); |
| 687 |
lcb.add_metamethod(l_str_inext, "inext"); |
| 688 |
lcb.add_function(l_str_func, "format", "format"); |
| 689 |
lcb.add_function(l_str_func, "lower", "lower"); |
| 690 |
lcb.add_function(l_str_func, "rep", "rep"); |
| 691 |
lcb.add_function(l_str_func, "reverse", "reverse"); |
| 692 |
lcb.add_function(l_str_func, "upper", "upper"); |
| 693 |
lcb.add_function(l_str_unwrap, "_unwrap"); |
| 694 |
lcb.add_function(l_str_reload, "reload"); |
| 695 |
} |