Debian Codegraph @

lupa/luaapi.pxd

raw
1
2 cdef extern from *:
3 ctypedef struct va_list
4
5 cdef extern from *: # "luaconf.h"
6 # Various tunables.
7 enum:
8 LUAI_MAXSTACK # 65500 /* Max. # of stack slots for a thread (<64K). */
9 LUAI_MAXCSTACK # 8000 /* Max. # of stack slots for a C func (<10K). */
10 LUAI_GCPAUSE # 200 /* Pause GC until memory is at 200%. */
11 LUAI_GCMUL # 200 /* Run GC at 200% of allocation speed. */
12 LUA_MAXCAPTURES # 32 /* Max. pattern captures. */
13
14 LUA_IDSIZE # 60 /* Size of lua_Debug.short_src. */
15 LUAL_BUFFERSIZE # BUFSIZ /* Size of lauxlib and io.* buffers. */
16
17 ################################################################################
18 # lua.h
19 ################################################################################
20
21 cdef extern from "lua.h" nogil:
22 char* LUA_VERSION
23 char* LUA_RELEASE
24 int LUA_VERSION_NUM
25 char* LUA_COPYRIGHT
26 char* LUA_AUTHORS
27
28 char* LUA_SIGNATURE
29 int LUA_MULTRET
30
31 int LUA_REGISTRYINDEX
32 int LUA_ENVIRONINDEX
33 int LUA_GLOBALSINDEX
34 int lua_upvalueindex(int i)
35
36 enum:
37 # thread status; 0 is OK
38 LUA_YIELD # 1
39 LUA_ERRRUN # 2
40 LUA_ERRSYNTAX # 3
41 LUA_ERRMEM # 4
42 LUA_ERRERR # 5
43
44 ctypedef struct lua_State
45
46 ctypedef int (*lua_CFunction) (lua_State *L)
47
48 ctypedef char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz)
49
50 ctypedef int (*lua_Writer) (lua_State *L, void* p, size_t sz, void* ud)
51
52 ctypedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize)
53
54 enum:
55 LUA_TNONE # -1
56
57 LUA_TNIL # 0
58 LUA_TBOOLEAN # 1
59 LUA_TLIGHTUSERDATA # 2
60 LUA_TNUMBER # 3
61 LUA_TSTRING # 4
62 LUA_TTABLE # 5
63 LUA_TFUNCTION # 6
64 LUA_TUSERDATA # 7
65 LUA_TTHREAD # 8
66
67 int LUA_MINSTACK # minimum Lua stack available to a C function
68
69 ctypedef float lua_Number # type of numbers in Lua
70 ctypedef int lua_Integer # type for integer functions
71
72 lua_State *lua_newstate (lua_Alloc f, void *ud)
73 void lua_close (lua_State *L)
74 lua_State *lua_newthread (lua_State *L)
75 const lua_Number *lua_version(lua_State *L)
76
77 lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf)
78
79 # basic stack manipulation
80 int lua_gettop (lua_State *L)
81 void lua_settop (lua_State *L, int idx)
82 void lua_pushvalue (lua_State *L, int idx)
83 void lua_remove (lua_State *L, int idx)
84 void lua_insert (lua_State *L, int idx)
85 void lua_replace (lua_State *L, int idx)
86 int lua_checkstack (lua_State *L, int sz)
87
88 void lua_xmove (lua_State *_from, lua_State *to, int n)
89
90 # access functions (stack -> C)
91 int lua_isnumber (lua_State *L, int idx)
92 int lua_isstring (lua_State *L, int idx)
93 int lua_iscfunction (lua_State *L, int idx)
94 int lua_isuserdata (lua_State *L, int idx)
95 int lua_type (lua_State *L, int idx)
96 const char *lua_typename (lua_State *L, int tp)
97
98 int lua_equal (lua_State *L, int idx1, int idx2)
99 int lua_rawequal (lua_State *L, int idx1, int idx2)
100 int lua_lessthan (lua_State *L, int idx1, int idx2)
101
102 lua_Number lua_tonumber (lua_State *L, int idx)
103 lua_Integer lua_tointeger (lua_State *L, int idx)
104 bint lua_toboolean (lua_State *L, int idx)
105 char *lua_tolstring (lua_State *L, int idx, size_t *len)
106 size_t lua_objlen (lua_State *L, int idx)
107 lua_CFunction lua_tocfunction (lua_State *L, int idx)
108 void *lua_touserdata (lua_State *L, int idx)
109 lua_State *lua_tothread (lua_State *L, int idx)
110 void *lua_topointer (lua_State *L, int idx)
111
112 # push functions (C -> stack)
113 void lua_pushnil (lua_State *L)
114 void lua_pushnumber (lua_State *L, lua_Number n)
115 void lua_pushinteger (lua_State *L, lua_Integer n)
116 void lua_pushlstring (lua_State *L, char *s, size_t l)
117 void lua_pushstring (lua_State *L, char *s)
118 char *lua_pushvfstring (lua_State *L, char *fmt, va_list argp)
119 char *lua_pushfstring (lua_State *L, char *fmt, ...)
120 void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n)
121 void lua_pushboolean (lua_State *L, bint b)
122 void lua_pushlightuserdata (lua_State *L, void *p)
123 int lua_pushthread (lua_State *L)
124
125 # get functions (Lua -> stack)
126 void lua_gettable (lua_State *L, int idx)
127 void lua_getfield (lua_State *L, int idx, char *k)
128 void lua_rawget (lua_State *L, int idx)
129 void lua_rawgeti (lua_State *L, int idx, int n)
130 void lua_createtable (lua_State *L, int narr, int nrec)
131 void *lua_newuserdata (lua_State *L, size_t sz)
132 int lua_getmetatable (lua_State *L, int objindex)
133 void lua_getfenv (lua_State *L, int idx)
134
135 # set functions (stack -> Lua)
136 void lua_settable (lua_State *L, int idx)
137 void lua_setfield (lua_State *L, int idx, char *k)
138 void lua_rawset (lua_State *L, int idx)
139 void lua_rawseti (lua_State *L, int idx, int n)
140 int lua_setmetatable (lua_State *L, int objindex)
141 int lua_setfenv (lua_State *L, int idx)
142
143 # `load' and `call' functions (load and run Lua code)
144 void lua_call (lua_State *L, int nargs, int nresults)
145 int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc)
146 int lua_cpcall (lua_State *L, lua_CFunction func, void *ud)
147 int lua_load (lua_State *L, lua_Reader reader, void *dt,
148 char *chunkname)
149
150 int lua_dump (lua_State *L, lua_Writer writer, void *data)
151
152 # coroutine functions
153 int lua_yield (lua_State *L, int nresults)
154 int lua_resume "__lupa_lua_resume" (lua_State *L, lua_State *from_, int narg, int *nresults)
155 int lua_status (lua_State *L)
156
157 # garbage-collection function and options
158 enum:
159 LUA_GCSTOP # 0
160 LUA_GCRESTART # 1
161 LUA_GCCOLLECT # 2
162 LUA_GCCOUNT # 3
163 LUA_GCCOUNTB # 4
164 LUA_GCSTEP # 5
165 LUA_GCSETPAUSE # 6
166 LUA_GCSETSTEPMUL # 7
167
168 int lua_gc (lua_State *L, int what, int data)
169
170 # miscellaneous functions
171 int lua_error (lua_State *L)
172 int lua_next (lua_State *L, int idx)
173 void lua_concat (lua_State *L, int n)
174 lua_Alloc lua_getallocf (lua_State *L, void **ud)
175 void lua_setallocf (lua_State *L, lua_Alloc f, void *ud)
176
177 # ===============================================================
178 # some useful macros
179 # ===============================================================
180
181 void lua_pop(lua_State *L, int n) # lua_settop(L, -(n)-1)
182 void lua_newtable(lua_State *L) # lua_createtable(L, 0, 0)
183 void lua_register(lua_State *L, char* n, lua_CFunction f) # (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
184 void lua_pushcfunction(lua_State *L, lua_CFunction fn) # lua_pushcclosure(L, (f), 0)
185 size_t lua_strlen(lua_State *L, int i) # lua_objlen(L, (i))
186
187 bint lua_isfunction(lua_State *L, int n) # (lua_type(L, (n)) == LUA_TFUNCTION)
188 bint lua_istable(lua_State *L, int n) # (lua_type(L, (n)) == LUA_TTABLE)
189 bint lua_islightuserdata(lua_State *L, int n) # (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
190 bint lua_isnil(lua_State *L, int n) # (lua_type(L, (n)) == LUA_TNIL)
191 bint lua_isboolean(lua_State *L, int n) # (lua_type(L, (n)) == LUA_TBOOLEAN)
192 bint lua_isthread(lua_State *L, int n) # (lua_type(L, (n)) == LUA_TTHREAD)
193 bint lua_isnone(lua_State *L,int n) # (lua_type(L, (n)) == LUA_TNONE)
194 bint lua_isnoneornil(lua_State *L, int n) # (lua_type(L, (n)) <= 0)
195
196 void lua_pushliteral(lua_State *L, char* s) # lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
197
198 void lua_setglobal(lua_State *L, char* s) # lua_setfield(L, LUA_GLOBALSINDEX, (s))
199 void lua_getglobal(lua_State *L, char* s) # lua_getfield(L, LUA_GLOBALSINDEX, (s))
200
201 char* lua_tostring(lua_State *L, int i) # lua_tolstring(L, (i), NULL)
202
203
204 # compatibility macros and functions
205 lua_State* luaL_newstate()
206 void lua_getregistry(lua_State *L) # lua_pushvalue(L, LUA_REGISTRYINDEX)
207 int lua_getgccount(lua_State *L)
208
209 # define lua_Chunkreader lua_Reader
210 # define lua_Chunkwriter lua_Writer
211
212 # hack
213 void lua_setlevel(lua_State *_from, lua_State *to)
214
215
216 # =======================================================================
217 # Debug API
218 # =======================================================================
219
220 # Event codes
221 enum:
222 LUA_HOOKCALL # 0
223 LUA_HOOKRET # 1
224 LUA_HOOKLINE # 2
225 LUA_HOOKCOUNT # 3
226 LUA_HOOKTAILRET # 4
227
228
229 # Event masks
230 enum:
231 LUA_MASKCALL # (1 << LUA_HOOKCALL)
232 LUA_MASKRET # (1 << LUA_HOOKRET)
233 LUA_MASKLINE # (1 << LUA_HOOKLINE)
234 LUA_MASKCOUNT # (1 << LUA_HOOKCOUNT)
235
236 ctypedef struct lua_Debug # activation record
237
238
239 # Functions to be called by the debuger in specific events
240 ctypedef void (*lua_Hook) (lua_State *L, lua_Debug *ar)
241
242 int lua_getstack (lua_State *L, int level, lua_Debug *ar)
243 int lua_getinfo (lua_State *L, char *what, lua_Debug *ar)
244 char *lua_getlocal (lua_State *L, lua_Debug *ar, int n)
245 char *lua_setlocal (lua_State *L, lua_Debug *ar, int n)
246 char *lua_getupvalue (lua_State *L, int funcindex, int n)
247 char *lua_setupvalue (lua_State *L, int funcindex, int n)
248
249 int lua_sethook (lua_State *L, lua_Hook func, int mask, int count)
250 lua_Hook lua_gethook (lua_State *L)
251 int lua_gethookmask (lua_State *L)
252 int lua_gethookcount (lua_State *L)
253
254 ctypedef struct lua_Debug:
255 int event
256 char *name # (n) */
257 char *namewhat # (n) `global', `local', `field', `method' */
258 char *what # (S) `Lua', `C', `main', `tail' */
259 char *source # (S) */
260 int currentline # (l) */
261 int nups # (u) number of upvalues */
262 int linedefined # (S) */
263 int lastlinedefined # (S) */
264 char short_src[LUA_IDSIZE] # (S) */
265 # private part
266 int i_ci # active function */
267
268
269 ################################################################################
270 # lauxlib.h
271 ################################################################################
272
273 cdef extern from "lauxlib.h" nogil:
274 size_t luaL_getn(lua_State *L, int i) # ((int)lua_objlen(L, i))
275 #void luaL_setn(lua_State *L, int i, int j) # ((void)0) /* no op! */
276
277 # extra error code for `luaL_load'
278 enum:
279 LUA_ERRFILE # (LUA_ERRERR+1)
280
281 # pre-defined references
282 enum:
283 LUA_NOREF # -2
284 LUA_REFNIL # -1
285
286 ctypedef struct luaL_Reg:
287 char *name
288 lua_CFunction func
289
290 void luaL_register (lua_State *L, char *libname, luaL_Reg *l)
291 void luaL_setfuncs (lua_State *L, luaL_Reg *l, int nup) # 5.2+
292 int luaL_getmetafield (lua_State *L, int obj, char *e)
293 int luaL_callmeta (lua_State *L, int obj, char *e)
294 int luaL_typerror (lua_State *L, int narg, char *tname)
295 int luaL_argerror (lua_State *L, int numarg, char *extramsg)
296 char *luaL_checklstring (lua_State *L, int numArg, size_t *l)
297 char *luaL_optlstring (lua_State *L, int numArg, char *default, size_t *l)
298 lua_Number luaL_checknumber (lua_State *L, int numArg)
299 lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number default)
300
301 lua_Integer luaL_checkinteger (lua_State *L, int numArg)
302 lua_Integer luaL_optinteger (lua_State *L, int nArg, lua_Integer default)
303
304 void luaL_checkstack (lua_State *L, int sz, char *msg)
305 void luaL_checktype (lua_State *L, int narg, int t)
306 void luaL_checkany (lua_State *L, int narg)
307
308 int luaL_newmetatable (lua_State *L, char *tname)
309 void *luaL_checkudata (lua_State *L, int ud, char *tname)
310
311 void luaL_where (lua_State *L, int lvl)
312 int luaL_error (lua_State *L, char *fmt, ...)
313
314 int luaL_checkoption (lua_State *L, int narg, char *default, char *lst[])
315
316 int luaL_ref (lua_State *L, int t)
317 void luaL_unref (lua_State *L, int t, int ref)
318
319 int luaL_loadfile (lua_State *L, const char *filename)
320 int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, const char *name)
321 int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode)
322 int luaL_loadstring (lua_State *L, const char *s)
323
324 lua_State *luaL_newstate ()
325
326
327 char *luaL_gsub (lua_State *L, char *s, char *p, char *r)
328
329
330 # ===============================================================
331 # some useful macros
332 # ===============================================================
333
334 int luaL_argcheck(lua_State *L, bint cond, int numarg, char *extramsg) # ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
335 char* luaL_checkstring(lua_State *L, int n) # (luaL_checklstring(L, (n), NULL))
336 char* luaL_optstring(lua_State *L, int n, char* d) # (luaL_optlstring(L, (n), (d), NULL))
337 int luaL_checkint(lua_State *L, int n) # ((int)luaL_checkinteger(L, (n)))
338 int luaL_optint(lua_State *L, int n, lua_Integer d) # ((int)luaL_optinteger(L, (n), (d)))
339 long luaL_checklong(lua_State *L, int n) # ((long)luaL_checkinteger(L, (n)))
340 long luaL_optlong(lua_State *L, int n, lua_Integer d) # ((long)luaL_optinteger(L, (n), (d)))
341 char* luaL_typename (lua_State *L, int i) # lua_typename(L, lua_type(L,(i)))
342 int luaL_dofile(lua_State *L, char* fn) # (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
343 int luaL_dostring(lua_State *L, char* s) # (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
344
345 void luaL_getmetatable(lua_State *L, char* n) # (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
346
347 #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
348
349
350 # =======================================================
351 # Generic Buffer manipulation
352 # =======================================================
353
354 '''
355 typedef struct luaL_Buffer {
356 char *p; /* current position in buffer */
357 int lvl; /* number of strings in the stack (level) */
358 lua_State *L;
359 char buffer[LUAL_BUFFERSIZE];
360 } luaL_Buffer;
361
362 #define luaL_addchar(B,c) \
363 ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
364 (*(B)->p++ = (char)(c)))
365
366 /* compatibility only */
367 #define luaL_putchar(B,c) luaL_addchar(B,c)
368
369 #define luaL_addsize(B,n) ((B)->p += (n))
370
371 void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
372 char *(luaL_prepbuffer) (luaL_Buffer *B);
373 void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
374 void (luaL_addstring) (luaL_Buffer *B, const char *s);
375 void (luaL_addvalue) (luaL_Buffer *B);
376 void (luaL_pushresult) (luaL_Buffer *B);
377
378
379 /* }====================================================== */
380
381
382 /* compatibility with ref system */
383
384 /* pre-defined references */
385 #define LUA_NOREF (-2)
386 #define LUA_REFNIL (-1)
387
388 #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
389 (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
390
391 #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
392
393 #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))
394
395
396 #define luaL_reg luaL_Reg
397
398 #endif
399 '''
400
401 cdef extern from "lualib.h":
402 char* LUA_COLIBNAME # "coroutine"
403 char* LUA_MATHLIBNAME # "math"
404 char* LUA_STRLIBNAME # "string"
405 char* LUA_TABLIBNAME # "table"
406 char* LUA_IOLIBNAME # "io"
407 char* LUA_OSLIBNAME # "os"
408 char* LUA_LOADLIBNAME # "package"
409 char* LUA_DBLIBNAME # "debug"
410 char* LUA_BITLIBNAME # "bit"
411 char* LUA_JITLIBNAME # "jit"
412
413 int luaopen_base(lua_State *L)
414 int luaopen_math(lua_State *L)
415 int luaopen_string(lua_State *L)
416 int luaopen_table(lua_State *L)
417 int luaopen_io(lua_State *L)
418 int luaopen_os(lua_State *L)
419 int luaopen_package(lua_State *L)
420 int luaopen_debug(lua_State *L)
421 int luaopen_bit(lua_State *L)
422 int luaopen_jit(lua_State *L)
423
424 void luaL_openlibs(lua_State *L)
425
426
427 cdef extern from * nogil:
428 # Compatibility definitions for Lupa.
429 """
430 #if LUA_VERSION_NUM >= 504
431 #define __lupa_lua_resume lua_resume
432 #else
433 LUA_API int __lupa_lua_resume (lua_State *L, lua_State *from, int nargs, int* nresults) {
434 #if LUA_VERSION_NUM >= 502
435 int status = lua_resume(L, from, nargs);
436 #else
437 int status = lua_resume(L, nargs);
438 #endif
439 *nresults = lua_gettop(L);
440 return status;
441 }
442 #endif
443
444 #if LUA_VERSION_NUM >= 502
445 #define lua_objlen(L, i) lua_rawlen(L, (i))
446 #endif
447
448 #if LUA_VERSION_NUM < 503
449 #define lua_isinteger(L, i) (((void) i), 0)
450 #endif
451
452 #if LUA_VERSION_NUM < 502
453 #define lua_tointegerx(L, i, isnum) (*(isnum) = lua_isnumber(L, i), lua_tointeger(L, i))
454 #define luaL_loadbufferx(L, buff, sz, name, mode) (((void)mode), luaL_loadbuffer(L, buff, sz, name))
455 #endif
456
457 #if LUA_VERSION_NUM >= 504
458 #define read_lua_version(L) ((int) lua_version(L))
459 #elif LUA_VERSION_NUM >= 502
460 #define read_lua_version(L) ((int) *lua_version(L))
461 #elif LUA_VERSION_NUM >= 501
462 #define read_lua_version(L) ((int) LUA_VERSION_NUM)
463 #else
464 #error Lupa requires at least Lua 5.1 or LuaJIT 2.x
465 #endif
466
467 #if LUA_VERSION_NUM < 502
468 #define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
469 #endif
470 """
471 int read_lua_version(lua_State *L)
472 int lua_isinteger(lua_State *L, int idx)
473 lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum)
474 void lua_pushglobaltable (lua_State *L)
475
476 cdef extern from *:
477 # Limits for Lua integers (in Lua<5.3: PTRDIFF_MIN, PTRDIFF_MAX)
478 """
479 #if LUA_VERSION_NUM < 503
480 #define LUA_MAXINTEGER 0
481 #define LUA_MININTEGER 0
482 #endif
483 """
484 lua_Integer LUA_MAXINTEGER
485 lua_Integer LUA_MININTEGER