dl
compat-5.3.h
浏览该文件的文档.
1#ifndef KEPLER_PROJECT_COMPAT53_H_
2#define KEPLER_PROJECT_COMPAT53_H_
3
4#include <stddef.h>
5#include <limits.h>
6#include <string.h>
7#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
8extern "C" {
9#endif
10#include <lua.h>
11#include <lauxlib.h>
12#include <lualib.h>
13#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
14}
15#endif
16
17#ifndef COMPAT53_PREFIX
18/* we chose this name because many other lua bindings / libs have
19* their own compatibility layer, and that use the compat53 declaration
20* frequently, causing all kinds of linker / compiler issues
21*/
22# define COMPAT53_PREFIX kp_compat53
23#endif // COMPAT53_PREFIX
24
25#ifndef COMPAT53_API
26# if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE
27# if defined(__GNUC__) || defined(__clang__)
28# define COMPAT53_API __attribute__((__unused__)) static inline
29# else
30# define COMPAT53_API static inline
31# endif /* Clang/GCC */
32# else /* COMPAT53_INCLUDE_SOURCE */
33/* we are not including source, so everything is extern */
34# define COMPAT53_API extern
35# endif /* COMPAT53_INCLUDE_SOURCE */
36#endif /* COMPAT53_PREFIX */
37
38
39#define COMPAT53_CONCAT_HELPER(a, b) a##b
40#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
41
42
43
44/* declarations for Lua 5.1 */
45#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
46
47/* XXX not implemented:
48* lua_arith (new operators)
49* lua_upvalueid
50* lua_upvaluejoin
51* lua_version
52* lua_yieldk
53*/
54
55#ifndef LUA_OK
56# define LUA_OK 0
57#endif
58#ifndef LUA_OPADD
59# define LUA_OPADD 0
60#endif
61#ifndef LUA_OPSUB
62# define LUA_OPSUB 1
63#endif
64#ifndef LUA_OPMUL
65# define LUA_OPMUL 2
66#endif
67#ifndef LUA_OPDIV
68# define LUA_OPDIV 3
69#endif
70#ifndef LUA_OPMOD
71# define LUA_OPMOD 4
72#endif
73#ifndef LUA_OPPOW
74# define LUA_OPPOW 5
75#endif
76#ifndef LUA_OPUNM
77# define LUA_OPUNM 6
78#endif
79#ifndef LUA_OPEQ
80# define LUA_OPEQ 0
81#endif
82#ifndef LUA_OPLT
83# define LUA_OPLT 1
84#endif
85#ifndef LUA_OPLE
86# define LUA_OPLE 2
87#endif
88
89/* LuaJIT/Lua 5.1 does not have the updated
90* error codes for thread status/function returns (but some patched versions do)
91* define it only if it's not found
92*/
93#if !defined(LUA_ERRGCMM)
94/* Use + 2 because in some versions of Lua (Lua 5.1)
95* LUA_ERRFILE is defined as (LUA_ERRERR+1)
96* so we need to avoid it (LuaJIT might have something at this
97* integer value too)
98*/
99# define LUA_ERRGCMM (LUA_ERRERR + 2)
100#endif /* LUA_ERRGCMM define */
101
102#if !defined(MOONJIT_VERSION)
103typedef size_t lua_Unsigned;
104#endif
105
106typedef struct luaL_Buffer_53 {
107 luaL_Buffer b; /* make incorrect code crash! */
108 char *ptr;
109 size_t nelems;
110 size_t capacity;
111 lua_State *L2;
112} luaL_Buffer_53;
113#define luaL_Buffer luaL_Buffer_53
114
115/* In PUC-Rio 5.1, userdata is a simple FILE*
116* In LuaJIT, it's a struct where the first member is a FILE*
117* We can't support the `closef` member
118*/
119typedef struct luaL_Stream {
120 FILE *f;
121} luaL_Stream;
122
123#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
124COMPAT53_API int lua_absindex(lua_State *L, int i);
125
126#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
127COMPAT53_API void lua_arith(lua_State *L, int op);
128
129#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
130COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op);
131
132#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
133COMPAT53_API void lua_copy(lua_State *L, int from, int to);
134
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)))
139
140#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
141COMPAT53_API void lua_len(lua_State *L, int i);
142
143#define lua_pushstring(L, s) \
144 (lua_pushstring((L), (s)), lua_tostring((L), -1))
145
146#define lua_pushlstring(L, s, len) \
147 ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
148
149#ifndef luaL_newlibtable
150# define luaL_newlibtable(L, l) \
151 (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
152#endif
153#ifndef luaL_newlib
154# define luaL_newlib(L, l) \
155 (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
156#endif
157
158#ifndef lua_pushglobaltable
159# define lua_pushglobaltable(L) \
160 lua_pushvalue((L), LUA_GLOBALSINDEX)
161#endif
162#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
163COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p);
164
165#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
166COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
167
168#define lua_rawlen(L, i) lua_objlen((L), (i))
169
170#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
171
172#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
173COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum);
174
175#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
176COMPAT53_API void luaL_checkversion(lua_State *L);
177
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);
180
181#define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
182COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode);
183
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);
186
187#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
188COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg);
189
190#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
191COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char *name);
192
193#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
194COMPAT53_API lua_Integer luaL_len(lua_State *L, int i);
195
196#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
197COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
198
199#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
200COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname);
201
202#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
203COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname);
204
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);
207
208#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
209COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname);
210
211#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
212COMPAT53_API int luaL_execresult(lua_State *L, int stat);
213
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)))
218
219#define lua_resume(L, from, nargs) \
220 ((void)(from), lua_resume((L), (nargs)))
221
222#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
223COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B);
224
225#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
226COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s);
227
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);
230
231#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
232COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B);
233
234#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
235COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B);
236
237#undef luaL_buffinitsize
238#define luaL_buffinitsize(L, B, s) \
239 (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
240
241#undef luaL_prepbuffer
242#define luaL_prepbuffer(B) \
243 luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
244
245#undef luaL_addchar
246#define luaL_addchar(B, c) \
247 ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
248 ((B)->ptr[(B)->nelems++] = (c)))
249
250#undef luaL_addsize
251#define luaL_addsize(B, s) \
252 ((B)->nelems += (s))
253
254#undef luaL_addstring
255#define luaL_addstring(B, s) \
256 luaL_addlstring((B), (s), strlen((s)))
257
258#undef luaL_pushresultsize
259#define luaL_pushresultsize(B, s) \
260 (luaL_addsize((B), (s)), luaL_pushresult((B)))
261
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)))
273#endif
274
275#endif /* Lua 5.1 only */
276
277
278
279/* declarations for Lua 5.1 and 5.2 */
280#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
281
282typedef int lua_KContext;
283
284typedef int(*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
285
286#define lua_dump(L, w, d, s) \
287 ((void)(s), lua_dump((L), (w), (d)))
288
289#define lua_getfield(L, i, k) \
290 (lua_getfield((L), (i), (k)), lua_type((L), -1))
291
292#define lua_gettable(L, i) \
293 (lua_gettable((L), (i)), lua_type((L), -1))
294
295#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
296COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i);
297
298#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
299COMPAT53_API int lua_isinteger(lua_State *L, int index);
300
301#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
302COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum);
303
304#define lua_numbertointeger(n, p) \
305 ((*(p) = (lua_Integer)(n)), 1)
306
307#define lua_rawget(L, i) \
308 (lua_rawget((L), (i)), lua_type((L), -1))
309
310#define lua_rawgeti(L, i, n) \
311 (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
312
313#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
314COMPAT53_API void lua_rotate(lua_State *L, int idx, int n);
315
316#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
317COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i);
318
319#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
320COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s);
321
322#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
323COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len);
324
325#define luaL_getmetafield(L, o, e) \
326 (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
327
328#define luaL_newmetatable(L, tn) \
329 (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
330
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);
334
335#endif /* Lua 5.1 and Lua 5.2 */
336
337
338
339/* declarations for Lua 5.2 */
340#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
341
342/* XXX not implemented:
343* lua_isyieldable
344* lua_getextraspace
345* lua_arith (new operators)
346* lua_pushfstring (new formats)
347*/
348
349#define lua_getglobal(L, n) \
350 (lua_getglobal((L), (n)), lua_type((L), -1))
351
352#define lua_getuservalue(L, i) \
353 (lua_getuservalue((L), (i)), lua_type((L), -1))
354
355#define lua_pushlstring(L, s, len) \
356 (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
357
358#define lua_rawgetp(L, i, p) \
359 (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
360
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) { \
364 lua_KContext ctx; \
365 int status = lua_getctx(L, &ctx); \
366 return (_name)(L, status, ctx); \
367 } \
368 static int (_name)(lua_State *L, int status, lua_KContext ctx)
369
370#define lua_pcallk(L, na, nr, err, ctx, cont) \
371 lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
372
373#define lua_callk(L, na, nr, ctx, cont) \
374 lua_callk((L), (na), (nr), (ctx), cont ## _52)
375
376#define lua_yieldk(L, nr, ctx, cont) \
377 lua_yieldk((L), (nr), (ctx), cont ## _52)
378
379#ifdef lua_call
380# undef lua_call
381# define lua_call(L, na, nr) \
382 (lua_callk)((L), (na), (nr), 0, NULL)
383#endif
384
385#ifdef lua_pcall
386# undef lua_pcall
387# define lua_pcall(L, na, nr, err) \
388 (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
389#endif
390
391#ifdef lua_yield
392# undef lua_yield
393# define lua_yield(L, nr) \
394 (lua_yieldk)((L), (nr), 0, NULL)
395#endif
396
397#endif /* Lua 5.2 only */
398
399
400
401/* other Lua versions */
402#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
403
404# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)"
405
406#endif /* other Lua versions except 5.1, 5.2, 5.3, and 5.4 */
407
408
409
410/* helper macro for defining continuation functions (for every version
411* *except* Lua 5.2) */
412#ifndef LUA_KFUNCTION
413#define LUA_KFUNCTION(_name) \
414 static int (_name)(lua_State *L, int status, lua_KContext ctx)
415#endif
416
417
418#if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE == 1
419# include "compat-5.3.c.h"
420#endif
421
422
423#endif /* KEPLER_PROJECT_COMPAT53_H_ */
424
#define COMPAT53_API