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