aboutsummaryrefslogtreecommitdiff
path: root/c-api
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-01-13 08:27:20 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2015-01-13 08:27:20 +0100
commit901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3 (patch)
tree698870c642950c4287c7ab57c2dcab28b3efb6a6 /c-api
parent46bbf75fbea482c19c4eb50bf97ef948f611e26a (diff)
downloadlua-compat-5.3-901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3.tar.gz
lua-compat-5.3-901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3.tar.bz2
lua-compat-5.3-901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3.zip
add initial implementation of c-api and license
Diffstat (limited to 'c-api')
-rw-r--r--c-api/compat-5.3.c430
-rw-r--r--c-api/compat-5.3.h294
2 files changed, 724 insertions, 0 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c
new file mode 100644
index 0000000..9edd5f1
--- /dev/null
+++ b/c-api/compat-5.3.c
@@ -0,0 +1,430 @@
1#include <stddef.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <errno.h>
6#include <lua.h>
7#include <lauxlib.h>
8#include "compat-5.3.h"
9
10/* don't compile it again if it already is included via compat53.h */
11#ifndef COMPAT53_C_
12#define COMPAT53_C_
13
14
15
16/* definitions for Lua 5.1 only */
17#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM == 501
18
19
20COMPAT53_API int lua_absindex (lua_State *L, int i) {
21 if (i < 0 && i > LUA_REGISTRYINDEX)
22 i += lua_gettop(L) + 1;
23 return i;
24}
25
26
27COMPAT53_API void lua_copy (lua_State *L, int from, int to) {
28 int abs_to = lua_absindex(L, to);
29 luaL_checkstack(L, 1, "not enough stack slots");
30 lua_pushvalue(L, from);
31 lua_replace(L, abs_to);
32}
33
34
35COMPAT53_API void lua_len (lua_State *L, int i) {
36 switch (lua_type(L, i)) {
37 case LUA_TSTRING: /* fall through */
38 case LUA_TTABLE:
39 lua_pushnumber(L, (int)lua_objlen(L, i));
40 break;
41 case LUA_TUSERDATA:
42 if (luaL_callmeta(L, i, "__len"))
43 break;
44 /* maybe fall through */
45 default:
46 luaL_error(L, "attempt to get length of a %s value",
47 lua_typename(L, lua_type(L, i)));
48 }
49}
50
51
52COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p) {
53 int abs_i = lua_absindex(L, i);
54 lua_pushlightuserdata(L, (void*)p);
55 lua_rawget(L, abs_i);
56 return lua_type(L, -1);
57}
58
59COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) {
60 int abs_i = lua_absindex(L, i);
61 luaL_checkstack(L, 1, "not enough stack slots");
62 lua_pushlightuserdata(L, (void*)p);
63 lua_insert(L, -2);
64 lua_rawset(L, abs_i);
65}
66
67
68COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
69 lua_Integer n = lua_tointeger(L, i);
70 if (isnum != NULL) {
71 *isnum = (n != 0 || lua_isnumber(L, i));
72}
73 return n;
74}
75
76
77COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) {
78 lua_Number n = lua_tonumber(L, i);
79 if (isnum != NULL) {
80 *isnum = (n != 0 || lua_isnumber(L, i));
81 }
82 return n;
83}
84
85
86COMPAT53_API void luaL_checkversion (lua_State *L) {
87 (void)L;
88}
89
90
91COMPAT53_API int luaL_getsubtable (lua_State *L, int i, const char *name) {
92 int abs_i = lua_absindex(L, i);
93 luaL_checkstack(L, 3, "not enough stack slots");
94 lua_pushstring(L, name);
95 lua_gettable(L, abs_i);
96 if (lua_istable(L, -1))
97 return 1;
98 lua_pop(L, 1);
99 lua_newtable(L);
100 lua_pushstring(L, name);
101 lua_pushvalue(L, -2);
102 lua_settable(L, abs_i);
103 return 0;
104}
105
106
107COMPAT53_API int luaL_len (lua_State *L, int i) {
108 int res = 0, isnum = 0;
109 luaL_checkstack(L, 1, "not enough stack slots");
110 lua_len(L, i);
111 res = (int)lua_tointegerx(L, -1, &isnum);
112 lua_pop(L, 1);
113 if (!isnum)
114 luaL_error(L, "object length is not a number");
115 return res;
116}
117
118
119COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
120 luaL_checkstack(L, nup+1, "too many upvalues");
121 for (; l->name != NULL; l++) { /* fill the table with given functions */
122 int i;
123 lua_pushstring(L, l->name);
124 for (i = 0; i < nup; i++) /* copy upvalues to the top */
125 lua_pushvalue(L, -(nup + 1));
126 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
127 lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
128 }
129 lua_pop(L, nup); /* remove upvalues */
130}
131
132
133COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname) {
134 luaL_checkstack(L, 1, "not enough stack slots");
135 luaL_getmetatable(L, tname);
136 lua_setmetatable(L, -2);
137}
138
139
140COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) {
141 void *p = lua_touserdata(L, i);
142 luaL_checkstack(L, 2, "not enough stack slots");
143 if (p == NULL || !lua_getmetatable(L, i))
144 return NULL;
145 else {
146 int res = 0;
147 luaL_getmetatable(L, tname);
148 res = lua_rawequal(L, -1, -2);
149 lua_pop(L, 2);
150 if (!res)
151 p = NULL;
152 }
153 return p;
154}
155
156
157COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
158 if (!luaL_callmeta(L, idx, "__tostring")) {
159 int t = lua_type(L, idx);
160 switch (t) {
161 case LUA_TNIL:
162 lua_pushliteral(L, "nil");
163 break;
164 case LUA_TSTRING:
165 case LUA_TNUMBER:
166 lua_pushvalue(L, idx);
167 break;
168 case LUA_TBOOLEAN:
169 if (lua_toboolean(L, idx))
170 lua_pushliteral(L, "true");
171 else
172 lua_pushliteral(L, "false");
173 break;
174 default:
175 lua_pushfstring(L, "%s: %p", lua_typename(L, t),
176 lua_topointer(L, idx));
177 break;
178 }
179 }
180 return lua_tolstring(L, -1, len);
181}
182
183
184#if !defined(COMPAT53_IS_LUAJIT)
185static int compat53_countlevels (lua_State *L) {
186 lua_Debug ar;
187 int li = 1, le = 1;
188 /* find an upper bound */
189 while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
190 /* do a binary search */
191 while (li < le) {
192 int m = (li + le)/2;
193 if (lua_getstack(L, m, &ar)) li = m + 1;
194 else le = m;
195 }
196 return le - 1;
197}
198
199static int compat53_findfield (lua_State *L, int objidx, int level) {
200 if (level == 0 || !lua_istable(L, -1))
201 return 0; /* not found */
202 lua_pushnil(L); /* start 'next' loop */
203 while (lua_next(L, -2)) { /* for each pair in table */
204 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
205 if (lua_rawequal(L, objidx, -1)) { /* found object? */
206 lua_pop(L, 1); /* remove value (but keep name) */
207 return 1;
208 }
209 else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
210 lua_remove(L, -2); /* remove table (but keep name) */
211 lua_pushliteral(L, ".");
212 lua_insert(L, -2); /* place '.' between the two names */
213 lua_concat(L, 3);
214 return 1;
215 }
216 }
217 lua_pop(L, 1); /* remove value */
218 }
219 return 0; /* not found */
220}
221
222static int compat53_pushglobalfuncname (lua_State *L, lua_Debug *ar) {
223 int top = lua_gettop(L);
224 lua_getinfo(L, "f", ar); /* push function */
225 lua_pushvalue(L, LUA_GLOBALSINDEX);
226 if (compat53_findfield(L, top + 1, 2)) {
227 lua_copy(L, -1, top + 1); /* move name to proper place */
228 lua_pop(L, 2); /* remove pushed values */
229 return 1;
230 }
231 else {
232 lua_settop(L, top); /* remove function and global table */
233 return 0;
234 }
235}
236
237static void compat53_pushfuncname (lua_State *L, lua_Debug *ar) {
238 if (*ar->namewhat != '\0') /* is there a name? */
239 lua_pushfstring(L, "function " LUA_QS, ar->name);
240 else if (*ar->what == 'm') /* main? */
241 lua_pushliteral(L, "main chunk");
242 else if (*ar->what == 'C') {
243 if (compat53_pushglobalfuncname(L, ar)) {
244 lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
245 lua_remove(L, -2); /* remove name */
246 }
247 else
248 lua_pushliteral(L, "?");
249 }
250 else
251 lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
252}
253
254#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
255#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
256
257COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1,
258 const char *msg, int level) {
259 lua_Debug ar;
260 int top = lua_gettop(L);
261 int numlevels = compat53_countlevels(L1);
262 int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
263 if (msg) lua_pushfstring(L, "%s\n", msg);
264 lua_pushliteral(L, "stack traceback:");
265 while (lua_getstack(L1, level++, &ar)) {
266 if (level == mark) { /* too many levels? */
267 lua_pushliteral(L, "\n\t..."); /* add a '...' */
268 level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
269 }
270 else {
271 lua_getinfo(L1, "Slnt", &ar);
272 lua_pushfstring(L, "\n\t%s:", ar.short_src);
273 if (ar.currentline > 0)
274 lua_pushfstring(L, "%d:", ar.currentline);
275 lua_pushliteral(L, " in ");
276 compat53_pushfuncname(L, &ar);
277 lua_concat(L, lua_gettop(L) - top);
278 }
279 }
280 lua_concat(L, lua_gettop(L) - top);
281}
282
283
284COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
285 int en = errno; /* calls to Lua API may change this value */
286 if (stat) {
287 lua_pushboolean(L, 1);
288 return 1;
289 }
290 else {
291 lua_pushnil(L);
292 if (fname)
293 lua_pushfstring(L, "%s: %s", fname, strerror(en));
294 else
295 lua_pushstring(L, strerror(en));
296 lua_pushnumber(L, (lua_Number)en);
297 return 3;
298 }
299}
300#endif /* not COMPAT53_IS_LUAJIT */
301
302
303#endif /* Lua 5.1 */
304
305
306
307/* definitions for Lua 5.1 and Lua 5.2 */
308#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502
309
310
311COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) {
312 index = lua_absindex(L, index);
313 lua_pushinteger(L, i);
314 lua_gettable(L, index);
315 return lua_type(L, -1);
316}
317
318
319COMPAT53_API int lua_isinteger (lua_State *L, int index) {
320 if (lua_type(L, index) == LUA_TNUMBER) {
321 lua_Number n = lua_tonumber(L, index);
322 lua_Integer i = lua_tointeger(L, index);
323 if (i == n)
324 return 1;
325 }
326 return 0;
327}
328
329
330static void compat53_reverse (lua_State *L, int a, int b) {
331 for (; a < b; ++a, --b) {
332 lua_pushvalue(L, a);
333 lua_pushvalue(L, b);
334 lua_replace(L, a);
335 lua_replace(L, b);
336 }
337}
338
339
340COMPAT53_API void lua_rotate (lua_State *L, int idx, int n) {
341 int n_elems = 0;
342 idx = lua_absindex(L, idx);
343 n_elems = lua_gettop(L)-idx+1;
344 if (n < 0)
345 n += n_elems;
346 if ( n > 0 && n < n_elems) {
347 luaL_checkstack(L, 2, "not enough stack slots available");
348 n = n_elems - n;
349 compat53_reverse(L, idx, idx+n-1);
350 compat53_reverse(L, idx+n, idx+n_elems-1);
351 compat53_reverse(L, idx, idx+n_elems-1);
352 }
353}
354
355
356COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i) {
357 luaL_checkstack(L, 1, "not enough stack slots available");
358 index = lua_absindex(L, index);
359 lua_pushinteger(L, i);
360 lua_insert(L, -2);
361 lua_settable(L, index);
362}
363
364
365COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s) {
366 char* endptr;
367 lua_Number n = lua_str2number(s, &endptr);
368 if (endptr != s) {
369 while (*endptr != '\0' && isspace((unsigned char)*endptr))
370 ++endptr;
371 if (*endptr == '\0') {
372 lua_pushnumber(L, n);
373 return endptr - s + 1;
374 }
375 }
376 return 0;
377}
378
379
380COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
381 lua_CFunction openf, int glb) {
382 luaL_checkstack(L, 3, "not enough stack slots available");
383 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
384 if (lua_getfield(L, -1, modname) == LUA_TNIL) {
385 lua_pop(L, 1);
386 lua_pushcfunction(L, openf);
387 lua_pushstring(L, modname);
388 lua_call(L, 1, 1);
389 lua_pushvalue(L, -1);
390 lua_setfield(L, -3, modname);
391 }
392 if (glb) {
393 lua_pushvalue(L, -1);
394 lua_setglobal(L, modname);
395 }
396 lua_replace(L, -2);
397}
398
399
400#endif /* Lua 5.1 and 5.2 */
401
402
403#endif /* COMPAT53_C_ */
404
405
406/*********************************************************************
407* This file contains parts of Lua 5.2's and Lua 5.3's source code:
408*
409* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
410*
411* Permission is hereby granted, free of charge, to any person obtaining
412* a copy of this software and associated documentation files (the
413* "Software"), to deal in the Software without restriction, including
414* without limitation the rights to use, copy, modify, merge, publish,
415* distribute, sublicense, and/or sell copies of the Software, and to
416* permit persons to whom the Software is furnished to do so, subject to
417* the following conditions:
418*
419* The above copyright notice and this permission notice shall be
420* included in all copies or substantial portions of the Software.
421*
422* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
423* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
424* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
425* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
426* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
427* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
428* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
429*********************************************************************/
430
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h
new file mode 100644
index 0000000..b288126
--- /dev/null
+++ b/c-api/compat-5.3.h
@@ -0,0 +1,294 @@
1#ifndef COMPAT53_H_
2#define COMPAT53_H_
3
4#include <stddef.h>
5#include <limits.h>
6#include <lua.h>
7#include <lauxlib.h>
8#include <lualib.h>
9
10
11#if defined(COMPAT53_PREFIX)
12/* - change the symbol names of functions to avoid linker conflicts
13 * - compat-5.3.c needs to be compiled (and linked) separately
14 */
15# if !defined(COMPAT53_API)
16# define COMPAT53_API extern
17# endif
18# undef COMPAT53_INCLUDE_SOURCE
19#else /* COMPAT53_PREFIX */
20/* - make all functions static and include the source.
21 * - don't mess with the symbol names of functions
22 * - compat-5.3.c doesn't need to be compiled (and linked) separately
23 */
24# define COMPAT53_PREFIX lua
25# undef COMPAT53_API
26# if defined(__GNUC__) || defined(__clang__)
27# define COMPAT53_API __attribute__((__unused__)) static
28# else
29# define COMPAT53_API static
30# endif
31# define COMPAT53_INCLUDE_SOURCE
32#endif /* COMPAT53_PREFIX */
33
34#define COMPAT53_CONCAT_HELPER(a, b) a##b
35#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
36
37
38
39/* declarations for Lua 5.1 */
40#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
41
42/* XXX not implemented:
43 * lua_arith
44 * lua_compare
45 * lua_upvalueid
46 * lua_upvaluejoin
47 * lua_version
48 * lua_yieldk
49 * luaL_buffinitsize
50 * luaL_execresult
51 * luaL_loadbufferx
52 * luaL_loadfilex
53 * luaL_prepbuffsize
54 * luaL_pushresultsize
55 */
56
57/* PUC-Rio Lua uses lconfig_h as include guard for luaconf.h,
58 * LuaJIT uses luaconf_h. If you use PUC-Rio's include files
59 * but LuaJIT's library, you will need to define the macro
60 * COMPAT53_IS_LUAJIT yourself! */
61#if !defined(COMPAT53_IS_LUAJIT) && defined(luaconf_h)
62# define COMPAT53_IS_LUAJIT
63#endif
64
65#define LUA_OK 0
66
67typedef struct luaL_Stream {
68 FILE *f;
69 lua_CFunction closef;
70} luaL_Stream;
71
72typedef size_t lua_Unsigned;
73
74#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
75COMPAT53_API int lua_absindex (lua_State *L, int i);
76
77#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
78COMPAT53_API void lua_copy (lua_State *L, int from, int to);
79
80#define lua_getuservalue(L, i) \
81 (lua_getfenv(L, i), lua_type(L, -1))
82#define lua_setuservalue(L, i) \
83 (luaL_checktype(L, -1, LUA_TTABLE), lua_setfenv(L, i))
84
85#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
86COMPAT53_API void lua_len (lua_State *L, int i);
87
88#define luaL_newlibtable(L, l) \
89 (lua_createtable(L, 0, sizeof(l)/sizeof(*(l))-1))
90#define luaL_newlib(L, l) \
91 (luaL_newlibtable(L, l), luaL_register(L, NULL, l))
92
93#define lua_pushglobaltable(L) \
94 lua_pushvalue(L, LUA_GLOBALSINDEX)
95
96#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
97COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p);
98
99#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
100COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
101
102#define lua_rawlen(L, i) lua_objlen(L, i)
103
104#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx)
105COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum);
106
107#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
108COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum);
109
110#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
111COMPAT53_API void luaL_checkversion (lua_State *L);
112
113#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
114COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name);
115
116#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
117COMPAT53_API int luaL_len (lua_State *L, int i);
118
119#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
120COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
121
122#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
123COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname);
124
125#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
126COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname);
127
128#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
129COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
130
131#if !defined(COMPAT53_IS_LUAJIT)
132#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
133COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);
134
135#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
136COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname);
137#endif /* COMPAT53_IS_LUAJIT */
138
139#define lua_callk(L, na, nr, ctx, cont) \
140 ((void)(ctx), (void)(cont), lua_call(L, na, nr))
141#define lua_pcallk(L, na, nr, err, ctx, cont) \
142 ((void)(ctx), (void)(cont), lua_pcall(L, na, nr, err))
143
144#if defined(LUA_COMPAT_APIINTCASTS)
145#define lua_pushunsigned(L, n) \
146 lua_pushinteger(L, (lua_Integer)(n))
147#define lua_tounsignedx(L, i, is) \
148 ((lua_Unsigned)lua_tointegerx(L, i, is))
149#define lua_tounsigned(L, i) \
150 lua_tounsignedx(L, i, NULL)
151#define luaL_checkunsigned(L, a) \
152 ((lua_Unsigned)luaL_checkinteger(L, a))
153#define luaL_optunsigned(L, a, d) \
154 ((lua_Unsigned)luaL_optinteger(L, a, (lua_Integer)(d)))
155#endif
156
157#endif /* Lua 5.1 only */
158
159
160
161/* declarations for Lua 5.1 and 5.2 */
162#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
163
164typedef int lua_KContext;
165
166typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
167
168#define lua_dump(L, w, d, s) \
169 ((void)(s), lua_dump(L, w, d))
170
171#define lua_getfield(L, i, k) \
172 (lua_getfield(L, i, k), lua_type(L, -1))
173
174#define lua_gettable(L, i) \
175 (lua_gettable(L, i), lua_type(L, -1))
176
177#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
178COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i);
179
180#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
181COMPAT53_API int lua_isinteger (lua_State *L, int index);
182
183#define lua_numbertointeger(n, p) \
184 ((*(p) = (lua_Integer)(n)), 1)
185
186#define lua_rawget(L, i) \
187 (lua_rawget(L, i), lua_type(L, -1))
188
189#define lua_rawgeti(L, i, n) \
190 (lua_rawgeti(L, i, n), lua_type(L, -1))
191
192#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
193COMPAT53_API void lua_rotate (lua_State *L, int idx, int n);
194
195#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
196COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i);
197
198#define lua_strtonum COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
199COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s);
200
201#define luaL_getmetafield(L, o, e) \
202 (luaL_getmetafield(L, o, e) ? lua_type(L, -1) : LUA_TNIL)
203
204#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_)
205COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
206 lua_CFunction openf, int glb );
207
208#endif /* Lua 5.1 and Lua 5.2 */
209
210
211
212/* declarations for Lua 5.2 */
213#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
214
215/* XXX not implemented:
216 * lua_isyieldable
217 * lua_getextraspace
218 * lua_arith (new operators)
219 * lua_pushfstring (new formats)
220 */
221
222#define lua_getglobal(L, n) \
223 (lua_getglobal(L, n), lua_type(L, -1))
224
225#define lua_getuservalue(L, i) \
226 (lua_getuservalue(L, i), lua_type(L, -1))
227
228#define lua_rawgetp(L, i, p) \
229 (lua_rawgetp(L, i, p), lua_type(L, -1))
230
231#define LUA_KFUNCTION(_name) \
232 static int (_name)(lua_State *L, int status, lua_KContext ctx); \
233 static int (_name ## _52)(lua_State *L) { \
234 luaK_Context ctx; \
235 int status = lua_getctx(L, &ctx); \
236 return (_name)(L, status, ctx); \
237 } \
238 static int (_name)(lua_State *L, int status, lua_KContext ctx)
239
240#define lua_pcallk(L, na, nr, err, ctx, cont) \
241 lua_pcallk(L, na, nr, err, ctx, cont ## _52)
242
243#define lua_callk(L, na, nr, ctx, cont) \
244 lua_callk(L, na, nr, ctx, cont ## _52)
245
246#define lua_yieldk(L, nr, ctx, cont) \
247 lua_yieldk(L, nr, ctx, cont ## _52)
248
249#ifdef lua_call
250# undef lua_call
251# define lua_call(L, na, nr) \
252 (lua_callk)(L, na, nr, 0, NULL)
253#endif
254
255#ifdef lua_pcall
256# undef lua_pcall
257# define lua_pcall(L, na, nr, err) \
258 (lua_pcallk)(L, na, nr, err, 0, NULL)
259#endif
260
261#ifdef lua_yield
262# undef lua_yield
263# define lua_yield(L, nr) \
264 (lua_yieldk)(L, nr, 0, NULL)
265#endif
266
267#endif /* Lua 5.2 only */
268
269
270
271/* other Lua versions */
272#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 503
273
274# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, or 5.3)"
275
276#endif /* other Lua versions except 5.1, 5.2, and 5.3 */
277
278
279
280/* helper macro for defining continuation functions (for every version
281 * *except* Lua 5.2) */
282#ifndef LUA_KFUNCTION
283#define LUA_KFUNCTION(_name) \
284 static int (_name)(lua_State *L, int status, lua_KContext ctx)
285#endif
286
287
288#if defined(COMPAT53_INCLUDE_SOURCE)
289# include "compat-5.3.c"
290#endif
291
292
293#endif /* COMPAT53_H_ */
294