Implement LuaExtraArgs

This commit is contained in:
2023-04-13 13:26:45 -04:00
parent cf8b573364
commit 30155ae388
4 changed files with 126 additions and 74 deletions

View File

@@ -33,10 +33,17 @@ void LuaSnap::serialize(StreamBuffer *sb) {
// Lua stack should be empty.
assert(lua_gettop(state_) == 0);
// lua variables that we'll need.
LuaVar key, value;
LuaRet permstable, regcopy;
LuaDefStack LS(state_, permstable, regcopy, key, value);
// We'll allocate stack slots manually, to ensure
// that the thread serializes as we expect it to.
lua_pushnil(state_); // permstable
lua_pushnil(state_); // regcopy
lua_pushnil(state_); // key
lua_pushnil(state_); // value
LuaSpecial permstable(1);
LuaSpecial regcopy(2);
LuaSpecial key(3);
LuaSpecial value(4);
LuaCoreStack LS(state_);
// Construct a copy of the registry table.
LS.set(regcopy, LuaNewTable);
@@ -62,10 +69,8 @@ void LuaSnap::serialize(StreamBuffer *sb) {
LS.rawget(permstable, LuaRegistry, "persist");
assert(LS.istable(permstable));
// When we call 'LS.result', this should leave the
// permstable and the regcopy on the stack.
LS.result();
assert(lua_gettop(state_) == 2);
// Remove key and value from stack, leaving permstable and regcopy.
lua_settop(state_, 2);
// Write dummy length, use eris to write data, then overwrite length.
sb->write_int64(0);
@@ -88,13 +93,17 @@ void LuaSnap::deserialize(StreamBuffer *sb) {
lua_pushstring(state_, "unpersist");
lua_rawget(state_, LUA_REGISTRYINDEX);
eris_undump(state_, sb->lua_reader, ud);
// Set up a stack frame manually. Eris deserialization
// should have left permstable and regcopy on the stack.
assert(lua_gettop(state_) == 2);
// Set up a stack frame.
LuaArg permstable, regcopy;
LuaVar key, value;
LuaDefStack LS(state_, permstable, regcopy, key, value);
LuaSpecial permstable(1);
LuaSpecial regcopy(2);
lua_pushnil(state_);
lua_pushnil(state_);
LuaSpecial key(3);
LuaSpecial value(4);
LuaCoreStack LS(state_);
assert(LS.istable(regcopy));
// Copy the contents of the snapshot over to the registry.
@@ -102,8 +111,7 @@ void LuaSnap::deserialize(StreamBuffer *sb) {
while (LS.next(regcopy, key, value) != 0) {
LS.rawset(LuaRegistry, key, value);
}
LS.result();
assert(lua_gettop(state_) == 0);
lua_settop(state_, 0);
}
// Snapshot and rollback can trivially be implemented on top of serialize and