summaryrefslogtreecommitdiff
path: root/upb/bindings/lua/upb.h
blob: 820a3174414edff6da0f4446696859fb100809bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
** Shared definitions for upb Lua modules.
*/

#ifndef UPB_LUA_UPB_H_
#define UPB_LUA_UPB_H_

#include "lauxlib.h"
#include "upb/def.h"
#include "upb/handlers.h"
#include "upb/msg.h"
#include "upb/msgfactory.h"

/* Lua 5.1/5.2 compatibility code. */
#if LUA_VERSION_NUM == 501

#define lua_rawlen lua_objlen

/* Lua >= 5.2's getuservalue/setuservalue functions do not exist in prior
 * versions but the older function lua_getfenv() can provide 100% of its
 * capabilities (the reverse is not true). */
#define lua_getuservalue(L, index) lua_getfenv(L, index)
#define lua_setuservalue(L, index) lua_setfenv(L, index)

void *luaL_testudata(lua_State *L, int ud, const char *tname);

#define lupb_setfuncs(L, l) luaL_register(L, NULL, l)

#elif LUA_VERSION_NUM == 502

int luaL_typerror(lua_State *L, int narg, const char *tname);

#define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)

#else
#error Only Lua 5.1 and 5.2 are supported
#endif

#define lupb_assert(L, predicate) \
  if (!(predicate))               \
    luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__);

/* Function for initializing the core library.  This function is idempotent,
 * and should be called at least once before calling any of the functions that
 * construct core upb types. */
int luaopen_upb(lua_State *L);

/* Gets or creates a package table for a C module that is uniquely identified by
 * "ptr".  The easiest way to supply a unique "ptr" is to pass the address of a
 * static variable private in the module's .c file.
 *
 * If this module has already been registered in this lua_State, pushes it and
 * returns true.
 *
 * Otherwise, creates a new module table for this module with the given name,
 * pushes it, and registers the given top-level functions in it.  It also sets
 * it as a global variable, but only if the current version of Lua expects that
 * (ie Lua 5.1/LuaJIT).
 *
 * If "false" is returned, the caller is guaranteed that this lib has not been
 * registered in this Lua state before (regardless of any funny business the
 * user might have done to the global state), so the caller can safely perform
 * one-time initialization. */
bool lupb_openlib(lua_State *L, void *ptr, const char *name,
                  const luaL_Reg *funcs);

/* Custom check/push functions.  Unlike the Lua equivalents, they are pinned to
 * specific types (instead of lua_Number, etc), and do not allow any implicit
 * conversion or data loss. */
int64_t lupb_checkint64(lua_State *L, int narg);
int32_t lupb_checkint32(lua_State *L, int narg);
uint64_t lupb_checkuint64(lua_State *L, int narg);
uint32_t lupb_checkuint32(lua_State *L, int narg);
double lupb_checkdouble(lua_State *L, int narg);
float lupb_checkfloat(lua_State *L, int narg);
bool lupb_checkbool(lua_State *L, int narg);
const char *lupb_checkstring(lua_State *L, int narg, size_t *len);
const char *lupb_checkname(lua_State *L, int narg);

void lupb_pushint64(lua_State *L, int64_t val);
void lupb_pushint32(lua_State *L, int32_t val);
void lupb_pushuint64(lua_State *L, uint64_t val);
void lupb_pushuint32(lua_State *L, uint32_t val);
void lupb_pushdouble(lua_State *L, double val);
void lupb_pushfloat(lua_State *L, float val);

/* Registers a type with the given name, methods, and metamethods.
 * If "refcount_gc" is true, adds a __gc metamethod that does an unref.
 * Refcounted types must be allocated with lupb_refcounted_push[new]wrapper. */
void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m,
                        const luaL_Reg *mm);

/* Checks the given upb_status and throws a Lua error if it is not ok. */
void lupb_checkstatus(lua_State *L, upb_status *s);


/** From def.c. ***************************************************************/

upb_fieldtype_t lupb_checkfieldtype(lua_State *L, int narg);

void *lupb_refcounted_check(lua_State *L, int narg, const char *type);
const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg);
const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg);
const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg);
upb_symtab *lupb_symtab_check(lua_State *L, int narg);

void lupb_refcounted_pushnewrapper(lua_State *L, const upb_refcounted *obj,
                                   const char *type, const void *ref_donor);
bool lupb_def_pushwrapper(lua_State *L, const upb_def *def,
                          const void *ref_donor);
void lupb_def_pushnewrapper(lua_State *L, const upb_def *def,
                            const void *ref_donor);
void lupb_msgdef_pushwrapper(lua_State *L, const upb_msgdef *m,
                             const void *ref_donor);
void lupb_symtab_pushwrapper(lua_State *L, const upb_symtab *s,
                             const void *ref_donor);
void lupb_symtab_pushnewrapper(lua_State *L, const upb_symtab *s,
                               const void *ref_donor);

void lupb_def_registertypes(lua_State *L);

int lupb_refcounted_gc(lua_State *L);


/** From msg.c. ***************************************************************/

struct lupb_msgclass;
typedef struct lupb_msgclass lupb_msgclass;

upb_arena *lupb_arena_check(lua_State *L, int narg);
int lupb_arena_new(lua_State *L);
int lupb_msg_pushref(lua_State *L, int msgclass, void *msg);
const upb_msg *lupb_msg_checkmsg(lua_State *L, int narg,
                                 const lupb_msgclass *lmsgclass);
upb_msg *lupb_msg_checkmsg2(lua_State *L, int narg,
                            const upb_msglayout **layout);

const lupb_msgclass *lupb_msgclass_check(lua_State *L, int narg);
const upb_msglayout *lupb_msgclass_getlayout(lua_State *L, int narg);
const upb_msgdef *lupb_msgclass_getmsgdef(const lupb_msgclass *lmsgclass);
upb_msgfactory *lupb_msgclass_getfactory(const lupb_msgclass *lmsgclass);
void lupb_msg_registertypes(lua_State *L);

#endif  /* UPB_LUA_UPB_H_ */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback