Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
c_packer.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2022 sfan5 <sfan5@live.de>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <string>
23#include <vector>
24#include "irrlichttypes.h"
25#include "util/basic_macros.h"
26
27extern "C" {
28#include <lua.h>
29}
30
31/*
32 This file defines an in-memory representation of Lua objects including
33 support for functions and userdata. It it used to move data between Lua
34 states and cannot be used for persistence or network transfer.
35*/
36
37#define INSTR_SETTABLE (-10)
38#define INSTR_POP (-11)
39#define INSTR_PUSHREF (-12)
40#define INSTR_SETMETATABLE (-13)
41
46{
47 s16 type; // LUA_T* or INSTR_*
48 u16 set_into; // set into table on stack
49 bool keep_ref; // referenced later by INSTR_PUSHREF?
50 bool pop; // remove from stack?
51 // Note: the remaining members are named by type, not usage
52 union {
53 bool bdata; // boolean: value
54 lua_Number ndata; // number: value
55 struct {
56 u16 uidata1, uidata2; // table: narr | nrec
57 };
58 struct {
59 /*
60 SETTABLE: key index | value index
61 POP: indices to remove
62 PUSHREF: index of referenced instr | unused
63 otherwise w/ set_into: numeric key | unused
64 */
66 };
67 void *ptrdata; // userdata: implementation defined
68 };
69 /*
70 - string: value
71 - function: buffer
72 - w/ set_into: string key (no null bytes!)
73 - userdata: name in registry
74 - INSTR_SETMETATABLE: name of the metatable
75 */
76 std::string sdata;
77
78 PackedInstr() : type(0), set_into(0), keep_ref(false), pop(false) {}
79};
80
87{
88 std::vector<PackedInstr> i;
89 // Indicates whether there are any userdata pointers that need to be deallocated
90 bool contains_userdata = false;
91
92 PackedValue() = default;
94
96
98};
99
100/*
101 * Packing callback: Turns a Lua value at given index into a void*
102 */
103typedef void *(*PackInFunc)(lua_State *L, int idx);
104/*
105 * Unpacking callback: Turns a void* back into the Lua value (left on top of stack)
106 *
107 * Note that this function must take ownership of the pointer, so make sure
108 * to free or keep the memory.
109 * `L` can be nullptr to indicate that data should just be discarded.
110 */
111typedef void (*PackOutFunc)(lua_State *L, void *ptr);
112/*
113 * Register a packable type with the name of its metatable.
114 *
115 * Even though the callbacks are global this must be called for every Lua state
116 * that supports objects of this type.
117 * This function is thread-safe.
118 */
119void script_register_packer(lua_State *L, const char *regname,
120 PackInFunc fin, PackOutFunc fout);
121
122// Pack a Lua value
123PackedValue *script_pack(lua_State *L, int idx);
124// Unpack a Lua value (left on top of stack)
125// Note that this may modify the PackedValue, reusability is not guaranteed!
126void script_unpack(lua_State *L, PackedValue *val);
127
128// Dump contents of PackedValue to stdout for debugging
129void script_dump_packed(const PackedValue *val);
#define DISABLE_CLASS_COPY(C)
Definition: basic_macros.h:35
#define ALLOW_CLASS_MOVE(C)
Definition: basic_macros.h:41
void *(* PackInFunc)(lua_State *L, int idx)
Definition: c_packer.h:103
void script_unpack(lua_State *L, PackedValue *val)
Definition: c_packer.cpp:508
PackedValue * script_pack(lua_State *L, int idx)
Definition: c_packer.cpp:491
void script_register_packer(lua_State *L, const char *regname, PackInFunc fin, PackOutFunc fout)
Definition: c_packer.cpp:199
void script_dump_packed(const PackedValue *val)
Definition: c_packer.cpp:645
void(* PackOutFunc)(lua_State *L, void *ptr)
Definition: c_packer.h:111
#define idx(x, y)
Definition: noise.cpp:552
Represents a single instruction that pushes a new value or operates with existing ones.
Definition: c_packer.h:46
s16 type
Definition: c_packer.h:47
bool keep_ref
Definition: c_packer.h:49
u16 uidata1
Definition: c_packer.h:56
std::string sdata
Definition: c_packer.h:76
void * ptrdata
Definition: c_packer.h:67
bool bdata
Definition: c_packer.h:53
lua_Number ndata
Definition: c_packer.h:54
u16 uidata2
Definition: c_packer.h:56
s32 sidata2
Definition: c_packer.h:65
u16 set_into
Definition: c_packer.h:48
PackedInstr()
Definition: c_packer.h:78
s32 sidata1
Definition: c_packer.h:65
bool pop
Definition: c_packer.h:50
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:87
PackedValue()=default
bool contains_userdata
Definition: c_packer.h:90
~PackedValue()
Definition: c_packer.cpp:624
std::vector< PackedInstr > i
Definition: c_packer.h:88