dl
compat-5.3.c.h
浏览该文件的文档.
2
3#include <stddef.h>
4#include <stdlib.h>
5#include <string.h>
6#include <ctype.h>
7#include <errno.h>
8#include <stdio.h>
9
10/* don't compile it again if it already is included via compat53.h */
11#ifndef KEPLER_PROJECT_COMPAT53_C_
12#define KEPLER_PROJECT_COMPAT53_C_
13
14
15
16/* definitions for Lua 5.1 only */
17#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
18
19#ifndef COMPAT53_FOPEN_NO_LOCK
20#if defined(_MSC_VER)
21#define COMPAT53_FOPEN_NO_LOCK 1
22#else /* otherwise */
23#define COMPAT53_FOPEN_NO_LOCK 0
24#endif /* VC++ only so far */
25#endif /* No-lock fopen_s usage if possible */
26
27#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
28#include <share.h>
29#endif /* VC++ _fsopen for share-allowed file read */
30
31#ifndef COMPAT53_HAVE_STRERROR_R
32#if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined(__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6))
33#define COMPAT53_HAVE_STRERROR_R 1
34#else /* none of the defines matched: define to 0 */
35#define COMPAT53_HAVE_STRERROR_R 0
36#endif /* have strerror_r of some form */
37#endif /* strerror_r */
38
39#ifndef COMPAT53_HAVE_STRERROR_S
40#if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__)
41#define COMPAT53_HAVE_STRERROR_S 1
42#else /* not VC++ or C11 */
43#define COMPAT53_HAVE_STRERROR_S 0
44#endif /* strerror_s from VC++ or C11 */
45#endif /* strerror_s */
46
47#ifndef COMPAT53_LUA_FILE_BUFFER_SIZE
48#define COMPAT53_LUA_FILE_BUFFER_SIZE 4096
49#endif /* Lua File Buffer Size */
50
51
52static char* compat53_strerror(int en, char* buff, size_t sz) {
53#if COMPAT53_HAVE_STRERROR_R
54 /* use strerror_r here, because it's available on these specific platforms */
55 if (sz > 0) {
56 buff[0] = '\0';
57 /* we don't care whether the GNU version or the XSI version is used: */
58 if (strerror_r(en, buff, sz)) {
59 /* Yes, we really DO want to ignore the return value!
60 * GCC makes that extra hard, not even a (void) cast will do. */
61 }
62 if (buff[0] == '\0') {
63 /* Buffer is unchanged, so we probably have called GNU strerror_r which
64 * returned a static constant string. Chances are that strerror will
65 * return the same static constant string and therefore be thread-safe. */
66 return strerror(en);
67 }
68 }
69 return buff; /* sz is 0 *or* strerror_r wrote into the buffer */
70#elif COMPAT53_HAVE_STRERROR_S
71 /* for MSVC and other C11 implementations, use strerror_s since it's
72 * provided by default by the libraries */
73 strerror_s(buff, sz, en);
74 return buff;
75#else
76 /* fallback, but strerror is not guaranteed to be threadsafe due to modifying
77 * errno itself and some impls not locking a static buffer for it ... but most
78 * known systems have threadsafe errno: this might only change if the locale
79 * is changed out from under someone while this function is being called */
80 (void)buff;
81 (void)sz;
82 return strerror(en);
83#endif
84}
85
86
87COMPAT53_API int lua_absindex(lua_State* L, int i) {
88 if (i < 0 && i > LUA_REGISTRYINDEX)
89 i += lua_gettop(L) + 1;
90 return i;
91}
92
93
94static void compat53_call_lua(lua_State* L, char const code[], size_t len, int nargs, int nret) {
95 lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
96 if (lua_type(L, -1) != LUA_TFUNCTION) {
97 lua_pop(L, 1);
98 if (luaL_loadbuffer(L, code, len, "=none"))
99 lua_error(L);
100 lua_pushvalue(L, -1);
101 lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
102 }
103 lua_insert(L, -nargs - 1);
104 lua_call(L, nargs, nret);
105}
106
107
108COMPAT53_API void lua_arith(lua_State* L, int op) {
109 static const char compat53_arith_code[]
110 = "local op,a,b=...\n"
111 "if op==0 then return a+b\n"
112 "elseif op==1 then return a-b\n"
113 "elseif op==2 then return a*b\n"
114 "elseif op==3 then return a/b\n"
115 "elseif op==4 then return a%b\n"
116 "elseif op==5 then return a^b\n"
117 "elseif op==6 then return -a\n"
118 "end\n";
119
120 if (op < LUA_OPADD || op > LUA_OPUNM)
121 luaL_error(L, "invalid 'op' argument for lua_arith");
122 luaL_checkstack(L, 5, "not enough stack slots");
123 if (op == LUA_OPUNM)
124 lua_pushvalue(L, -1);
125 lua_pushnumber(L, op);
126 lua_insert(L, -3);
127 compat53_call_lua(L, compat53_arith_code, sizeof(compat53_arith_code) - 1, 3, 1);
128}
129
130
131COMPAT53_API int lua_compare(lua_State* L, int idx1, int idx2, int op) {
132 static const char compat53_compare_code[]
133 = "local a,b=...\n"
134 "return a<=b\n";
135
136 int result = 0;
137 switch (op) {
138 case LUA_OPEQ:
139 return lua_equal(L, idx1, idx2);
140 case LUA_OPLT:
141 return lua_lessthan(L, idx1, idx2);
142 case LUA_OPLE:
143 luaL_checkstack(L, 5, "not enough stack slots");
144 idx1 = lua_absindex(L, idx1);
145 idx2 = lua_absindex(L, idx2);
146 lua_pushvalue(L, idx1);
147 lua_pushvalue(L, idx2);
148 compat53_call_lua(L, compat53_compare_code, sizeof(compat53_compare_code) - 1, 2, 1);
149 result = lua_toboolean(L, -1);
150 lua_pop(L, 1);
151 return result;
152 default:
153 luaL_error(L, "invalid 'op' argument for lua_compare");
154 }
155 return 0;
156}
157
158
159COMPAT53_API void lua_copy(lua_State* L, int from, int to) {
160 int abs_to = lua_absindex(L, to);
161 luaL_checkstack(L, 1, "not enough stack slots");
162 lua_pushvalue(L, from);
163 lua_replace(L, abs_to);
164}
165
166
167COMPAT53_API void lua_len(lua_State* L, int i) {
168 switch (lua_type(L, i)) {
169 case LUA_TSTRING:
170 lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
171 break;
172 case LUA_TTABLE:
173 if (!luaL_callmeta(L, i, "__len"))
174 lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
175 break;
176 case LUA_TUSERDATA:
177 if (luaL_callmeta(L, i, "__len"))
178 break;
179 /* FALLTHROUGH */
180 default:
181 luaL_error(L, "attempt to get length of a %s value", lua_typename(L, lua_type(L, i)));
182 }
183}
184
185
186COMPAT53_API int lua_rawgetp(lua_State* L, int i, const void* p) {
187 int abs_i = lua_absindex(L, i);
188 lua_pushlightuserdata(L, (void*)p);
189 lua_rawget(L, abs_i);
190 return lua_type(L, -1);
191}
192
193COMPAT53_API void lua_rawsetp(lua_State* L, int i, const void* p) {
194 int abs_i = lua_absindex(L, i);
195 luaL_checkstack(L, 1, "not enough stack slots");
196 lua_pushlightuserdata(L, (void*)p);
197 lua_insert(L, -2);
198 lua_rawset(L, abs_i);
199}
200
201
202COMPAT53_API lua_Number lua_tonumberx(lua_State* L, int i, int* isnum) {
203 lua_Number n = lua_tonumber(L, i);
204 if (isnum != NULL) {
205 *isnum = (n != 0 || lua_isnumber(L, i));
206 }
207 return n;
208}
209
210
211COMPAT53_API void luaL_checkversion(lua_State* L) {
212 (void)L;
213}
214
215
216COMPAT53_API void luaL_checkstack(lua_State* L, int sp, const char* msg) {
217 if (!lua_checkstack(L, sp + LUA_MINSTACK)) {
218 if (msg != NULL)
219 luaL_error(L, "stack overflow (%s)", msg);
220 else {
221 lua_pushliteral(L, "stack overflow");
222 lua_error(L);
223 }
224 }
225}
226
227
228COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char* name) {
229 int abs_i = lua_absindex(L, i);
230 luaL_checkstack(L, 3, "not enough stack slots");
231 lua_pushstring(L, name);
232 lua_gettable(L, abs_i);
233 if (lua_istable(L, -1))
234 return 1;
235 lua_pop(L, 1);
236 lua_newtable(L);
237 lua_pushstring(L, name);
238 lua_pushvalue(L, -2);
239 lua_settable(L, abs_i);
240 return 0;
241}
242
243
244COMPAT53_API lua_Integer luaL_len(lua_State* L, int i) {
245 lua_Integer res = 0;
246 int isnum = 0;
247 luaL_checkstack(L, 1, "not enough stack slots");
248 lua_len(L, i);
249 res = lua_tointegerx(L, -1, &isnum);
250 lua_pop(L, 1);
251 if (!isnum)
252 luaL_error(L, "object length is not an integer");
253 return res;
254}
255
256
257COMPAT53_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
258 luaL_checkstack(L, nup + 1, "too many upvalues");
259 for (; l->name != NULL; l++) { /* fill the table with given functions */
260 int i;
261 lua_pushstring(L, l->name);
262 for (i = 0; i < nup; i++) /* copy upvalues to the top */
263 lua_pushvalue(L, -(nup + 1));
264 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
265 lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
266 }
267 lua_pop(L, nup); /* remove upvalues */
268}
269
270
271COMPAT53_API void luaL_setmetatable(lua_State* L, const char* tname) {
272 luaL_checkstack(L, 1, "not enough stack slots");
273 luaL_getmetatable(L, tname);
274 lua_setmetatable(L, -2);
275}
276
277
278COMPAT53_API void* luaL_testudata(lua_State* L, int i, const char* tname) {
279 void* p = lua_touserdata(L, i);
280 luaL_checkstack(L, 2, "not enough stack slots");
281 if (p == NULL || !lua_getmetatable(L, i))
282 return NULL;
283 else {
284 int res = 0;
285 luaL_getmetatable(L, tname);
286 res = lua_rawequal(L, -1, -2);
287 lua_pop(L, 2);
288 if (!res)
289 p = NULL;
290 }
291 return p;
292}
293
294
295static int compat53_countlevels(lua_State* L) {
296 lua_Debug ar;
297 int li = 1, le = 1;
298 /* find an upper bound */
299 while (lua_getstack(L, le, &ar)) {
300 li = le;
301 le *= 2;
302 }
303 /* do a binary search */
304 while (li < le) {
305 int m = (li + le) / 2;
306 if (lua_getstack(L, m, &ar))
307 li = m + 1;
308 else
309 le = m;
310 }
311 return le - 1;
312}
313
314static int compat53_findfield(lua_State* L, int objidx, int level) {
315 if (level == 0 || !lua_istable(L, -1))
316 return 0; /* not found */
317 lua_pushnil(L); /* start 'next' loop */
318 while (lua_next(L, -2)) { /* for each pair in table */
319 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
320 if (lua_rawequal(L, objidx, -1)) { /* found object? */
321 lua_pop(L, 1); /* remove value (but keep name) */
322 return 1;
323 }
324 else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
325 lua_remove(L, -2); /* remove table (but keep name) */
326 lua_pushliteral(L, ".");
327 lua_insert(L, -2); /* place '.' between the two names */
328 lua_concat(L, 3);
329 return 1;
330 }
331 }
332 lua_pop(L, 1); /* remove value */
333 }
334 return 0; /* not found */
335}
336
337static int compat53_pushglobalfuncname(lua_State* L, lua_Debug* ar) {
338 int top = lua_gettop(L);
339 lua_getinfo(L, "f", ar); /* push function */
340 lua_pushvalue(L, LUA_GLOBALSINDEX);
341 if (compat53_findfield(L, top + 1, 2)) {
342 lua_copy(L, -1, top + 1); /* move name to proper place */
343 lua_pop(L, 2); /* remove pushed values */
344 return 1;
345 }
346 else {
347 lua_settop(L, top); /* remove function and global table */
348 return 0;
349 }
350}
351
352static void compat53_pushfuncname(lua_State* L, lua_Debug* ar) {
353 if (*ar->namewhat != '\0') /* is there a name? */
354 lua_pushfstring(L, "function " LUA_QS, ar->name);
355 else if (*ar->what == 'm') /* main? */
356 lua_pushliteral(L, "main chunk");
357 else if (*ar->what == 'C') {
358 if (compat53_pushglobalfuncname(L, ar)) {
359 lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
360 lua_remove(L, -2); /* remove name */
361 }
362 else
363 lua_pushliteral(L, "?");
364 }
365 else
366 lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
367}
368
369#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
370#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
371
372COMPAT53_API void luaL_traceback(lua_State* L, lua_State* L1, const char* msg, int level) {
373 lua_Debug ar;
374 int top = lua_gettop(L);
375 int numlevels = compat53_countlevels(L1);
376 int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
377 if (msg)
378 lua_pushfstring(L, "%s\n", msg);
379 lua_pushliteral(L, "stack traceback:");
380 while (lua_getstack(L1, level++, &ar)) {
381 if (level == mark) { /* too many levels? */
382 lua_pushliteral(L, "\n\t..."); /* add a '...' */
383 level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
384 }
385 else {
386 lua_getinfo(L1, "Slnt", &ar);
387 lua_pushfstring(L, "\n\t%s:", ar.short_src);
388 if (ar.currentline > 0)
389 lua_pushfstring(L, "%d:", ar.currentline);
390 lua_pushliteral(L, " in ");
391 compat53_pushfuncname(L, &ar);
392 lua_concat(L, lua_gettop(L) - top);
393 }
394 }
395 lua_concat(L, lua_gettop(L) - top);
396}
397
398
399COMPAT53_API int luaL_fileresult(lua_State* L, int stat, const char* fname) {
400 const char* serr = NULL;
401 int en = errno; /* calls to Lua API may change this value */
402 char buf[512] = { 0 };
403 if (stat) {
404 lua_pushboolean(L, 1);
405 return 1;
406 }
407 else {
408 lua_pushnil(L);
409 serr = compat53_strerror(en, buf, sizeof(buf));
410 if (fname)
411 lua_pushfstring(L, "%s: %s", fname, serr);
412 else
413 lua_pushstring(L, serr);
414 lua_pushnumber(L, (lua_Number)en);
415 return 3;
416 }
417}
418
419
420static int compat53_checkmode(lua_State* L, const char* mode, const char* modename, int err) {
421 if (mode && strchr(mode, modename[0]) == NULL) {
422 lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode);
423 return err;
424 }
425 return LUA_OK;
426}
427
428
429typedef struct {
430 lua_Reader reader;
431 void* ud;
432 int has_peeked_data;
433 const char* peeked_data;
434 size_t peeked_data_size;
435} compat53_reader_data;
436
437
438static const char* compat53_reader(lua_State* L, void* ud, size_t* size) {
439 compat53_reader_data* data = (compat53_reader_data*)ud;
440 if (data->has_peeked_data) {
441 data->has_peeked_data = 0;
442 *size = data->peeked_data_size;
443 return data->peeked_data;
444 }
445 else
446 return data->reader(L, data->ud, size);
447}
448
449
450COMPAT53_API int lua_load(lua_State* L, lua_Reader reader, void* data, const char* source, const char* mode) {
451 int status = LUA_OK;
452 compat53_reader_data compat53_data = { reader, data, 1, 0, 0 };
453 compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size));
454 if (compat53_data.peeked_data && compat53_data.peeked_data_size && compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */
455 status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
456 else
457 status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
458 if (status != LUA_OK)
459 return status;
460 /* we need to call the original 5.1 version of lua_load! */
461#undef lua_load
462 return lua_load(L, compat53_reader, &compat53_data, source);
463#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
464}
465
466
467typedef struct {
468 int n; /* number of pre-read characters */
469 FILE* f; /* file being read */
470 char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
471} compat53_LoadF;
472
473
474static const char* compat53_getF(lua_State* L, void* ud, size_t* size) {
475 compat53_LoadF* lf = (compat53_LoadF*)ud;
476 (void)L; /* not used */
477 if (lf->n > 0) { /* are there pre-read characters to be read? */
478 *size = lf->n; /* return them (chars already in buffer) */
479 lf->n = 0; /* no more pre-read characters */
480 }
481 else { /* read a block from file */
482 /* 'fread' can return > 0 *and* set the EOF flag. If next call to
483 'compat53_getF' called 'fread', it might still wait for user input.
484 The next check avoids this problem. */
485 if (feof(lf->f))
486 return NULL;
487 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
488 }
489 return lf->buff;
490}
491
492
493static int compat53_errfile(lua_State* L, const char* what, int fnameindex) {
494 char buf[512] = { 0 };
495 const char* serr = compat53_strerror(errno, buf, sizeof(buf));
496 const char* filename = lua_tostring(L, fnameindex) + 1;
497 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
498 lua_remove(L, fnameindex);
499 return LUA_ERRFILE;
500}
501
502
503static int compat53_skipBOM(compat53_LoadF* lf) {
504 const char* p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
505 int c;
506 lf->n = 0;
507 do {
508 c = getc(lf->f);
509 if (c == EOF || c != *(const unsigned char*)p++)
510 return c;
511 lf->buff[lf->n++] = (char)c; /* to be read by the parser */
512 } while (*p != '\0');
513 lf->n = 0; /* prefix matched; discard it */
514 return getc(lf->f); /* return next character */
515}
516
517
518/*
519** reads the first character of file 'f' and skips an optional BOM mark
520** in its beginning plus its first line if it starts with '#'. Returns
521** true if it skipped the first line. In any case, '*cp' has the
522** first "valid" character of the file (after the optional BOM and
523** a first-line comment).
524*/
525static int compat53_skipcomment(compat53_LoadF* lf, int* cp) {
526 int c = *cp = compat53_skipBOM(lf);
527 if (c == '#') { /* first line is a comment (Unix exec. file)? */
528 do { /* skip first line */
529 c = getc(lf->f);
530 } while (c != EOF && c != '\n');
531 *cp = getc(lf->f); /* skip end-of-line, if present */
532 return 1; /* there was a comment */
533 }
534 else
535 return 0; /* no comment */
536}
537
538
539COMPAT53_API int luaL_loadfilex(lua_State* L, const char* filename, const char* mode) {
540 compat53_LoadF lf;
541 int status, readstatus;
542 int c;
543 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
544 if (filename == NULL) {
545 lua_pushliteral(L, "=stdin");
546 lf.f = stdin;
547 }
548 else {
549 lua_pushfstring(L, "@%s", filename);
550#if defined(_MSC_VER)
551 /* This code is here to stop a deprecation error that stops builds
552 * if a certain macro is defined. While normally not caring would
553 * be best, some header-only libraries and builds can't afford to
554 * dictate this to the user. A quick check shows that fopen_s this
555 * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
556 * possibly even before that so we don't need to do any version
557 * number checks, since this has been there since forever. */
558
559 /* TO USER: if you want the behavior of typical fopen_s/fopen,
560 * which does lock the file on VC++, define the macro used below to 0 */
561#if COMPAT53_FOPEN_NO_LOCK
562 lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
563 if (lf.f == NULL)
564 return compat53_errfile(L, "open", fnameindex);
565#else /* use default locking version */
566 if (fopen_s(&lf.f, filename, "r") != 0)
567 return compat53_errfile(L, "open", fnameindex);
568#endif /* Locking vs. No-locking fopen variants */
569#else
570 lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
571 if (lf.f == NULL)
572 return compat53_errfile(L, "open", fnameindex);
573#endif
574 }
575 if (compat53_skipcomment(&lf, &c)) /* read initial portion */
576 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
577 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
578#if defined(_MSC_VER)
579 if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
580 return compat53_errfile(L, "reopen", fnameindex);
581#else
582 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
583 if (lf.f == NULL)
584 return compat53_errfile(L, "reopen", fnameindex);
585#endif
586 compat53_skipcomment(&lf, &c); /* re-read initial portion */
587 }
588 if (c != EOF)
589 lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
590 status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
591 readstatus = ferror(lf.f);
592 if (filename)
593 fclose(lf.f); /* close file (even in case of errors) */
594 if (readstatus) {
595 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
596 return compat53_errfile(L, "read", fnameindex);
597 }
598 lua_remove(L, fnameindex);
599 return status;
600}
601
602
603COMPAT53_API int luaL_loadbufferx(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode) {
604 int status = LUA_OK;
605 if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) {
606 status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
607 }
608 else {
609 status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
610 }
611 if (status != LUA_OK)
612 return status;
613 return luaL_loadbuffer(L, buff, sz, name);
614}
615
616
617#if !defined(l_inspectstat) \
618 && (defined(unix) || defined(__unix) || defined(__unix__) || defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || (defined(__APPLE__) && defined(__MACH__)))
619/* some form of unix; check feature macros in unistd.h for details */
620#include <unistd.h>
621/* check posix version; the relevant include files and macros probably
622 * were available before 2001, but I'm not sure */
623#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
624#include <sys/wait.h>
625#define l_inspectstat(stat, what) \
626 if (WIFEXITED(stat)) { \
627 stat = WEXITSTATUS(stat); \
628 } \
629 else if (WIFSIGNALED(stat)) { \
630 stat = WTERMSIG(stat); \
631 what = "signal"; \
632 }
633#endif
634#endif
635
636/* provide default (no-op) version */
637#if !defined(l_inspectstat)
638#define l_inspectstat(stat, what) ((void)0)
639#endif
640
641
642COMPAT53_API int luaL_execresult(lua_State* L, int stat) {
643 const char* what = "exit";
644 if (stat == -1)
645 return luaL_fileresult(L, 0, NULL);
646 else {
647 l_inspectstat(stat, what);
648 if (*what == 'e' && stat == 0)
649 lua_pushboolean(L, 1);
650 else
651 lua_pushnil(L);
652 lua_pushstring(L, what);
653 lua_pushinteger(L, stat);
654 return 3;
655 }
656}
657
658
659COMPAT53_API void luaL_buffinit(lua_State* L, luaL_Buffer_53* B) {
660 /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
661 B->b.p = NULL;
662 B->b.L = NULL;
663 B->b.lvl = 0;
664 /* reuse the buffer from the 5.1-style luaL_Buffer though! */
665 B->ptr = B->b.buffer;
666 B->capacity = LUAL_BUFFERSIZE;
667 B->nelems = 0;
668 B->L2 = L;
669}
670
671
672COMPAT53_API char* luaL_prepbuffsize(luaL_Buffer_53* B, size_t s) {
673 if (B->capacity - B->nelems < s) { /* needs to grow */
674 char* newptr = NULL;
675 size_t newcap = B->capacity * 2;
676 if (newcap - B->nelems < s)
677 newcap = B->nelems + s;
678 if (newcap < B->capacity) /* overflow */
679 luaL_error(B->L2, "buffer too large");
680#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
681 newptr = (char*)lua_newuserdatauv(B->L2, newcap, 0);
682#else
683 newptr = (char*)lua_newuserdata(B->L2, newcap);
684#endif
685 memcpy(newptr, B->ptr, B->nelems);
686 if (B->ptr != B->b.buffer)
687 lua_replace(B->L2, -2); /* remove old buffer */
688 B->ptr = newptr;
689 B->capacity = newcap;
690 }
691 return B->ptr + B->nelems;
692}
693
694
695COMPAT53_API void luaL_addlstring(luaL_Buffer_53* B, const char* s, size_t l) {
696 memcpy(luaL_prepbuffsize(B, l), s, l);
697 luaL_addsize(B, l);
698}
699
700
701COMPAT53_API void luaL_addvalue(luaL_Buffer_53* B) {
702 size_t len = 0;
703 const char* s = lua_tolstring(B->L2, -1, &len);
704 if (!s)
705 luaL_error(B->L2, "cannot convert value to string");
706 if (B->ptr != B->b.buffer)
707 lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
708 luaL_addlstring(B, s, len);
709 lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
710}
711
712
713void luaL_pushresult(luaL_Buffer_53* B) {
714 lua_pushlstring(B->L2, B->ptr, B->nelems);
715 if (B->ptr != B->b.buffer)
716 lua_replace(B->L2, -2); /* remove userdata buffer */
717}
718
719
720#endif /* Lua 5.1 */
721
722
723
724/* definitions for Lua 5.1 and Lua 5.2 */
725#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
726
727
728COMPAT53_API int lua_geti(lua_State* L, int index, lua_Integer i) {
729 index = lua_absindex(L, index);
730 lua_pushinteger(L, i);
731 lua_gettable(L, index);
732 return lua_type(L, -1);
733}
734
735
736COMPAT53_API int lua_isinteger(lua_State* L, int index) {
737 if (lua_type(L, index) == LUA_TNUMBER) {
738 lua_Number n = lua_tonumber(L, index);
739 lua_Integer i = lua_tointeger(L, index);
740 if (i == n)
741 return 1;
742 }
743 return 0;
744}
745
746
747COMPAT53_API lua_Integer lua_tointegerx(lua_State* L, int i, int* isnum) {
748 int ok = 0;
749 lua_Number n = lua_tonumberx(L, i, &ok);
750 if (ok) {
751 if (n == (lua_Integer)n) {
752 if (isnum)
753 *isnum = 1;
754 return (lua_Integer)n;
755 }
756 }
757 if (isnum)
758 *isnum = 0;
759 return 0;
760}
761
762
763static void compat53_reverse(lua_State* L, int a, int b) {
764 for (; a < b; ++a, --b) {
765 lua_pushvalue(L, a);
766 lua_pushvalue(L, b);
767 lua_replace(L, a);
768 lua_replace(L, b);
769 }
770}
771
772
773COMPAT53_API void lua_rotate(lua_State* L, int idx, int n) {
774 int n_elems = 0;
775 idx = lua_absindex(L, idx);
776 n_elems = lua_gettop(L) - idx + 1;
777 if (n < 0)
778 n += n_elems;
779 if (n > 0 && n < n_elems) {
780 luaL_checkstack(L, 2, "not enough stack slots available");
781 n = n_elems - n;
782 compat53_reverse(L, idx, idx + n - 1);
783 compat53_reverse(L, idx + n, idx + n_elems - 1);
784 compat53_reverse(L, idx, idx + n_elems - 1);
785 }
786}
787
788
789COMPAT53_API void lua_seti(lua_State* L, int index, lua_Integer i) {
790 luaL_checkstack(L, 1, "not enough stack slots available");
791 index = lua_absindex(L, index);
792 lua_pushinteger(L, i);
793 lua_insert(L, -2);
794 lua_settable(L, index);
795}
796
797
798#if !defined(lua_str2number)
799#define lua_str2number(s, p) strtod((s), (p))
800#endif
801
802COMPAT53_API size_t lua_stringtonumber(lua_State* L, const char* s) {
803 char* endptr;
804 lua_Number n = lua_str2number(s, &endptr);
805 if (endptr != s) {
806 while (*endptr != '\0' && isspace((unsigned char)*endptr))
807 ++endptr;
808 if (*endptr == '\0') {
809 lua_pushnumber(L, n);
810 return endptr - s + 1;
811 }
812 }
813 return 0;
814}
815
816
817COMPAT53_API const char* luaL_tolstring(lua_State* L, int idx, size_t* len) {
818 if (!luaL_callmeta(L, idx, "__tostring")) {
819 int t = lua_type(L, idx), tt = 0;
820 char const* name = NULL;
821 switch (t) {
822 case LUA_TNIL:
823 lua_pushliteral(L, "nil");
824 break;
825 case LUA_TSTRING:
826 case LUA_TNUMBER:
827 lua_pushvalue(L, idx);
828 break;
829 case LUA_TBOOLEAN:
830 if (lua_toboolean(L, idx))
831 lua_pushliteral(L, "true");
832 else
833 lua_pushliteral(L, "false");
834 break;
835 default:
836 tt = luaL_getmetafield(L, idx, "__name");
837 name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
838 lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
839 if (tt != LUA_TNIL)
840 lua_replace(L, -2);
841 break;
842 }
843 }
844 else {
845 if (!lua_isstring(L, -1))
846 luaL_error(L, "'__tostring' must return a string");
847 }
848 return lua_tolstring(L, -1, len);
849}
850
851
852COMPAT53_API void luaL_requiref(lua_State* L, const char* modname, lua_CFunction openf, int glb) {
853 luaL_checkstack(L, 3, "not enough stack slots available");
854 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
855 if (lua_getfield(L, -1, modname) == LUA_TNIL) {
856 lua_pop(L, 1);
857 lua_pushcfunction(L, openf);
858 lua_pushstring(L, modname);
859 lua_call(L, 1, 1);
860 lua_pushvalue(L, -1);
861 lua_setfield(L, -3, modname);
862 }
863 if (glb) {
864 lua_pushvalue(L, -1);
865 lua_setglobal(L, modname);
866 }
867 lua_replace(L, -2);
868}
869
870
871#endif /* Lua 5.1 and 5.2 */
872
873
874#endif /* KEPLER_PROJECT_COMPAT53_C_ */
875
876
877/*********************************************************************
878 * This file contains parts of Lua 5.2's and Lua 5.3's source code:
879 *
880 * Copyright (C) 1994-2014 Lua.org, PUC-Rio.
881 *
882 * Permission is hereby granted, free of charge, to any person obtaining
883 * a copy of this software and associated documentation files (the
884 * "Software"), to deal in the Software without restriction, including
885 * without limitation the rights to use, copy, modify, merge, publish,
886 * distribute, sublicense, and/or sell copies of the Software, and to
887 * permit persons to whom the Software is furnished to do so, subject to
888 * the following conditions:
889 *
890 * The above copyright notice and this permission notice shall be
891 * included in all copies or substantial portions of the Software.
892 *
893 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
894 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
895 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
896 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
897 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
898 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
899 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
900 *********************************************************************/
#define COMPAT53_API