| 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 "persist_lua.h" |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <errno.h> |
| 28 |
|
| 29 |
#include <array> |
| 30 |
#include <climits> |
| 31 |
#include <cmath> |
| 32 |
#include <cstdio> |
| 33 |
#include <cstring> |
| 34 |
#include <new> |
| 35 |
#include <string> |
| 36 |
|
| 37 |
#include "lua.hpp" |
| 38 |
#include "th_lua.h" |
| 39 |
#ifdef _MSC_VER |
| 40 |
#pragma warning( \ |
| 41 |
disable : 4996) // Disable "std::strcpy unsafe" warnings under MSVC |
| 42 |
#endif |
| 43 |
|
| 44 |
namespace { |
| 45 |
|
| 46 |
enum persist_type { |
| 47 |
// LUA_TNIL = 0, |
| 48 |
// LUA_TBOOLEAN, // Used for false |
| 49 |
// LUA_TLIGHTUSERDATA, // Not currently persisted |
| 50 |
// LUA_TNUMBER, |
| 51 |
// LUA_TSTRING, |
| 52 |
// LUA_TTABLE, // Used for tables without metatables |
| 53 |
// LUA_TFUNCTION, // Used for Lua closures |
| 54 |
// LUA_TUSERDATA, |
| 55 |
// LUA_TTHREAD, // Not currently persisted |
| 56 |
PERSIST_TPERMANENT = LUA_TTHREAD + 1, |
| 57 |
PERSIST_TTRUE, |
| 58 |
PERSIST_TTABLE_WITH_META, |
| 59 |
PERSIST_TINTEGER, |
| 60 |
PERSIST_TPROTOTYPE, |
| 61 |
PERSIST_TRESERVED1, // Not currently used |
| 62 |
PERSIST_TRESERVED2, // Not currently used |
| 63 |
PERSIST_TCOUNT, // must equal 16 (for compatibility) |
| 64 |
}; |
| 65 |
|
| 66 |
template <class T> |
| 67 |
int l_crude_gc(lua_State* L) { |
| 68 |
// This __gc metamethod does not verify that the given value is the correct |
| 69 |
// type of userdata, or that the value is userdata at all. |
| 70 |
static_cast<T*>(lua_touserdata(L, 1))->~T(); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
} // namespace |
| 75 |
|
| 76 |
constexpr size_t load_multi_buffer_capacity = 3; |
| 77 |
|
| 78 |
//! Structure for loading multiple strings as a Lua chunk, avoiding |
| 79 |
//! concatenation |
| 80 |
/*! |
| 81 |
luaL_loadbuffer() is a good way to load a string as a Lua chunk. If there |
| 82 |
are several strings which need to be concatenated before being loaded, then |
| 83 |
it can be more efficient to call lua_load with a callback function for |
| 84 |
loading the strings one at a time than to concatenate the strings into a |
| 85 |
single buffer. |
| 86 |
|
| 87 |
This class provides the data structure and callback function to do this. |
| 88 |
|
| 89 |
``` |
| 90 |
load_multi_buffer ls; |
| 91 |
ls.piece[0] = lua_tolstring(L, -2, &ls.piece_size[0]); |
| 92 |
ls.piece[1] = lua_tolstring(L, -1, &ls.piece_size[1]); |
| 93 |
luaT_load(L, load_multi_buffer::load_fn, &ls, "chunk name", "bt"); |
| 94 |
``` |
| 95 |
|
| 96 |
Because of the api of lua_tolstring the API of this class allows direct |
| 97 |
and independent assignment of the chunk pieces and their sizes. |
| 98 |
|
| 99 |
It is up to the caller to keep track of how many pieces are used and to |
| 100 |
ensure that no more than load_multi_buffer_capacity pieces are used. |
| 101 |
*/ |
| 102 |
class load_multi_buffer { |
| 103 |
public: |
| 104 |
/// lua_Reader callback function for lua_load or luaT_load |
| 105 |
/*! |
| 106 |
Called repeatedly by lua to get the next piece of data to load until a |
| 107 |
null pointer is returned. |
| 108 |
|
| 109 |
\param L The Lua state |
| 110 |
\param ud Pointer to the load_multi_buffer instance |
| 111 |
\param size Pointer to size_t to receive the size of the returned string |
| 112 |
|
| 113 |
\see https://www.lua.org/manual/5.4/manual.html#lua_Reader |
| 114 |
*/ |
| 115 |
static const char* load_fn(lua_State* L, void* ud, size_t* size) { |
| 116 |
auto* me = static_cast<load_multi_buffer*>(ud); |
| 117 |
|
| 118 |
// Skip empty chunks if any which would cause lua_load to stop early |
| 119 |
while (me->n < load_multi_buffer_capacity && me->piece_size[me->n] == 0) { |
| 120 |
++me->n; |
| 121 |
} |
| 122 |
|
| 123 |
if (me->n < load_multi_buffer_capacity) { |
| 124 |
*size = me->piece_size[me->n]; |
| 125 |
return me->piece[me->n++]; |
| 126 |
} |
| 127 |
|
| 128 |
*size = 0; |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
|
| 132 |
/// Insert the given chunk at the given index in the buffer |
| 133 |
void insert(std::string_view piece, size_t index) { |
| 134 |
this->piece[index] = piece.data(); |
| 135 |
this->piece_size[index] = piece.size(); |
| 136 |
} |
| 137 |
|
| 138 |
/// Pieces of the lua code chunk to be loaded. |
| 139 |
const char* piece[load_multi_buffer_capacity]{nullptr}; |
| 140 |
|
| 141 |
/// Lengths of the pieces. |
| 142 |
size_t piece_size[load_multi_buffer_capacity]{}; |
| 143 |
|
| 144 |
private: |
| 145 |
/// The next piece index to be loaded |
| 146 |
int n{}; |
| 147 |
}; |
| 148 |
|
| 149 |
//! Basic implementation of persistence interface |
| 150 |
/*! |
| 151 |
self - Instance of lua_persist_basic_writer allocated as a Lua userdata |
| 152 |
self metatable: |
| 153 |
`__gc` - ~lua_persist_basic_writer (via l_crude_gc) |
| 154 |
`<file>:<line>` - index of function prototype in already written data |
| 155 |
[1] - pre-populated prototype persistence names |
| 156 |
`<file>:<line>` - `<name>` |
| 157 |
err - an object which could not be persisted |
| 158 |
self environment: |
| 159 |
`<object>` - index of object in already written data |
| 160 |
[1] - permanents table |
| 161 |
self environment metatable |
| 162 |
*/ |
| 163 |
class lua_persist_basic_writer : public lua_persist_writer { |
| 164 |
public: |
| 165 |
explicit lua_persist_basic_writer(lua_State* L) : L(L) {} |
| 166 |
|
| 167 |
~lua_persist_basic_writer() override = default; |
| 168 |
|
| 169 |
lua_State* get_stack() override { return L; } |
| 170 |
|
| 171 |
void init() { |
| 172 |
lua_createtable(L, 1, 8); // Environment |
| 173 |
lua_pushvalue(L, 2); // Permanent objects |
| 174 |
lua_rawseti(L, -2, 1); |
| 175 |
lua_createtable(L, 1, 0); // Environment metatable |
| 176 |
lua_setmetatable(L, -2); |
| 177 |
lua_setfenv(L, 1); |
| 178 |
lua_createtable(L, 1, 4); // Metatable |
| 179 |
luaT_pushcclosure(L, l_crude_gc<lua_persist_basic_writer>, 0); |
| 180 |
lua_setfield(L, -2, "__gc"); |
| 181 |
lua_pushvalue(L, luaT_upvalueindex(1)); // Prototype persistence names |
| 182 |
lua_rawseti(L, -2, 1); |
| 183 |
lua_setmetatable(L, 1); |
| 184 |
} |
| 185 |
|
| 186 |
int finish() { |
| 187 |
if (get_error() != nullptr) { |
| 188 |
lua_pushnil(L); |
| 189 |
lua_pushstring(L, get_error()); |
| 190 |
lua_getmetatable(L, 1); |
| 191 |
lua_getfield(L, -1, "err"); |
| 192 |
lua_replace(L, -2); |
| 193 |
return 3; |
| 194 |
} else { |
| 195 |
lua_pushlstring(L, data.c_str(), data.length()); |
| 196 |
return 1; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void fast_write_stack_object(int iIndex) override { |
| 201 |
if (lua_type(L, iIndex) != LUA_TUSERDATA) { |
| 202 |
write_stack_object(iIndex); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
// Convert index from relative to absolute |
| 207 |
if (iIndex < 0 && iIndex > LUA_REGISTRYINDEX) |
| 208 |
iIndex = lua_gettop(L) + 1 + iIndex; |
| 209 |
|
| 210 |
// Check for no cycle |
| 211 |
lua_getfenv(L, 1); |
| 212 |
lua_pushvalue(L, iIndex); |
| 213 |
lua_rawget(L, -2); |
| 214 |
lua_rawgeti(L, -2, 1); |
| 215 |
lua_pushvalue(L, iIndex); |
| 216 |
lua_gettable(L, -2); |
| 217 |
lua_replace(L, -2); |
| 218 |
if (!lua_isnil(L, -1) || !lua_isnil(L, -2)) { |
| 219 |
lua_pop(L, 3); |
| 220 |
write_stack_object(iIndex); |
| 221 |
return; |
| 222 |
} |
| 223 |
lua_pop(L, 2); |
| 224 |
|
| 225 |
// Save the index to the cache |
| 226 |
lua_pushvalue(L, iIndex); |
| 227 |
lua_pushnumber(L, (lua_Number)(next_index++)); |
| 228 |
lua_settable(L, -3); |
| 229 |
|
| 230 |
if (!check_that_userdata_can_be_depersisted(iIndex)) return; |
| 231 |
|
| 232 |
// Write type, metatable, and then environment |
| 233 |
uint8_t iType = LUA_TUSERDATA; |
| 234 |
write_byte_stream(&iType, 1); |
| 235 |
write_stack_object(-1); |
| 236 |
lua_getfenv(L, iIndex); |
| 237 |
write_stack_object(-1); |
| 238 |
lua_pop(L, 1); |
| 239 |
|
| 240 |
// Write the raw data |
| 241 |
if (lua_type(L, -1) == LUA_TTABLE) { |
| 242 |
lua_getfield(L, -1, "__persist"); |
| 243 |
if (lua_isnil(L, -1)) |
| 244 |
lua_pop(L, 1); |
| 245 |
else { |
| 246 |
lua_pushvalue(L, iIndex); |
| 247 |
lua_checkstack(L, 20); |
| 248 |
lua_CFunction fn = lua_tocfunction(L, -2); |
| 249 |
fn(L); |
| 250 |
lua_pop(L, 2); |
| 251 |
} |
| 252 |
} |
| 253 |
write_uint((uint64_t)0x42); // sync marker |
| 254 |
lua_pop(L, 1); |
| 255 |
} |
| 256 |
|
| 257 |
void write_stack_object(int iIndex) override { |
| 258 |
// Convert index from relative to absolute |
| 259 |
if (iIndex < 0 && iIndex > LUA_REGISTRYINDEX) |
| 260 |
iIndex = lua_gettop(L) + 1 + iIndex; |
| 261 |
|
| 262 |
if (lua_type(L, 2) != LUA_TTABLE) { |
| 263 |
luaL_error(L, "Permanents table was lost!"); |
| 264 |
} |
| 265 |
|
| 266 |
// Basic types always have their value written |
| 267 |
int iType = lua_type(L, iIndex); |
| 268 |
if (iType == LUA_TNIL || iType == LUA_TNONE) { |
| 269 |
uint8_t iByte = LUA_TNIL; |
| 270 |
write_byte_stream(&iByte, 1); |
| 271 |
} else if (iType == LUA_TBOOLEAN) { |
| 272 |
uint8_t iByte; |
| 273 |
if (lua_toboolean(L, iIndex)) |
| 274 |
iByte = PERSIST_TTRUE; |
| 275 |
else |
| 276 |
iByte = LUA_TBOOLEAN; |
| 277 |
write_byte_stream(&iByte, 1); |
| 278 |
} else if (iType == LUA_TNUMBER) { |
| 279 |
double fValue = lua_tonumber(L, iIndex); |
| 280 |
if (floor(fValue) == fValue && 0.0 <= fValue && fValue <= 16383.0) { |
| 281 |
// Small integers are written as just a few bytes |
| 282 |
// NB: 16383 = 2^14-1, which is the maximum value which |
| 283 |
// can fit into two bytes of VUInt. |
| 284 |
uint8_t iByte = PERSIST_TINTEGER; |
| 285 |
write_byte_stream(&iByte, 1); |
| 286 |
uint16_t iValue = (uint16_t)fValue; |
| 287 |
write_uint(iValue); |
| 288 |
} else { |
| 289 |
// Other numbers are written as an 8 byte double |
| 290 |
uint8_t iByte = LUA_TNUMBER; |
| 291 |
write_byte_stream(&iByte, 1); |
| 292 |
write_byte_stream(reinterpret_cast<uint8_t*>(&fValue), sizeof(double)); |
| 293 |
} |
| 294 |
} else { |
| 295 |
// Complex values are cached, and are only written once (if this |
| 296 |
// weren't done, then cycles in the object graph would break |
| 297 |
// things). |
| 298 |
lua_getfenv(L, 1); |
| 299 |
lua_pushvalue(L, iIndex); |
| 300 |
|
| 301 |
lua_rawget(L, -2); |
| 302 |
uint64_t iValue = static_cast<uint64_t>(lua_tonumber(L, -1)); |
| 303 |
if (iValue != 0) { |
| 304 |
lua_pop(L, 2); |
| 305 |
// If the value has not previously been written, then |
| 306 |
// write_object_raw would have been called, and the appropriate |
| 307 |
// data written, and 0 would be returned. Otherwise, the index |
| 308 |
// would be returned, which we offset by the number of types, |
| 309 |
// and then write. |
| 310 |
write_uint(iValue + PERSIST_TCOUNT - 1); |
| 311 |
} else { |
| 312 |
lua_pop(L, 1); |
| 313 |
|
| 314 |
// Save the index to the cache |
| 315 |
lua_pushvalue(L, iIndex); |
| 316 |
lua_pushnumber(L, static_cast<lua_Number>(next_index++)); |
| 317 |
lua_rawset(L, -3); |
| 318 |
|
| 319 |
// Remove the fenv and add the item back to the top |
| 320 |
lua_pop(L, 1); |
| 321 |
lua_pushvalue(L, iIndex); |
| 322 |
|
| 323 |
// write the item and restore the stack to the start state |
| 324 |
write_object_raw(); |
| 325 |
lua_pop(L, 1); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
void write_object_raw() { |
| 331 |
int top = lua_gettop(L); |
| 332 |
int item_index = top; // same position in write_stack_object |
| 333 |
int self_index = 1; // index 1 in write_stack_object |
| 334 |
int permanents_index = 2; // also in fenv self [1] |
| 335 |
uint8_t item_type = lua_type(L, item_index); |
| 336 |
|
| 337 |
lua_checkstack(L, top + 5); |
| 338 |
|
| 339 |
// Lookup the object in the permanents table |
| 340 |
lua_pushvalue(L, item_index); |
| 341 |
lua_gettable(L, permanents_index); |
| 342 |
if (lua_type(L, -1) != LUA_TNIL) { |
| 343 |
// Object is in the permanents table. |
| 344 |
uint8_t item_type = PERSIST_TPERMANENT; |
| 345 |
write_byte_stream(&item_type, 1); |
| 346 |
|
| 347 |
// Write the key corresponding to the permanent object |
| 348 |
write_stack_object(-1); |
| 349 |
lua_pop(L, 1); |
| 350 |
} else { |
| 351 |
// Object is not in the permanents table. |
| 352 |
lua_pop(L, 1); |
| 353 |
|
| 354 |
switch (item_type) { |
| 355 |
// LUA_TNIL handled in write_stack_object |
| 356 |
// LUA_TBOOLEAN handled in write_stack_object |
| 357 |
// LUA_TNUMBER handled in write_stack_object |
| 358 |
|
| 359 |
case LUA_TSTRING: { |
| 360 |
write_byte_stream(&item_type, 1); |
| 361 |
// Strings are simple: length and then bytes (not null |
| 362 |
// terminated) |
| 363 |
size_t iLength; |
| 364 |
const char* sString = lua_tolstring(L, item_index, &iLength); |
| 365 |
write_uint(iLength); |
| 366 |
write_byte_stream(reinterpret_cast<const uint8_t*>(sString), iLength); |
| 367 |
break; |
| 368 |
} |
| 369 |
|
| 370 |
case LUA_TTABLE: { |
| 371 |
// Save env and insert prior to table |
| 372 |
lua_getfenv(L, self_index); |
| 373 |
lua_insert(L, item_index); |
| 374 |
int table_env_index = item_index; |
| 375 |
item_index += 1; |
| 376 |
|
| 377 |
// Handle the metatable |
| 378 |
if (lua_getmetatable(L, item_index)) { |
| 379 |
item_type = PERSIST_TTABLE_WITH_META; |
| 380 |
write_byte_stream(&item_type, 1); |
| 381 |
write_stack_object(-1); |
| 382 |
lua_pop(L, 1); |
| 383 |
} else { |
| 384 |
write_byte_stream(&item_type, 1); |
| 385 |
} |
| 386 |
|
| 387 |
// Write the children as key, value pairs |
| 388 |
lua_pushnil(L); |
| 389 |
while (lua_next(L, item_index)) { |
| 390 |
write_stack_object(-2); // write the key |
| 391 |
write_stack_object(-1); // write the value |
| 392 |
lua_pop(L, 1); // remove the value |
| 393 |
// key is passed back to lua_next |
| 394 |
} |
| 395 |
|
| 396 |
// Write a nil to mark the end of the children (as nil is |
| 397 |
// the only value which cannot be used as a key in a table). |
| 398 |
uint8_t nil_type = LUA_TNIL; |
| 399 |
write_byte_stream(&nil_type, 1); |
| 400 |
|
| 401 |
lua_remove(L, table_env_index); |
| 402 |
} break; |
| 403 |
|
| 404 |
case LUA_TFUNCTION: |
| 405 |
if (lua_iscfunction(L, item_index)) { |
| 406 |
set_error_object(item_index, self_index); |
| 407 |
set_error("Cannot persist C functions"); |
| 408 |
break; |
| 409 |
} |
| 410 |
write_byte_stream(&item_type, 1); |
| 411 |
|
| 412 |
// Write the prototype (the part of a function which is |
| 413 |
// common across multiple closures - see LClosure / |
| 414 |
// Proto in Lua's lobject.h). |
| 415 |
lua_Debug proto_info; |
| 416 |
lua_pushvalue(L, item_index); |
| 417 |
lua_getinfo(L, ">Su", &proto_info); |
| 418 |
write_prototype(&proto_info, item_index); |
| 419 |
|
| 420 |
// Write the values of the upvalues |
| 421 |
// If available, also write the upvalue IDs (so that in |
| 422 |
// the future, we could hypothetically rejoin shared |
| 423 |
// upvalues). An ID is just an opaque sequence of bytes. |
| 424 |
write_uint(proto_info.nups); |
| 425 |
#if LUA_VERSION_NUM >= 502 |
| 426 |
write_uint(sizeof(void*)); |
| 427 |
#else |
| 428 |
write_uint(0); |
| 429 |
#endif |
| 430 |
for (int i = 1; i <= proto_info.nups; ++i) { |
| 431 |
lua_getupvalue(L, item_index, i); |
| 432 |
write_stack_object(-1); |
| 433 |
#if LUA_VERSION_NUM >= 502 |
| 434 |
void* pUpvalueID = lua_upvalueid(L, item_index, i); |
| 435 |
write_byte_stream((uint8_t*)&pUpvalueID, sizeof(void*)); |
| 436 |
#endif |
| 437 |
lua_pop(L, 1); |
| 438 |
} |
| 439 |
|
| 440 |
// Write the environment table |
| 441 |
lua_getfenv(L, item_index); |
| 442 |
write_stack_object(-1); |
| 443 |
lua_pop(L, 1); |
| 444 |
break; |
| 445 |
|
| 446 |
case LUA_TUSERDATA: |
| 447 |
if (!check_that_userdata_can_be_depersisted(item_index)) { |
| 448 |
break; |
| 449 |
} |
| 450 |
|
| 451 |
// Write type, metatable, and then environment |
| 452 |
write_byte_stream(&item_type, 1); |
| 453 |
write_stack_object(-1); |
| 454 |
lua_getfenv(L, item_index); |
| 455 |
write_stack_object(-1); |
| 456 |
lua_pop(L, 1); |
| 457 |
|
| 458 |
// Write the raw data |
| 459 |
if (lua_type(L, -1) == LUA_TTABLE) { |
| 460 |
lua_getfield(L, -1, "__persist"); |
| 461 |
if (lua_isnil(L, -1)) { |
| 462 |
lua_pop(L, 1); |
| 463 |
} else { |
| 464 |
lua_pushvalue(L, item_index); |
| 465 |
lua_pushvalue(L, self_index); |
| 466 |
lua_pushvalue(L, permanents_index); |
| 467 |
lua_call(L, 3, 0); |
| 468 |
} |
| 469 |
} |
| 470 |
lua_pop(L, 1); // remove userdata metatable |
| 471 |
write_uint((uint64_t)0x42); // sync marker |
| 472 |
break; |
| 473 |
|
| 474 |
default: |
| 475 |
set_error(lua_pushfstring(L, "Cannot persist %s values", |
| 476 |
luaL_typename(L, item_type))); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
if (had_error) { |
| 481 |
luaL_error(L, get_error()); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
// Checks if userdata can be persisted to file, that is if it has a size |
| 486 |
// of zero, or has a metatable with a __depersist_size, __persist, and |
| 487 |
// __depersist function. The metatable is pushed to the top of the stack. |
| 488 |
bool check_that_userdata_can_be_depersisted(int iIndex) { |
| 489 |
if (lua_getmetatable(L, iIndex)) { |
| 490 |
lua_getfield(L, -1, "__depersist_size"); |
| 491 |
if (lua_isnumber(L, -1)) { |
| 492 |
if (static_cast<int>(lua_tointeger(L, -1)) != |
| 493 |
static_cast<int>(lua_objlen(L, iIndex))) { |
| 494 |
set_error(lua_pushfstring(L, |
| 495 |
"__depersist_size is " |
| 496 |
"incorrect (%d vs. %d)", |
| 497 |
(int)lua_objlen(L, iIndex), |
| 498 |
(int)lua_tointeger(L, -1))); |
| 499 |
return false; |
| 500 |
} |
| 501 |
if (lua_objlen(L, iIndex) != 0) { |
| 502 |
lua_getfield(L, -2, "__persist"); |
| 503 |
lua_getfield(L, -3, "__depersist"); |
| 504 |
if (lua_isnil(L, -1) || lua_isnil(L, -2)) { |
| 505 |
set_error( |
| 506 |
"Can only persist non-empty userdata" |
| 507 |
" if they have __persist and __depersist " |
| 508 |
"metamethods"); |
| 509 |
return false; |
| 510 |
} |
| 511 |
lua_pop(L, 2); |
| 512 |
} |
| 513 |
} else { |
| 514 |
if (lua_objlen(L, iIndex) != 0) { |
| 515 |
set_error( |
| 516 |
"Can only persist non-empty userdata if " |
| 517 |
"they have a __depersist_size metafield"); |
| 518 |
return false; |
| 519 |
} |
| 520 |
} |
| 521 |
lua_pop(L, 1); |
| 522 |
} else { |
| 523 |
if (lua_objlen(L, iIndex) != 0) { |
| 524 |
set_error( |
| 525 |
"Can only persist userdata without a metatable" |
| 526 |
" if their size is zero"); |
| 527 |
return false; |
| 528 |
} |
| 529 |
lua_pushnil(L); |
| 530 |
} |
| 531 |
return true; |
| 532 |
} |
| 533 |
|
| 534 |
void write_prototype(lua_Debug* pProtoInfo, int iInstanceIndex) { |
| 535 |
// Sanity checks |
| 536 |
if (pProtoInfo->source[0] != '@') { |
| 537 |
// @ denotes that the source was a file |
| 538 |
// (http://www.lua.org/manual/5.1/manual.html#lua_Debug) |
| 539 |
set_error("Can only persist Lua functions defined in source files"); |
| 540 |
return; |
| 541 |
} |
| 542 |
if (std::strcmp(pProtoInfo->what, "Lua") != 0) { |
| 543 |
// what == "C" should have been caught by write_object_raw(). |
| 544 |
// what == "tail" should be impossible. |
| 545 |
// Hence "Lua" and "main" should be the only values seen. |
| 546 |
// NB: Chunks are not functions defined *in* source files, because |
| 547 |
// chunks *are* source files. |
| 548 |
set_error(lua_pushfstring(L, "Cannot persist entire Lua chunks (%s)", |
| 549 |
pProtoInfo->source + 1)); |
| 550 |
lua_pop(L, 1); |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
// Attempt cached lookup (prototypes are not publicly visible Lua |
| 555 |
// objects, and hence cannot be cached in the normal way of self's |
| 556 |
// environment). |
| 557 |
lua_getmetatable(L, 1); |
| 558 |
lua_pushfstring(L, "%s:%d", pProtoInfo->source + 1, |
| 559 |
pProtoInfo->linedefined); |
| 560 |
lua_pushvalue(L, -1); |
| 561 |
lua_rawget(L, -3); |
| 562 |
if (!lua_isnil(L, -1)) { |
| 563 |
uint64_t iValue = (uint64_t)lua_tonumber(L, -1); |
| 564 |
lua_pop(L, 3); |
| 565 |
write_uint(iValue + PERSIST_TCOUNT - 1); |
| 566 |
return; |
| 567 |
} |
| 568 |
lua_pop(L, 1); |
| 569 |
lua_pushvalue(L, -1); |
| 570 |
lua_pushnumber(L, (lua_Number)next_index++); |
| 571 |
lua_rawset(L, -4); |
| 572 |
|
| 573 |
uint8_t iType = PERSIST_TPROTOTYPE; |
| 574 |
write_byte_stream(&iType, 1); |
| 575 |
|
| 576 |
// Write upvalue names |
| 577 |
write_uint(pProtoInfo->nups); |
| 578 |
for (int i = 1; i <= pProtoInfo->nups; ++i) { |
| 579 |
lua_pushstring(L, lua_getupvalue(L, iInstanceIndex, i)); |
| 580 |
write_stack_object(-1); |
| 581 |
lua_pop(L, 2); |
| 582 |
} |
| 583 |
|
| 584 |
// Write the function's persist name |
| 585 |
lua_rawgeti(L, -2, 1); |
| 586 |
lua_replace(L, -3); |
| 587 |
lua_rawget(L, -2); |
| 588 |
if (lua_isnil(L, -1)) { |
| 589 |
set_error(lua_pushfstring( |
| 590 |
L, |
| 591 |
"Lua functions must be given a unique " |
| 592 |
"persistable name in order to be persisted (attempt to " |
| 593 |
"persist" |
| 594 |
" %s:%d)", |
| 595 |
pProtoInfo->source + 1, pProtoInfo->linedefined)); |
| 596 |
lua_pop(L, 2); |
| 597 |
return; |
| 598 |
} |
| 599 |
write_stack_object(-1); |
| 600 |
lua_pop(L, 2); |
| 601 |
} |
| 602 |
|
| 603 |
void write_byte_stream(const uint8_t* pBytes, size_t iCount) override { |
| 604 |
if (had_error) { |
| 605 |
// If an error occurred, then silently fail to write any |
| 606 |
// data. |
| 607 |
return; |
| 608 |
} |
| 609 |
|
| 610 |
data.append(reinterpret_cast<const char*>(pBytes), iCount); |
| 611 |
} |
| 612 |
|
| 613 |
void set_error(const char* sError) override { |
| 614 |
// If multiple errors occur, only record the first. |
| 615 |
if (had_error) { |
| 616 |
return; |
| 617 |
} |
| 618 |
had_error = true; |
| 619 |
|
| 620 |
// Use the written data buffer to store the error message |
| 621 |
data.assign(sError); |
| 622 |
} |
| 623 |
|
| 624 |
void set_error_object(int iStackObject, int self_index) { |
| 625 |
if (had_error) return; |
| 626 |
|
| 627 |
lua_pushvalue(L, iStackObject); |
| 628 |
lua_getmetatable(L, self_index); |
| 629 |
lua_insert(L, -2); |
| 630 |
lua_setfield(L, -2, "err"); |
| 631 |
lua_pop(L, 1); |
| 632 |
} |
| 633 |
|
| 634 |
const char* get_error() { |
| 635 |
if (had_error) |
| 636 |
return data.c_str(); |
| 637 |
else |
| 638 |
return nullptr; |
| 639 |
} |
| 640 |
|
| 641 |
private: |
| 642 |
lua_State* L; |
| 643 |
uint64_t next_index{1}; |
| 644 |
std::string data; |
| 645 |
size_t data_size{0}; |
| 646 |
bool had_error{false}; |
| 647 |
}; |
| 648 |
|
| 649 |
//! Basic implementation of depersistence interface |
| 650 |
/*! |
| 651 |
self - Instance of lua_persist_basic_reader allocated as a Lua userdata |
| 652 |
self environment: |
| 653 |
[-3] - self |
| 654 |
[-2] - pre-populated prototype persistence code |
| 655 |
`<name>` - `<code>` |
| 656 |
[-1] - pre-populated prototype persistence filenames |
| 657 |
`<name>` - `<filename>` |
| 658 |
[ 0] - permanents table |
| 659 |
`<index>` - object already depersisted |
| 660 |
self metatable: |
| 661 |
`__gc` - ~lua_persist_basic_reader (via l_crude_gc) |
| 662 |
`<N>` - userdata to have second `__depersist` call |
| 663 |
*/ |
| 664 |
class lua_persist_basic_reader : public lua_persist_reader { |
| 665 |
public: |
| 666 |
lua_persist_basic_reader(lua_State* L, const uint8_t* pData, size_t iLength) |
| 667 |
: L(L), data(pData), data_buffer_size(iLength) {} |
| 668 |
|
| 669 |
~lua_persist_basic_reader() override = default; |
| 670 |
|
| 671 |
lua_State* get_stack() override { return L; } |
| 672 |
|
| 673 |
void set_error(const char* sError) override { |
| 674 |
had_error = true; |
| 675 |
string_buffer.assign(sError); |
| 676 |
} |
| 677 |
|
| 678 |
void init() { |
| 679 |
lua_createtable(L, 32, 0); // Environment |
| 680 |
lua_pushvalue(L, 2); |
| 681 |
lua_rawseti(L, -2, 0); |
| 682 |
lua_pushvalue(L, luaT_upvalueindex(1)); |
| 683 |
lua_rawseti(L, -2, -1); |
| 684 |
lua_pushvalue(L, luaT_upvalueindex(2)); |
| 685 |
lua_rawseti(L, -2, -2); |
| 686 |
lua_pushvalue(L, 1); |
| 687 |
lua_rawseti(L, -2, -3); |
| 688 |
lua_setfenv(L, 1); |
| 689 |
lua_createtable(L, 0, 1); // Metatable |
| 690 |
luaT_pushcclosure(L, l_crude_gc<lua_persist_basic_reader>, 0); |
| 691 |
lua_setfield(L, -2, "__gc"); |
| 692 |
lua_setmetatable(L, 1); |
| 693 |
} |
| 694 |
|
| 695 |
bool read_stack_object() override { |
| 696 |
uint64_t iIndex; |
| 697 |
if (!read_uint(iIndex)) { |
| 698 |
set_error("Expected stack object"); |
| 699 |
return false; |
| 700 |
} |
| 701 |
if (lua_type(L, 1) != LUA_TTABLE) { |
| 702 |
// Ensure that index #1 is self environment |
| 703 |
lua_getfenv(L, 1); |
| 704 |
lua_replace(L, 1); |
| 705 |
} |
| 706 |
if (iIndex >= PERSIST_TCOUNT) { |
| 707 |
iIndex += 1 - PERSIST_TCOUNT; |
| 708 |
if (iIndex < (uint64_t)INT_MAX) |
| 709 |
lua_rawgeti(L, 1, (int)iIndex); |
| 710 |
else { |
| 711 |
lua_pushnumber(L, (lua_Number)iIndex); |
| 712 |
lua_rawget(L, 1); |
| 713 |
} |
| 714 |
if (lua_isnil(L, -1)) { |
| 715 |
set_error( |
| 716 |
"Cycle while depersisting permanent object key or " |
| 717 |
"userdata metatable"); |
| 718 |
return false; |
| 719 |
} |
| 720 |
} else { |
| 721 |
uint8_t iType = (uint8_t)iIndex; |
| 722 |
switch (iType) { |
| 723 |
case LUA_TNIL: |
| 724 |
lua_pushnil(L); |
| 725 |
break; |
| 726 |
case PERSIST_TPERMANENT: { |
| 727 |
uint64_t iOldIndex = next_index; |
| 728 |
++next_index; // Temporary marker |
| 729 |
lua_rawgeti(L, 1, 0); // Permanents table |
| 730 |
if (!read_stack_object()) return false; |
| 731 |
lua_gettable(L, -2); |
| 732 |
lua_replace(L, -2); |
| 733 |
// Replace marker with actual object |
| 734 |
uint64_t iNewIndex = next_index; |
| 735 |
next_index = iOldIndex; |
| 736 |
save_stack_object(); |
| 737 |
next_index = iNewIndex; |
| 738 |
break; |
| 739 |
} |
| 740 |
case LUA_TBOOLEAN: |
| 741 |
lua_pushboolean(L, 0); |
| 742 |
break; |
| 743 |
case PERSIST_TTRUE: |
| 744 |
lua_pushboolean(L, 1); |
| 745 |
break; |
| 746 |
case LUA_TSTRING: { |
| 747 |
size_t iLength; |
| 748 |
if (!read_uint(iLength)) return false; |
| 749 |
if (!read_byte_stream(string_buffer, iLength)) return false; |
| 750 |
lua_pushlstring(L, string_buffer.c_str(), string_buffer.length()); |
| 751 |
save_stack_object(); |
| 752 |
break; |
| 753 |
} |
| 754 |
case LUA_TTABLE: |
| 755 |
lua_newtable(L); |
| 756 |
save_stack_object(); |
| 757 |
if (!lua_checkstack(L, 8)) return false; |
| 758 |
if (!read_table_contents()) return false; |
| 759 |
break; |
| 760 |
case PERSIST_TTABLE_WITH_META: |
| 761 |
lua_newtable(L); |
| 762 |
save_stack_object(); |
| 763 |
if (!lua_checkstack(L, 8)) return false; |
| 764 |
if (!read_stack_object()) return false; |
| 765 |
lua_setmetatable(L, -2); |
| 766 |
if (!read_table_contents()) return false; |
| 767 |
break; |
| 768 |
case LUA_TNUMBER: { |
| 769 |
double fValue; |
| 770 |
if (!read_byte_stream(reinterpret_cast<uint8_t*>(&fValue), |
| 771 |
sizeof(double))) |
| 772 |
return false; |
| 773 |
lua_pushnumber(L, fValue); |
| 774 |
break; |
| 775 |
} |
| 776 |
case LUA_TFUNCTION: { |
| 777 |
if (!lua_checkstack(L, 8)) return false; |
| 778 |
uint64_t iOldIndex = next_index; |
| 779 |
++next_index; // Temporary marker |
| 780 |
if (!read_stack_object()) return false; |
| 781 |
lua_call(L, 0, 2); |
| 782 |
// Replace marker with closure |
| 783 |
uint64_t iNewIndex = next_index; |
| 784 |
next_index = iOldIndex; |
| 785 |
save_stack_object(); |
| 786 |
next_index = iNewIndex; |
| 787 |
// Set upvalues |
| 788 |
lua_insert(L, -2); |
| 789 |
int iNups, i; |
| 790 |
if (!read_uint(iNups)) return false; |
| 791 |
size_t iIDSize; |
| 792 |
if (!read_uint(iIDSize)) return false; |
| 793 |
for (i = 0; i < iNups; ++i) { |
| 794 |
if (!read_stack_object()) return false; |
| 795 |
// For now, just skip over the upvalue IDs. In the |
| 796 |
// future, the ID may be used to rejoin shared upvalues. |
| 797 |
if (!read_byte_stream(nullptr, iIDSize)) return false; |
| 798 |
} |
| 799 |
lua_call(L, iNups, 0); |
| 800 |
// Read environment |
| 801 |
if (!read_stack_object()) return false; |
| 802 |
lua_setfenv(L, -2); |
| 803 |
break; |
| 804 |
} |
| 805 |
case PERSIST_TPROTOTYPE: { |
| 806 |
if (!lua_checkstack(L, 8)) return false; |
| 807 |
|
| 808 |
uint64_t iOldIndex = next_index; |
| 809 |
++next_index; // Temporary marker |
| 810 |
int iNups; |
| 811 |
if (!read_uint(iNups)) return false; |
| 812 |
if (iNups == 0) |
| 813 |
lua_pushliteral(L, "return function() end,"); |
| 814 |
else { |
| 815 |
lua_pushliteral(L, "local "); |
| 816 |
lua_checkstack(L, (iNups + 1) * 2); |
| 817 |
for (int i = 0; i < iNups; ++i) { |
| 818 |
if (i != 0) lua_pushliteral(L, ","); |
| 819 |
if (!read_stack_object()) return false; |
| 820 |
if (lua_type(L, -1) != LUA_TSTRING) { |
| 821 |
set_error("Upvalue name not a string"); |
| 822 |
return false; |
| 823 |
} |
| 824 |
} |
| 825 |
lua_concat(L, iNups * 2 - 1); |
| 826 |
lua_pushliteral(L, ";return function(...)"); |
| 827 |
lua_pushvalue(L, -2); |
| 828 |
lua_pushliteral(L, "=...end,"); |
| 829 |
lua_concat(L, 5); |
| 830 |
} |
| 831 |
// Fetch name and then lookup filename and code |
| 832 |
if (!read_stack_object()) return false; |
| 833 |
lua_pushliteral(L, "@"); |
| 834 |
|
| 835 |
lua_rawgeti(L, 1, -1); |
| 836 |
lua_pushvalue(L, -3); |
| 837 |
lua_gettable(L, -2); |
| 838 |
lua_replace(L, -2); |
| 839 |
|
| 840 |
if (lua_isnil(L, -1)) { |
| 841 |
set_error(lua_pushfstring(L, |
| 842 |
"Unable to depersist prototype" |
| 843 |
" \'%s\'", |
| 844 |
lua_tostring(L, -3))); |
| 845 |
return false; |
| 846 |
} |
| 847 |
lua_concat(L, 2); // Prepend the @ to the filename |
| 848 |
lua_rawgeti(L, 1, -2); |
| 849 |
lua_pushvalue(L, -3); |
| 850 |
|
| 851 |
lua_gettable(L, -2); |
| 852 |
lua_replace(L, -2); |
| 853 |
lua_remove(L, -3); |
| 854 |
// Construct the closure factory |
| 855 |
load_multi_buffer ls; |
| 856 |
ls.piece[0] = lua_tolstring(L, -3, &ls.piece_size[0]); |
| 857 |
ls.piece[1] = lua_tolstring(L, -1, &ls.piece_size[1]); |
| 858 |
if (luaT_load(L, load_multi_buffer::load_fn, &ls, lua_tostring(L, -2), |
| 859 |
"bt") != 0) { |
| 860 |
// Should never happen |
| 861 |
lua_error(L); |
| 862 |
return false; |
| 863 |
} |
| 864 |
lua_replace(L, -4); |
| 865 |
lua_pop(L, 2); |
| 866 |
// Replace marker with closure factory |
| 867 |
uint64_t iNewIndex = next_index; |
| 868 |
next_index = iOldIndex; |
| 869 |
save_stack_object(); |
| 870 |
next_index = iNewIndex; |
| 871 |
break; |
| 872 |
} |
| 873 |
case LUA_TUSERDATA: { |
| 874 |
bool bHasSetMetatable = false; |
| 875 |
uint64_t iOldIndex = next_index; |
| 876 |
++next_index; // Temporary marker |
| 877 |
// Read metatable |
| 878 |
if (!read_stack_object()) return false; |
| 879 |
lua_getfield(L, -1, "__depersist_size"); |
| 880 |
if (!lua_isnumber(L, -1)) { |
| 881 |
set_error("Userdata missing __depersist_size metafield"); |
| 882 |
return false; |
| 883 |
} |
| 884 |
lua_newuserdata(L, (size_t)lua_tonumber(L, -1)); |
| 885 |
lua_replace(L, -2); |
| 886 |
// Replace marker with userdata |
| 887 |
uint64_t iNewIndex = next_index; |
| 888 |
next_index = iOldIndex; |
| 889 |
save_stack_object(); |
| 890 |
next_index = iNewIndex; |
| 891 |
// Perform immediate initialisation |
| 892 |
lua_getfield(L, -2, "__pre_depersist"); |
| 893 |
if (lua_isnil(L, -1)) |
| 894 |
lua_pop(L, 1); |
| 895 |
else { |
| 896 |
// Set metatable now, as pre-depersister may expect it |
| 897 |
// NB: Setting metatable if there isn't a |
| 898 |
// pre-depersister is not a good idea, as if there is an |
| 899 |
// error while the environment table is being |
| 900 |
// de-persisted, then the __gc handler of the userdata |
| 901 |
// will eventually be called with the userdata's |
| 902 |
// contents still being uninitialised. |
| 903 |
lua_pushvalue(L, -3); |
| 904 |
lua_setmetatable(L, -3); |
| 905 |
bHasSetMetatable = true; |
| 906 |
lua_pushvalue(L, -2); |
| 907 |
lua_call(L, 1, 0); |
| 908 |
} |
| 909 |
// Read environment |
| 910 |
if (!read_stack_object()) return false; |
| 911 |
lua_setfenv(L, -2); |
| 912 |
// Set metatable and read the raw data |
| 913 |
if (!bHasSetMetatable) { |
| 914 |
lua_pushvalue(L, -2); |
| 915 |
lua_setmetatable(L, -2); |
| 916 |
} |
| 917 |
lua_getfield(L, -2, "__depersist"); |
| 918 |
if (lua_isnil(L, -1)) |
| 919 |
lua_pop(L, 1); |
| 920 |
else { |
| 921 |
// Call the __depersist function with the userdata. |
| 922 |
// Note: Unless the __pre_depersist method was called above, the |
| 923 |
// new userdata has been created but any initialization such as |
| 924 |
// the constructor normally called by luaT_stdnew has not yet been |
| 925 |
// run. If the object is a C++ class, call the placement new |
| 926 |
// constructor before performing any other operations on the object. |
| 927 |
lua_pushvalue(L, -2); |
| 928 |
lua_rawgeti(L, 1, -3); |
| 929 |
lua_call(L, 2, 1); |
| 930 |
if (lua_toboolean(L, -1) != 0) { |
| 931 |
lua_pop(L, 1); |
| 932 |
lua_rawgeti(L, 1, -3); |
| 933 |
lua_getmetatable(L, -1); |
| 934 |
lua_replace(L, -2); |
| 935 |
lua_pushvalue(L, -2); |
| 936 |
lua_rawseti(L, -2, (int)lua_objlen(L, -2) + 1); |
| 937 |
} |
| 938 |
lua_pop(L, 1); |
| 939 |
} |
| 940 |
lua_replace(L, -2); |
| 941 |
uint64_t iSyncMarker; |
| 942 |
if (!read_uint(iSyncMarker)) return false; |
| 943 |
if (iSyncMarker != 0x42) { |
| 944 |
set_error("sync fail"); |
| 945 |
return false; |
| 946 |
} |
| 947 |
break; |
| 948 |
} |
| 949 |
case PERSIST_TINTEGER: { |
| 950 |
uint16_t iValue; |
| 951 |
if (!read_uint(iValue)) return false; |
| 952 |
lua_pushinteger(L, iValue); |
| 953 |
break; |
| 954 |
} |
| 955 |
default: |
| 956 |
lua_pushliteral(L, "Unable to depersist values of type \'"); |
| 957 |
if (iType <= LUA_TTHREAD) |
| 958 |
lua_pushstring(L, lua_typename(L, iType)); |
| 959 |
else { |
| 960 |
switch (iType) { |
| 961 |
case PERSIST_TPERMANENT: |
| 962 |
lua_pushliteral(L, "permanent"); |
| 963 |
break; |
| 964 |
case PERSIST_TTRUE: |
| 965 |
lua_pushliteral(L, "boolean-true"); |
| 966 |
break; |
| 967 |
case PERSIST_TTABLE_WITH_META: |
| 968 |
lua_pushliteral(L, "table-with-metatable"); |
| 969 |
break; |
| 970 |
case PERSIST_TINTEGER: |
| 971 |
lua_pushliteral(L, "integer"); |
| 972 |
break; |
| 973 |
case PERSIST_TPROTOTYPE: |
| 974 |
lua_pushliteral(L, "prototype"); |
| 975 |
break; |
| 976 |
case PERSIST_TRESERVED1: |
| 977 |
lua_pushliteral(L, "reserved1"); |
| 978 |
break; |
| 979 |
case PERSIST_TRESERVED2: |
| 980 |
lua_pushliteral(L, "reserved2"); |
| 981 |
break; |
| 982 |
} |
| 983 |
} |
| 984 |
lua_pushliteral(L, "\'"); |
| 985 |
lua_concat(L, 3); |
| 986 |
set_error(lua_tostring(L, -1)); |
| 987 |
lua_pop(L, 1); |
| 988 |
return false; |
| 989 |
} |
| 990 |
} |
| 991 |
return true; |
| 992 |
} |
| 993 |
|
| 994 |
void save_stack_object() { |
| 995 |
if (next_index < (uint64_t)INT_MAX) { |
| 996 |
lua_pushvalue(L, -1); |
| 997 |
lua_rawseti(L, 1, (int)next_index); |
| 998 |
} else { |
| 999 |
lua_pushnumber(L, (lua_Number)next_index); |
| 1000 |
lua_pushvalue(L, -2); |
| 1001 |
lua_rawset(L, 1); |
| 1002 |
} |
| 1003 |
++next_index; |
| 1004 |
} |
| 1005 |
|
| 1006 |
bool read_table_contents() { |
| 1007 |
while (true) { |
| 1008 |
if (!read_stack_object()) return false; |
| 1009 |
if (lua_type(L, -1) == LUA_TNIL) { |
| 1010 |
lua_pop(L, 1); |
| 1011 |
return true; |
| 1012 |
} |
| 1013 |
if (!read_stack_object()) return false; |
| 1014 |
// NB: lua_rawset used rather than lua_settable to avoid invoking |
| 1015 |
// any metamethods, as they may not have been designed to be called |
| 1016 |
// during depersistence. |
| 1017 |
lua_rawset(L, -3); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
bool finish() { |
| 1022 |
// Ensure that all data has been read |
| 1023 |
if (data_buffer_size != 0) { |
| 1024 |
set_error(lua_pushfstring(L, "%d bytes of data remain unpersisted", |
| 1025 |
(int)data_buffer_size)); |
| 1026 |
return false; |
| 1027 |
} |
| 1028 |
|
| 1029 |
// Ensure that index #1 is self environment |
| 1030 |
if (lua_type(L, 1) != LUA_TTABLE) { |
| 1031 |
lua_getfenv(L, 1); |
| 1032 |
lua_replace(L, 1); |
| 1033 |
} |
| 1034 |
// Ensure that index #1 is self metatable |
| 1035 |
lua_rawgeti(L, 1, -3); |
| 1036 |
lua_getmetatable(L, -1); |
| 1037 |
lua_replace(L, 1); |
| 1038 |
lua_pop(L, 1); |
| 1039 |
|
| 1040 |
// Call all the __depersist functions which need a 2nd call |
| 1041 |
int iNumCalls = (int)lua_objlen(L, 1); |
| 1042 |
for (int i = 1; i <= iNumCalls; ++i) { |
| 1043 |
lua_rawgeti(L, 1, i); |
| 1044 |
luaL_getmetafield(L, -1, "__depersist"); |
| 1045 |
lua_insert(L, -2); |
| 1046 |
lua_call(L, 1, 0); |
| 1047 |
} |
| 1048 |
return true; |
| 1049 |
} |
| 1050 |
|
| 1051 |
bool read_byte_stream(uint8_t* pBytes, size_t iCount) override { |
| 1052 |
if (iCount > data_buffer_size) { |
| 1053 |
set_error(lua_pushfstring( |
| 1054 |
L, "End of input reached while attempting to read %d byte%s", |
| 1055 |
(int)iCount, iCount == 1 ? "" : "s")); |
| 1056 |
lua_pop(L, 1); |
| 1057 |
return false; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (pBytes != nullptr) std::memcpy(pBytes, data, iCount); |
| 1061 |
|
| 1062 |
data += iCount; |
| 1063 |
data_buffer_size -= iCount; |
| 1064 |
return true; |
| 1065 |
} |
| 1066 |
|
| 1067 |
bool read_byte_stream(std::string& bytes, size_t iCount) { |
| 1068 |
if (iCount > data_buffer_size) { |
| 1069 |
set_error(lua_pushfstring( |
| 1070 |
L, "End of input reached while attempting to read %d byte%s", |
| 1071 |
(int)iCount, iCount == 1 ? "" : "s")); |
| 1072 |
lua_pop(L, 1); |
| 1073 |
return false; |
| 1074 |
} |
| 1075 |
|
| 1076 |
bytes.assign(reinterpret_cast<const char*>(data), iCount); |
| 1077 |
|
| 1078 |
data += iCount; |
| 1079 |
data_buffer_size -= iCount; |
| 1080 |
return true; |
| 1081 |
} |
| 1082 |
|
| 1083 |
const uint8_t* get_pointer() { return data; } |
| 1084 |
uint64_t get_object_count() { return next_index; } |
| 1085 |
|
| 1086 |
const char* get_error() { |
| 1087 |
if (had_error) |
| 1088 |
return string_buffer.c_str(); |
| 1089 |
else |
| 1090 |
return nullptr; |
| 1091 |
} |
| 1092 |
|
| 1093 |
private: |
| 1094 |
lua_State* L; |
| 1095 |
uint64_t next_index{1}; |
| 1096 |
const uint8_t* data; |
| 1097 |
size_t data_buffer_size; |
| 1098 |
std::string string_buffer; |
| 1099 |
bool had_error{false}; |
| 1100 |
}; |
| 1101 |
|
| 1102 |
namespace { |
| 1103 |
|
| 1104 |
int l_dump_toplevel(lua_State* L) { |
| 1105 |
luaL_checktype(L, 2, LUA_TTABLE); |
| 1106 |
lua_settop(L, 2); |
| 1107 |
lua_pushvalue(L, 1); |
| 1108 |
lua_persist_basic_writer* pWriter = |
| 1109 |
new (lua_newuserdata(L, sizeof(lua_persist_basic_writer))) |
| 1110 |
lua_persist_basic_writer(L); |
| 1111 |
lua_replace(L, 1); |
| 1112 |
pWriter->init(); |
| 1113 |
pWriter->write_stack_object(3); |
| 1114 |
return pWriter->finish(); |
| 1115 |
} |
| 1116 |
|
| 1117 |
int l_load_toplevel(lua_State* L) { |
| 1118 |
size_t iDataLength; |
| 1119 |
const uint8_t* pData = luaT_checkfile(L, 1, &iDataLength); |
| 1120 |
luaL_checktype(L, 2, LUA_TTABLE); |
| 1121 |
lua_settop(L, 2); |
| 1122 |
lua_pushvalue(L, 1); |
| 1123 |
lua_persist_basic_reader* pReader = |
| 1124 |
new (lua_newuserdata(L, sizeof(lua_persist_basic_reader))) |
| 1125 |
lua_persist_basic_reader(L, pData, iDataLength); |
| 1126 |
lua_replace(L, 1); |
| 1127 |
pReader->init(); |
| 1128 |
if (!pReader->read_stack_object() || !pReader->finish()) { |
| 1129 |
int iNumObjects = (int)pReader->get_object_count(); |
| 1130 |
int iNumBytes = (int)(pReader->get_pointer() - pData); |
| 1131 |
lua_pushnil(L); |
| 1132 |
lua_pushfstring(L, "%s after %d objects (%d bytes)", |
| 1133 |
pReader->get_error() ? pReader->get_error() |
| 1134 |
: "Error while depersisting", |
| 1135 |
iNumObjects, iNumBytes); |
| 1136 |
return 2; |
| 1137 |
} else { |
| 1138 |
return 1; |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
int calculate_line_number(const char* sStart, const char* sPosition) { |
| 1143 |
int iLine = 1; |
| 1144 |
for (; sStart != sPosition; ++sStart) { |
| 1145 |
switch (sStart[0]) { |
| 1146 |
case '\0': |
| 1147 |
return -1; // error return value |
| 1148 |
case '\n': |
| 1149 |
++iLine; |
| 1150 |
if (sStart[1] == '\r') ++sStart; |
| 1151 |
break; |
| 1152 |
case '\r': |
| 1153 |
++iLine; |
| 1154 |
if (sStart[1] == '\n') ++sStart; |
| 1155 |
break; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
return iLine; |
| 1159 |
} |
| 1160 |
|
| 1161 |
const char* find_function_end(lua_State* L, const char* sStart) { |
| 1162 |
const char* sEnd = sStart; |
| 1163 |
while (sEnd) { |
| 1164 |
sEnd = std::strstr(sEnd, "end"); |
| 1165 |
if (sEnd) { |
| 1166 |
sEnd += 3; |
| 1167 |
load_multi_buffer ls; |
| 1168 |
ls.insert("return function", 0); |
| 1169 |
ls.piece[1] = sStart; |
| 1170 |
ls.piece_size[1] = sEnd - sStart; |
| 1171 |
if (luaT_load(L, load_multi_buffer::load_fn, &ls, "", "bt") == 0) { |
| 1172 |
lua_pop(L, 1); |
| 1173 |
return sEnd; |
| 1174 |
} |
| 1175 |
lua_pop(L, 1); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
return nullptr; |
| 1179 |
} |
| 1180 |
|
| 1181 |
int l_persist_dofile(lua_State* L) { |
| 1182 |
const char* sFilename = luaL_checkstring(L, 1); |
| 1183 |
lua_settop(L, 1); |
| 1184 |
|
| 1185 |
// Read entire file into memory |
| 1186 |
std::FILE* fFile = std::fopen(sFilename, "r"); |
| 1187 |
if (fFile == nullptr) { |
| 1188 |
const char* sError = std::strerror(errno); |
| 1189 |
return luaL_error(L, "cannot open %s: %s", sFilename, sError); |
| 1190 |
} |
| 1191 |
size_t iBufferSize = lua_objlen(L, luaT_upvalueindex(1)); |
| 1192 |
size_t iBufferUsed = 0; |
| 1193 |
while (!std::ferror(fFile) && !std::feof(fFile)) { |
| 1194 |
iBufferUsed += |
| 1195 |
std::fread(static_cast<char*>(lua_touserdata(L, luaT_upvalueindex(1))) + |
| 1196 |
iBufferUsed, |
| 1197 |
1, iBufferSize - iBufferUsed, fFile); |
| 1198 |
if (iBufferUsed == iBufferSize) { |
| 1199 |
iBufferSize *= 2; |
| 1200 |
std::memcpy(lua_newuserdata(L, iBufferSize), |
| 1201 |
lua_touserdata(L, luaT_upvalueindex(1)), iBufferUsed); |
| 1202 |
lua_replace(L, luaT_upvalueindex(1)); |
| 1203 |
} else |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
int iStatus = std::ferror(fFile); |
| 1207 |
std::fclose(fFile); |
| 1208 |
if (iStatus) { |
| 1209 |
const char* sError = std::strerror(errno); |
| 1210 |
return luaL_error(L, "cannot read %s: %s", sFilename, sError); |
| 1211 |
} |
| 1212 |
|
| 1213 |
// Check file |
| 1214 |
char* sFile = static_cast<char*>(lua_touserdata(L, luaT_upvalueindex(1))); |
| 1215 |
sFile[iBufferUsed] = 0; |
| 1216 |
if (sFile[0] == '#') { |
| 1217 |
do { |
| 1218 |
++sFile; |
| 1219 |
--iBufferUsed; |
| 1220 |
} while (sFile[0] != 0 && sFile[0] != '\r' && sFile[0] != '\n'); |
| 1221 |
} |
| 1222 |
if (sFile[0] == LUA_SIGNATURE[0]) { |
| 1223 |
return luaL_error(L, "cannot load %s: compiled files not permitted", |
| 1224 |
sFilename); |
| 1225 |
} |
| 1226 |
|
| 1227 |
// Load and do file |
| 1228 |
lua_pushliteral(L, "@"); |
| 1229 |
lua_pushvalue(L, 1); |
| 1230 |
lua_concat(L, 2); |
| 1231 |
if (luaL_loadbuffer(L, sFile, iBufferUsed, lua_tostring(L, -1)) != 0) |
| 1232 |
return lua_error(L); |
| 1233 |
lua_remove(L, -2); |
| 1234 |
int iBufferCopyIndex = lua_gettop(L); |
| 1235 |
std::memcpy(lua_newuserdata(L, iBufferUsed + 1), sFile, iBufferUsed + 1); |
| 1236 |
lua_insert(L, -2); |
| 1237 |
lua_call(L, 0, LUA_MULTRET); |
| 1238 |
sFile = static_cast<char*>(lua_touserdata(L, luaT_upvalueindex(1))); |
| 1239 |
std::memcpy(sFile, lua_touserdata(L, iBufferCopyIndex), iBufferUsed + 1); |
| 1240 |
lua_remove(L, iBufferCopyIndex); |
| 1241 |
|
| 1242 |
// Extract persistable functions |
| 1243 |
const char* sPosition = sFile; |
| 1244 |
while (true) { |
| 1245 |
sPosition = std::strstr(sPosition, "--[[persistable:"); |
| 1246 |
if (!sPosition) break; |
| 1247 |
sPosition += 16; |
| 1248 |
const char* sNameEnd = std::strstr(sPosition, "]]"); |
| 1249 |
if (sNameEnd) { |
| 1250 |
int iLineNumber = calculate_line_number(sFile, sNameEnd); |
| 1251 |
const char* sFunctionArgs = std::strchr(sNameEnd + 2, '('); |
| 1252 |
const char* sFunctionEnd = find_function_end(L, sFunctionArgs); |
| 1253 |
if ((sNameEnd - sPosition) == 1 && *sPosition == ':') { |
| 1254 |
// --[[persistable::]] means take the existing name of the |
| 1255 |
// function |
| 1256 |
sPosition = std::strstr(sNameEnd, "function") + 8; |
| 1257 |
sPosition += std::strspn(sPosition, " \t"); |
| 1258 |
sNameEnd = sFunctionArgs; |
| 1259 |
while (sNameEnd[-1] == ' ') --sNameEnd; |
| 1260 |
} |
| 1261 |
if (iLineNumber != -1 && sFunctionArgs && sFunctionEnd) { |
| 1262 |
// Save <filename>:<line> => <name> |
| 1263 |
lua_pushfstring(L, "%s:%d", sFilename, iLineNumber); |
| 1264 |
lua_pushvalue(L, -1); |
| 1265 |
lua_gettable(L, luaT_upvalueindex(2)); |
| 1266 |
if (lua_isnil(L, -1)) { |
| 1267 |
lua_pop(L, 1); |
| 1268 |
lua_pushlstring(L, sPosition, sNameEnd - sPosition); |
| 1269 |
lua_settable(L, luaT_upvalueindex(2)); |
| 1270 |
} else { |
| 1271 |
return luaL_error(L, |
| 1272 |
"Multiple persistable functions defined" |
| 1273 |
"on the same line (%s:%d)", |
| 1274 |
sFilename, iLineNumber); |
| 1275 |
} |
| 1276 |
|
| 1277 |
// Save <name> => <filename> |
| 1278 |
lua_pushlstring(L, sPosition, sNameEnd - sPosition); |
| 1279 |
lua_pushvalue(L, -1); |
| 1280 |
lua_gettable(L, luaT_upvalueindex(3)); |
| 1281 |
if (lua_isnil(L, -1)) { |
| 1282 |
lua_pop(L, 1); |
| 1283 |
lua_pushvalue(L, 1); |
| 1284 |
lua_settable(L, luaT_upvalueindex(3)); |
| 1285 |
} else { |
| 1286 |
return luaL_error(L, |
| 1287 |
"Persistable function name \'%s\' is" |
| 1288 |
" not unique (defined in both %s and %s)", |
| 1289 |
lua_tostring(L, -2), lua_tostring(L, -1), |
| 1290 |
sFilename); |
| 1291 |
} |
| 1292 |
|
| 1293 |
// Save <name> => <code> |
| 1294 |
lua_pushlstring(L, sPosition, sNameEnd - sPosition); |
| 1295 |
lua_pushliteral(L, "\n"); |
| 1296 |
lua_getfield(L, -1, "rep"); |
| 1297 |
lua_insert(L, -2); |
| 1298 |
lua_pushinteger(L, iLineNumber - 1); |
| 1299 |
lua_call(L, 2, 1); |
| 1300 |
lua_pushliteral(L, "function"); |
| 1301 |
lua_pushlstring(L, sFunctionArgs, sFunctionEnd - sFunctionArgs); |
| 1302 |
lua_concat(L, 3); |
| 1303 |
lua_settable(L, luaT_upvalueindex(4)); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
// Finish |
| 1309 |
return lua_gettop(L) - 1; |
| 1310 |
} |
| 1311 |
|
| 1312 |
int l_errcatch(lua_State* L) { |
| 1313 |
// Dummy function for debugging - place a breakpoint on the following |
| 1314 |
// return statement to inspect the full C call stack when a Lua error |
| 1315 |
// occurs (assuming that l_errcatch is used as the error catch handler). |
| 1316 |
return 1; |
| 1317 |
} |
| 1318 |
|
| 1319 |
// Due to the various required upvalues, functions are registered manually, but |
| 1320 |
// we still need a dummy to pass to luaL_register. |
| 1321 |
constexpr std::array<luaL_Reg, 2> persist_lib{ |
| 1322 |
{{"errcatch", l_errcatch}, {nullptr, nullptr}}}; |
| 1323 |
|
| 1324 |
} // namespace |
| 1325 |
|
| 1326 |
int luaopen_persist(lua_State* L) { |
| 1327 |
luaT_register(L, "persist", persist_lib); |
| 1328 |
lua_newuserdata(L, 512); // buffer for dofile |
| 1329 |
lua_newtable(L); |
| 1330 |
lua_newtable(L); |
| 1331 |
lua_newtable(L); |
| 1332 |
lua_pushvalue(L, -3); |
| 1333 |
luaT_pushcclosure(L, l_dump_toplevel, 1); |
| 1334 |
lua_setfield(L, -6, "dump"); |
| 1335 |
lua_pushvalue(L, -2); |
| 1336 |
lua_pushvalue(L, -2); |
| 1337 |
luaT_pushcclosure(L, l_load_toplevel, 2); |
| 1338 |
lua_setfield(L, -6, "load"); |
| 1339 |
luaT_pushcclosure(L, l_persist_dofile, 4); |
| 1340 |
lua_setfield(L, -2, "dofile"); |
| 1341 |
return 1; |
| 1342 |
} |