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