2 ** $Id: lua.h,v 1.175c 2003/03/18 12:31:39 roberto Exp $
3 ** Lua - An Extensible Extension Language
4 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5 ** http://www.lua.org mailto:info@lua.org
6 ** See Copyright Notice at the end of this file
17 #define LUA_VERSION "Lua 5.0.3"
18 #define LUA_COPYRIGHT "Copyright (C) 1994-2006 Tecgraf, PUC-Rio"
19 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
23 /* option for multiple returns in `lua_pcall' and `lua_call' */
24 #define LUA_MULTRET (-1)
30 #define LUA_REGISTRYINDEX (-10000)
31 #define LUA_GLOBALSINDEX (-10001)
32 #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
35 /* error codes for `lua_load' and `lua_pcall' */
38 #define LUA_ERRSYNTAX 3
43 typedef struct lua_State lua_State
;
45 typedef int (*lua_CFunction
) (lua_State
*L
);
49 ** functions that read/write blocks when loading/dumping Lua chunks
51 typedef const char * (*lua_Chunkreader
) (lua_State
*L
, void *ud
, size_t *sz
);
53 typedef int (*lua_Chunkwriter
) (lua_State
*L
, const void* p
,
60 #define LUA_TNONE (-1)
63 #define LUA_TBOOLEAN 1
64 #define LUA_TLIGHTUSERDATA 2
68 #define LUA_TFUNCTION 6
69 #define LUA_TUSERDATA 7
73 /* minimum Lua stack available to a C function */
74 #define LUA_MINSTACK 20
78 ** generic extra include file
85 /* type of numbers in Lua */
87 typedef double lua_Number
;
89 typedef LUA_NUMBER lua_Number
;
93 /* mark for all API functions */
95 #define LUA_API extern
100 ** state manipulation
102 LUA_API lua_State
*lua_open (void);
103 LUA_API
void lua_close (lua_State
*L
);
104 LUA_API lua_State
*lua_newthread (lua_State
*L
);
106 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
);
110 ** basic stack manipulation
112 LUA_API
int lua_gettop (lua_State
*L
);
113 LUA_API
void lua_settop (lua_State
*L
, int idx
);
114 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
);
115 LUA_API
void lua_remove (lua_State
*L
, int idx
);
116 LUA_API
void lua_insert (lua_State
*L
, int idx
);
117 LUA_API
void lua_replace (lua_State
*L
, int idx
);
118 LUA_API
int lua_checkstack (lua_State
*L
, int sz
);
120 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
);
124 ** access functions (stack -> C)
127 LUA_API
int lua_isnumber (lua_State
*L
, int idx
);
128 LUA_API
int lua_isstring (lua_State
*L
, int idx
);
129 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
);
130 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
);
131 LUA_API
int lua_type (lua_State
*L
, int idx
);
132 LUA_API
const char *lua_typename (lua_State
*L
, int tp
);
134 LUA_API
int lua_equal (lua_State
*L
, int idx1
, int idx2
);
135 LUA_API
int lua_rawequal (lua_State
*L
, int idx1
, int idx2
);
136 LUA_API
int lua_lessthan (lua_State
*L
, int idx1
, int idx2
);
138 LUA_API lua_Number
lua_tonumber (lua_State
*L
, int idx
);
139 LUA_API
int lua_toboolean (lua_State
*L
, int idx
);
140 LUA_API
const char *lua_tostring (lua_State
*L
, int idx
);
141 LUA_API
size_t lua_strlen (lua_State
*L
, int idx
);
142 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
);
143 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
);
144 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
);
145 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
);
149 ** push functions (C -> stack)
151 LUA_API
void lua_pushnil (lua_State
*L
);
152 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
);
153 LUA_API
void lua_pushlstring (lua_State
*L
, const char *s
, size_t l
);
154 LUA_API
void lua_pushstring (lua_State
*L
, const char *s
);
155 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
157 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...);
158 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
);
159 LUA_API
void lua_pushboolean (lua_State
*L
, int b
);
160 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
);
164 ** get functions (Lua -> stack)
166 LUA_API
void lua_gettable (lua_State
*L
, int idx
);
167 LUA_API
void lua_rawget (lua_State
*L
, int idx
);
168 LUA_API
void lua_rawgeti (lua_State
*L
, int idx
, int n
);
169 LUA_API
void lua_newtable (lua_State
*L
);
170 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t sz
);
171 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
);
172 LUA_API
void lua_getfenv (lua_State
*L
, int idx
);
176 ** set functions (stack -> Lua)
178 LUA_API
void lua_settable (lua_State
*L
, int idx
);
179 LUA_API
void lua_rawset (lua_State
*L
, int idx
);
180 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, int n
);
181 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
);
182 LUA_API
int lua_setfenv (lua_State
*L
, int idx
);
186 ** `load' and `call' functions (load and run Lua code)
188 LUA_API
void lua_call (lua_State
*L
, int nargs
, int nresults
);
189 LUA_API
int lua_pcall (lua_State
*L
, int nargs
, int nresults
, int errfunc
);
190 LUA_API
int lua_cpcall (lua_State
*L
, lua_CFunction func
, void *ud
);
191 LUA_API
int lua_load (lua_State
*L
, lua_Chunkreader reader
, void *dt
,
192 const char *chunkname
);
194 LUA_API
int lua_dump (lua_State
*L
, lua_Chunkwriter writer
, void *data
);
198 ** coroutine functions
200 LUA_API
int lua_yield (lua_State
*L
, int nresults
);
201 LUA_API
int lua_resume (lua_State
*L
, int narg
);
204 ** garbage-collection functions
206 LUA_API
int lua_getgcthreshold (lua_State
*L
);
207 LUA_API
int lua_getgccount (lua_State
*L
);
208 LUA_API
void lua_setgcthreshold (lua_State
*L
, int newthreshold
);
211 ** miscellaneous functions
214 LUA_API
const char *lua_version (void);
216 LUA_API
int lua_error (lua_State
*L
);
218 LUA_API
int lua_next (lua_State
*L
, int idx
);
220 LUA_API
void lua_concat (lua_State
*L
, int n
);
225 ** ===============================================================
226 ** some useful macros
227 ** ===============================================================
230 #define lua_boxpointer(L,u) \
231 (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
233 #define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
235 #define lua_pop(L,n) lua_settop(L, -(n)-1)
237 #define lua_register(L,n,f) \
238 (lua_pushstring(L, n), \
239 lua_pushcfunction(L, f), \
240 lua_settable(L, LUA_GLOBALSINDEX))
242 #define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
244 #define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
245 #define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
246 #define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA)
247 #define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
248 #define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN)
249 #define lua_isnone(L,n) (lua_type(L,n) == LUA_TNONE)
250 #define lua_isnoneornil(L, n) (lua_type(L,n) <= 0)
252 #define lua_pushliteral(L, s) \
253 lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
258 ** compatibility macros and functions
262 LUA_API
int lua_pushupvalues (lua_State
*L
);
264 #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
265 #define lua_setglobal(L,s) \
266 (lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX))
268 #define lua_getglobal(L,s) \
269 (lua_pushstring(L, s), lua_gettable(L, LUA_GLOBALSINDEX))
272 /* compatibility with ref system */
274 /* pre-defined references */
275 #define LUA_NOREF (-2)
276 #define LUA_REFNIL (-1)
278 #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
279 (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
281 #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
283 #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
288 ** {======================================================================
289 ** useful definitions for Lua kernel and libraries
290 ** =======================================================================
293 /* formats for Lua numbers */
294 #ifndef LUA_NUMBER_SCAN
295 #define LUA_NUMBER_SCAN "%lf"
298 #ifndef LUA_NUMBER_FMT
299 #define LUA_NUMBER_FMT "%.14g"
302 /* }====================================================================== */
306 ** {======================================================================
308 ** =======================================================================
315 #define LUA_HOOKCALL 0
316 #define LUA_HOOKRET 1
317 #define LUA_HOOKLINE 2
318 #define LUA_HOOKCOUNT 3
319 #define LUA_HOOKTAILRET 4
325 #define LUA_MASKCALL (1 << LUA_HOOKCALL)
326 #define LUA_MASKRET (1 << LUA_HOOKRET)
327 #define LUA_MASKLINE (1 << LUA_HOOKLINE)
328 #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
330 typedef struct lua_Debug lua_Debug
; /* activation record */
332 typedef void (*lua_Hook
) (lua_State
*L
, lua_Debug
*ar
);
335 LUA_API
int lua_getstack (lua_State
*L
, int level
, lua_Debug
*ar
);
336 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
);
337 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
);
338 LUA_API
const char *lua_setlocal (lua_State
*L
, const lua_Debug
*ar
, int n
);
339 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
);
340 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
);
342 LUA_API
int lua_sethook (lua_State
*L
, lua_Hook func
, int mask
, int count
);
343 LUA_API lua_Hook
lua_gethook (lua_State
*L
);
344 LUA_API
int lua_gethookmask (lua_State
*L
);
345 LUA_API
int lua_gethookcount (lua_State
*L
);
348 #define LUA_IDSIZE 60
352 const char *name
; /* (n) */
353 const char *namewhat
; /* (n) `global', `local', `field', `method' */
354 const char *what
; /* (S) `Lua', `C', `main', `tail' */
355 const char *source
; /* (S) */
356 int currentline
; /* (l) */
357 int nups
; /* (u) number of upvalues */
358 int linedefined
; /* (S) */
359 char short_src
[LUA_IDSIZE
]; /* (S) */
361 int i_ci
; /* active function */
364 /* }====================================================================== */
367 /******************************************************************************
368 * Copyright (C) 1994-2006 Tecgraf, PUC-Rio. All rights reserved.
370 * Permission is hereby granted, free of charge, to any person obtaining
371 * a copy of this software and associated documentation files (the
372 * "Software"), to deal in the Software without restriction, including
373 * without limitation the rights to use, copy, modify, merge, publish,
374 * distribute, sublicense, and/or sell copies of the Software, and to
375 * permit persons to whom the Software is furnished to do so, subject to
376 * the following conditions:
378 * The above copyright notice and this permission notice shall be
379 * included in all copies or substantial portions of the Software.
381 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
382 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
383 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
384 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
385 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
386 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
387 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
388 ******************************************************************************/