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.h | |
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.h')
-rw-r--r-- | c-api/compat-5.3.h | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h new file mode 100644 index 0000000..b288126 --- /dev/null +++ b/c-api/compat-5.3.h | |||
@@ -0,0 +1,294 @@ | |||
1 | #ifndef COMPAT53_H_ | ||
2 | #define COMPAT53_H_ | ||
3 | |||
4 | #include <stddef.h> | ||
5 | #include <limits.h> | ||
6 | #include <lua.h> | ||
7 | #include <lauxlib.h> | ||
8 | #include <lualib.h> | ||
9 | |||
10 | |||
11 | #if defined(COMPAT53_PREFIX) | ||
12 | /* - change the symbol names of functions to avoid linker conflicts | ||
13 | * - compat-5.3.c needs to be compiled (and linked) separately | ||
14 | */ | ||
15 | # if !defined(COMPAT53_API) | ||
16 | # define COMPAT53_API extern | ||
17 | # endif | ||
18 | # undef COMPAT53_INCLUDE_SOURCE | ||
19 | #else /* COMPAT53_PREFIX */ | ||
20 | /* - make all functions static and include the source. | ||
21 | * - don't mess with the symbol names of functions | ||
22 | * - compat-5.3.c doesn't need to be compiled (and linked) separately | ||
23 | */ | ||
24 | # define COMPAT53_PREFIX lua | ||
25 | # undef COMPAT53_API | ||
26 | # if defined(__GNUC__) || defined(__clang__) | ||
27 | # define COMPAT53_API __attribute__((__unused__)) static | ||
28 | # else | ||
29 | # define COMPAT53_API static | ||
30 | # endif | ||
31 | # define COMPAT53_INCLUDE_SOURCE | ||
32 | #endif /* COMPAT53_PREFIX */ | ||
33 | |||
34 | #define COMPAT53_CONCAT_HELPER(a, b) a##b | ||
35 | #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b) | ||
36 | |||
37 | |||
38 | |||
39 | /* declarations for Lua 5.1 */ | ||
40 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 | ||
41 | |||
42 | /* XXX not implemented: | ||
43 | * lua_arith | ||
44 | * lua_compare | ||
45 | * lua_upvalueid | ||
46 | * lua_upvaluejoin | ||
47 | * lua_version | ||
48 | * lua_yieldk | ||
49 | * luaL_buffinitsize | ||
50 | * luaL_execresult | ||
51 | * luaL_loadbufferx | ||
52 | * luaL_loadfilex | ||
53 | * luaL_prepbuffsize | ||
54 | * luaL_pushresultsize | ||
55 | */ | ||
56 | |||
57 | /* PUC-Rio Lua uses lconfig_h as include guard for luaconf.h, | ||
58 | * LuaJIT uses luaconf_h. If you use PUC-Rio's include files | ||
59 | * but LuaJIT's library, you will need to define the macro | ||
60 | * COMPAT53_IS_LUAJIT yourself! */ | ||
61 | #if !defined(COMPAT53_IS_LUAJIT) && defined(luaconf_h) | ||
62 | # define COMPAT53_IS_LUAJIT | ||
63 | #endif | ||
64 | |||
65 | #define LUA_OK 0 | ||
66 | |||
67 | typedef struct luaL_Stream { | ||
68 | FILE *f; | ||
69 | lua_CFunction closef; | ||
70 | } luaL_Stream; | ||
71 | |||
72 | typedef size_t lua_Unsigned; | ||
73 | |||
74 | #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex) | ||
75 | COMPAT53_API int lua_absindex (lua_State *L, int i); | ||
76 | |||
77 | #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy) | ||
78 | COMPAT53_API void lua_copy (lua_State *L, int from, int to); | ||
79 | |||
80 | #define lua_getuservalue(L, i) \ | ||
81 | (lua_getfenv(L, i), lua_type(L, -1)) | ||
82 | #define lua_setuservalue(L, i) \ | ||
83 | (luaL_checktype(L, -1, LUA_TTABLE), lua_setfenv(L, i)) | ||
84 | |||
85 | #define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len) | ||
86 | COMPAT53_API void lua_len (lua_State *L, int i); | ||
87 | |||
88 | #define luaL_newlibtable(L, l) \ | ||
89 | (lua_createtable(L, 0, sizeof(l)/sizeof(*(l))-1)) | ||
90 | #define luaL_newlib(L, l) \ | ||
91 | (luaL_newlibtable(L, l), luaL_register(L, NULL, l)) | ||
92 | |||
93 | #define lua_pushglobaltable(L) \ | ||
94 | lua_pushvalue(L, LUA_GLOBALSINDEX) | ||
95 | |||
96 | #define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp) | ||
97 | COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p); | ||
98 | |||
99 | #define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp) | ||
100 | COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p); | ||
101 | |||
102 | #define lua_rawlen(L, i) lua_objlen(L, i) | ||
103 | |||
104 | #define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx) | ||
105 | COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum); | ||
106 | |||
107 | #define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx) | ||
108 | COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum); | ||
109 | |||
110 | #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion) | ||
111 | COMPAT53_API void luaL_checkversion (lua_State *L); | ||
112 | |||
113 | #define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable) | ||
114 | COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name); | ||
115 | |||
116 | #define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len) | ||
117 | COMPAT53_API int luaL_len (lua_State *L, int i); | ||
118 | |||
119 | #define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs) | ||
120 | COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); | ||
121 | |||
122 | #define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable) | ||
123 | COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname); | ||
124 | |||
125 | #define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata) | ||
126 | COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname); | ||
127 | |||
128 | #define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring) | ||
129 | COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len); | ||
130 | |||
131 | #if !defined(COMPAT53_IS_LUAJIT) | ||
132 | #define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback) | ||
133 | COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level); | ||
134 | |||
135 | #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult) | ||
136 | COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname); | ||
137 | #endif /* COMPAT53_IS_LUAJIT */ | ||
138 | |||
139 | #define lua_callk(L, na, nr, ctx, cont) \ | ||
140 | ((void)(ctx), (void)(cont), lua_call(L, na, nr)) | ||
141 | #define lua_pcallk(L, na, nr, err, ctx, cont) \ | ||
142 | ((void)(ctx), (void)(cont), lua_pcall(L, na, nr, err)) | ||
143 | |||
144 | #if defined(LUA_COMPAT_APIINTCASTS) | ||
145 | #define lua_pushunsigned(L, n) \ | ||
146 | lua_pushinteger(L, (lua_Integer)(n)) | ||
147 | #define lua_tounsignedx(L, i, is) \ | ||
148 | ((lua_Unsigned)lua_tointegerx(L, i, is)) | ||
149 | #define lua_tounsigned(L, i) \ | ||
150 | lua_tounsignedx(L, i, NULL) | ||
151 | #define luaL_checkunsigned(L, a) \ | ||
152 | ((lua_Unsigned)luaL_checkinteger(L, a)) | ||
153 | #define luaL_optunsigned(L, a, d) \ | ||
154 | ((lua_Unsigned)luaL_optinteger(L, a, (lua_Integer)(d))) | ||
155 | #endif | ||
156 | |||
157 | #endif /* Lua 5.1 only */ | ||
158 | |||
159 | |||
160 | |||
161 | /* declarations for Lua 5.1 and 5.2 */ | ||
162 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 | ||
163 | |||
164 | typedef int lua_KContext; | ||
165 | |||
166 | typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx); | ||
167 | |||
168 | #define lua_dump(L, w, d, s) \ | ||
169 | ((void)(s), lua_dump(L, w, d)) | ||
170 | |||
171 | #define lua_getfield(L, i, k) \ | ||
172 | (lua_getfield(L, i, k), lua_type(L, -1)) | ||
173 | |||
174 | #define lua_gettable(L, i) \ | ||
175 | (lua_gettable(L, i), lua_type(L, -1)) | ||
176 | |||
177 | #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) | ||
178 | COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i); | ||
179 | |||
180 | #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) | ||
181 | COMPAT53_API int lua_isinteger (lua_State *L, int index); | ||
182 | |||
183 | #define lua_numbertointeger(n, p) \ | ||
184 | ((*(p) = (lua_Integer)(n)), 1) | ||
185 | |||
186 | #define lua_rawget(L, i) \ | ||
187 | (lua_rawget(L, i), lua_type(L, -1)) | ||
188 | |||
189 | #define lua_rawgeti(L, i, n) \ | ||
190 | (lua_rawgeti(L, i, n), lua_type(L, -1)) | ||
191 | |||
192 | #define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate) | ||
193 | COMPAT53_API void lua_rotate (lua_State *L, int idx, int n); | ||
194 | |||
195 | #define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti) | ||
196 | COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i); | ||
197 | |||
198 | #define lua_strtonum COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber) | ||
199 | COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s); | ||
200 | |||
201 | #define luaL_getmetafield(L, o, e) \ | ||
202 | (luaL_getmetafield(L, o, e) ? lua_type(L, -1) : LUA_TNIL) | ||
203 | |||
204 | #define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_) | ||
205 | COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, | ||
206 | lua_CFunction openf, int glb ); | ||
207 | |||
208 | #endif /* Lua 5.1 and Lua 5.2 */ | ||
209 | |||
210 | |||
211 | |||
212 | /* declarations for Lua 5.2 */ | ||
213 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502 | ||
214 | |||
215 | /* XXX not implemented: | ||
216 | * lua_isyieldable | ||
217 | * lua_getextraspace | ||
218 | * lua_arith (new operators) | ||
219 | * lua_pushfstring (new formats) | ||
220 | */ | ||
221 | |||
222 | #define lua_getglobal(L, n) \ | ||
223 | (lua_getglobal(L, n), lua_type(L, -1)) | ||
224 | |||
225 | #define lua_getuservalue(L, i) \ | ||
226 | (lua_getuservalue(L, i), lua_type(L, -1)) | ||
227 | |||
228 | #define lua_rawgetp(L, i, p) \ | ||
229 | (lua_rawgetp(L, i, p), lua_type(L, -1)) | ||
230 | |||
231 | #define LUA_KFUNCTION(_name) \ | ||
232 | static int (_name)(lua_State *L, int status, lua_KContext ctx); \ | ||
233 | static int (_name ## _52)(lua_State *L) { \ | ||
234 | luaK_Context ctx; \ | ||
235 | int status = lua_getctx(L, &ctx); \ | ||
236 | return (_name)(L, status, ctx); \ | ||
237 | } \ | ||
238 | static int (_name)(lua_State *L, int status, lua_KContext ctx) | ||
239 | |||
240 | #define lua_pcallk(L, na, nr, err, ctx, cont) \ | ||
241 | lua_pcallk(L, na, nr, err, ctx, cont ## _52) | ||
242 | |||
243 | #define lua_callk(L, na, nr, ctx, cont) \ | ||
244 | lua_callk(L, na, nr, ctx, cont ## _52) | ||
245 | |||
246 | #define lua_yieldk(L, nr, ctx, cont) \ | ||
247 | lua_yieldk(L, nr, ctx, cont ## _52) | ||
248 | |||
249 | #ifdef lua_call | ||
250 | # undef lua_call | ||
251 | # define lua_call(L, na, nr) \ | ||
252 | (lua_callk)(L, na, nr, 0, NULL) | ||
253 | #endif | ||
254 | |||
255 | #ifdef lua_pcall | ||
256 | # undef lua_pcall | ||
257 | # define lua_pcall(L, na, nr, err) \ | ||
258 | (lua_pcallk)(L, na, nr, err, 0, NULL) | ||
259 | #endif | ||
260 | |||
261 | #ifdef lua_yield | ||
262 | # undef lua_yield | ||
263 | # define lua_yield(L, nr) \ | ||
264 | (lua_yieldk)(L, nr, 0, NULL) | ||
265 | #endif | ||
266 | |||
267 | #endif /* Lua 5.2 only */ | ||
268 | |||
269 | |||
270 | |||
271 | /* other Lua versions */ | ||
272 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 503 | ||
273 | |||
274 | # error "unsupported Lua version (i.e. not Lua 5.1, 5.2, or 5.3)" | ||
275 | |||
276 | #endif /* other Lua versions except 5.1, 5.2, and 5.3 */ | ||
277 | |||
278 | |||
279 | |||
280 | /* helper macro for defining continuation functions (for every version | ||
281 | * *except* Lua 5.2) */ | ||
282 | #ifndef LUA_KFUNCTION | ||
283 | #define LUA_KFUNCTION(_name) \ | ||
284 | static int (_name)(lua_State *L, int status, lua_KContext ctx) | ||
285 | #endif | ||
286 | |||
287 | |||
288 | #if defined(COMPAT53_INCLUDE_SOURCE) | ||
289 | # include "compat-5.3.c" | ||
290 | #endif | ||
291 | |||
292 | |||
293 | #endif /* COMPAT53_H_ */ | ||
294 | |||