• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Shared definitions for upb Lua modules.
3 */
4 
5 #ifndef UPB_LUA_UPB_H_
6 #define UPB_LUA_UPB_H_
7 
8 #include "lauxlib.h"
9 #include "upb/def.h"
10 #include "upb/msg.h"
11 
12 /* Lua changes its API in incompatible ways in every minor release.
13  * This is some shim code to paper over the differences. */
14 
15 #if LUA_VERSION_NUM == 501
16 #define lua_rawlen lua_objlen
17 #define lua_setuservalue(L, idx) lua_setfenv(L, idx)
18 #define lua_getuservalue(L, idx) lua_getfenv(L, idx)
19 #define lupb_setfuncs(L, l) luaL_register(L, NULL, l)
20 #elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504
21 #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)
22 #else
23 #error Only Lua 5.1-5.4 are supported
24 #endif
25 
26 /* Create a new userdata with the given type and |n| uservals, which are popped
27  * from the stack to initialize the userdata. */
28 void *lupb_newuserdata(lua_State *L, size_t size, int n, const char *type);
29 
30 #if LUA_VERSION_NUM < 504
31 /* Polyfills for this Lua 5.4 function.  Pushes userval |n| for the userdata at
32  * |index|. */
33 int lua_setiuservalue(lua_State *L, int index, int n);
34 int lua_getiuservalue(lua_State *L, int index, int n);
35 #endif
36 
37 /* Registers a type with the given name, methods, and metamethods. */
38 void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m,
39                         const luaL_Reg *mm);
40 
41 /* Checks the given upb_status and throws a Lua error if it is not ok. */
42 void lupb_checkstatus(lua_State *L, upb_status *s);
43 
44 int luaopen_lupb(lua_State *L);
45 
46 /* C <-> Lua value conversions. ***********************************************/
47 
48 /* Custom check/push functions.  Unlike the Lua equivalents, they are pinned to
49  * specific C types (instead of lua_Number, etc), and do not allow any implicit
50  * conversion or data loss. */
51 int64_t lupb_checkint64(lua_State *L, int narg);
52 int32_t lupb_checkint32(lua_State *L, int narg);
53 uint64_t lupb_checkuint64(lua_State *L, int narg);
54 uint32_t lupb_checkuint32(lua_State *L, int narg);
55 double lupb_checkdouble(lua_State *L, int narg);
56 float lupb_checkfloat(lua_State *L, int narg);
57 bool lupb_checkbool(lua_State *L, int narg);
58 const char *lupb_checkstring(lua_State *L, int narg, size_t *len);
59 const char *lupb_checkname(lua_State *L, int narg);
60 
61 void lupb_pushint64(lua_State *L, int64_t val);
62 void lupb_pushint32(lua_State *L, int32_t val);
63 void lupb_pushuint64(lua_State *L, uint64_t val);
64 void lupb_pushuint32(lua_State *L, uint32_t val);
65 
66 /** From def.c. ***************************************************************/
67 
68 const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg);
69 const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg);
70 const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg);
71 upb_symtab *lupb_symtab_check(lua_State *L, int narg);
72 void lupb_msgdef_pushsubmsgdef(lua_State *L, const upb_fielddef *f);
73 
74 void lupb_def_registertypes(lua_State *L);
75 
76 /** From msg.c. ***************************************************************/
77 
78 int lupb_msgdef_call(lua_State *L);
79 upb_arena *lupb_arena_pushnew(lua_State *L);
80 
81 void lupb_msg_registertypes(lua_State *L);
82 
83 #define lupb_assert(L, predicate) \
84   if (!(predicate))               \
85     luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__);
86 
87 #define LUPB_UNUSED(var) (void)var
88 
89 #if defined(__GNUC__) || defined(__clang__)
90 #define LUPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
91 #else
92 #define LUPB_UNREACHABLE() do { assert(0); } while(0)
93 #endif
94 
95 
96 #endif  /* UPB_LUA_UPB_H_ */
97