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/handlers.h"
11 #include "upb/msg.h"
12 #include "upb/msgfactory.h"
13 
14 /* Lua 5.1/5.2 compatibility code. */
15 #if LUA_VERSION_NUM == 501
16 
17 #define lua_rawlen lua_objlen
18 
19 /* Lua >= 5.2's getuservalue/setuservalue functions do not exist in prior
20  * versions but the older function lua_getfenv() can provide 100% of its
21  * capabilities (the reverse is not true). */
22 #define lua_getuservalue(L, index) lua_getfenv(L, index)
23 #define lua_setuservalue(L, index) lua_setfenv(L, index)
24 
25 void *luaL_testudata(lua_State *L, int ud, const char *tname);
26 
27 #define lupb_setfuncs(L, l) luaL_register(L, NULL, l)
28 
29 #elif LUA_VERSION_NUM == 502
30 
31 int luaL_typerror(lua_State *L, int narg, const char *tname);
32 
33 #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)
34 
35 #else
36 #error Only Lua 5.1 and 5.2 are supported
37 #endif
38 
39 #define lupb_assert(L, predicate) \
40   if (!(predicate))               \
41     luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__);
42 
43 /* Function for initializing the core library.  This function is idempotent,
44  * and should be called at least once before calling any of the functions that
45  * construct core upb types. */
46 int luaopen_upb(lua_State *L);
47 
48 /* Gets or creates a package table for a C module that is uniquely identified by
49  * "ptr".  The easiest way to supply a unique "ptr" is to pass the address of a
50  * static variable private in the module's .c file.
51  *
52  * If this module has already been registered in this lua_State, pushes it and
53  * returns true.
54  *
55  * Otherwise, creates a new module table for this module with the given name,
56  * pushes it, and registers the given top-level functions in it.  It also sets
57  * it as a global variable, but only if the current version of Lua expects that
58  * (ie Lua 5.1/LuaJIT).
59  *
60  * If "false" is returned, the caller is guaranteed that this lib has not been
61  * registered in this Lua state before (regardless of any funny business the
62  * user might have done to the global state), so the caller can safely perform
63  * one-time initialization. */
64 bool lupb_openlib(lua_State *L, void *ptr, const char *name,
65                   const luaL_Reg *funcs);
66 
67 /* Custom check/push functions.  Unlike the Lua equivalents, they are pinned to
68  * specific types (instead of lua_Number, etc), and do not allow any implicit
69  * conversion or data loss. */
70 int64_t lupb_checkint64(lua_State *L, int narg);
71 int32_t lupb_checkint32(lua_State *L, int narg);
72 uint64_t lupb_checkuint64(lua_State *L, int narg);
73 uint32_t lupb_checkuint32(lua_State *L, int narg);
74 double lupb_checkdouble(lua_State *L, int narg);
75 float lupb_checkfloat(lua_State *L, int narg);
76 bool lupb_checkbool(lua_State *L, int narg);
77 const char *lupb_checkstring(lua_State *L, int narg, size_t *len);
78 const char *lupb_checkname(lua_State *L, int narg);
79 
80 void lupb_pushint64(lua_State *L, int64_t val);
81 void lupb_pushint32(lua_State *L, int32_t val);
82 void lupb_pushuint64(lua_State *L, uint64_t val);
83 void lupb_pushuint32(lua_State *L, uint32_t val);
84 void lupb_pushdouble(lua_State *L, double val);
85 void lupb_pushfloat(lua_State *L, float val);
86 
87 /* Registers a type with the given name, methods, and metamethods. */
88 void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m,
89                         const luaL_Reg *mm);
90 
91 /* Checks the given upb_status and throws a Lua error if it is not ok. */
92 void lupb_checkstatus(lua_State *L, upb_status *s);
93 
94 
95 /** From def.c. ***************************************************************/
96 
97 upb_fieldtype_t lupb_checkfieldtype(lua_State *L, int narg);
98 
99 const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg);
100 const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg);
101 const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg);
102 upb_symtab *lupb_symtab_check(lua_State *L, int narg);
103 
104 void lupb_def_registertypes(lua_State *L);
105 
106 
107 /** From msg.c. ***************************************************************/
108 
109 struct lupb_msgclass;
110 typedef struct lupb_msgclass lupb_msgclass;
111 
112 upb_arena *lupb_arena_check(lua_State *L, int narg);
113 int lupb_arena_new(lua_State *L);
114 upb_arena *lupb_arena_get(lua_State *L);
115 int lupb_msg_pushref(lua_State *L, int msgclass, void *msg);
116 const upb_msg *lupb_msg_checkmsg(lua_State *L, int narg,
117                                  const lupb_msgclass *lmsgclass);
118 upb_msg *lupb_msg_checkmsg2(lua_State *L, int narg,
119                             const upb_msglayout **layout);
120 
121 const lupb_msgclass *lupb_msgclass_check(lua_State *L, int narg);
122 const upb_msglayout *lupb_msgclass_getlayout(lua_State *L, int narg);
123 const upb_msgdef *lupb_msgclass_getmsgdef(const lupb_msgclass *lmsgclass);
124 upb_msgfactory *lupb_msgclass_getfactory(const lupb_msgclass *lmsgclass);
125 void lupb_msg_registertypes(lua_State *L);
126 
127 #endif  /* UPB_LUA_UPB_H_ */
128