Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
c_packer.cpp File Reference
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include "c_packer.h"
#include "c_internal.h"
#include "log.h"
#include "debug.h"
#include "threading/mutex_auto_lock.h"
#include <lauxlib.h>
+ Include dependency graph for c_packer.cpp:

Functions

static int absidx (lua_State *L, int idx)
 
static bool uses_sdata (int type)
 
static bool uses_union (int type)
 
static bool can_set_into (int ktype, int vtype)
 
static bool suitable_key (lua_State *L, int idx)
 
static bool get_known_lua_metatables (lua_State *L)
 Push core.known_metatables to the stack if it exists.
 
static auto emplace (PackedValue &pv, s16 type)
 Append instruction to end.
 
void script_register_packer (lua_State *L, const char *regname, PackInFunc fin, PackOutFunc fout)
 
static bool find_packer (const char *regname, PackerTuple &out)
 Find a packer for a metatable.
 
static bool find_packer (lua_State *L, int idx, PackerTuple &out)
 Find a packer matching the metatable of the Lua value.
 
static VectorRef< PackedInstrrecord_object (lua_State *L, int idx, PackedValue &pv, std::unordered_map< const void *, s32 > &seen)
 Keeps track of seen objects, which is needed to make circular references work.
 
static VectorRef< PackedInstrpack_inner (lua_State *L, int idx, int vidx, PackedValue &pv, std::unordered_map< const void *, s32 > &seen)
 Pack a single Lua value and add it to the instruction stream.
 
PackedValuescript_pack (lua_State *L, int idx)
 
void script_unpack (lua_State *L, PackedValue *pv)
 
void script_dump_packed (const PackedValue *val)
 

Variables

static std::unordered_map< std::string, Packer > g_packers
 
static std::mutex g_packers_lock
 

Function Documentation

◆ absidx()

static int absidx ( lua_State * L,
int idx )
inlinestatic

References idx.

Referenced by pack_inner(), and script_pack().

+ Here is the caller graph for this function:

◆ can_set_into()

static bool can_set_into ( int ktype,
int vtype )
inlinestatic

References uses_sdata(), and uses_union().

Referenced by pack_inner().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emplace()

static auto emplace ( PackedValue & pv,
s16 type )
inlinestatic

Append instruction to end.

Parameters
pvtarget
typeinstruction type
Returns
reference to instruction

References PackedValue::i, and INSTR_POP.

Referenced by pack_inner(), and record_object().

+ Here is the caller graph for this function:

◆ find_packer() [1/2]

static bool find_packer ( const char * regname,
PackerTuple & out )
static

Find a packer for a metatable.

Parameters
regnamemetatable name
outpacker will be placed here
Returns
success

References g_packers, and g_packers_lock.

Referenced by PackedValue::~PackedValue(), find_packer(), pack_inner(), and script_unpack().

+ Here is the caller graph for this function:

◆ find_packer() [2/2]

static bool find_packer ( lua_State * L,
int idx,
PackerTuple & out )
static

Find a packer matching the metatable of the Lua value.

Parameters
LLua state
idxIndex on stack
outpacker will be placed here
Returns
success

References CUSTOM_RIDX_METATABLE_MAP, FATAL_ERROR_IF, find_packer(), and idx.

+ Here is the call graph for this function:

◆ get_known_lua_metatables()

static bool get_known_lua_metatables ( lua_State * L)
inlinestatic

Push core.known_metatables to the stack if it exists.

Parameters
LLua state
Returns
true if core.known_metatables exists, false otherwise.

Referenced by pack_inner(), and script_unpack().

+ Here is the caller graph for this function:

◆ pack_inner()

static VectorRef< PackedInstr > pack_inner ( lua_State * L,
int idx,
int vidx,
PackedValue & pv,
std::unordered_map< const void *, s32 > & seen )
static

Pack a single Lua value and add it to the instruction stream.

Parameters
LLua state
idxIndex of value on Lua stack. Must be positive, use absidx if not!
vidxNext free index on the stack as it would look during unpacking. (v = virtual)
pvtarget
seenMap of seen objects (see record_object)
Returns
reference to the instruction that creates the value

References absidx(), call_string_dump(), can_set_into(), PackedValue::contains_userdata, emplace(), find_packer(), get_known_lua_metatables(), idx, INSTR_POP, INSTR_SETMETATABLE, INSTR_SETTABLE, pack_inner(), record_object(), and suitable_key().

Referenced by pack_inner(), and script_pack().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ record_object()

static VectorRef< PackedInstr > record_object ( lua_State * L,
int idx,
PackedValue & pv,
std::unordered_map< const void *, s32 > & seen )
static

Keeps track of seen objects, which is needed to make circular references work.

The first time an object is seen it remembers the instruction index. The caller is expected to add instructions that produce the value immediately after. For second, third, ... calls it pushes an instruction that references the already created value.

Parameters
LLua state
idxIndex of value on Lua stack
pvtarget
seenMap of seen objects
Returns
empty reference (first time) or reference to instruction that reproduces the value (otherwise)

References emplace(), PackedValue::i, idx, INSTR_PUSHREF, and S32_MAX.

Referenced by pack_inner().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_dump_packed()

void script_dump_packed ( const PackedValue * val)

References FATAL_ERROR, PackedValue::i, INSTR_POP, INSTR_PUSHREF, INSTR_SETMETATABLE, INSTR_SETTABLE, and uses_sdata().

Referenced by ModApiServer::l_serialize_roundtrip().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_pack()

PackedValue * script_pack ( lua_State * L,
int idx )

References absidx(), idx, and pack_inner().

Referenced by ModApiServer::l_do_async_callback(), ModApiServer::l_serialize_roundtrip(), read_pv(), AsyncWorkerThread::run(), and ServerScripting::saveGlobals().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_register_packer()

void script_register_packer ( lua_State * L,
const char * regname,
PackInFunc fin,
PackOutFunc fout )

References CUSTOM_RIDX_METATABLE_MAP, FATAL_ERROR_IF, g_packers, and g_packers_lock.

Referenced by LuaItemStack::Register(), LuaPerlinNoise::Register(), LuaPerlinNoiseMap::Register(), and LuaVoxelManip::Register().

+ Here is the caller graph for this function:

◆ script_unpack()

void script_unpack ( lua_State * L,
PackedValue * pv )

References PackedValue::contains_userdata, find_packer(), get_known_lua_metatables(), PackedValue::i, INSTR_POP, INSTR_PUSHREF, INSTR_SETMETATABLE, INSTR_SETTABLE, sanity_check, and uses_sdata().

Referenced by EmergeScripting::EmergeScripting(), ServerScripting::InitializeAsync(), ModApiIPC::l_ipc_cas(), ModApiIPC::l_ipc_get(), ModApiServer::l_serialize_roundtrip(), AsyncWorkerThread::run(), and AsyncEngine::stepJobResults().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ suitable_key()

static bool suitable_key ( lua_State * L,
int idx )
inlinestatic

References idx, S32_MAX, and S32_MIN.

Referenced by pack_inner().

+ Here is the caller graph for this function:

◆ uses_sdata()

static bool uses_sdata ( int type)
inlinestatic

Referenced by can_set_into(), script_dump_packed(), and script_unpack().

+ Here is the caller graph for this function:

◆ uses_union()

static bool uses_union ( int type)
inlinestatic

Referenced by can_set_into().

+ Here is the caller graph for this function:

Variable Documentation

◆ g_packers

std::unordered_map<std::string, Packer> g_packers
static

◆ g_packers_lock

std::mutex g_packers_lock
static