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