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