lua.h (17191B)
1 /* 2 ** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ 3 ** Lua - An Extensible Extension Language 4 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 5 ** See Copyright Notice at the end of this file 6 */ 7 8 9 #ifndef lua_h 10 #define lua_h 11 12 #include <stdarg.h> 13 #include <stddef.h> 14 15 16 #include "luaconf.h" 17 18 19 #define LUA_VERSION_LJX 1 20 #define LUA_VERSION_MAJOR "5" 21 22 #if LJ_51 23 #define LUA_VERSION_MINOR "1" 24 #define LUA_VERSION_NUM 501 25 #elif LJ_53 26 #define LUA_VERSION_MINOR "3" 27 #define LUA_VERSION_NUM 503 28 #else 29 #define LUA_VERSION_MINOR "2" 30 #define LUA_VERSION_NUM 502 31 #endif 32 33 #if LJ_ABIVER==53 34 #define LUA_ABIVER_STRING "5.3" 35 #elif LJ_ABIVER==52 36 #define LUA_ABIVER_STRING "5.2" 37 #else 38 #define LUA_ABIVER_STRING "5.1" 39 #endif 40 41 #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 42 #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE 43 44 #define LUA_COPYRIGHT "Copyright (C) 1994-2013 Lua.org, PUC-Rio" 45 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" 46 #define LUA_URL "http://lua.org/" 47 48 49 /* mark for precompiled code (`<esc>Lua') */ 50 #define LUA_SIGNATURE "\033Lua" 51 52 /* option for multiple returns in `lua_pcall' and `lua_call' */ 53 #define LUA_MULTRET (-1) 54 55 /* Rio derives this from LUA_MAX_STACK */ 56 57 /* 58 ** XABI: pseudo-indices 59 ** Those are carefuly laid out depending on ABI. Note that all indice 60 ** types are supported (ie you can use 5.1 indices with 5.3 ABI), but 61 ** then it will be only source compatible. 62 */ 63 #if LJ_ABIVER == 51 64 #define LUAI_FIRSTPSEUDOIDX (-10000) 65 #define LUA_REGISTRYINDEX (LUAI_FIRSTPSEUDOIDX) 66 #define LUA_ENVIRONINDEX (LUAI_FIRSTPSEUDOIDX-1) 67 #define LUA_GLOBALSINDEX (LUAI_FIRSTPSEUDOIDX-2) 68 #define LUA_UVINDEX LUA_GLOBALSINDEX 69 #else /* 5.2 and 5.3 */ 70 #define LUAI_FIRSTPSEUDOIDX (-1000000 - 1000 + 3) 71 #define LUA_ENVIRONINDEX (LUAI_FIRSTPSEUDOIDX-1) 72 #define LUA_GLOBALSINDEX (LUAI_FIRSTPSEUDOIDX-2) 73 #define LUA_REGISTRYINDEX (LUAI_FIRSTPSEUDOIDX-3) 74 #define LUA_UVINDEX LUA_REGISTRYINDEX 75 #endif 76 #define lua_upvalueindex(i) (LUA_UVINDEX-(i)) 77 78 /* 5.2/3: predefined values in the registry */ 79 #define LUA_RIDX_MAINTHREAD 1 80 #define LUA_RIDX_GLOBALS 2 81 #define LUA_RIDX_LAST LUA_RIDX_GLOBALS 82 #define LUA_RIDX_USERVAL 3 /* For lua_setuservalue(). */ 83 #define LUA_RIDX_COUNT LUA_RIDX_USERVAL 84 85 /* thread status; 0 is OK */ 86 #define LUA_OK 0 87 #define LUA_YIELD 1 88 #define LUA_ERRRUN 2 89 #define LUA_ERRSYNTAX 3 90 #define LUA_ERRMEM 4 91 /* XABI */ 92 #if LJ_ABIVER == 51 93 #define LUA_ERRERR 5 94 #else 95 #define LUA_ERRGCMM 5 96 #define LUA_ERRERR 6 97 #endif 98 99 100 typedef struct lua_State lua_State; 101 102 typedef int (*lua_CFunction) (lua_State *L); 103 typedef LUA_KCONTEXT lua_KContext; 104 typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); 105 106 107 /* 108 ** functions that read/write blocks when loading/dumping Lua chunks 109 */ 110 typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 111 112 typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); 113 114 115 /* 116 ** prototype for memory-allocation functions 117 */ 118 typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 119 120 121 /* 122 ** basic types 123 */ 124 #define LUA_TNONE (-1) 125 126 #define LUA_TNIL 0 127 #define LUA_TBOOLEAN 1 128 #define LUA_TLIGHTUSERDATA 2 129 #define LUA_TNUMBER 3 130 #define LUA_TSTRING 4 131 #define LUA_TTABLE 5 132 #define LUA_TFUNCTION 6 133 #define LUA_TUSERDATA 7 134 #define LUA_TTHREAD 8 135 136 #define LUA_NUMTAGS 9 137 138 /* minimum Lua stack available to a C function */ 139 #define LUA_MINSTACK 20 140 141 142 /* 143 ** generic extra include file 144 */ 145 #if defined(LUA_USER_H) 146 #include LUA_USER_H 147 #endif 148 149 150 /* type of numbers in Lua */ 151 typedef LUA_NUMBER lua_Number; 152 153 154 /* type for integer functions */ 155 typedef LUA_INTEGER lua_Integer; 156 typedef LUA_UNSIGNED lua_Unsigned; 157 158 159 160 /* 161 ** state manipulation 162 */ 163 LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 164 LUA_API void (lua_close) (lua_State *L); 165 LUA_API lua_State *(lua_newthread) (lua_State *L); 166 167 LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 168 169 170 /* 171 ** basic stack manipulation 172 */ 173 LUA_API int (lua_absindex) (lua_State *L, int idx); 174 LUA_API int (lua_gettop) (lua_State *L); 175 LUA_API void (lua_settop) (lua_State *L, int idx); 176 LUA_API void (lua_rotate) (lua_State *L, int idx, int n); 177 LUA_API void (lua_pushvalue) (lua_State *L, int idx); 178 LUA_API void (lua_remove) (lua_State *L, int idx); 179 LUA_API void (lua_insert) (lua_State *L, int idx); 180 LUA_API void (lua_replace) (lua_State *L, int idx); 181 LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); 182 LUA_API int (lua_checkstack) (lua_State *L, int sz); 183 184 LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 185 186 187 /* 188 ** access functions (stack -> C) 189 */ 190 191 LUA_API int (lua_isnumber) (lua_State *L, int idx); 192 LUA_API int (lua_isstring) (lua_State *L, int idx); 193 LUA_API int (lua_iscfunction) (lua_State *L, int idx); 194 LUA_API int (lua_isuserdata) (lua_State *L, int idx); 195 LUA_API int (lua_type) (lua_State *L, int idx); 196 LUA_API const char *(lua_typename) (lua_State *L, int tp); 197 198 /* 199 ** Comparison and arithmetic functions 200 */ 201 #define LUA_OPADD 0 202 #define LUA_OPSUB 1 203 #define LUA_OPMUL 2 204 205 #if LJ_ABIVER==53 206 #define LUA_OPMOD 3 207 #define LUA_OPPOW 4 208 #define LUA_OPDIV 5 209 #define LUA_OPIDIV 6 210 #define LUA_OPBAND 7 211 #define LUA_OPBOR 8 212 #define LUA_OPBXOR 9 213 #define LUA_OPSHL 10 214 #define LUA_OPSHR 11 215 #define LUA_OPUNM 12 216 #define LUA_OPBNOT 13 217 #else /* 5.2, 5.1 */ 218 #define LUA_OPDIV 3 219 #define LUA_OPMOD 4 220 #define LUA_OPPOW 5 221 #define LUA_OPUNM 6 222 #define LUA_OPIDIV 7 223 #define LUA_OPBAND 8 224 #define LUA_OPBOR 9 225 #define LUA_OPBXOR 10 226 #define LUA_OPSHL 11 227 #define LUA_OPSHR 12 228 #define LUA_OPBNOT 13 229 #endif 230 231 LUA_API void (lua_arith) (lua_State *L, int op); 232 233 #define LUA_OPEQ 0 234 #define LUA_OPLT 1 235 #define LUA_OPLE 2 236 237 LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); 238 LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 239 LUA_API int (lua_compare) (lua_State *L, int index1, int index2, int op); 240 LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); 241 242 LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); 243 LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *succ); 244 LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); 245 LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *succ); 246 LUA_API lua_Unsigned (lua_tounsigned) (lua_State *L, int idx); 247 LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *succ); 248 249 LUA_API int (lua_toboolean) (lua_State *L, int idx); 250 LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 251 LUA_API void (lua_len) (lua_State *L, int i); 252 LUA_API size_t (lua_objlen) (lua_State *L, int idx); 253 LUA_API size_t (lua_rawlen) (lua_State *L, int idx); 254 LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 255 LUA_API void *(lua_touserdata) (lua_State *L, int idx); 256 LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 257 LUA_API const void *(lua_topointer) (lua_State *L, int idx); 258 LUA_API const lua_Number *(lua_version) (lua_State *L); 259 260 261 /* 262 ** push functions (C -> stack) 263 */ 264 LUA_API void (lua_pushnil) (lua_State *L); 265 LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 266 LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 267 LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n); 268 LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); 269 LUA_API void (lua_pushstring) (lua_State *L, const char *s); 270 LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 271 va_list argp); 272 LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 273 LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 274 LUA_API void (lua_pushboolean) (lua_State *L, int b); 275 LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 276 LUA_API void (lua_pushuserdata_native) (lua_State *L, void *p); 277 #define lua_pushuserdata lua_pushuserdata_native 278 LUA_API int (lua_pushthread) (lua_State *L); 279 280 281 /* 282 ** get functions (Lua -> stack) 283 */ 284 LUA_API int (lua_gettable) (lua_State *L, int idx); 285 LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); 286 LUA_API int (lua_rawget) (lua_State *L, int idx); 287 LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); 288 LUA_API int (lua_rawgeti) (lua_State *L, int idx, int n); 289 LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *); 290 291 LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 292 LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 293 LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 294 LUA_API void (lua_getfenv) (lua_State *L, int idx); 295 LUA_API int (lua_getuservalue) (lua_State *L, int idx); 296 297 298 /* 299 ** set functions (stack -> Lua) 300 */ 301 LUA_API void (lua_settable) (lua_State *L, int idx); 302 LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 303 LUA_API void (lua_rawset) (lua_State *L, int idx); 304 LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); 305 LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); 306 LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 307 LUA_API int (lua_setfenv) (lua_State *L, int idx); 308 LUA_API void (lua_setuservalue) (lua_State *L, int idx); 309 #if LJ_ABIVER==51 310 /* Some users check for those macros */ 311 #define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) 312 #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) 313 #else 314 LUA_API void (lua_setglobal) (lua_State *L, const char *var); 315 LUA_API int (lua_getglobal) (lua_State *L, const char *var); 316 #endif 317 318 319 320 /* 321 ** `load' and `call' functions (load and run Lua code) 322 */ 323 LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, 324 lua_KContext ctx, lua_KFunction k); 325 LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, 326 lua_KContext ctx, lua_KFunction k); 327 LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); 328 LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); 329 LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); 330 LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *data, 331 const char *chunkname 332 #if LJ_ABIVER!=51 333 , const char *mode 334 #endif 335 ); 336 LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); 337 338 339 /* 340 ** coroutine functions 341 */ 342 LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, 343 lua_KFunction k); 344 LUA_API int (lua_yield) (lua_State *L, int nresults); 345 LUA_API int (lua_resume) (lua_State *L, int narg); 346 LUA_API int (lua_status) (lua_State *L); 347 348 /* 349 ** garbage-collection function and options 350 */ 351 352 #define LUA_GCSTOP 0 353 #define LUA_GCRESTART 1 354 #define LUA_GCCOLLECT 2 355 #define LUA_GCCOUNT 3 356 #define LUA_GCCOUNTB 4 357 #define LUA_GCSTEP 5 358 #define LUA_GCSETPAUSE 6 359 #define LUA_GCSETSTEPMUL 7 360 #define LUA_GCSETMAJORINC 8 361 #define LUA_GCISRUNNING 9 362 #define LUA_GCGEN 10 363 #define LUA_GCINC 11 364 365 LUA_API int (lua_gc) (lua_State *L, int what, int data); 366 367 368 /* 369 ** miscellaneous functions 370 */ 371 372 LUA_API int (lua_error) (lua_State *L); 373 374 LUA_API int (lua_next) (lua_State *L, int idx); 375 376 LUA_API void (lua_concat) (lua_State *L, int n); 377 378 LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); 379 LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 380 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); 381 382 383 384 /* 385 ** =============================================================== 386 ** some useful macros 387 ** =============================================================== 388 */ 389 390 #define lua_pop(L,n) lua_settop(L, -(n)-1) 391 392 #define lua_newtable(L) lua_createtable(L, 0, 0) 393 394 #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 395 396 #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 397 398 #define lua_strlen(L,i) lua_objlen(L, (i)) 399 400 #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 401 #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 402 #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 403 #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 404 #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 405 #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 406 #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 407 #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 408 409 #define lua_pushliteral(L, s) \ 410 lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) 411 412 #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 413 414 #define lua_pushglobaltable(L) \ 415 lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) 416 417 418 419 /* 420 ** compatibility macros and functions 421 */ 422 423 #define lua_open() luaL_newstate() 424 425 #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) 426 427 #define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) 428 429 #define lua_Chunkreader lua_Reader 430 #define lua_Chunkwriter lua_Writer 431 432 433 /* hack */ 434 LUA_API void lua_setlevel (lua_State *from, lua_State *to); 435 436 437 /* 438 ** {====================================================================== 439 ** Debug API 440 ** ======================================================================= 441 */ 442 443 444 /* 445 ** Event codes 446 */ 447 #define LUA_HOOKCALL 0 448 #define LUA_HOOKRET 1 449 #define LUA_HOOKLINE 2 450 #define LUA_HOOKCOUNT 3 451 #define LUA_HOOKTAILRET 4 452 #define LUA_HOOKTAILCALL 4 453 454 455 /* 456 ** Event masks 457 */ 458 #define LUA_MASKCALL (1 << LUA_HOOKCALL) 459 #define LUA_MASKRET (1 << LUA_HOOKRET) 460 #define LUA_MASKLINE (1 << LUA_HOOKLINE) 461 #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 462 463 typedef struct lua_Debug lua_Debug; /* activation record */ 464 465 466 /* Functions to be called by the debuger in specific events */ 467 typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 468 469 470 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); 471 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); 472 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); 473 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); 474 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); 475 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); 476 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); 477 LUA_API lua_Hook lua_gethook (lua_State *L); 478 LUA_API int lua_gethookmask (lua_State *L); 479 LUA_API int lua_gethookcount (lua_State *L); 480 481 /* From Lua 5.2. */ 482 LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); 483 LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); 484 LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, 485 const char *chunkname, const char *mode); 486 487 488 struct lua_Debug { 489 int event; 490 const char *name; /* (n) */ 491 const char *namewhat; /* (n) `global', `local', `field', `method' */ 492 const char *what; /* (S) `Lua', `C', `main', `tail' */ 493 const char *source; /* (S) */ 494 #if LJ_ABIVER==51 495 int currentline; /* (l) */ 496 int nups; /* (u) number of upvalues */ 497 int linedefined; /* (S) */ 498 int lastlinedefined; /* (S) */ 499 char short_src[LUA_IDSIZE]; /* (S) */ 500 int i_ci; /* active function */ 501 #else 502 int currentline; /* (l) */ 503 int linedefined; /* (S) */ 504 int lastlinedefined; /* (S) */ 505 unsigned char nups; /* (u) number of upvalues */ 506 unsigned char nparams;/* (u) number of parameters */ 507 char isvararg; /* (u) */ 508 char istailcall; /* (t) */ 509 char short_src[LUA_IDSIZE]; /* (S) */ 510 /* private part */ 511 int i_ci; /* active function */ 512 #endif 513 }; 514 515 /* }====================================================================== */ 516 517 518 /****************************************************************************** 519 * Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. 520 * 521 * Permission is hereby granted, free of charge, to any person obtaining 522 * a copy of this software and associated documentation files (the 523 * "Software"), to deal in the Software without restriction, including 524 * without limitation the rights to use, copy, modify, merge, publish, 525 * distribute, sublicense, and/or sell copies of the Software, and to 526 * permit persons to whom the Software is furnished to do so, subject to 527 * the following conditions: 528 * 529 * The above copyright notice and this permission notice shall be 530 * included in all copies or substantial portions of the Software. 531 * 532 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 533 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 534 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 535 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 536 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 537 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 538 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 539 ******************************************************************************/ 540 541 542 #endif