Files
integration/luprex/core/cpp/luasnap.hpp

60 lines
1.6 KiB
C++
Raw Normal View History

2021-02-16 13:31:34 -05:00
/////////////////////////////////////////////////////////////////////////////
2021-02-09 17:15:54 -05:00
//
// LUASNAP
//
2021-02-16 13:31:34 -05:00
// A lua interpreter that can be checkpointed (snapshotted). This makes it
// possible to roll the entire interpreter back to a previously-snapshotted
// state.
2021-02-09 17:15:54 -05:00
//
2021-02-16 13:31:34 -05:00
// To accomplish this, we take advantage of the 'allocf' parameter to
// lua_newstate. This lets us hook the lua allocator, which in turn lets us
// find every block of memory currently in use by lua. To snapshot, we 'memcpy'
// every block of memory that lua is using into a buffer. To rollback, we just
// 'memcpy' the data back.
2021-02-09 17:15:54 -05:00
//
2021-02-16 13:31:34 -05:00
// The current implementation is a proof-of-concept. It's quite wasteful of
// memory, roughly doubling the amount of RAM that LUA uses. It's also not
// super-fast, since it allocates tons of tiny blocks. But it totally
// works, so it demonstrate that this method of snapshot and rollback is
// feasible.
2021-02-09 17:15:54 -05:00
//
2021-02-16 13:31:34 -05:00
/////////////////////////////////////////////////////////////////////////////
2021-02-09 17:15:54 -05:00
#ifndef LUASNAP_HPP
#define LUASNAP_HPP
#include "luastack.hpp"
class LuaSnap {
private:
lua_State *state_;
std::string snapshot_;
2021-02-09 17:15:54 -05:00
public:
LuaSnap();
~LuaSnap();
// Get the lua intepreter.
//
lua_State *state() const { return state_; }
// Return true if there's a saved snapshot.
//
bool have_snapshot() const;
// snapshot the state of the lua interpreter.
//
// If there is already a snapshot, this panics.
//
void snapshot();
// Rollback the lua intepreter to the snapshotted state.
//
// If there is no snapshot, this panics.
//
void rollback();
};
#endif // LUASNAP_HPP