summaryrefslogtreecommitdiff
path: root/vendor/compat53/c-api
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-08-30 13:55:59 +1000
committerdaurnimator <quae@daurnimator.com>2017-08-30 13:56:07 +1000
commitb8e3766294b6bf11d70a8a202e633b2569675e77 (patch)
treec1f29081daf4a1dc3965ee260d88e2aeae9aa902 /vendor/compat53/c-api
parentb92fca3b68e551d2583754c80196d524890e5ee4 (diff)
parent7333333568b13db56136e2354c55556adc7714ed (diff)
downloadluaossl-b8e3766294b6bf11d70a8a202e633b2569675e77.tar.gz
luaossl-b8e3766294b6bf11d70a8a202e633b2569675e77.tar.bz2
luaossl-b8e3766294b6bf11d70a8a202e633b2569675e77.zip
Merge commit '7333333568b13db56136e2354c55556adc7714ed' as 'vendor/compat53'
Diffstat (limited to 'vendor/compat53/c-api')
-rw-r--r--vendor/compat53/c-api/compat-5.3.c617
-rw-r--r--vendor/compat53/c-api/compat-5.3.h388
2 files changed, 1005 insertions, 0 deletions
diff --git a/vendor/compat53/c-api/compat-5.3.c b/vendor/compat53/c-api/compat-5.3.c
new file mode 100644
index 0000000..883efb8
--- /dev/null
+++ b/vendor/compat53/c-api/compat-5.3.c
@@ -0,0 +1,617 @@
1#include <stddef.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <errno.h>
6#include "compat-5.3.h"
7
8/* don't compile it again if it already is included via compat53.h */
9#ifndef COMPAT53_C_
10#define COMPAT53_C_
11
12
13
14/* definitions for Lua 5.1 only */
15#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
16
17
18COMPAT53_API int lua_absindex (lua_State *L, int i) {
19 if (i < 0 && i > LUA_REGISTRYINDEX)
20 i += lua_gettop(L) + 1;
21 return i;
22}
23
24
25static void compat53_call_lua (lua_State *L, char const code[], size_t len,
26 int nargs, int nret) {
27 lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
28 if (lua_type(L, -1) != LUA_TFUNCTION) {
29 lua_pop(L, 1);
30 if (luaL_loadbuffer(L, code, len, "=none"))
31 lua_error(L);
32 lua_pushvalue(L, -1);
33 lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
34 }
35 lua_insert(L, -nargs-1);
36 lua_call(L, nargs, nret);
37}
38
39
40static const char compat53_arith_code[] =
41 "local op,a,b=...\n"
42 "if op==0 then return a+b\n"
43 "elseif op==1 then return a-b\n"
44 "elseif op==2 then return a*b\n"
45 "elseif op==3 then return a/b\n"
46 "elseif op==4 then return a%b\n"
47 "elseif op==5 then return a^b\n"
48 "elseif op==6 then return -a\n"
49 "end\n";
50
51COMPAT53_API void lua_arith (lua_State *L, int op) {
52 if (op < LUA_OPADD || op > LUA_OPUNM)
53 luaL_error(L, "invalid 'op' argument for lua_arith");
54 luaL_checkstack(L, 5, "not enough stack slots");
55 if (op == LUA_OPUNM)
56 lua_pushvalue(L, -1);
57 lua_pushnumber(L, op);
58 lua_insert(L, -3);
59 compat53_call_lua(L, compat53_arith_code,
60 sizeof(compat53_arith_code)-1, 3, 1);
61}
62
63
64static const char compat53_compare_code[] =
65 "local a,b=...\n"
66 "return a<=b\n";
67
68COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) {
69 int result = 0;
70 switch (op) {
71 case LUA_OPEQ:
72 return lua_equal(L, idx1, idx2);
73 case LUA_OPLT:
74 return lua_lessthan(L, idx1, idx2);
75 case LUA_OPLE:
76 luaL_checkstack(L, 5, "not enough stack slots");
77 idx1 = lua_absindex(L, idx1);
78 idx2 = lua_absindex(L, idx2);
79 lua_pushvalue(L, idx1);
80 lua_pushvalue(L, idx2);
81 compat53_call_lua(L, compat53_compare_code,
82 sizeof(compat53_compare_code)-1, 2, 1);
83 result = lua_toboolean(L, -1);
84 lua_pop(L, 1);
85 return result;
86 default:
87 luaL_error(L, "invalid 'op' argument for lua_compare");
88 }
89 return 0;
90}
91
92
93COMPAT53_API void lua_copy (lua_State *L, int from, int to) {
94 int abs_to = lua_absindex(L, to);
95 luaL_checkstack(L, 1, "not enough stack slots");
96 lua_pushvalue(L, from);
97 lua_replace(L, abs_to);
98}
99
100
101COMPAT53_API void lua_len (lua_State *L, int i) {
102 switch (lua_type(L, i)) {
103 case LUA_TSTRING:
104 lua_pushnumber(L, (lua_Integer)lua_objlen(L, i));
105 break;
106 case LUA_TTABLE:
107 if (!luaL_callmeta(L, i, "__len"))
108 lua_pushnumber(L, (lua_Integer)lua_objlen(L, i));
109 break;
110 case LUA_TUSERDATA:
111 if (luaL_callmeta(L, i, "__len"))
112 break;
113 /* maybe fall through */
114 default:
115 luaL_error(L, "attempt to get length of a %s value",
116 lua_typename(L, lua_type(L, i)));
117 }
118}
119
120
121COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p) {
122 int abs_i = lua_absindex(L, i);
123 lua_pushlightuserdata(L, (void*)p);
124 lua_rawget(L, abs_i);
125 return lua_type(L, -1);
126}
127
128COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) {
129 int abs_i = lua_absindex(L, i);
130 luaL_checkstack(L, 1, "not enough stack slots");
131 lua_pushlightuserdata(L, (void*)p);
132 lua_insert(L, -2);
133 lua_rawset(L, abs_i);
134}
135
136
137COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
138 lua_Integer n = lua_tointeger(L, i);
139 if (isnum != NULL) {
140 *isnum = (n != 0 || lua_isnumber(L, i));
141 }
142 return n;
143}
144
145
146COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) {
147 lua_Number n = lua_tonumber(L, i);
148 if (isnum != NULL) {
149 *isnum = (n != 0 || lua_isnumber(L, i));
150 }
151 return n;
152}
153
154
155COMPAT53_API void luaL_checkversion (lua_State *L) {
156 (void)L;
157}
158
159
160COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg) {
161 if (!lua_checkstack(L, sp+LUA_MINSTACK)) {
162 if (msg != NULL)
163 luaL_error(L, "stack overflow (%s)", msg);
164 else {
165 lua_pushliteral(L, "stack overflow");
166 lua_error(L);
167 }
168 }
169}
170
171
172COMPAT53_API int luaL_getsubtable (lua_State *L, int i, const char *name) {
173 int abs_i = lua_absindex(L, i);
174 luaL_checkstack(L, 3, "not enough stack slots");
175 lua_pushstring(L, name);
176 lua_gettable(L, abs_i);
177 if (lua_istable(L, -1))
178 return 1;
179 lua_pop(L, 1);
180 lua_newtable(L);
181 lua_pushstring(L, name);
182 lua_pushvalue(L, -2);
183 lua_settable(L, abs_i);
184 return 0;
185}
186
187
188COMPAT53_API lua_Integer luaL_len (lua_State *L, int i) {
189 lua_Integer res = 0;
190 int isnum = 0;
191 luaL_checkstack(L, 1, "not enough stack slots");
192 lua_len(L, i);
193 res = lua_tointegerx(L, -1, &isnum);
194 lua_pop(L, 1);
195 if (!isnum)
196 luaL_error(L, "object length is not an integer");
197 return res;
198}
199
200
201COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
202 luaL_checkstack(L, nup+1, "too many upvalues");
203 for (; l->name != NULL; l++) { /* fill the table with given functions */
204 int i;
205 lua_pushstring(L, l->name);
206 for (i = 0; i < nup; i++) /* copy upvalues to the top */
207 lua_pushvalue(L, -(nup + 1));
208 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
209 lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
210 }
211 lua_pop(L, nup); /* remove upvalues */
212}
213
214
215COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname) {
216 luaL_checkstack(L, 1, "not enough stack slots");
217 luaL_getmetatable(L, tname);
218 lua_setmetatable(L, -2);
219}
220
221
222COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) {
223 void *p = lua_touserdata(L, i);
224 luaL_checkstack(L, 2, "not enough stack slots");
225 if (p == NULL || !lua_getmetatable(L, i))
226 return NULL;
227 else {
228 int res = 0;
229 luaL_getmetatable(L, tname);
230 res = lua_rawequal(L, -1, -2);
231 lua_pop(L, 2);
232 if (!res)
233 p = NULL;
234 }
235 return p;
236}
237
238
239static int compat53_countlevels (lua_State *L) {
240 lua_Debug ar;
241 int li = 1, le = 1;
242 /* find an upper bound */
243 while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
244 /* do a binary search */
245 while (li < le) {
246 int m = (li + le)/2;
247 if (lua_getstack(L, m, &ar)) li = m + 1;
248 else le = m;
249 }
250 return le - 1;
251}
252
253static int compat53_findfield (lua_State *L, int objidx, int level) {
254 if (level == 0 || !lua_istable(L, -1))
255 return 0; /* not found */
256 lua_pushnil(L); /* start 'next' loop */
257 while (lua_next(L, -2)) { /* for each pair in table */
258 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
259 if (lua_rawequal(L, objidx, -1)) { /* found object? */
260 lua_pop(L, 1); /* remove value (but keep name) */
261 return 1;
262 }
263 else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
264 lua_remove(L, -2); /* remove table (but keep name) */
265 lua_pushliteral(L, ".");
266 lua_insert(L, -2); /* place '.' between the two names */
267 lua_concat(L, 3);
268 return 1;
269 }
270 }
271 lua_pop(L, 1); /* remove value */
272 }
273 return 0; /* not found */
274}
275
276static int compat53_pushglobalfuncname (lua_State *L, lua_Debug *ar) {
277 int top = lua_gettop(L);
278 lua_getinfo(L, "f", ar); /* push function */
279 lua_pushvalue(L, LUA_GLOBALSINDEX);
280 if (compat53_findfield(L, top + 1, 2)) {
281 lua_copy(L, -1, top + 1); /* move name to proper place */
282 lua_pop(L, 2); /* remove pushed values */
283 return 1;
284 }
285 else {
286 lua_settop(L, top); /* remove function and global table */
287 return 0;
288 }
289}
290
291static void compat53_pushfuncname (lua_State *L, lua_Debug *ar) {
292 if (*ar->namewhat != '\0') /* is there a name? */
293 lua_pushfstring(L, "function " LUA_QS, ar->name);
294 else if (*ar->what == 'm') /* main? */
295 lua_pushliteral(L, "main chunk");
296 else if (*ar->what == 'C') {
297 if (compat53_pushglobalfuncname(L, ar)) {
298 lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
299 lua_remove(L, -2); /* remove name */
300 }
301 else
302 lua_pushliteral(L, "?");
303 }
304 else
305 lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
306}
307
308#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
309#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
310
311COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1,
312 const char *msg, int level) {
313 lua_Debug ar;
314 int top = lua_gettop(L);
315 int numlevels = compat53_countlevels(L1);
316 int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
317 if (msg) lua_pushfstring(L, "%s\n", msg);
318 lua_pushliteral(L, "stack traceback:");
319 while (lua_getstack(L1, level++, &ar)) {
320 if (level == mark) { /* too many levels? */
321 lua_pushliteral(L, "\n\t..."); /* add a '...' */
322 level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
323 }
324 else {
325 lua_getinfo(L1, "Slnt", &ar);
326 lua_pushfstring(L, "\n\t%s:", ar.short_src);
327 if (ar.currentline > 0)
328 lua_pushfstring(L, "%d:", ar.currentline);
329 lua_pushliteral(L, " in ");
330 compat53_pushfuncname(L, &ar);
331 lua_concat(L, lua_gettop(L) - top);
332 }
333 }
334 lua_concat(L, lua_gettop(L) - top);
335}
336
337
338COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
339 int en = errno; /* calls to Lua API may change this value */
340 if (stat) {
341 lua_pushboolean(L, 1);
342 return 1;
343 }
344 else {
345 lua_pushnil(L);
346 if (fname)
347 lua_pushfstring(L, "%s: %s", fname, strerror(en));
348 else
349 lua_pushstring(L, strerror(en));
350 lua_pushnumber(L, (lua_Number)en);
351 return 3;
352 }
353}
354
355
356#if !defined(l_inspectstat) && \
357 (defined(unix) || defined(__unix) || defined(__unix__) || \
358 defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \
359 (defined(__APPLE__) && defined(__MACH__)))
360/* some form of unix; check feature macros in unistd.h for details */
361# include <unistd.h>
362/* check posix version; the relevant include files and macros probably
363 * were available before 2001, but I'm not sure */
364# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
365# include <sys/wait.h>
366# define l_inspectstat(stat,what) \
367 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
368 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
369# endif
370#endif
371
372/* provide default (no-op) version */
373#if !defined(l_inspectstat)
374# define l_inspectstat(stat,what) ((void)0)
375#endif
376
377
378COMPAT53_API int luaL_execresult (lua_State *L, int stat) {
379 const char *what = "exit";
380 if (stat == -1)
381 return luaL_fileresult(L, 0, NULL);
382 else {
383 l_inspectstat(stat, what);
384 if (*what == 'e' && stat == 0)
385 lua_pushboolean(L, 1);
386 else
387 lua_pushnil(L);
388 lua_pushstring(L, what);
389 lua_pushinteger(L, stat);
390 return 3;
391 }
392}
393
394
395COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) {
396 /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
397 B->b.p = NULL;
398 B->b.L = NULL;
399 B->b.lvl = 0;
400 /* reuse the buffer from the 5.1-style luaL_Buffer though! */
401 B->ptr = B->b.buffer;
402 B->capacity = LUAL_BUFFERSIZE;
403 B->nelems = 0;
404 B->L2 = L;
405}
406
407
408COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) {
409 if (B->capacity - B->nelems < s) { /* needs to grow */
410 char* newptr = NULL;
411 size_t newcap = B->capacity * 2;
412 if (newcap - B->nelems < s)
413 newcap = B->nelems + s;
414 if (newcap < B->capacity) /* overflow */
415 luaL_error(B->L2, "buffer too large");
416 newptr = (char*)lua_newuserdata(B->L2, newcap);
417 memcpy(newptr, B->ptr, B->nelems);
418 if (B->ptr != B->b.buffer)
419 lua_replace(B->L2, -2); /* remove old buffer */
420 B->ptr = newptr;
421 B->capacity = newcap;
422 }
423 return B->ptr+B->nelems;
424}
425
426
427COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l) {
428 memcpy(luaL_prepbuffsize(B, l), s, l);
429 luaL_addsize(B, l);
430}
431
432
433COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B) {
434 size_t len = 0;
435 const char *s = lua_tolstring(B->L2, -1, &len);
436 if (!s)
437 luaL_error(B->L2, "cannot convert value to string");
438 if (B->ptr != B->b.buffer)
439 lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
440 luaL_addlstring(B, s, len);
441 lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
442}
443
444
445void luaL_pushresult (luaL_Buffer_53 *B) {
446 lua_pushlstring(B->L2, B->ptr, B->nelems);
447 if (B->ptr != B->b.buffer)
448 lua_replace(B->L2, -2); /* remove userdata buffer */
449}
450
451
452#endif /* Lua 5.1 */
453
454
455
456/* definitions for Lua 5.1 and Lua 5.2 */
457#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502
458
459
460COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) {
461 index = lua_absindex(L, index);
462 lua_pushinteger(L, i);
463 lua_gettable(L, index);
464 return lua_type(L, -1);
465}
466
467
468COMPAT53_API int lua_isinteger (lua_State *L, int index) {
469 if (lua_type(L, index) == LUA_TNUMBER) {
470 lua_Number n = lua_tonumber(L, index);
471 lua_Integer i = lua_tointeger(L, index);
472 if (i == n)
473 return 1;
474 }
475 return 0;
476}
477
478
479static void compat53_reverse (lua_State *L, int a, int b) {
480 for (; a < b; ++a, --b) {
481 lua_pushvalue(L, a);
482 lua_pushvalue(L, b);
483 lua_replace(L, a);
484 lua_replace(L, b);
485 }
486}
487
488
489COMPAT53_API void lua_rotate (lua_State *L, int idx, int n) {
490 int n_elems = 0;
491 idx = lua_absindex(L, idx);
492 n_elems = lua_gettop(L)-idx+1;
493 if (n < 0)
494 n += n_elems;
495 if ( n > 0 && n < n_elems) {
496 luaL_checkstack(L, 2, "not enough stack slots available");
497 n = n_elems - n;
498 compat53_reverse(L, idx, idx+n-1);
499 compat53_reverse(L, idx+n, idx+n_elems-1);
500 compat53_reverse(L, idx, idx+n_elems-1);
501 }
502}
503
504
505COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i) {
506 luaL_checkstack(L, 1, "not enough stack slots available");
507 index = lua_absindex(L, index);
508 lua_pushinteger(L, i);
509 lua_insert(L, -2);
510 lua_settable(L, index);
511}
512
513
514#if !defined(lua_str2number)
515# define lua_str2number(s, p) strtod((s), (p))
516#endif
517
518COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s) {
519 char* endptr;
520 lua_Number n = lua_str2number(s, &endptr);
521 if (endptr != s) {
522 while (*endptr != '\0' && isspace((unsigned char)*endptr))
523 ++endptr;
524 if (*endptr == '\0') {
525 lua_pushnumber(L, n);
526 return endptr - s + 1;
527 }
528 }
529 return 0;
530}
531
532
533COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
534 if (!luaL_callmeta(L, idx, "__tostring")) {
535 int t = lua_type(L, idx), tt = 0;
536 char const* name = NULL;
537 switch (t) {
538 case LUA_TNIL:
539 lua_pushliteral(L, "nil");
540 break;
541 case LUA_TSTRING:
542 case LUA_TNUMBER:
543 lua_pushvalue(L, idx);
544 break;
545 case LUA_TBOOLEAN:
546 if (lua_toboolean(L, idx))
547 lua_pushliteral(L, "true");
548 else
549 lua_pushliteral(L, "false");
550 break;
551 default:
552 tt = luaL_getmetafield(L, idx, "__name");
553 name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
554 lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
555 if (tt != LUA_TNIL)
556 lua_replace(L, -2);
557 break;
558 }
559 } else {
560 if (!lua_isstring(L, -1))
561 luaL_error(L, "'__tostring' must return a string");
562 }
563 return lua_tolstring(L, -1, len);
564}
565
566
567COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
568 lua_CFunction openf, int glb) {
569 luaL_checkstack(L, 3, "not enough stack slots available");
570 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
571 if (lua_getfield(L, -1, modname) == LUA_TNIL) {
572 lua_pop(L, 1);
573 lua_pushcfunction(L, openf);
574 lua_pushstring(L, modname);
575 lua_call(L, 1, 1);
576 lua_pushvalue(L, -1);
577 lua_setfield(L, -3, modname);
578 }
579 if (glb) {
580 lua_pushvalue(L, -1);
581 lua_setglobal(L, modname);
582 }
583 lua_replace(L, -2);
584}
585
586
587#endif /* Lua 5.1 and 5.2 */
588
589
590#endif /* COMPAT53_C_ */
591
592
593/*********************************************************************
594* This file contains parts of Lua 5.2's and Lua 5.3's source code:
595*
596* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
597*
598* Permission is hereby granted, free of charge, to any person obtaining
599* a copy of this software and associated documentation files (the
600* "Software"), to deal in the Software without restriction, including
601* without limitation the rights to use, copy, modify, merge, publish,
602* distribute, sublicense, and/or sell copies of the Software, and to
603* permit persons to whom the Software is furnished to do so, subject to
604* the following conditions:
605*
606* The above copyright notice and this permission notice shall be
607* included in all copies or substantial portions of the Software.
608*
609* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
610* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
611* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
612* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
613* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
614* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
615* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
616*********************************************************************/
617
diff --git a/vendor/compat53/c-api/compat-5.3.h b/vendor/compat53/c-api/compat-5.3.h
new file mode 100644
index 0000000..bee77a1
--- /dev/null
+++ b/vendor/compat53/c-api/compat-5.3.h
@@ -0,0 +1,388 @@
1#ifndef COMPAT53_H_
2#define 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#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
13}
14#endif
15
16
17#if defined(COMPAT53_PREFIX)
18/* - change the symbol names of functions to avoid linker conflicts
19 * - compat-5.3.c needs to be compiled (and linked) separately
20 */
21# if !defined(COMPAT53_API)
22# define COMPAT53_API extern
23# endif
24# undef COMPAT53_INCLUDE_SOURCE
25#else /* COMPAT53_PREFIX */
26/* - make all functions static and include the source.
27 * - compat-5.3.c doesn't need to be compiled (and linked) separately
28 */
29# define COMPAT53_PREFIX compat53
30# undef COMPAT53_API
31# if defined(__GNUC__) || defined(__clang__)
32# define COMPAT53_API __attribute__((__unused__)) static
33# else
34# define COMPAT53_API static
35# endif
36# define COMPAT53_INCLUDE_SOURCE
37#endif /* COMPAT53_PREFIX */
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 * luaL_loadbufferx
54 * luaL_loadfilex
55 */
56
57#ifndef LUA_OK
58# define LUA_OK 0
59#endif
60#ifndef LUA_OPADD
61# define LUA_OPADD 0
62#endif
63#ifndef LUA_OPSUB
64# define LUA_OPSUB 1
65#endif
66#ifndef LUA_OPMUL
67# define LUA_OPMUL 2
68#endif
69#ifndef LUA_OPDIV
70# define LUA_OPDIV 3
71#endif
72#ifndef LUA_OPMOD
73# define LUA_OPMOD 4
74#endif
75#ifndef LUA_OPPOW
76# define LUA_OPPOW 5
77#endif
78#ifndef LUA_OPUNM
79# define LUA_OPUNM 6
80#endif
81#ifndef LUA_OPEQ
82# define LUA_OPEQ 0
83#endif
84#ifndef LUA_OPLT
85# define LUA_OPLT 1
86#endif
87#ifndef LUA_OPLE
88# define LUA_OPLE 2
89#endif
90
91typedef size_t lua_Unsigned;
92
93typedef 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
102#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
103COMPAT53_API int lua_absindex (lua_State *L, int i);
104
105#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
106COMPAT53_API void lua_arith (lua_State *L, int op);
107
108#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
109COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op);
110
111#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
112COMPAT53_API void lua_copy (lua_State *L, int from, int to);
113
114#define lua_getuservalue(L, i) \
115 (lua_getfenv((L), (i)), lua_type((L), -1))
116#define lua_setuservalue(L, i) \
117 (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
118
119#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
120COMPAT53_API void lua_len (lua_State *L, int i);
121
122#define lua_pushstring(L, s) \
123 (lua_pushstring((L), (s)), lua_tostring((L), -1))
124
125#define lua_pushlstring(L, s, len) \
126 ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
127
128#ifndef luaL_newlibtable
129# define luaL_newlibtable(L, l) \
130 (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
131#endif
132#ifndef luaL_newlib
133# define luaL_newlib(L, l) \
134 (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
135#endif
136
137#define lua_pushglobaltable(L) \
138 lua_pushvalue((L), LUA_GLOBALSINDEX)
139
140#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
141COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p);
142
143#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
144COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
145
146#define lua_rawlen(L, i) lua_objlen((L), (i))
147
148#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx)
149COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum);
150
151#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
152COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum);
153
154#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
155COMPAT53_API void luaL_checkversion (lua_State *L);
156
157#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
158COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg);
159
160#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
161COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name);
162
163#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
164COMPAT53_API lua_Integer luaL_len (lua_State *L, int i);
165
166#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
167COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
168
169#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
170COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname);
171
172#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
173COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname);
174
175#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
176COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);
177
178#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
179COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname);
180
181#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
182COMPAT53_API int luaL_execresult (lua_State *L, int stat);
183
184#define lua_callk(L, na, nr, ctx, cont) \
185 ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
186#define lua_pcallk(L, na, nr, err, ctx, cont) \
187 ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
188
189#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
190COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B);
191
192#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
193COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s);
194
195#define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
196COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l);
197
198#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
199COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B);
200
201#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
202COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B);
203
204#undef luaL_buffinitsize
205#define luaL_buffinitsize(L, B, s) \
206 (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
207
208#undef luaL_prepbuffer
209#define luaL_prepbuffer(B) \
210 luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
211
212#undef luaL_addchar
213#define luaL_addchar(B, c) \
214 ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
215 ((B)->ptr[(B)->nelems++] = (c)))
216
217#undef luaL_addsize
218#define luaL_addsize(B, s) \
219 ((B)->nelems += (s))
220
221#undef luaL_addstring
222#define luaL_addstring(B, s) \
223 luaL_addlstring((B), (s), strlen((s)))
224
225#undef luaL_pushresultsize
226#define luaL_pushresultsize(B, s) \
227 (luaL_addsize((B), (s)), luaL_pushresult((B)))
228
229#if defined(LUA_COMPAT_APIINTCASTS)
230#define lua_pushunsigned(L, n) \
231 lua_pushinteger((L), (lua_Integer)(n))
232#define lua_tounsignedx(L, i, is) \
233 ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
234#define lua_tounsigned(L, i) \
235 lua_tounsignedx((L), (i), NULL)
236#define luaL_checkunsigned(L, a) \
237 ((lua_Unsigned)luaL_checkinteger((L), (a)))
238#define luaL_optunsigned(L, a, d) \
239 ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
240#endif
241
242#endif /* Lua 5.1 only */
243
244
245
246/* declarations for Lua 5.1 and 5.2 */
247#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
248
249typedef int lua_KContext;
250
251typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
252
253#define lua_dump(L, w, d, s) \
254 ((void)(s), lua_dump((L), (w), (d)))
255
256#define lua_getfield(L, i, k) \
257 (lua_getfield((L), (i), (k)), lua_type((L), -1))
258
259#define lua_gettable(L, i) \
260 (lua_gettable((L), (i)), lua_type((L), -1))
261
262#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
263COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i);
264
265#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
266COMPAT53_API int lua_isinteger (lua_State *L, int index);
267
268#define lua_numbertointeger(n, p) \
269 ((*(p) = (lua_Integer)(n)), 1)
270
271#define lua_rawget(L, i) \
272 (lua_rawget((L), (i)), lua_type((L), -1))
273
274#define lua_rawgeti(L, i, n) \
275 (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
276
277#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
278COMPAT53_API void lua_rotate (lua_State *L, int idx, int n);
279
280#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
281COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i);
282
283#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
284COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s);
285
286#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
287COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
288
289#define luaL_getmetafield(L, o, e) \
290 (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
291
292#define luaL_newmetatable(L, tn) \
293 (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
294
295#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
296COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
297 lua_CFunction openf, int glb );
298
299#endif /* Lua 5.1 and Lua 5.2 */
300
301
302
303/* declarations for Lua 5.2 */
304#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
305
306/* XXX not implemented:
307 * lua_isyieldable
308 * lua_getextraspace
309 * lua_arith (new operators)
310 * lua_pushfstring (new formats)
311 */
312
313#define lua_getglobal(L, n) \
314 (lua_getglobal((L), (n)), lua_type((L), -1))
315
316#define lua_getuservalue(L, i) \
317 (lua_getuservalue((L), (i)), lua_type((L), -1))
318
319#define lua_pushlstring(L, s, len) \
320 (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
321
322#define lua_rawgetp(L, i, p) \
323 (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
324
325#define LUA_KFUNCTION(_name) \
326 static int (_name)(lua_State *L, int status, lua_KContext ctx); \
327 static int (_name ## _52)(lua_State *L) { \
328 lua_KContext ctx; \
329 int status = lua_getctx(L, &ctx); \
330 return (_name)(L, status, ctx); \
331 } \
332 static int (_name)(lua_State *L, int status, lua_KContext ctx)
333
334#define lua_pcallk(L, na, nr, err, ctx, cont) \
335 lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
336
337#define lua_callk(L, na, nr, ctx, cont) \
338 lua_callk((L), (na), (nr), (ctx), cont ## _52)
339
340#define lua_yieldk(L, nr, ctx, cont) \
341 lua_yieldk((L), (nr), (ctx), cont ## _52)
342
343#ifdef lua_call
344# undef lua_call
345# define lua_call(L, na, nr) \
346 (lua_callk)((L), (na), (nr), 0, NULL)
347#endif
348
349#ifdef lua_pcall
350# undef lua_pcall
351# define lua_pcall(L, na, nr, err) \
352 (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
353#endif
354
355#ifdef lua_yield
356# undef lua_yield
357# define lua_yield(L, nr) \
358 (lua_yieldk)((L), (nr), 0, NULL)
359#endif
360
361#endif /* Lua 5.2 only */
362
363
364
365/* other Lua versions */
366#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 503
367
368# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, or 5.3)"
369
370#endif /* other Lua versions except 5.1, 5.2, and 5.3 */
371
372
373
374/* helper macro for defining continuation functions (for every version
375 * *except* Lua 5.2) */
376#ifndef LUA_KFUNCTION
377#define LUA_KFUNCTION(_name) \
378 static int (_name)(lua_State *L, int status, lua_KContext ctx)
379#endif
380
381
382#if defined(COMPAT53_INCLUDE_SOURCE)
383# include "compat-5.3.c"
384#endif
385
386
387#endif /* COMPAT53_H_ */
388