1#ifndef KEPLER_PROJECT_COMPAT53_H_
2#define KEPLER_PROJECT_COMPAT53_H_
7#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
13#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
17#ifndef COMPAT53_PREFIX
22# define COMPAT53_PREFIX kp_compat53
26# if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE
27# if defined(__GNUC__) || defined(__clang__)
28# define COMPAT53_API __attribute__((__unused__)) static inline
30# define COMPAT53_API static inline
34# define COMPAT53_API extern
39#define COMPAT53_CONCAT_HELPER(a, b) a##b
40#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
45#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
93#if !defined(LUA_ERRGCMM)
99# define LUA_ERRGCMM (LUA_ERRERR + 2)
102#if !defined(MOONJIT_VERSION)
103typedef size_t lua_Unsigned;
106typedef struct luaL_Buffer_53 {
113#define luaL_Buffer luaL_Buffer_53
119typedef struct luaL_Stream {
123#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
126#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
129#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
130COMPAT53_API int lua_compare(lua_State *L,
int idx1,
int idx2,
int op);
132#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
133COMPAT53_API void lua_copy(lua_State *L,
int from,
int to);
135#define lua_getuservalue(L, i) \
136 (lua_getfenv((L), (i)), lua_type((L), -1))
137#define lua_setuservalue(L, i) \
138 (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
140#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
143#define lua_pushstring(L, s) \
144 (lua_pushstring((L), (s)), lua_tostring((L), -1))
146#define lua_pushlstring(L, s, len) \
147 ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
149#ifndef luaL_newlibtable
150# define luaL_newlibtable(L, l) \
151 (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
154# define luaL_newlib(L, l) \
155 (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
158#ifndef lua_pushglobaltable
159# define lua_pushglobaltable(L) \
160 lua_pushvalue((L), LUA_GLOBALSINDEX)
162#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
163COMPAT53_API int lua_rawgetp(lua_State *L,
int i,
const void *p);
165#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
166COMPAT53_API void lua_rawsetp(lua_State *L,
int i,
const void *p);
168#define lua_rawlen(L, i) lua_objlen((L), (i))
170#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
172#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
173COMPAT53_API lua_Number lua_tonumberx(lua_State *L,
int i,
int *isnum);
175#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
178#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
179COMPAT53_API int lua_load(lua_State *L, lua_Reader reader,
void *data,
const char* source,
const char* mode);
181#define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
182COMPAT53_API int luaL_loadfilex(lua_State *L,
const char *filename,
const char *mode);
184#define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx)
185COMPAT53_API int luaL_loadbufferx(lua_State *L,
const char *buff,
size_t sz,
const char *name,
const char *mode);
187#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
188COMPAT53_API void luaL_checkstack(lua_State *L,
int sp,
const char *msg);
190#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
191COMPAT53_API int luaL_getsubtable(lua_State* L,
int i,
const char *name);
193#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
196#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
197COMPAT53_API void luaL_setfuncs(lua_State *L,
const luaL_Reg *l,
int nup);
199#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
200COMPAT53_API void luaL_setmetatable(lua_State *L,
const char *tname);
202#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
203COMPAT53_API void *luaL_testudata(lua_State *L,
int i,
const char *tname);
205#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
206COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1,
const char *msg,
int level);
208#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
209COMPAT53_API int luaL_fileresult(lua_State *L,
int stat,
const char *fname);
211#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
212COMPAT53_API int luaL_execresult(lua_State *L,
int stat);
214#define lua_callk(L, na, nr, ctx, cont) \
215 ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
216#define lua_pcallk(L, na, nr, err, ctx, cont) \
217 ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
219#define lua_resume(L, from, nargs) \
220 ((void)(from), lua_resume((L), (nargs)))
222#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
223COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B);
225#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
226COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B,
size_t s);
228#define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
229COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B,
const char *s,
size_t l);
231#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
234#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
237#undef luaL_buffinitsize
238#define luaL_buffinitsize(L, B, s) \
239 (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
241#undef luaL_prepbuffer
242#define luaL_prepbuffer(B) \
243 luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
246#define luaL_addchar(B, c) \
247 ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
248 ((B)->ptr[(B)->nelems++] = (c)))
251#define luaL_addsize(B, s) \
255#define luaL_addstring(B, s) \
256 luaL_addlstring((B), (s), strlen((s)))
258#undef luaL_pushresultsize
259#define luaL_pushresultsize(B, s) \
260 (luaL_addsize((B), (s)), luaL_pushresult((B)))
262#if defined(LUA_COMPAT_APIINTCASTS)
263#define lua_pushunsigned(L, n) \
264 lua_pushinteger((L), (lua_Integer)(n))
265#define lua_tounsignedx(L, i, is) \
266 ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
267#define lua_tounsigned(L, i) \
268 lua_tounsignedx((L), (i), NULL)
269#define luaL_checkunsigned(L, a) \
270 ((lua_Unsigned)luaL_checkinteger((L), (a)))
271#define luaL_optunsigned(L, a, d) \
272 ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
280#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
282typedef int lua_KContext;
284typedef int(*lua_KFunction)(lua_State *
L,
int status, lua_KContext ctx);
286#define lua_dump(L, w, d, s) \
287 ((void)(s), lua_dump((L), (w), (d)))
289#define lua_getfield(L, i, k) \
290 (lua_getfield((L), (i), (k)), lua_type((L), -1))
292#define lua_gettable(L, i) \
293 (lua_gettable((L), (i)), lua_type((L), -1))
295#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
296COMPAT53_API int lua_geti(lua_State *L,
int index, lua_Integer i);
298#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
301#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
302COMPAT53_API lua_Integer lua_tointegerx(lua_State *L,
int i,
int *isnum);
304#define lua_numbertointeger(n, p) \
305 ((*(p) = (lua_Integer)(n)), 1)
307#define lua_rawget(L, i) \
308 (lua_rawget((L), (i)), lua_type((L), -1))
310#define lua_rawgeti(L, i, n) \
311 (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
313#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
314COMPAT53_API void lua_rotate(lua_State *L,
int idx,
int n);
316#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
317COMPAT53_API void lua_seti(lua_State *L,
int index, lua_Integer i);
319#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
320COMPAT53_API size_t lua_stringtonumber(lua_State *L,
const char *s);
322#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
323COMPAT53_API const char *luaL_tolstring(lua_State *L,
int idx,
size_t *len);
325#define luaL_getmetafield(L, o, e) \
326 (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
328#define luaL_newmetatable(L, tn) \
329 (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
331#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
332COMPAT53_API void luaL_requiref(lua_State *L,
const char *modname,
333 lua_CFunction openf,
int glb);
340#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
349#define lua_getglobal(L, n) \
350 (lua_getglobal((L), (n)), lua_type((L), -1))
352#define lua_getuservalue(L, i) \
353 (lua_getuservalue((L), (i)), lua_type((L), -1))
355#define lua_pushlstring(L, s, len) \
356 (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
358#define lua_rawgetp(L, i, p) \
359 (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
361#define LUA_KFUNCTION(_name) \
362 static int (_name)(lua_State *L, int status, lua_KContext ctx); \
363 static int (_name ## _52)(lua_State *L) { \
365 int status = lua_getctx(L, &ctx); \
366 return (_name)(L, status, ctx); \
368 static int (_name)(lua_State *L, int status, lua_KContext ctx)
370#define lua_pcallk(L, na, nr, err, ctx, cont) \
371 lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
373#define lua_callk(L, na, nr, ctx, cont) \
374 lua_callk((L), (na), (nr), (ctx), cont ## _52)
376#define lua_yieldk(L, nr, ctx, cont) \
377 lua_yieldk((L), (nr), (ctx), cont ## _52)
381# define lua_call(L, na, nr) \
382 (lua_callk)((L), (na), (nr), 0, NULL)
387# define lua_pcall(L, na, nr, err) \
388 (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
393# define lua_yield(L, nr) \
394 (lua_yieldk)((L), (nr), 0, NULL)
402#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
404# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)"
413#define LUA_KFUNCTION(_name) \
414 static int (_name)(lua_State *L, int status, lua_KContext ctx)
418#if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE == 1