upb/upb/bindings/lua/upb.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of Google LLC nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * Shared definitions for upb Lua modules.
30  */
31 
32 #ifndef UPB_LUA_UPB_H_
33 #define UPB_LUA_UPB_H_
34 
35 #include "lauxlib.h"
36 #include "upb/def.h"
37 #include "upb/msg.h"
38 #include "upb/reflection.h"
39 
40 /* Lua changes its API in incompatible ways in every minor release.
41  * This is some shim code to paper over the differences. */
42 
43 #if LUA_VERSION_NUM == 501
44 #define lua_rawlen lua_objlen
45 #define lua_setuservalue(L, idx) lua_setfenv(L, idx)
46 #define lua_getuservalue(L, idx) lua_getfenv(L, idx)
47 #define lupb_setfuncs(L, l) luaL_register(L, NULL, l)
48 #elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504
49 #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)
50 #else
51 #error Only Lua 5.1-5.4 are supported
52 #endif
53 
54 /* Create a new userdata with the given type and |n| uservals, which are popped
55  * from the stack to initialize the userdata. */
56 void* lupb_newuserdata(lua_State* L, size_t size, int n, const char* type);
57 
58 #if LUA_VERSION_NUM < 504
59 /* Polyfills for this Lua 5.4 function. Pushes userval |n| for the userdata at
60  * |index|. */
61 int lua_setiuservalue(lua_State* L, int index, int n);
62 int lua_getiuservalue(lua_State* L, int index, int n);
63 #endif
64 
65 /* Registers a type with the given name, methods, and metamethods. */
66 void lupb_register_type(lua_State* L, const char* name, const luaL_Reg* m,
67  const luaL_Reg* mm);
68 
69 /* Checks the given upb_Status and throws a Lua error if it is not ok. */
70 void lupb_checkstatus(lua_State* L, upb_Status* s);
71 
72 int luaopen_lupb(lua_State* L);
73 
74 /* C <-> Lua value conversions. ***********************************************/
75 
76 /* Custom check/push functions. Unlike the Lua equivalents, they are pinned to
77  * specific C types (instead of lua_Number, etc), and do not allow any implicit
78  * conversion or data loss. */
79 int64_t lupb_checkint64(lua_State* L, int narg);
80 int32_t lupb_checkint32(lua_State* L, int narg);
81 uint64_t lupb_checkuint64(lua_State* L, int narg);
82 uint32_t lupb_checkuint32(lua_State* L, int narg);
83 double lupb_checkdouble(lua_State* L, int narg);
84 float lupb_checkfloat(lua_State* L, int narg);
85 bool lupb_checkbool(lua_State* L, int narg);
86 const char* lupb_checkstring(lua_State* L, int narg, size_t* len);
87 const char* lupb_checkname(lua_State* L, int narg);
88 
89 void lupb_pushint64(lua_State* L, int64_t val);
90 void lupb_pushint32(lua_State* L, int32_t val);
91 void lupb_pushuint64(lua_State* L, uint64_t val);
92 void lupb_pushuint32(lua_State* L, uint32_t val);
93 
96 const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg);
97 const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg);
98 const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg);
99 upb_DefPool* lupb_DefPool_check(lua_State* L, int narg);
100 void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f);
101 
102 void lupb_def_registertypes(lua_State* L);
103 
106 void lupb_pushmsgval(lua_State* L, int container, upb_CType type,
107  upb_MessageValue val);
108 int lupb_MessageDef_call(lua_State* L);
109 upb_Arena* lupb_Arena_pushnew(lua_State* L);
110 
111 void lupb_msg_registertypes(lua_State* L);
112 
113 #define lupb_assert(L, predicate) \
114  if (!(predicate)) \
115  luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__);
116 
117 #define LUPB_UNUSED(var) (void)var
118 
119 #if defined(__GNUC__) || defined(__clang__)
120 #define LUPB_UNREACHABLE() \
121  do { \
122  assert(0); \
123  __builtin_unreachable(); \
124  } while (0)
125 #else
126 #define LUPB_UNREACHABLE() \
127  do { \
128  assert(0); \
129  } while (0)
130 #endif
131 
132 #endif /* UPB_LUA_UPB_H_ */
lupb_checkstatus
void lupb_checkstatus(lua_State *L, upb_Status *s)
Definition: upb/upb/bindings/lua/upb.c:72
lupb_pushmsgval
void lupb_pushmsgval(lua_State *L, int container, upb_CType type, upb_MessageValue val)
Definition: bindings/lua/msg.c:319
upb_CType
upb_CType
Definition: upb/upb/upb.h:286
lupb_MessageDef_pushsubmsgdef
void lupb_MessageDef_pushsubmsgdef(lua_State *L, const upb_FieldDef *f)
Definition: upb/upb/bindings/lua/def.c:89
lupb_checkname
const char * lupb_checkname(lua_State *L, int narg)
upb_MessageDef
Definition: upb/upb/def.c:100
lupb_checkint32
int32_t lupb_checkint32(lua_State *L, int narg)
lupb_Arena_pushnew
upb_Arena * lupb_Arena_pushnew(lua_State *L)
Definition: bindings/lua/msg.c:202
lupb_checkuint64
uint64_t lupb_checkuint64(lua_State *L, int narg)
lupb_EnumDef_check
const upb_EnumDef * lupb_EnumDef_check(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/def.c:522
setup.name
name
Definition: setup.py:542
lupb_DefPool_check
upb_DefPool * lupb_DefPool_check(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/def.c:712
msg.h
lupb_pushuint32
void lupb_pushuint32(lua_State *L, uint32_t val)
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
upb_MessageValue
Definition: upb/upb/reflection.h:40
lupb_pushuint64
void lupb_pushuint64(lua_State *L, uint64_t val)
lua_getiuservalue
int lua_getiuservalue(lua_State *L, int index, int n)
Definition: upb/upb/bindings/lua/upb.c:106
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
lupb_MessageDef_call
int lupb_MessageDef_call(lua_State *L)
Definition: bindings/lua/msg.c:789
lupb_pushint32
void lupb_pushint32(lua_State *L, int32_t val)
lupb_def_registertypes
void lupb_def_registertypes(lua_State *L)
Definition: upb/upb/bindings/lua/def.c:889
lupb_pushint64
void lupb_pushint64(lua_State *L, int64_t val)
lupb_checkstring
const char * lupb_checkstring(lua_State *L, int narg, size_t *len)
Definition: upb/upb/bindings/lua/upb.c:172
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
lupb_checkfloat
float lupb_checkfloat(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/upb.c:227
lupb_checkint64
int64_t lupb_checkint64(lua_State *L, int narg)
lua_setiuservalue
int lua_setiuservalue(lua_State *L, int index, int n)
Definition: upb/upb/bindings/lua/upb.c:98
reflection.h
lupb_checkbool
bool lupb_checkbool(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/upb.c:163
lupb_msg_registertypes
void lupb_msg_registertypes(lua_State *L)
Definition: bindings/lua/msg.c:1097
upb_Status
Definition: upb/upb/upb.h:52
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
upb_FieldDef
Definition: upb/upb/def.c:56
lupb_MessageDef_check
const upb_MessageDef * lupb_MessageDef_check(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/def.c:322
lupb_register_type
void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m, const luaL_Reg *mm)
Definition: upb/upb/bindings/lua/upb.c:132
def.h
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
lupb_checkdouble
double lupb_checkdouble(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/upb.c:215
lupb_checkuint32
uint32_t lupb_checkuint32(lua_State *L, int narg)
upb_EnumDef
Definition: upb/upb/def.c:134
lupb_newuserdata
void * lupb_newuserdata(lua_State *L, size_t size, int n, const char *type)
Definition: upb/upb/bindings/lua/upb.c:80
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
regress.m
m
Definition: regress/regress.py:25
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
upb_DefPool
Definition: upb/upb/def.c:217
lupb_FieldDef_check
const upb_FieldDef * lupb_FieldDef_check(lua_State *L, int narg)
Definition: upb/upb/bindings/lua/def.c:98
upb_Arena
Definition: upb_internal.h:36
luaopen_lupb
int luaopen_lupb(lua_State *L)
Definition: upb/upb/bindings/lua/upb.c:241
container
static struct async_container * container
Definition: benchmark-million-async.c:33


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:48