Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
c_packer.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2022 sfan5 <sfan5@live.de>
4
5#pragma once
6
7#include <string>
8#include <vector>
9#include "irrlichttypes.h"
10#include "util/basic_macros.h"
11
12extern "C" {
13#include <lua.h>
14}
15
16/*
17 This file defines an in-memory representation of Lua objects including
18 support for functions and userdata. It it used to move data between Lua
19 states and cannot be used for persistence or network transfer.
20*/
21
22#define INSTR_SETTABLE (-10)
23#define INSTR_POP (-11)
24#define INSTR_PUSHREF (-12)
25#define INSTR_SETMETATABLE (-13)
26
31{
32 s16 type; // LUA_T* or INSTR_*
33 u16 set_into; // set into table on stack
34 bool keep_ref; // referenced later by INSTR_PUSHREF?
35 bool pop; // remove from stack?
36 // Note: the remaining members are named by type, not usage
37 union {
38 bool bdata; // boolean: value
39 lua_Number ndata; // number: value
40 struct {
41 u16 uidata1, uidata2; // table: narr | nrec
42 };
43 struct {
44 /*
45 SETTABLE: key index | value index
46 POP: indices to remove
47 PUSHREF: index of referenced instr | unused
48 otherwise w/ set_into: numeric key | unused
49 */
51 };
52 void *ptrdata; // userdata: implementation defined
53 };
54 /*
55 - string: value
56 - function: buffer
57 - w/ set_into: string key (no null bytes!)
58 - userdata: name in registry
59 - INSTR_SETMETATABLE: name of the metatable
60 */
61 std::string sdata;
62
63 PackedInstr() : type(0), set_into(0), keep_ref(false), pop(false) {}
64};
65
72{
73 std::vector<PackedInstr> i;
74 // Indicates whether there are any userdata pointers that need to be deallocated
75 bool contains_userdata = false;
76
77 PackedValue() = default;
79
81
83};
84
85/*
86 * Packing callback: Turns a Lua value at given index into a void*
87 */
88typedef void *(*PackInFunc)(lua_State *L, int idx);
89/*
90 * Unpacking callback: Turns a void* back into the Lua value (left on top of stack)
91 *
92 * Note that this function must take ownership of the pointer, so make sure
93 * to free or keep the memory.
94 * `L` can be nullptr to indicate that data should just be discarded.
95 */
96typedef void (*PackOutFunc)(lua_State *L, void *ptr);
97/*
98 * Register a packable type with the name of its metatable.
99 *
100 * Even though the callbacks are global this must be called for every Lua state
101 * that supports objects of this type.
102 * This function is thread-safe.
103 */
104void script_register_packer(lua_State *L, const char *regname,
105 PackInFunc fin, PackOutFunc fout);
106
107// Pack a Lua value
108PackedValue *script_pack(lua_State *L, int idx);
109// Unpack a Lua value (left on top of stack)
110// Note that this may modify the PackedValue, reusability is not guaranteed!
111void script_unpack(lua_State *L, PackedValue *val);
112
113// Dump contents of PackedValue to stdout for debugging
114void script_dump_packed(const PackedValue *val);
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
#define ALLOW_CLASS_MOVE(C)
Definition basic_macros.h:32
void *(* PackInFunc)(lua_State *L, int idx)
Definition c_packer.h:88
void script_unpack(lua_State *L, PackedValue *val)
Definition c_packer.cpp:493
PackedValue * script_pack(lua_State *L, int idx)
Definition c_packer.cpp:476
void script_register_packer(lua_State *L, const char *regname, PackInFunc fin, PackOutFunc fout)
Definition c_packer.cpp:184
void script_dump_packed(const PackedValue *val)
Definition c_packer.cpp:631
void(* PackOutFunc)(lua_State *L, void *ptr)
Definition c_packer.h:96
#define idx(x, y)
Definition noise.cpp:490
Represents a single instruction that pushes a new value or operates with existing ones.
Definition c_packer.h:31
s16 type
Definition c_packer.h:32
bool keep_ref
Definition c_packer.h:34
u16 uidata1
Definition c_packer.h:41
std::string sdata
Definition c_packer.h:61
void * ptrdata
Definition c_packer.h:52
bool bdata
Definition c_packer.h:38
lua_Number ndata
Definition c_packer.h:39
u16 uidata2
Definition c_packer.h:41
s32 sidata2
Definition c_packer.h:50
u16 set_into
Definition c_packer.h:33
PackedInstr()
Definition c_packer.h:63
s32 sidata1
Definition c_packer.h:50
bool pop
Definition c_packer.h:35
A packed value can be a primitive like a string or number but also a table including all of its conte...
Definition c_packer.h:72
PackedValue()=default
bool contains_userdata
Definition c_packer.h:75
~PackedValue()
Definition c_packer.cpp:610
std::vector< PackedInstr > i
Definition c_packer.h:73