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