compat-5.3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #ifndef KEPLER_PROJECT_COMPAT53_H_
  2. #define KEPLER_PROJECT_COMPAT53_H_
  3. #include <stddef.h>
  4. #include <limits.h>
  5. #include <string.h>
  6. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  7. extern "C" {
  8. #endif
  9. #include <lua.h>
  10. #include <lauxlib.h>
  11. #include <lualib.h>
  12. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  13. }
  14. #endif
  15. #ifndef COMPAT53_PREFIX
  16. /* we chose this name because many other lua bindings / libs have
  17. * their own compatibility layer, and that use the compat53 declaration
  18. * frequently, causing all kinds of linker / compiler issues
  19. */
  20. # define COMPAT53_PREFIX kp_compat53
  21. #endif // COMPAT53_PREFIX
  22. #ifndef COMPAT53_API
  23. # if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE
  24. # if defined(__GNUC__) || defined(__clang__)
  25. # define COMPAT53_API __attribute__((__unused__)) static inline
  26. # else
  27. # define COMPAT53_API static inline
  28. # endif /* Clang/GCC */
  29. # else /* COMPAT53_INCLUDE_SOURCE */
  30. /* we are not including source, so everything is extern */
  31. # define COMPAT53_API extern
  32. # endif /* COMPAT53_INCLUDE_SOURCE */
  33. #endif /* COMPAT53_PREFIX */
  34. #define COMPAT53_CONCAT_HELPER(a, b) a##b
  35. #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
  36. /* declarations for Lua 5.1 */
  37. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  38. /* XXX not implemented:
  39. * lua_arith (new operators)
  40. * lua_upvalueid
  41. * lua_upvaluejoin
  42. * lua_version
  43. * lua_yieldk
  44. */
  45. #ifndef LUA_OK
  46. # define LUA_OK 0
  47. #endif
  48. #ifndef LUA_OPADD
  49. # define LUA_OPADD 0
  50. #endif
  51. #ifndef LUA_OPSUB
  52. # define LUA_OPSUB 1
  53. #endif
  54. #ifndef LUA_OPMUL
  55. # define LUA_OPMUL 2
  56. #endif
  57. #ifndef LUA_OPDIV
  58. # define LUA_OPDIV 3
  59. #endif
  60. #ifndef LUA_OPMOD
  61. # define LUA_OPMOD 4
  62. #endif
  63. #ifndef LUA_OPPOW
  64. # define LUA_OPPOW 5
  65. #endif
  66. #ifndef LUA_OPUNM
  67. # define LUA_OPUNM 6
  68. #endif
  69. #ifndef LUA_OPEQ
  70. # define LUA_OPEQ 0
  71. #endif
  72. #ifndef LUA_OPLT
  73. # define LUA_OPLT 1
  74. #endif
  75. #ifndef LUA_OPLE
  76. # define LUA_OPLE 2
  77. #endif
  78. /* LuaJIT/Lua 5.1 does not have the updated
  79. * error codes for thread status/function returns (but some patched versions do)
  80. * define it only if it's not found
  81. */
  82. #if !defined(LUA_ERRGCMM)
  83. /* Use + 2 because in some versions of Lua (Lua 5.1)
  84. * LUA_ERRFILE is defined as (LUA_ERRERR+1)
  85. * so we need to avoid it (LuaJIT might have something at this
  86. * integer value too)
  87. */
  88. # define LUA_ERRGCMM (LUA_ERRERR + 2)
  89. #endif /* LUA_ERRGCMM define */
  90. #if !defined(MOONJIT_VERSION)
  91. typedef size_t lua_Unsigned;
  92. #endif
  93. typedef struct luaL_Buffer_53 {
  94. luaL_Buffer b; /* make incorrect code crash! */
  95. char *ptr;
  96. size_t nelems;
  97. size_t capacity;
  98. lua_State *L2;
  99. } luaL_Buffer_53;
  100. #define luaL_Buffer luaL_Buffer_53
  101. /* In PUC-Rio 5.1, userdata is a simple FILE*
  102. * In LuaJIT, it's a struct where the first member is a FILE*
  103. * We can't support the `closef` member
  104. */
  105. typedef struct luaL_Stream {
  106. FILE *f;
  107. } luaL_Stream;
  108. #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
  109. COMPAT53_API int lua_absindex(lua_State *L, int i);
  110. #define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
  111. COMPAT53_API void lua_arith(lua_State *L, int op);
  112. #define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
  113. COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op);
  114. #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
  115. COMPAT53_API void lua_copy(lua_State *L, int from, int to);
  116. #define lua_getuservalue(L, i) \
  117. (lua_getfenv((L), (i)), lua_type((L), -1))
  118. #define lua_setuservalue(L, i) \
  119. (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
  120. #define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
  121. COMPAT53_API void lua_len(lua_State *L, int i);
  122. #define lua_pushstring(L, s) \
  123. (lua_pushstring((L), (s)), lua_tostring((L), -1))
  124. #define lua_pushlstring(L, s, len) \
  125. ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
  126. #ifndef luaL_newlibtable
  127. # define luaL_newlibtable(L, l) \
  128. (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
  129. #endif
  130. #ifndef luaL_newlib
  131. # define luaL_newlib(L, l) \
  132. (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
  133. #endif
  134. #ifndef lua_pushglobaltable
  135. # define lua_pushglobaltable(L) \
  136. lua_pushvalue((L), LUA_GLOBALSINDEX)
  137. #endif
  138. #define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
  139. COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p);
  140. #define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
  141. COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
  142. #define lua_rawlen(L, i) lua_objlen((L), (i))
  143. #define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
  144. #define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
  145. COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum);
  146. #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
  147. COMPAT53_API void luaL_checkversion(lua_State *L);
  148. #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
  149. COMPAT53_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode);
  150. #define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
  151. COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode);
  152. #define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx)
  153. COMPAT53_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
  154. #define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
  155. COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg);
  156. #define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
  157. COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char *name);
  158. #define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
  159. COMPAT53_API lua_Integer luaL_len(lua_State *L, int i);
  160. #define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
  161. COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
  162. #define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
  163. COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname);
  164. #define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
  165. COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname);
  166. #define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
  167. COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level);
  168. #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
  169. COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname);
  170. #define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
  171. COMPAT53_API int luaL_execresult(lua_State *L, int stat);
  172. #define lua_callk(L, na, nr, ctx, cont) \
  173. ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
  174. #define lua_pcallk(L, na, nr, err, ctx, cont) \
  175. ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
  176. #define lua_resume(L, from, nargs) \
  177. ((void)(from), lua_resume((L), (nargs)))
  178. #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
  179. COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B);
  180. #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
  181. COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s);
  182. #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
  183. COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l);
  184. #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
  185. COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B);
  186. #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
  187. COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B);
  188. #undef luaL_buffinitsize
  189. #define luaL_buffinitsize(L, B, s) \
  190. (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
  191. #undef luaL_prepbuffer
  192. #define luaL_prepbuffer(B) \
  193. luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
  194. #undef luaL_addchar
  195. #define luaL_addchar(B, c) \
  196. ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
  197. ((B)->ptr[(B)->nelems++] = (c)))
  198. #undef luaL_addsize
  199. #define luaL_addsize(B, s) \
  200. ((B)->nelems += (s))
  201. #undef luaL_addstring
  202. #define luaL_addstring(B, s) \
  203. luaL_addlstring((B), (s), strlen((s)))
  204. #undef luaL_pushresultsize
  205. #define luaL_pushresultsize(B, s) \
  206. (luaL_addsize((B), (s)), luaL_pushresult((B)))
  207. #if defined(LUA_COMPAT_APIINTCASTS)
  208. #define lua_pushunsigned(L, n) \
  209. lua_pushinteger((L), (lua_Integer)(n))
  210. #define lua_tounsignedx(L, i, is) \
  211. ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
  212. #define lua_tounsigned(L, i) \
  213. lua_tounsignedx((L), (i), NULL)
  214. #define luaL_checkunsigned(L, a) \
  215. ((lua_Unsigned)luaL_checkinteger((L), (a)))
  216. #define luaL_optunsigned(L, a, d) \
  217. ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
  218. #endif
  219. #endif /* Lua 5.1 only */
  220. /* declarations for Lua 5.1 and 5.2 */
  221. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  222. typedef int lua_KContext;
  223. typedef int(*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
  224. #define lua_dump(L, w, d, s) \
  225. ((void)(s), lua_dump((L), (w), (d)))
  226. #define lua_getfield(L, i, k) \
  227. (lua_getfield((L), (i), (k)), lua_type((L), -1))
  228. #define lua_gettable(L, i) \
  229. (lua_gettable((L), (i)), lua_type((L), -1))
  230. #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
  231. COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i);
  232. #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
  233. COMPAT53_API int lua_isinteger(lua_State *L, int index);
  234. #define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
  235. COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum);
  236. #define lua_numbertointeger(n, p) \
  237. ((*(p) = (lua_Integer)(n)), 1)
  238. #define lua_rawget(L, i) \
  239. (lua_rawget((L), (i)), lua_type((L), -1))
  240. #define lua_rawgeti(L, i, n) \
  241. (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
  242. #define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
  243. COMPAT53_API void lua_rotate(lua_State *L, int idx, int n);
  244. #define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
  245. COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i);
  246. #define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
  247. COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s);
  248. #define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
  249. COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len);
  250. #define luaL_getmetafield(L, o, e) \
  251. (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
  252. #define luaL_newmetatable(L, tn) \
  253. (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
  254. #define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
  255. COMPAT53_API void luaL_requiref(lua_State *L, const char *modname,
  256. lua_CFunction openf, int glb);
  257. #endif /* Lua 5.1 and Lua 5.2 */
  258. /* declarations for Lua 5.2 */
  259. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
  260. /* XXX not implemented:
  261. * lua_isyieldable
  262. * lua_getextraspace
  263. * lua_arith (new operators)
  264. * lua_pushfstring (new formats)
  265. */
  266. #define lua_getglobal(L, n) \
  267. (lua_getglobal((L), (n)), lua_type((L), -1))
  268. #define lua_getuservalue(L, i) \
  269. (lua_getuservalue((L), (i)), lua_type((L), -1))
  270. #define lua_pushlstring(L, s, len) \
  271. (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
  272. #define lua_rawgetp(L, i, p) \
  273. (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
  274. #define LUA_KFUNCTION(_name) \
  275. static int (_name)(lua_State *L, int status, lua_KContext ctx); \
  276. static int (_name ## _52)(lua_State *L) { \
  277. lua_KContext ctx; \
  278. int status = lua_getctx(L, &ctx); \
  279. return (_name)(L, status, ctx); \
  280. } \
  281. static int (_name)(lua_State *L, int status, lua_KContext ctx)
  282. #define lua_pcallk(L, na, nr, err, ctx, cont) \
  283. lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
  284. #define lua_callk(L, na, nr, ctx, cont) \
  285. lua_callk((L), (na), (nr), (ctx), cont ## _52)
  286. #define lua_yieldk(L, nr, ctx, cont) \
  287. lua_yieldk((L), (nr), (ctx), cont ## _52)
  288. #ifdef lua_call
  289. # undef lua_call
  290. # define lua_call(L, na, nr) \
  291. (lua_callk)((L), (na), (nr), 0, NULL)
  292. #endif
  293. #ifdef lua_pcall
  294. # undef lua_pcall
  295. # define lua_pcall(L, na, nr, err) \
  296. (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
  297. #endif
  298. #ifdef lua_yield
  299. # undef lua_yield
  300. # define lua_yield(L, nr) \
  301. (lua_yieldk)((L), (nr), 0, NULL)
  302. #endif
  303. #endif /* Lua 5.2 only */
  304. /* other Lua versions */
  305. #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
  306. # error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)"
  307. #endif /* other Lua versions except 5.1, 5.2, 5.3, and 5.4 */
  308. /* helper macro for defining continuation functions (for every version
  309. * *except* Lua 5.2) */
  310. #ifndef LUA_KFUNCTION
  311. #define LUA_KFUNCTION(_name) \
  312. static int (_name)(lua_State *L, int status, lua_KContext ctx)
  313. #endif
  314. #if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE == 1
  315. # include "compat-5.3.c.h"
  316. #endif
  317. #endif /* KEPLER_PROJECT_COMPAT53_H_ */