Luanti 5.19.0-dev
Loading...
Searching...
No Matches
c_packer.cpp File Reference
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <unordered_map>
#include "c_packer.h"
#include "c_internal.h"
#include "log.h"
#include "debug.h"
#include "threading/mutex_auto_lock.h"
#include "common/c_types.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< PackedInstr > record_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< PackedInstr > pack_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.
PackedValue * script_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()

int absidx ( lua_State * L,
int idx )
inlinestatic

References absidx(), and idx.

Referenced by absidx(), pack_inner(), and script_pack().

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

◆ can_set_into()

bool can_set_into ( int ktype,
int vtype )
inlinestatic

References can_set_into(), uses_sdata(), and uses_union().

Referenced by can_set_into(), and pack_inner().

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

◆ emplace()

auto emplace ( PackedValue & pv,
s16 type )
inlinestatic

Append instruction to end.

Parameters
pvtarget
typeinstruction type
Returns
reference to instruction

References emplace(), PackedValue::i, and INSTR_POP.

Referenced by emplace(), pack_inner(), and record_object().

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

◆ find_packer() [1/2]

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 find_packer(), g_packers, and g_packers_lock.

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

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

◆ find_packer() [2/2]

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()

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.

References get_known_lua_metatables().

Referenced by get_known_lua_metatables(), pack_inner(), and script_unpack().

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

◆ pack_inner()

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 LuaError::LuaError(), absidx(), can_set_into(), PackedValue::contains_userdata, dump_function_to_string(), 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()

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, record_object(), and S32_MAX.

Referenced by pack_inner(), and record_object().

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, script_dump_packed(), and uses_sdata().

Referenced by ModApiServer::l_serialize_roundtrip(), and script_dump_packed().

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 PackedValue::PackedValue(), absidx(), idx, pack_inner(), and script_pack().

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

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, g_packers_lock, and script_register_packer().

Referenced by LuaItemStack::Register(), LuaValueNoise::Register(), LuaValueNoiseMap::Register(), LuaVoxelManip::Register(), and script_register_packer().

Here is the call graph for this function:
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, script_unpack(), and uses_sdata().

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

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

◆ suitable_key()

bool suitable_key ( lua_State * L,
int idx )
inlinestatic

References idx, S32_MAX, S32_MIN, and suitable_key().

Referenced by pack_inner(), and suitable_key().

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

◆ uses_sdata()

bool uses_sdata ( int type)
inlinestatic

References uses_sdata().

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

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

◆ uses_union()

bool uses_union ( int type)
inlinestatic

References uses_union().

Referenced by can_set_into(), and uses_union().

Here is the call graph for this function:
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