├── LICENSE.txt ├── LUA_LICENSE.txt ├── README.md ├── include ├── Lua │ ├── lapi.h │ ├── lauxlib.h │ ├── lcode.h │ ├── lctype.h │ ├── ldebug.h │ ├── ldo.h │ ├── lfunc.h │ ├── lgc.h │ ├── ljumptab.h │ ├── llex.h │ ├── llimits.h │ ├── lmem.h │ ├── lobject.h │ ├── lopcodes.h │ ├── lopnames.h │ ├── lparser.h │ ├── lprefix.h │ ├── lstate.h │ ├── lstring.h │ ├── ltable.h │ ├── ltm.h │ ├── lua.h │ ├── lua.hpp │ ├── luaconf.h │ ├── lualib.h │ ├── lundump.h │ ├── lvm.h │ └── lzio.h ├── PluginSDK.h └── sdk │ ├── LuaFunction.hpp │ ├── LuaFunctionUtils.hpp │ ├── LuaTable.hpp │ ├── LuaTypes.hpp │ ├── LuaValue.hpp │ ├── LuaValueLuaImpl.hpp │ └── PluginApi.hpp └── lib ├── Lua.lib └── libluaplugin.a /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlueMountainsIO/OnsetSDK/16d7b2ad5aa3ea1e41c5bc79be600a8bd45eaad8/LICENSE.txt -------------------------------------------------------------------------------- /LUA_LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * Copyright (C) 1994-2019 Lua.org, PUC-Rio. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining 6 | * a copy of this software and associated documentation files (the 7 | * "Software"), to deal in the Software without restriction, including 8 | * without limitation the rights to use, copy, modify, merge, publish, 9 | * distribute, sublicense, and/or sell copies of the Software, and to 10 | * permit persons to whom the Software is furnished to do so, subject to 11 | * the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be 14 | * included in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | ******************************************************************************/ 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### Windows 2 | Add folder "include" to VC++ Directories -> Include Directories 3 | Add folder "lib" to VC++ Directoriers -> Library Directories 4 | Add "Lua.lib" to Linker -> Input 5 | Set C/C++ -> Language -> C++ Language Standard to "ISO C++17 Standard" 6 | 7 | Output as Dynamic Link Library (".dll") 8 | 9 | 10 | #### Linux 11 | Compiler flags: -m64 -fPIC -std=gnu++17 12 | Linker flags: -shared 13 | Link libluaplugin.a 14 | 15 | Output as shared object (".so") 16 | 17 | ### OPEN SOURCE LICENSE NOTICE FOR USE WITH ONSET 18 | ------- 19 | This Open Source License (“License”) applies to any use by you of the source and object code for the video game with 20 | the title Onset (“Game”) as provided here by Blue Mountains GmbH (“we,” “us,” or “our”), including any code 21 | created prior or after this date, (“Code”) and any derivatives of the Code (“Derivatives”) as well as any source and 22 | object code from a contributor embedded in the Code or the Derivatives (“Contributor Code”) (the Code, 23 | Derivatives, and Contributor Code, collectively, the “Licensed Code”). 24 | 25 | By using the Licensed Code you agree to and are legally bound by the terms and conditions of this License. If you do 26 | not agree to the terms and conditions of this License, do not use the Licensed Code and any use of the Licensed 27 | Code outside of the terms and Conditions of the License is an infringement of the Licensed Code. 28 | 29 | Grant. We grant you the worldwide, revocable, royalty-free, and nonsublicensable, rights to use the Licensed Code 30 | as follows: To reproduce, modify, display, perform, sublicense and distribute the Licensed Code solely to use the 31 | Licensed Code with and within the Game. (“Grant”) This Grant does not permit you to use our trade names, 32 | trademarks, service marks, or names, except as required for reasonable and customary use in describing the origin of 33 | the Licensed Content and reproducing the content of the copyright notice below. Nothing in this License shall be 34 | construed to grant you any ownership whatsoever in the Licensed Code and the property and intellectual property 35 | contained therein. 36 | 37 | Obligations. The Grant is subject to your express compliance with the following obligations: 38 | 39 | * Attribution. Notice: You shall keep intact all copyright, patent or trademarks notices and all notices that refer to 40 | the License and to the disclaimer of warranties. You shall include a copy of such notices and a copy of the 41 | License with every copy of the Licensed Code you distribute or communicate as follows: 42 | 43 | Copyright (C) 2020 Blue Mountains GmbH 44 | 45 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset Open Source 46 | License as published by Blue Mountains GmbH. 47 | 48 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 49 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Onset Open Source 50 | License for more details. 51 | 52 | You should have received a copy of the Onset Open Source License along with this program. If not, see <[URL]>. 53 | 54 | * Copyleft: If you distribute or communicate copies of the Licensed Code, this distribution or communication will 55 | be done under the terms of this License or of a later version of this License, unless the Licensed Content is 56 | expressly distributed only under this version of the License. You cannot offer or impose any additional terms or 57 | conditions on the Licensed Content that alter or restrict the terms of the License. 58 | 59 | * Provision of Source Code: When distributing or communicating copies of the Licensed Code, you shall provide a 60 | machine-readable copy of the source code or indicate a repository where this source code easily and freely is 61 | available for as long as you continue to distribute or communicate the Licensed Content. 62 | 63 | NO WARRANTIES. LICSENSED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS'' BASIS, WITHOUT WARRANTY 64 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE LICENSED 65 | CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE 66 | RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED SOFTWARE IS WITH YOU. SHOULD THE LICENSED 67 | CONTET PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT US, OUR LICENSORS OR AFFILIATES OR ANY OTHER 68 | CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF 69 | WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF THE LICENSED CONTENT IS AUTHORIZED 70 | HEREUNDER EXCEPT UNDER THIS DISCLAIMER. 71 | 72 | LIMITATION OF LIABILITY. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT (INCLUDING 73 | NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, US, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF 74 | THE LICENSED CODE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR ANY INDIRECT, 75 | SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, 76 | DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL 77 | OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN INFORMED OF THE POSSIBILITY 78 | OF SUCH DAMAGES. THIS LIMITATION OF LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY 79 | RESULTING FROM SUCH PARTY'S GROSS NEGLIGENCE TO THE EXTENT APPLICABLE LAW PROHIBITS SUCH 80 | LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL OR 81 | CONSEQUENTIAL DAMAGES, BUT MAY ALLOW LIABILITY TO BE LIMITED; IN SUCH CASES, A PARTY'S, ITS EMPLOYEES, 82 | LICENSORS OR AFFILIATES' LIABILITY SHALL BE LIMITED TO U.S. $50. 83 | 84 | Nothing contained in this License shall prejudice the statutory rights of any party dealing as a consumer. 85 | 86 | Term. This License and the rights granted hereunder will terminate automatically if you fail to comply with terms 87 | herein and fail to cure such breach within 30 days of becoming aware of the breach. All sublicenses to the Licensed 88 | Content which are properly granted shall survive any termination of this License. Provisions which, by their nature, 89 | must remain in effect beyond the termination of this License shall survive. 90 | 91 | Miscellaneous. This License represents the complete agreement concerning the subject matter hereof. All rights in 92 | the Licensed Content not expressly granted under this License are reserved. We may amend the Licensed Code and 93 | or the License at any time at our convenience. This License is governed by the laws of Germany, excluding its 94 | conflict-of-law provisions. All disputes arising from or relating to this Agreement shall be heard by the state and 95 | federal courts in Munich, Germany. If any part of this Agreement is found void and unenforceable, it will not affect 96 | the validity of the balance of the Agreement, which shall remain valid and enforceable according to its terms. 97 | 98 | UPDATE: 2019-08-02 99 | 100 | VERSION: 1.0 101 | -------------------------------------------------------------------------------- /include/Lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | 15 | /* Increments 'L->top', checking for stack overflows */ 16 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 17 | "stack overflow");} 18 | 19 | 20 | /* 21 | ** If a call returns too many multiple returns, the callee may not have 22 | ** stack space to accommodate all results. In this case, this macro 23 | ** increases its stack space ('L->ci->top'). 24 | */ 25 | #define adjustresults(L,nres) \ 26 | { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 27 | 28 | 29 | /* Ensure the stack has at least 'n' elements */ 30 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 31 | "not enough elements in the stack") 32 | 33 | 34 | /* 35 | ** To reduce the overhead of returning from C functions, the presence of 36 | ** to-be-closed variables in these functions is coded in the CallInfo's 37 | ** field 'nresults', in a way that functions with no to-be-closed variables 38 | ** with zero, one, or "all" wanted results have no overhead. Functions 39 | ** with other number of wanted results, as well as functions with 40 | ** variables to be closed, have an extra check. 41 | */ 42 | 43 | #define hastocloseCfunc(n) ((n) < LUA_MULTRET) 44 | 45 | /* Map [-1, inf) (range of 'nresults') into (-inf, -2] */ 46 | #define codeNresults(n) (-(n) - 3) 47 | #define decodeNresults(n) (-(n) - 3) 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/Lua/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "luaconf.h" 16 | #include "lua.h" 17 | 18 | 19 | /* global table */ 20 | #define LUA_GNAME "_G" 21 | 22 | 23 | typedef struct luaL_Buffer luaL_Buffer; 24 | 25 | 26 | /* extra error code for 'luaL_loadfilex' */ 27 | #define LUA_ERRFILE (LUA_ERRERR+1) 28 | 29 | 30 | /* key, in the registry, for table of loaded modules */ 31 | #define LUA_LOADED_TABLE "_LOADED" 32 | 33 | 34 | /* key, in the registry, for table of preloaded loaders */ 35 | #define LUA_PRELOAD_TABLE "_PRELOAD" 36 | 37 | 38 | typedef struct luaL_Reg { 39 | const char *name; 40 | lua_CFunction func; 41 | } luaL_Reg; 42 | 43 | 44 | #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 45 | 46 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 47 | #define luaL_checkversion(L) \ 48 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 49 | 50 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 51 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 52 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 53 | LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 54 | LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); 55 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 56 | size_t *l); 57 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 58 | const char *def, size_t *l); 59 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 60 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 61 | 62 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 63 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 64 | lua_Integer def); 65 | 66 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 67 | LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 68 | LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 69 | 70 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 71 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 72 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 73 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 74 | 75 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 76 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 77 | 78 | LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 79 | const char *const lst[]); 80 | 81 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 82 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 83 | 84 | 85 | /* predefined references */ 86 | #define LUA_NOREF (-2) 87 | #define LUA_REFNIL (-1) 88 | 89 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 90 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 91 | 92 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 93 | const char *mode); 94 | 95 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 96 | 97 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 98 | const char *name, const char *mode); 99 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 100 | 101 | LUALIB_API lua_State *(luaL_newstate) (void); 102 | 103 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 104 | 105 | LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, 106 | const char *p, const char *r); 107 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, 108 | const char *p, const char *r); 109 | 110 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 111 | 112 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 113 | 114 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 115 | const char *msg, int level); 116 | 117 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 118 | lua_CFunction openf, int glb); 119 | 120 | /* 121 | ** =============================================================== 122 | ** some useful macros 123 | ** =============================================================== 124 | */ 125 | 126 | 127 | #define luaL_newlibtable(L,l) \ 128 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 129 | 130 | #define luaL_newlib(L,l) \ 131 | (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 132 | 133 | #define luaL_argcheck(L, cond,arg,extramsg) \ 134 | ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) 135 | 136 | #define luaL_argexpected(L,cond,arg,tname) \ 137 | ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) 138 | 139 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 140 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 141 | 142 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 143 | 144 | #define luaL_dofile(L, fn) \ 145 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 146 | 147 | #define luaL_dostring(L, s) \ 148 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 149 | 150 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 151 | 152 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 153 | 154 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 155 | 156 | 157 | /* 158 | ** Perform arithmetic operations on lua_Integer values with wrap-around 159 | ** semantics, as the Lua core does. 160 | */ 161 | #define luaL_intop(op,v1,v2) \ 162 | ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) 163 | 164 | 165 | /* push the value used to represent failure/error */ 166 | #define luaL_pushfail(L) lua_pushnil(L) 167 | 168 | 169 | /* 170 | ** Internal assertions for in-house debugging 171 | */ 172 | #if !defined(lua_assert) 173 | 174 | #if defined LUAI_ASSERT 175 | #include 176 | #define lua_assert(c) assert(c) 177 | #else 178 | #define lua_assert(c) ((void)0) 179 | #endif 180 | 181 | #endif 182 | 183 | 184 | 185 | /* 186 | ** {====================================================== 187 | ** Generic Buffer manipulation 188 | ** ======================================================= 189 | */ 190 | 191 | struct luaL_Buffer { 192 | char *b; /* buffer address */ 193 | size_t size; /* buffer size */ 194 | size_t n; /* number of characters in buffer */ 195 | lua_State *L; 196 | union { 197 | LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ 198 | char b[LUAL_BUFFERSIZE]; /* initial buffer */ 199 | } init; 200 | }; 201 | 202 | 203 | #define luaL_bufflen(bf) ((bf)->n) 204 | #define luaL_buffaddr(bf) ((bf)->b) 205 | 206 | 207 | #define luaL_addchar(B,c) \ 208 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 209 | ((B)->b[(B)->n++] = (c))) 210 | 211 | #define luaL_addsize(B,s) ((B)->n += (s)) 212 | 213 | #define luaL_buffsub(B,s) ((B)->n -= (s)) 214 | 215 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 216 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 217 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 218 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 219 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 220 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 221 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 222 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 223 | 224 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 225 | 226 | /* }====================================================== */ 227 | 228 | 229 | 230 | /* 231 | ** {====================================================== 232 | ** File handles for IO library 233 | ** ======================================================= 234 | */ 235 | 236 | /* 237 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 238 | ** initial structure 'luaL_Stream' (it may contain other fields 239 | ** after that initial structure). 240 | */ 241 | 242 | #define LUA_FILEHANDLE "FILE*" 243 | 244 | 245 | typedef struct luaL_Stream { 246 | FILE *f; /* stream (NULL for incompletely created streams) */ 247 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 248 | } luaL_Stream; 249 | 250 | /* }====================================================== */ 251 | 252 | /* 253 | ** {================================================================== 254 | ** "Abstraction Layer" for basic report of messages and errors 255 | ** =================================================================== 256 | */ 257 | 258 | /* print a string */ 259 | #if !defined(lua_writestring) 260 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 261 | #endif 262 | 263 | /* print a newline and flush the output */ 264 | #if !defined(lua_writeline) 265 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 266 | #endif 267 | 268 | /* print an error message */ 269 | #if !defined(lua_writestringerror) 270 | #define lua_writestringerror(s,p) \ 271 | (fprintf(stderr, (s), (p)), fflush(stderr)) 272 | #endif 273 | 274 | /* }================================================================== */ 275 | 276 | 277 | /* 278 | ** {============================================================ 279 | ** Compatibility with deprecated conversions 280 | ** ============================================================= 281 | */ 282 | #if defined(LUA_COMPAT_APIINTCASTS) 283 | 284 | #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 285 | #define luaL_optunsigned(L,a,d) \ 286 | ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 287 | 288 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 289 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 290 | 291 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 292 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 293 | 294 | #endif 295 | /* }============================================================ */ 296 | 297 | 298 | 299 | #endif 300 | 301 | 302 | -------------------------------------------------------------------------------- /include/Lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | /* arithmetic operators */ 28 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 29 | OPR_DIV, OPR_IDIV, 30 | /* bitwise operators */ 31 | OPR_BAND, OPR_BOR, OPR_BXOR, 32 | OPR_SHL, OPR_SHR, 33 | /* string operator */ 34 | OPR_CONCAT, 35 | /* comparison operators */ 36 | OPR_EQ, OPR_LT, OPR_LE, 37 | OPR_NE, OPR_GT, OPR_GE, 38 | /* logical operators */ 39 | OPR_AND, OPR_OR, 40 | OPR_NOBINOPR 41 | } BinOpr; 42 | 43 | 44 | /* true if operation is foldable (that is, it is arithmetic or bitwise) */ 45 | #define foldbinop(op) ((op) <= OPR_SHR) 46 | 47 | 48 | #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) 49 | 50 | 51 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 52 | 53 | 54 | /* get (pointer to) instruction of given 'expdesc' */ 55 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 56 | 57 | 58 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 59 | 60 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 61 | 62 | LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); 63 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 64 | LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); 65 | LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, 66 | int B, int C, int k); 67 | LUAI_FUNC int luaK_isKint (expdesc *e); 68 | LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); 69 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 70 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 71 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 72 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 73 | LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); 74 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 75 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 76 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 77 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 78 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 79 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 80 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 81 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 82 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 83 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 84 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 85 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 86 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 87 | LUAI_FUNC int luaK_jump (FuncState *fs); 88 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 89 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 90 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 91 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 92 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 93 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 94 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 95 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 96 | expdesc *v2, int line); 97 | LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, 98 | int ra, int asize, int hsize); 99 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 100 | LUAI_FUNC void luaK_finish (FuncState *fs); 101 | LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); 102 | 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /include/Lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua. 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | 65 | /* 66 | ** In ASCII, this 'ltolower' is correct for alphabetic characters and 67 | ** for '.'. That is enough for Lua needs. ('check_exp' ensures that 68 | ** the character either is an upper-case letter or is unchanged by 69 | ** the transformation, which holds for lower-case letters and '.'.) 70 | */ 71 | #define ltolower(c) \ 72 | check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ 73 | (c) | ('A' ^ 'a')) 74 | 75 | 76 | /* one entry for each character and for -1 (EOZ) */ 77 | LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) 78 | 79 | 80 | #else /* }{ */ 81 | 82 | /* 83 | ** use standard C ctypes 84 | */ 85 | 86 | #include 87 | 88 | 89 | #define lislalpha(c) (isalpha(c) || (c) == '_') 90 | #define lislalnum(c) (isalnum(c) || (c) == '_') 91 | #define lisdigit(c) (isdigit(c)) 92 | #define lisspace(c) (isspace(c)) 93 | #define lisprint(c) (isprint(c)) 94 | #define lisxdigit(c) (isxdigit(c)) 95 | 96 | #define ltolower(c) (tolower(c)) 97 | 98 | #endif /* } */ 99 | 100 | #endif 101 | 102 | -------------------------------------------------------------------------------- /include/Lua/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) 15 | 16 | 17 | /* Active Lua function (given call info) */ 18 | #define ci_func(ci) (clLvalue(s2v((ci)->func))) 19 | 20 | 21 | #define resethookcount(L) (L->hookcount = L->basehookcount) 22 | 23 | /* 24 | ** mark for entries in 'lineinfo' array that has absolute information in 25 | ** 'abslineinfo' array 26 | */ 27 | #define ABSLINEINFO (-0x80) 28 | 29 | 30 | /* 31 | ** MAXimum number of successive Instructions WiTHout ABSolute line 32 | ** information. (A power of two allows fast divisions.) 33 | */ 34 | #if !defined(MAXIWTHABS) 35 | #define MAXIWTHABS 128 36 | #endif 37 | 38 | 39 | LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); 40 | LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, 41 | StkId *pos); 42 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 43 | const char *opname); 44 | LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); 45 | LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, 46 | const char *what); 47 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 48 | const TValue *p2); 49 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 50 | const TValue *p2, 51 | const char *msg); 52 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 53 | const TValue *p2); 54 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 55 | const TValue *p2); 56 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 57 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 58 | TString *src, int line); 59 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 60 | LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); 61 | 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /include/Lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** It also allows the running of one GC step when the stack is 21 | ** reallocated. 22 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 23 | ** at every check. 24 | */ 25 | #define luaD_checkstackaux(L,n,pre,pos) \ 26 | if (l_unlikely(L->stack_last - L->top <= (n))) \ 27 | { pre; luaD_growstack(L, n, 1); pos; } \ 28 | else { condmovestack(L,pre,pos); } 29 | 30 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 31 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 32 | 33 | 34 | 35 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 36 | #define restorestack(L,n) ((StkId)((char *)L->stack + (n))) 37 | 38 | 39 | /* macro to check stack size, preserving 'p' */ 40 | #define checkstackGCp(L,n,p) \ 41 | luaD_checkstackaux(L, n, \ 42 | ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ 43 | luaC_checkGC(L), /* stack grow uses memory */ \ 44 | p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ 45 | 46 | 47 | /* macro to check stack size and GC */ 48 | #define checkstackGC(L,fsize) \ 49 | luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) 50 | 51 | 52 | /* type of protected functions, to be ran by 'runprotected' */ 53 | typedef void (*Pfunc) (lua_State *L, void *ud); 54 | 55 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 56 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 57 | const char *mode); 58 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, 59 | int fTransfer, int nTransfer); 60 | LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); 61 | LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); 62 | LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); 63 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 64 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 65 | LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); 66 | LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); 67 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 68 | ptrdiff_t oldtop, ptrdiff_t ef); 69 | LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); 70 | LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); 71 | LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); 72 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 73 | LUAI_FUNC void luaD_inctop (lua_State *L); 74 | 75 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 76 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 77 | 78 | #endif 79 | 80 | -------------------------------------------------------------------------------- /include/Lua/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ 15 | cast_int(sizeof(TValue)) * (n)) 16 | 17 | #define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ 18 | cast_int(sizeof(TValue *)) * (n)) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | #define upisopen(up) ((up)->v != &(up)->u.value) 33 | 34 | 35 | #define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) 36 | 37 | 38 | /* 39 | ** maximum number of misses before giving up the cache of closures 40 | ** in prototypes 41 | */ 42 | #define MAXMISS 10 43 | 44 | 45 | 46 | /* special status to close upvalues preserving the top of the stack */ 47 | #define CLOSEKTOP (-1) 48 | 49 | 50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); 52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); 53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 55 | LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); 56 | LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); 57 | LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); 58 | LUAI_FUNC void luaF_unlinkupval (UpVal *uv); 59 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 60 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 61 | int pc); 62 | 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /include/Lua/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which means 16 | ** the object is not marked; gray, which means the object is marked, but 17 | ** its references may be not marked; and black, which means that the 18 | ** object and all its references are marked. The main invariant of the 19 | ** garbage collector, while marking objects, is that a black object can 20 | ** never point to a white one. Moreover, any gray object must be in a 21 | ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it 22 | ** can be visited again before finishing the collection cycle. (Open 23 | ** upvalues are an exception to this rule.) These lists have no meaning 24 | ** when the invariant is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | /* 29 | ** Possible states of the Garbage Collector 30 | */ 31 | #define GCSpropagate 0 32 | #define GCSenteratomic 1 33 | #define GCSatomic 2 34 | #define GCSswpallgc 3 35 | #define GCSswpfinobj 4 36 | #define GCSswptobefnz 5 37 | #define GCSswpend 6 38 | #define GCScallfin 7 39 | #define GCSpause 8 40 | 41 | 42 | #define issweepphase(g) \ 43 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 44 | 45 | 46 | /* 47 | ** macro to tell when main invariant (white objects cannot point to black 48 | ** ones) must be kept. During a collection, the sweep 49 | ** phase may break the invariant, as objects turned white may point to 50 | ** still-black objects. The invariant is restored when sweep ends and 51 | ** all objects are white again. 52 | */ 53 | 54 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 55 | 56 | 57 | /* 58 | ** some useful bit tricks 59 | */ 60 | #define resetbits(x,m) ((x) &= cast_byte(~(m))) 61 | #define setbits(x,m) ((x) |= (m)) 62 | #define testbits(x,m) ((x) & (m)) 63 | #define bitmask(b) (1<<(b)) 64 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 65 | #define l_setbit(x,b) setbits(x, bitmask(b)) 66 | #define resetbit(x,b) resetbits(x, bitmask(b)) 67 | #define testbit(x,b) testbits(x, bitmask(b)) 68 | 69 | 70 | /* 71 | ** Layout for bit use in 'marked' field. First three bits are 72 | ** used for object "age" in generational mode. Last bit is used 73 | ** by tests. 74 | */ 75 | #define WHITE0BIT 3 /* object is white (type 0) */ 76 | #define WHITE1BIT 4 /* object is white (type 1) */ 77 | #define BLACKBIT 5 /* object is black */ 78 | #define FINALIZEDBIT 6 /* object has been marked for finalization */ 79 | 80 | #define TESTBIT 7 81 | 82 | 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) ((m) & (ow)) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define nw2black(x) \ 100 | check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) 101 | 102 | #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) 103 | 104 | 105 | /* object age in generational mode */ 106 | #define G_NEW 0 /* created in current cycle */ 107 | #define G_SURVIVAL 1 /* created in previous cycle */ 108 | #define G_OLD0 2 /* marked old by frw. barrier in this cycle */ 109 | #define G_OLD1 3 /* first full cycle as old */ 110 | #define G_OLD 4 /* really old object (not to be visited) */ 111 | #define G_TOUCHED1 5 /* old object touched this cycle */ 112 | #define G_TOUCHED2 6 /* old object touched in previous cycle */ 113 | 114 | #define AGEBITS 7 /* all age bits (111) */ 115 | 116 | #define getage(o) ((o)->marked & AGEBITS) 117 | #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) 118 | #define isold(o) (getage(o) > G_SURVIVAL) 119 | 120 | #define changeage(o,f,t) \ 121 | check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) 122 | 123 | 124 | /* Default Values for GC parameters */ 125 | #define LUAI_GENMAJORMUL 100 126 | #define LUAI_GENMINORMUL 20 127 | 128 | /* wait memory to double before starting new cycle */ 129 | #define LUAI_GCPAUSE 200 130 | 131 | /* 132 | ** some gc parameters are stored divided by 4 to allow a maximum value 133 | ** up to 1023 in a 'lu_byte'. 134 | */ 135 | #define getgcparam(p) ((p) * 4) 136 | #define setgcparam(p,v) ((p) = (v) / 4) 137 | 138 | #define LUAI_GCMUL 100 139 | 140 | /* how much to allocate before next GC step (log2) */ 141 | #define LUAI_GCSTEPSIZE 13 /* 8 KB */ 142 | 143 | 144 | /* 145 | ** Check whether the declared GC mode is generational. While in 146 | ** generational mode, the collector can go temporarily to incremental 147 | ** mode to improve performance. This is signaled by 'g->lastatomic != 0'. 148 | */ 149 | #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) 150 | 151 | 152 | /* 153 | ** Control when GC is running: 154 | */ 155 | #define GCSTPUSR 1 /* bit true when GC stopped by user */ 156 | #define GCSTPGC 2 /* bit true when GC stopped by itself */ 157 | #define GCSTPCLS 4 /* bit true when closing Lua state */ 158 | #define gcrunning(g) ((g)->gcstp == 0) 159 | 160 | 161 | /* 162 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 163 | ** allows some adjustments to be done only when needed. macro 164 | ** 'condchangemem' is used only for heavy tests (forcing a full 165 | ** GC cycle on every opportunity) 166 | */ 167 | #define luaC_condGC(L,pre,pos) \ 168 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 169 | condchangemem(L,pre,pos); } 170 | 171 | /* more often than not, 'pre'/'pos' are empty */ 172 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 173 | 174 | 175 | #define luaC_barrier(L,p,v) ( \ 176 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 177 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 178 | 179 | #define luaC_barrierback(L,p,v) ( \ 180 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 181 | luaC_barrierback_(L,p) : cast_void(0)) 182 | 183 | #define luaC_objbarrier(L,p,o) ( \ 184 | (isblack(p) && iswhite(o)) ? \ 185 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 186 | 187 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 188 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 189 | LUAI_FUNC void luaC_step (lua_State *L); 190 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 191 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 192 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 193 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 194 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 195 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 196 | LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); 197 | 198 | 199 | #endif 200 | -------------------------------------------------------------------------------- /include/Lua/ljumptab.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ljumptab.h $ 3 | ** Jump Table for the Lua interpreter 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #undef vmdispatch 9 | #undef vmcase 10 | #undef vmbreak 11 | 12 | #define vmdispatch(x) goto *disptab[x]; 13 | 14 | #define vmcase(l) L_##l: 15 | 16 | #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); 17 | 18 | 19 | static const void *const disptab[NUM_OPCODES] = { 20 | 21 | #if 0 22 | ** you can update the following list with this command: 23 | ** 24 | ** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h 25 | ** 26 | #endif 27 | 28 | &&L_OP_MOVE, 29 | &&L_OP_LOADI, 30 | &&L_OP_LOADF, 31 | &&L_OP_LOADK, 32 | &&L_OP_LOADKX, 33 | &&L_OP_LOADFALSE, 34 | &&L_OP_LFALSESKIP, 35 | &&L_OP_LOADTRUE, 36 | &&L_OP_LOADNIL, 37 | &&L_OP_GETUPVAL, 38 | &&L_OP_SETUPVAL, 39 | &&L_OP_GETTABUP, 40 | &&L_OP_GETTABLE, 41 | &&L_OP_GETI, 42 | &&L_OP_GETFIELD, 43 | &&L_OP_SETTABUP, 44 | &&L_OP_SETTABLE, 45 | &&L_OP_SETI, 46 | &&L_OP_SETFIELD, 47 | &&L_OP_NEWTABLE, 48 | &&L_OP_SELF, 49 | &&L_OP_ADDI, 50 | &&L_OP_ADDK, 51 | &&L_OP_SUBK, 52 | &&L_OP_MULK, 53 | &&L_OP_MODK, 54 | &&L_OP_POWK, 55 | &&L_OP_DIVK, 56 | &&L_OP_IDIVK, 57 | &&L_OP_BANDK, 58 | &&L_OP_BORK, 59 | &&L_OP_BXORK, 60 | &&L_OP_SHRI, 61 | &&L_OP_SHLI, 62 | &&L_OP_ADD, 63 | &&L_OP_SUB, 64 | &&L_OP_MUL, 65 | &&L_OP_MOD, 66 | &&L_OP_POW, 67 | &&L_OP_DIV, 68 | &&L_OP_IDIV, 69 | &&L_OP_BAND, 70 | &&L_OP_BOR, 71 | &&L_OP_BXOR, 72 | &&L_OP_SHL, 73 | &&L_OP_SHR, 74 | &&L_OP_MMBIN, 75 | &&L_OP_MMBINI, 76 | &&L_OP_MMBINK, 77 | &&L_OP_UNM, 78 | &&L_OP_BNOT, 79 | &&L_OP_NOT, 80 | &&L_OP_LEN, 81 | &&L_OP_CONCAT, 82 | &&L_OP_CLOSE, 83 | &&L_OP_TBC, 84 | &&L_OP_JMP, 85 | &&L_OP_EQ, 86 | &&L_OP_LT, 87 | &&L_OP_LE, 88 | &&L_OP_EQK, 89 | &&L_OP_EQI, 90 | &&L_OP_LTI, 91 | &&L_OP_LEI, 92 | &&L_OP_GTI, 93 | &&L_OP_GEI, 94 | &&L_OP_TEST, 95 | &&L_OP_TESTSET, 96 | &&L_OP_CALL, 97 | &&L_OP_TAILCALL, 98 | &&L_OP_RETURN, 99 | &&L_OP_RETURN0, 100 | &&L_OP_RETURN1, 101 | &&L_OP_FORLOOP, 102 | &&L_OP_FORPREP, 103 | &&L_OP_TFORPREP, 104 | &&L_OP_TFORCALL, 105 | &&L_OP_TFORLOOP, 106 | &&L_OP_SETLIST, 107 | &&L_OP_CLOSURE, 108 | &&L_OP_VARARG, 109 | &&L_OP_VARARGPREP, 110 | &&L_OP_EXTRAARG 111 | 112 | }; 113 | -------------------------------------------------------------------------------- /include/Lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include 11 | 12 | #include "lobject.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Single-char tokens (terminal symbols) are represented by their own 18 | ** numeric code. Other tokens start at the following value. 19 | */ 20 | #define FIRST_RESERVED (UCHAR_MAX + 1) 21 | 22 | 23 | #if !defined(LUA_ENV) 24 | #define LUA_ENV "_ENV" 25 | #endif 26 | 27 | 28 | /* 29 | * WARNING: if you change the order of this enumeration, 30 | * grep "ORDER RESERVED" 31 | */ 32 | enum RESERVED { 33 | /* terminal symbols denoted by reserved words */ 34 | TK_AND = FIRST_RESERVED, TK_BREAK, 35 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 36 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 37 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 38 | /* other terminal symbols */ 39 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 40 | TK_SHL, TK_SHR, 41 | TK_DBCOLON, TK_EOS, 42 | TK_FLT, TK_INT, TK_NAME, TK_STRING 43 | }; 44 | 45 | /* number of reserved words */ 46 | #define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) 47 | 48 | 49 | typedef union { 50 | lua_Number r; 51 | lua_Integer i; 52 | TString *ts; 53 | } SemInfo; /* semantics information */ 54 | 55 | 56 | typedef struct Token { 57 | int token; 58 | SemInfo seminfo; 59 | } Token; 60 | 61 | 62 | /* state of the lexer plus state of the parser when shared by all 63 | functions */ 64 | typedef struct LexState { 65 | int current; /* current character (charint) */ 66 | int linenumber; /* input line counter */ 67 | int lastline; /* line of last token 'consumed' */ 68 | Token t; /* current token */ 69 | Token lookahead; /* look ahead token */ 70 | struct FuncState *fs; /* current function (parser) */ 71 | struct lua_State *L; 72 | ZIO *z; /* input stream */ 73 | Mbuffer *buff; /* buffer for tokens */ 74 | Table *h; /* to avoid collection/reuse strings */ 75 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 76 | TString *source; /* current source name */ 77 | TString *envn; /* environment variable name */ 78 | } LexState; 79 | 80 | 81 | LUAI_FUNC void luaX_init (lua_State *L); 82 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 83 | TString *source, int firstchar); 84 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 85 | LUAI_FUNC void luaX_next (LexState *ls); 86 | LUAI_FUNC int luaX_lookahead (LexState *ls); 87 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 88 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 89 | 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /include/Lua/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h $ 3 | ** Limits, basic types, and some other 'installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | /* 19 | ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count 20 | ** the total memory used by Lua (in bytes). Usually, 'size_t' and 21 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. 22 | */ 23 | #if defined(LUAI_MEM) /* { external definitions? */ 24 | typedef LUAI_UMEM lu_mem; 25 | typedef LUAI_MEM l_mem; 26 | #elif LUAI_IS32INT /* }{ */ 27 | typedef size_t lu_mem; 28 | typedef ptrdiff_t l_mem; 29 | #else /* 16-bit ints */ /* }{ */ 30 | typedef unsigned long lu_mem; 31 | typedef long l_mem; 32 | #endif /* } */ 33 | 34 | 35 | /* chars used as small naturals (so that 'char' is reserved for characters) */ 36 | typedef unsigned char lu_byte; 37 | typedef signed char ls_byte; 38 | 39 | 40 | /* maximum value for size_t */ 41 | #define MAX_SIZET ((size_t)(~(size_t)0)) 42 | 43 | /* maximum size visible for Lua (must be representable in a lua_Integer) */ 44 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ 45 | : (size_t)(LUA_MAXINTEGER)) 46 | 47 | 48 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) 49 | 50 | #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) 51 | 52 | 53 | #define MAX_INT INT_MAX /* maximum value of an int */ 54 | 55 | 56 | /* 57 | ** floor of the log2 of the maximum signed value for integral type 't'. 58 | ** (That is, maximum 'n' such that '2^n' fits in the given signed type.) 59 | */ 60 | #define log2maxs(t) (sizeof(t) * 8 - 2) 61 | 62 | 63 | /* 64 | ** test whether an unsigned value is a power of 2 (or zero) 65 | */ 66 | #define ispow2(x) (((x) & ((x) - 1)) == 0) 67 | 68 | 69 | /* number of chars of a literal string without the ending \0 */ 70 | #define LL(x) (sizeof(x)/sizeof(char) - 1) 71 | 72 | 73 | /* 74 | ** conversion of pointer to unsigned integer: 75 | ** this is for hashing only; there is no problem if the integer 76 | ** cannot hold the whole pointer value 77 | */ 78 | #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) 79 | 80 | 81 | 82 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */ 83 | typedef LUAI_UACNUMBER l_uacNumber; 84 | typedef LUAI_UACINT l_uacInt; 85 | 86 | 87 | /* 88 | ** Internal assertions for in-house debugging 89 | */ 90 | #if defined LUAI_ASSERT 91 | #undef NDEBUG 92 | #include 93 | #define lua_assert(c) assert(c) 94 | #endif 95 | 96 | #if defined(lua_assert) 97 | #define check_exp(c,e) (lua_assert(c), (e)) 98 | /* to avoid problems with conditions too long */ 99 | #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) 100 | #else 101 | #define lua_assert(c) ((void)0) 102 | #define check_exp(c,e) (e) 103 | #define lua_longassert(c) ((void)0) 104 | #endif 105 | 106 | /* 107 | ** assertion for checking API calls 108 | */ 109 | #if !defined(luai_apicheck) 110 | #define luai_apicheck(l,e) ((void)l, lua_assert(e)) 111 | #endif 112 | 113 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 114 | 115 | 116 | /* macro to avoid warnings about unused variables */ 117 | #if !defined(UNUSED) 118 | #define UNUSED(x) ((void)(x)) 119 | #endif 120 | 121 | 122 | /* type casts (a macro highlights casts in the code) */ 123 | #define cast(t, exp) ((t)(exp)) 124 | 125 | #define cast_void(i) cast(void, (i)) 126 | #define cast_voidp(i) cast(void *, (i)) 127 | #define cast_num(i) cast(lua_Number, (i)) 128 | #define cast_int(i) cast(int, (i)) 129 | #define cast_uint(i) cast(unsigned int, (i)) 130 | #define cast_byte(i) cast(lu_byte, (i)) 131 | #define cast_uchar(i) cast(unsigned char, (i)) 132 | #define cast_char(i) cast(char, (i)) 133 | #define cast_charp(i) cast(char *, (i)) 134 | #define cast_sizet(i) cast(size_t, (i)) 135 | 136 | 137 | /* cast a signed lua_Integer to lua_Unsigned */ 138 | #if !defined(l_castS2U) 139 | #define l_castS2U(i) ((lua_Unsigned)(i)) 140 | #endif 141 | 142 | /* 143 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is 144 | ** not strict ISO C, but two-complement architectures should 145 | ** work fine. 146 | */ 147 | #if !defined(l_castU2S) 148 | #define l_castU2S(i) ((lua_Integer)(i)) 149 | #endif 150 | 151 | 152 | /* 153 | ** non-return type 154 | */ 155 | #if !defined(l_noret) 156 | 157 | #if defined(__GNUC__) 158 | #define l_noret void __attribute__((noreturn)) 159 | #elif defined(_MSC_VER) && _MSC_VER >= 1200 160 | #define l_noret void __declspec(noreturn) 161 | #else 162 | #define l_noret void 163 | #endif 164 | 165 | #endif 166 | 167 | 168 | /* 169 | ** Inline functions 170 | */ 171 | #if !defined(LUA_USE_C89) 172 | #define l_inline inline 173 | #elif defined(__GNUC__) 174 | #define l_inline __inline__ 175 | #else 176 | #define l_inline /* empty */ 177 | #endif 178 | 179 | #define l_sinline static l_inline 180 | 181 | 182 | /* 183 | ** type for virtual-machine instructions; 184 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 185 | */ 186 | #if LUAI_IS32INT 187 | typedef unsigned int l_uint32; 188 | #else 189 | typedef unsigned long l_uint32; 190 | #endif 191 | 192 | typedef l_uint32 Instruction; 193 | 194 | 195 | 196 | /* 197 | ** Maximum length for short strings, that is, strings that are 198 | ** internalized. (Cannot be smaller than reserved words or tags for 199 | ** metamethods, as these strings must be internalized; 200 | ** #("function") = 8, #("__newindex") = 10.) 201 | */ 202 | #if !defined(LUAI_MAXSHORTLEN) 203 | #define LUAI_MAXSHORTLEN 40 204 | #endif 205 | 206 | 207 | /* 208 | ** Initial size for the string table (must be power of 2). 209 | ** The Lua core alone registers ~50 strings (reserved words + 210 | ** metaevent keys + a few others). Libraries would typically add 211 | ** a few dozens more. 212 | */ 213 | #if !defined(MINSTRTABSIZE) 214 | #define MINSTRTABSIZE 128 215 | #endif 216 | 217 | 218 | /* 219 | ** Size of cache for strings in the API. 'N' is the number of 220 | ** sets (better be a prime) and "M" is the size of each set (M == 1 221 | ** makes a direct cache.) 222 | */ 223 | #if !defined(STRCACHE_N) 224 | #define STRCACHE_N 53 225 | #define STRCACHE_M 2 226 | #endif 227 | 228 | 229 | /* minimum size for string buffer */ 230 | #if !defined(LUA_MINBUFFER) 231 | #define LUA_MINBUFFER 32 232 | #endif 233 | 234 | 235 | /* 236 | ** Maximum depth for nested C calls, syntactical nested non-terminals, 237 | ** and other features implemented through recursion in C. (Value must 238 | ** fit in a 16-bit unsigned integer. It must also be compatible with 239 | ** the size of the C stack.) 240 | */ 241 | #if !defined(LUAI_MAXCCALLS) 242 | #define LUAI_MAXCCALLS 200 243 | #endif 244 | 245 | 246 | /* 247 | ** macros that are executed whenever program enters the Lua core 248 | ** ('lua_lock') and leaves the core ('lua_unlock') 249 | */ 250 | #if !defined(lua_lock) 251 | #define lua_lock(L) ((void) 0) 252 | #define lua_unlock(L) ((void) 0) 253 | #endif 254 | 255 | /* 256 | ** macro executed during Lua functions at points where the 257 | ** function can yield. 258 | */ 259 | #if !defined(luai_threadyield) 260 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 261 | #endif 262 | 263 | 264 | /* 265 | ** these macros allow user-specific actions when a thread is 266 | ** created/deleted/resumed/yielded. 267 | */ 268 | #if !defined(luai_userstateopen) 269 | #define luai_userstateopen(L) ((void)L) 270 | #endif 271 | 272 | #if !defined(luai_userstateclose) 273 | #define luai_userstateclose(L) ((void)L) 274 | #endif 275 | 276 | #if !defined(luai_userstatethread) 277 | #define luai_userstatethread(L,L1) ((void)L) 278 | #endif 279 | 280 | #if !defined(luai_userstatefree) 281 | #define luai_userstatefree(L,L1) ((void)L) 282 | #endif 283 | 284 | #if !defined(luai_userstateresume) 285 | #define luai_userstateresume(L,n) ((void)L) 286 | #endif 287 | 288 | #if !defined(luai_userstateyield) 289 | #define luai_userstateyield(L,n) ((void)L) 290 | #endif 291 | 292 | 293 | 294 | /* 295 | ** The luai_num* macros define the primitive operations over numbers. 296 | */ 297 | 298 | /* floor division (defined as 'floor(a/b)') */ 299 | #if !defined(luai_numidiv) 300 | #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) 301 | #endif 302 | 303 | /* float division */ 304 | #if !defined(luai_numdiv) 305 | #define luai_numdiv(L,a,b) ((a)/(b)) 306 | #endif 307 | 308 | /* 309 | ** modulo: defined as 'a - floor(a/b)*b'; the direct computation 310 | ** using this definition has several problems with rounding errors, 311 | ** so it is better to use 'fmod'. 'fmod' gives the result of 312 | ** 'a - trunc(a/b)*b', and therefore must be corrected when 313 | ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a 314 | ** non-integer negative result: non-integer result is equivalent to 315 | ** a non-zero remainder 'm'; negative result is equivalent to 'a' and 316 | ** 'b' with different signs, or 'm' and 'b' with different signs 317 | ** (as the result 'm' of 'fmod' has the same sign of 'a'). 318 | */ 319 | #if !defined(luai_nummod) 320 | #define luai_nummod(L,a,b,m) \ 321 | { (void)L; (m) = l_mathop(fmod)(a,b); \ 322 | if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } 323 | #endif 324 | 325 | /* exponentiation */ 326 | #if !defined(luai_numpow) 327 | #define luai_numpow(L,a,b) \ 328 | ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) 329 | #endif 330 | 331 | /* the others are quite standard operations */ 332 | #if !defined(luai_numadd) 333 | #define luai_numadd(L,a,b) ((a)+(b)) 334 | #define luai_numsub(L,a,b) ((a)-(b)) 335 | #define luai_nummul(L,a,b) ((a)*(b)) 336 | #define luai_numunm(L,a) (-(a)) 337 | #define luai_numeq(a,b) ((a)==(b)) 338 | #define luai_numlt(a,b) ((a)<(b)) 339 | #define luai_numle(a,b) ((a)<=(b)) 340 | #define luai_numgt(a,b) ((a)>(b)) 341 | #define luai_numge(a,b) ((a)>=(b)) 342 | #define luai_numisnan(a) (!luai_numeq((a), (a))) 343 | #endif 344 | 345 | 346 | 347 | 348 | 349 | /* 350 | ** macro to control inclusion of some hard tests on stack reallocation 351 | */ 352 | #if !defined(HARDSTACKTESTS) 353 | #define condmovestack(L,pre,pos) ((void)0) 354 | #else 355 | /* realloc stack keeping its size */ 356 | #define condmovestack(L,pre,pos) \ 357 | { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } 358 | #endif 359 | 360 | #if !defined(HARDMEMTESTS) 361 | #define condchangemem(L,pre,pos) ((void)0) 362 | #else 363 | #define condchangemem(L,pre,pos) \ 364 | { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } 365 | #endif 366 | 367 | #endif 368 | -------------------------------------------------------------------------------- /include/Lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /include/Lua/lobject.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.h $ 3 | ** Type definitions for Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lobject_h 9 | #define lobject_h 10 | 11 | 12 | #include 13 | 14 | 15 | #include "llimits.h" 16 | #include "lua.h" 17 | 18 | 19 | /* 20 | ** Extra types for collectable non-values 21 | */ 22 | #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ 23 | #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ 24 | #define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ 25 | 26 | 27 | 28 | /* 29 | ** number of all possible types (including LUA_TNONE but excluding DEADKEY) 30 | */ 31 | #define LUA_TOTALTYPES (LUA_TPROTO + 2) 32 | 33 | 34 | /* 35 | ** tags for Tagged Values have the following use of bits: 36 | ** bits 0-3: actual tag (a LUA_T* constant) 37 | ** bits 4-5: variant bits 38 | ** bit 6: whether value is collectable 39 | */ 40 | 41 | /* add variant bits to a type */ 42 | #define makevariant(t,v) ((t) | ((v) << 4)) 43 | 44 | 45 | 46 | /* 47 | ** Union of all Lua values 48 | */ 49 | typedef union Value { 50 | struct GCObject *gc; /* collectable objects */ 51 | void *p; /* light userdata */ 52 | lua_CFunction f; /* light C functions */ 53 | lua_Integer i; /* integer numbers */ 54 | lua_Number n; /* float numbers */ 55 | } Value; 56 | 57 | 58 | /* 59 | ** Tagged Values. This is the basic representation of values in Lua: 60 | ** an actual value plus a tag with its type. 61 | */ 62 | 63 | #define TValuefields Value value_; lu_byte tt_ 64 | 65 | typedef struct TValue { 66 | TValuefields; 67 | } TValue; 68 | 69 | 70 | #define val_(o) ((o)->value_) 71 | #define valraw(o) (val_(o)) 72 | 73 | 74 | /* raw type tag of a TValue */ 75 | #define rawtt(o) ((o)->tt_) 76 | 77 | /* tag with no variants (bits 0-3) */ 78 | #define novariant(t) ((t) & 0x0F) 79 | 80 | /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ 81 | #define withvariant(t) ((t) & 0x3F) 82 | #define ttypetag(o) withvariant(rawtt(o)) 83 | 84 | /* type of a TValue */ 85 | #define ttype(o) (novariant(rawtt(o))) 86 | 87 | 88 | /* Macros to test type */ 89 | #define checktag(o,t) (rawtt(o) == (t)) 90 | #define checktype(o,t) (ttype(o) == (t)) 91 | 92 | 93 | /* Macros for internal tests */ 94 | 95 | /* collectable object has the same tag as the original value */ 96 | #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) 97 | 98 | /* 99 | ** Any value being manipulated by the program either is non 100 | ** collectable, or the collectable object has the right tag 101 | ** and it is not dead. The option 'L == NULL' allows other 102 | ** macros using this one to be used where L is not available. 103 | */ 104 | #define checkliveness(L,obj) \ 105 | ((void)L, lua_longassert(!iscollectable(obj) || \ 106 | (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) 107 | 108 | 109 | /* Macros to set values */ 110 | 111 | /* set a value's tag */ 112 | #define settt_(o,t) ((o)->tt_=(t)) 113 | 114 | 115 | /* main macro to copy values (from 'obj2' to 'obj1') */ 116 | #define setobj(L,obj1,obj2) \ 117 | { TValue *io1=(obj1); const TValue *io2=(obj2); \ 118 | io1->value_ = io2->value_; settt_(io1, io2->tt_); \ 119 | checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } 120 | 121 | /* 122 | ** Different types of assignments, according to source and destination. 123 | ** (They are mostly equal now, but may be different in the future.) 124 | */ 125 | 126 | /* from stack to stack */ 127 | #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) 128 | /* to stack (not from same stack) */ 129 | #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) 130 | /* from table to same table */ 131 | #define setobjt2t setobj 132 | /* to new object */ 133 | #define setobj2n setobj 134 | /* to table */ 135 | #define setobj2t setobj 136 | 137 | 138 | /* 139 | ** Entries in a Lua stack. Field 'tbclist' forms a list of all 140 | ** to-be-closed variables active in this stack. Dummy entries are 141 | ** used when the distance between two tbc variables does not fit 142 | ** in an unsigned short. They are represented by delta==0, and 143 | ** their real delta is always the maximum value that fits in 144 | ** that field. 145 | */ 146 | typedef union StackValue { 147 | TValue val; 148 | struct { 149 | TValuefields; 150 | unsigned short delta; 151 | } tbclist; 152 | } StackValue; 153 | 154 | 155 | /* index to stack elements */ 156 | typedef StackValue *StkId; 157 | 158 | /* convert a 'StackValue' to a 'TValue' */ 159 | #define s2v(o) (&(o)->val) 160 | 161 | 162 | 163 | /* 164 | ** {================================================================== 165 | ** Nil 166 | ** =================================================================== 167 | */ 168 | 169 | /* Standard nil */ 170 | #define LUA_VNIL makevariant(LUA_TNIL, 0) 171 | 172 | /* Empty slot (which might be different from a slot containing nil) */ 173 | #define LUA_VEMPTY makevariant(LUA_TNIL, 1) 174 | 175 | /* Value returned for a key not found in a table (absent key) */ 176 | #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) 177 | 178 | 179 | /* macro to test for (any kind of) nil */ 180 | #define ttisnil(v) checktype((v), LUA_TNIL) 181 | 182 | 183 | /* macro to test for a standard nil */ 184 | #define ttisstrictnil(o) checktag((o), LUA_VNIL) 185 | 186 | 187 | #define setnilvalue(obj) settt_(obj, LUA_VNIL) 188 | 189 | 190 | #define isabstkey(v) checktag((v), LUA_VABSTKEY) 191 | 192 | 193 | /* 194 | ** macro to detect non-standard nils (used only in assertions) 195 | */ 196 | #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) 197 | 198 | 199 | /* 200 | ** By default, entries with any kind of nil are considered empty. 201 | ** (In any definition, values associated with absent keys must also 202 | ** be accepted as empty.) 203 | */ 204 | #define isempty(v) ttisnil(v) 205 | 206 | 207 | /* macro defining a value corresponding to an absent key */ 208 | #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY 209 | 210 | 211 | /* mark an entry as empty */ 212 | #define setempty(v) settt_(v, LUA_VEMPTY) 213 | 214 | 215 | 216 | /* }================================================================== */ 217 | 218 | 219 | /* 220 | ** {================================================================== 221 | ** Booleans 222 | ** =================================================================== 223 | */ 224 | 225 | 226 | #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) 227 | #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) 228 | 229 | #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) 230 | #define ttisfalse(o) checktag((o), LUA_VFALSE) 231 | #define ttistrue(o) checktag((o), LUA_VTRUE) 232 | 233 | 234 | #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) 235 | 236 | 237 | #define setbfvalue(obj) settt_(obj, LUA_VFALSE) 238 | #define setbtvalue(obj) settt_(obj, LUA_VTRUE) 239 | 240 | /* }================================================================== */ 241 | 242 | 243 | /* 244 | ** {================================================================== 245 | ** Threads 246 | ** =================================================================== 247 | */ 248 | 249 | #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) 250 | 251 | #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) 252 | 253 | #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) 254 | 255 | #define setthvalue(L,obj,x) \ 256 | { TValue *io = (obj); lua_State *x_ = (x); \ 257 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ 258 | checkliveness(L,io); } 259 | 260 | #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) 261 | 262 | /* }================================================================== */ 263 | 264 | 265 | /* 266 | ** {================================================================== 267 | ** Collectable Objects 268 | ** =================================================================== 269 | */ 270 | 271 | /* 272 | ** Common Header for all collectable objects (in macro form, to be 273 | ** included in other objects) 274 | */ 275 | #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked 276 | 277 | 278 | /* Common type for all collectable objects */ 279 | typedef struct GCObject { 280 | CommonHeader; 281 | } GCObject; 282 | 283 | 284 | /* Bit mark for collectable types */ 285 | #define BIT_ISCOLLECTABLE (1 << 6) 286 | 287 | #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) 288 | 289 | /* mark a tag as collectable */ 290 | #define ctb(t) ((t) | BIT_ISCOLLECTABLE) 291 | 292 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) 293 | 294 | #define gcvalueraw(v) ((v).gc) 295 | 296 | #define setgcovalue(L,obj,x) \ 297 | { TValue *io = (obj); GCObject *i_g=(x); \ 298 | val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } 299 | 300 | /* }================================================================== */ 301 | 302 | 303 | /* 304 | ** {================================================================== 305 | ** Numbers 306 | ** =================================================================== 307 | */ 308 | 309 | /* Variant tags for numbers */ 310 | #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ 311 | #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ 312 | 313 | #define ttisnumber(o) checktype((o), LUA_TNUMBER) 314 | #define ttisfloat(o) checktag((o), LUA_VNUMFLT) 315 | #define ttisinteger(o) checktag((o), LUA_VNUMINT) 316 | 317 | #define nvalue(o) check_exp(ttisnumber(o), \ 318 | (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) 319 | #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) 320 | #define ivalue(o) check_exp(ttisinteger(o), val_(o).i) 321 | 322 | #define fltvalueraw(v) ((v).n) 323 | #define ivalueraw(v) ((v).i) 324 | 325 | #define setfltvalue(obj,x) \ 326 | { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } 327 | 328 | #define chgfltvalue(obj,x) \ 329 | { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } 330 | 331 | #define setivalue(obj,x) \ 332 | { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } 333 | 334 | #define chgivalue(obj,x) \ 335 | { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } 336 | 337 | /* }================================================================== */ 338 | 339 | 340 | /* 341 | ** {================================================================== 342 | ** Strings 343 | ** =================================================================== 344 | */ 345 | 346 | /* Variant tags for strings */ 347 | #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ 348 | #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ 349 | 350 | #define ttisstring(o) checktype((o), LUA_TSTRING) 351 | #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) 352 | #define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) 353 | 354 | #define tsvalueraw(v) (gco2ts((v).gc)) 355 | 356 | #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) 357 | 358 | #define setsvalue(L,obj,x) \ 359 | { TValue *io = (obj); TString *x_ = (x); \ 360 | val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ 361 | checkliveness(L,io); } 362 | 363 | /* set a string to the stack */ 364 | #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) 365 | 366 | /* set a string to a new object */ 367 | #define setsvalue2n setsvalue 368 | 369 | 370 | /* 371 | ** Header for a string value. 372 | */ 373 | typedef struct TString { 374 | CommonHeader; 375 | lu_byte extra; /* reserved words for short strings; "has hash" for longs */ 376 | lu_byte shrlen; /* length for short strings */ 377 | unsigned int hash; 378 | union { 379 | size_t lnglen; /* length for long strings */ 380 | struct TString *hnext; /* linked list for hash table */ 381 | } u; 382 | char contents[1]; 383 | } TString; 384 | 385 | 386 | 387 | /* 388 | ** Get the actual string (array of bytes) from a 'TString'. 389 | */ 390 | #define getstr(ts) ((ts)->contents) 391 | 392 | 393 | /* get the actual string (array of bytes) from a Lua value */ 394 | #define svalue(o) getstr(tsvalue(o)) 395 | 396 | /* get string length from 'TString *s' */ 397 | #define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) 398 | 399 | /* get string length from 'TValue *o' */ 400 | #define vslen(o) tsslen(tsvalue(o)) 401 | 402 | /* }================================================================== */ 403 | 404 | 405 | /* 406 | ** {================================================================== 407 | ** Userdata 408 | ** =================================================================== 409 | */ 410 | 411 | 412 | /* 413 | ** Light userdata should be a variant of userdata, but for compatibility 414 | ** reasons they are also different types. 415 | */ 416 | #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) 417 | 418 | #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) 419 | 420 | #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) 421 | #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) 422 | 423 | #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) 424 | #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) 425 | 426 | #define pvalueraw(v) ((v).p) 427 | 428 | #define setpvalue(obj,x) \ 429 | { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } 430 | 431 | #define setuvalue(L,obj,x) \ 432 | { TValue *io = (obj); Udata *x_ = (x); \ 433 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ 434 | checkliveness(L,io); } 435 | 436 | 437 | /* Ensures that addresses after this type are always fully aligned. */ 438 | typedef union UValue { 439 | TValue uv; 440 | LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ 441 | } UValue; 442 | 443 | 444 | /* 445 | ** Header for userdata with user values; 446 | ** memory area follows the end of this structure. 447 | */ 448 | typedef struct Udata { 449 | CommonHeader; 450 | unsigned short nuvalue; /* number of user values */ 451 | size_t len; /* number of bytes */ 452 | struct Table *metatable; 453 | GCObject *gclist; 454 | UValue uv[1]; /* user values */ 455 | } Udata; 456 | 457 | 458 | /* 459 | ** Header for userdata with no user values. These userdata do not need 460 | ** to be gray during GC, and therefore do not need a 'gclist' field. 461 | ** To simplify, the code always use 'Udata' for both kinds of userdata, 462 | ** making sure it never accesses 'gclist' on userdata with no user values. 463 | ** This structure here is used only to compute the correct size for 464 | ** this representation. (The 'bindata' field in its end ensures correct 465 | ** alignment for binary data following this header.) 466 | */ 467 | typedef struct Udata0 { 468 | CommonHeader; 469 | unsigned short nuvalue; /* number of user values */ 470 | size_t len; /* number of bytes */ 471 | struct Table *metatable; 472 | union {LUAI_MAXALIGN;} bindata; 473 | } Udata0; 474 | 475 | 476 | /* compute the offset of the memory area of a userdata */ 477 | #define udatamemoffset(nuv) \ 478 | ((nuv) == 0 ? offsetof(Udata0, bindata) \ 479 | : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) 480 | 481 | /* get the address of the memory block inside 'Udata' */ 482 | #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) 483 | 484 | /* compute the size of a userdata */ 485 | #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) 486 | 487 | /* }================================================================== */ 488 | 489 | 490 | /* 491 | ** {================================================================== 492 | ** Prototypes 493 | ** =================================================================== 494 | */ 495 | 496 | #define LUA_VPROTO makevariant(LUA_TPROTO, 0) 497 | 498 | 499 | /* 500 | ** Description of an upvalue for function prototypes 501 | */ 502 | typedef struct Upvaldesc { 503 | TString *name; /* upvalue name (for debug information) */ 504 | lu_byte instack; /* whether it is in stack (register) */ 505 | lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ 506 | lu_byte kind; /* kind of corresponding variable */ 507 | } Upvaldesc; 508 | 509 | 510 | /* 511 | ** Description of a local variable for function prototypes 512 | ** (used for debug information) 513 | */ 514 | typedef struct LocVar { 515 | TString *varname; 516 | int startpc; /* first point where variable is active */ 517 | int endpc; /* first point where variable is dead */ 518 | } LocVar; 519 | 520 | 521 | /* 522 | ** Associates the absolute line source for a given instruction ('pc'). 523 | ** The array 'lineinfo' gives, for each instruction, the difference in 524 | ** lines from the previous instruction. When that difference does not 525 | ** fit into a byte, Lua saves the absolute line for that instruction. 526 | ** (Lua also saves the absolute line periodically, to speed up the 527 | ** computation of a line number: we can use binary search in the 528 | ** absolute-line array, but we must traverse the 'lineinfo' array 529 | ** linearly to compute a line.) 530 | */ 531 | typedef struct AbsLineInfo { 532 | int pc; 533 | int line; 534 | } AbsLineInfo; 535 | 536 | /* 537 | ** Function Prototypes 538 | */ 539 | typedef struct Proto { 540 | CommonHeader; 541 | lu_byte numparams; /* number of fixed (named) parameters */ 542 | lu_byte is_vararg; 543 | lu_byte maxstacksize; /* number of registers needed by this function */ 544 | int sizeupvalues; /* size of 'upvalues' */ 545 | int sizek; /* size of 'k' */ 546 | int sizecode; 547 | int sizelineinfo; 548 | int sizep; /* size of 'p' */ 549 | int sizelocvars; 550 | int sizeabslineinfo; /* size of 'abslineinfo' */ 551 | int linedefined; /* debug information */ 552 | int lastlinedefined; /* debug information */ 553 | TValue *k; /* constants used by the function */ 554 | Instruction *code; /* opcodes */ 555 | struct Proto **p; /* functions defined inside the function */ 556 | Upvaldesc *upvalues; /* upvalue information */ 557 | ls_byte *lineinfo; /* information about source lines (debug information) */ 558 | AbsLineInfo *abslineinfo; /* idem */ 559 | LocVar *locvars; /* information about local variables (debug information) */ 560 | TString *source; /* used for debug information */ 561 | GCObject *gclist; 562 | } Proto; 563 | 564 | /* }================================================================== */ 565 | 566 | 567 | /* 568 | ** {================================================================== 569 | ** Functions 570 | ** =================================================================== 571 | */ 572 | 573 | #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) 574 | 575 | 576 | /* Variant tags for functions */ 577 | #define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ 578 | #define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ 579 | #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ 580 | 581 | #define ttisfunction(o) checktype(o, LUA_TFUNCTION) 582 | #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) 583 | #define ttislcf(o) checktag((o), LUA_VLCF) 584 | #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) 585 | #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) 586 | 587 | 588 | #define isLfunction(o) ttisLclosure(o) 589 | 590 | #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) 591 | #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) 592 | #define fvalue(o) check_exp(ttislcf(o), val_(o).f) 593 | #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) 594 | 595 | #define fvalueraw(v) ((v).f) 596 | 597 | #define setclLvalue(L,obj,x) \ 598 | { TValue *io = (obj); LClosure *x_ = (x); \ 599 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ 600 | checkliveness(L,io); } 601 | 602 | #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) 603 | 604 | #define setfvalue(obj,x) \ 605 | { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } 606 | 607 | #define setclCvalue(L,obj,x) \ 608 | { TValue *io = (obj); CClosure *x_ = (x); \ 609 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ 610 | checkliveness(L,io); } 611 | 612 | 613 | /* 614 | ** Upvalues for Lua closures 615 | */ 616 | typedef struct UpVal { 617 | CommonHeader; 618 | lu_byte tbc; /* true if it represents a to-be-closed variable */ 619 | TValue *v; /* points to stack or to its own value */ 620 | union { 621 | struct { /* (when open) */ 622 | struct UpVal *next; /* linked list */ 623 | struct UpVal **previous; 624 | } open; 625 | TValue value; /* the value (when closed) */ 626 | } u; 627 | } UpVal; 628 | 629 | 630 | 631 | #define ClosureHeader \ 632 | CommonHeader; lu_byte nupvalues; GCObject *gclist 633 | 634 | typedef struct CClosure { 635 | ClosureHeader; 636 | lua_CFunction f; 637 | TValue upvalue[1]; /* list of upvalues */ 638 | } CClosure; 639 | 640 | 641 | typedef struct LClosure { 642 | ClosureHeader; 643 | struct Proto *p; 644 | UpVal *upvals[1]; /* list of upvalues */ 645 | } LClosure; 646 | 647 | 648 | typedef union Closure { 649 | CClosure c; 650 | LClosure l; 651 | } Closure; 652 | 653 | 654 | #define getproto(o) (clLvalue(o)->p) 655 | 656 | /* }================================================================== */ 657 | 658 | 659 | /* 660 | ** {================================================================== 661 | ** Tables 662 | ** =================================================================== 663 | */ 664 | 665 | #define LUA_VTABLE makevariant(LUA_TTABLE, 0) 666 | 667 | #define ttistable(o) checktag((o), ctb(LUA_VTABLE)) 668 | 669 | #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) 670 | 671 | #define sethvalue(L,obj,x) \ 672 | { TValue *io = (obj); Table *x_ = (x); \ 673 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ 674 | checkliveness(L,io); } 675 | 676 | #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) 677 | 678 | 679 | /* 680 | ** Nodes for Hash tables: A pack of two TValue's (key-value pairs) 681 | ** plus a 'next' field to link colliding entries. The distribution 682 | ** of the key's fields ('key_tt' and 'key_val') not forming a proper 683 | ** 'TValue' allows for a smaller size for 'Node' both in 4-byte 684 | ** and 8-byte alignments. 685 | */ 686 | typedef union Node { 687 | struct NodeKey { 688 | TValuefields; /* fields for value */ 689 | lu_byte key_tt; /* key type */ 690 | int next; /* for chaining */ 691 | Value key_val; /* key value */ 692 | } u; 693 | TValue i_val; /* direct access to node's value as a proper 'TValue' */ 694 | } Node; 695 | 696 | 697 | /* copy a value into a key */ 698 | #define setnodekey(L,node,obj) \ 699 | { Node *n_=(node); const TValue *io_=(obj); \ 700 | n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ 701 | checkliveness(L,io_); } 702 | 703 | 704 | /* copy a value from a key */ 705 | #define getnodekey(L,obj,node) \ 706 | { TValue *io_=(obj); const Node *n_=(node); \ 707 | io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ 708 | checkliveness(L,io_); } 709 | 710 | 711 | /* 712 | ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the 713 | ** real size of 'array'. Otherwise, the real size of 'array' is the 714 | ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' 715 | ** is zero); 'alimit' is then used as a hint for #t. 716 | */ 717 | 718 | #define BITRAS (1 << 7) 719 | #define isrealasize(t) (!((t)->flags & BITRAS)) 720 | #define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) 721 | #define setnorealasize(t) ((t)->flags |= BITRAS) 722 | 723 | 724 | typedef struct Table { 725 | CommonHeader; 726 | lu_byte flags; /* 1<

u.key_tt) 741 | #define keyval(node) ((node)->u.key_val) 742 | 743 | #define keyisnil(node) (keytt(node) == LUA_TNIL) 744 | #define keyisinteger(node) (keytt(node) == LUA_VNUMINT) 745 | #define keyival(node) (keyval(node).i) 746 | #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) 747 | #define keystrval(node) (gco2ts(keyval(node).gc)) 748 | 749 | #define setnilkey(node) (keytt(node) = LUA_TNIL) 750 | 751 | #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) 752 | 753 | #define gckey(n) (keyval(n).gc) 754 | #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) 755 | 756 | 757 | /* 758 | ** Dead keys in tables have the tag DEADKEY but keep their original 759 | ** gcvalue. This distinguishes them from regular keys but allows them to 760 | ** be found when searched in a special way. ('next' needs that to find 761 | ** keys removed from a table during a traversal.) 762 | */ 763 | #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) 764 | #define keyisdead(node) (keytt(node) == LUA_TDEADKEY) 765 | 766 | /* }================================================================== */ 767 | 768 | 769 | 770 | /* 771 | ** 'module' operation for hashing (size is always a power of 2) 772 | */ 773 | #define lmod(s,size) \ 774 | (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) 775 | 776 | 777 | #define twoto(x) (1<<(x)) 778 | #define sizenode(t) (twoto((t)->lsizenode)) 779 | 780 | 781 | /* size of buffer for 'luaO_utf8esc' function */ 782 | #define UTF8BUFFSZ 8 783 | 784 | LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); 785 | LUAI_FUNC int luaO_ceillog2 (unsigned int x); 786 | LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, 787 | const TValue *p2, TValue *res); 788 | LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, 789 | const TValue *p2, StkId res); 790 | LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); 791 | LUAI_FUNC int luaO_hexavalue (int c); 792 | LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); 793 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 794 | va_list argp); 795 | LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 796 | LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); 797 | 798 | 799 | #endif 800 | 801 | -------------------------------------------------------------------------------- /include/Lua/lopcodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.h $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lopcodes_h 8 | #define lopcodes_h 9 | 10 | #include "llimits.h" 11 | 12 | 13 | /*=========================================================================== 14 | We assume that instructions are unsigned 32-bit integers. 15 | All instructions have an opcode in the first 7 bits. 16 | Instructions can have the following formats: 17 | 18 | 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 19 | 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 20 | iABC C(8) | B(8) |k| A(8) | Op(7) | 21 | iABx Bx(17) | A(8) | Op(7) | 22 | iAsBx sBx (signed)(17) | A(8) | Op(7) | 23 | iAx Ax(25) | Op(7) | 24 | isJ sJ(25) | Op(7) | 25 | 26 | A signed argument is represented in excess K: the represented value is 27 | the written unsigned value minus K, where K is half the maximum for the 28 | corresponding unsigned argument. 29 | ===========================================================================*/ 30 | 31 | 32 | enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ 33 | 34 | 35 | /* 36 | ** size and position of opcode arguments. 37 | */ 38 | #define SIZE_C 8 39 | #define SIZE_B 8 40 | #define SIZE_Bx (SIZE_C + SIZE_B + 1) 41 | #define SIZE_A 8 42 | #define SIZE_Ax (SIZE_Bx + SIZE_A) 43 | #define SIZE_sJ (SIZE_Bx + SIZE_A) 44 | 45 | #define SIZE_OP 7 46 | 47 | #define POS_OP 0 48 | 49 | #define POS_A (POS_OP + SIZE_OP) 50 | #define POS_k (POS_A + SIZE_A) 51 | #define POS_B (POS_k + 1) 52 | #define POS_C (POS_B + SIZE_B) 53 | 54 | #define POS_Bx POS_k 55 | 56 | #define POS_Ax POS_A 57 | 58 | #define POS_sJ POS_A 59 | 60 | 61 | /* 62 | ** limits for opcode arguments. 63 | ** we use (signed) 'int' to manipulate most arguments, 64 | ** so they must fit in ints. 65 | */ 66 | 67 | /* Check whether type 'int' has at least 'b' bits ('b' < 32) */ 68 | #define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1) 69 | 70 | 71 | #if L_INTHASBITS(SIZE_Bx) 72 | #define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ 78 | 79 | 80 | #if L_INTHASBITS(SIZE_Ax) 81 | #define MAXARG_Ax ((1<> 1) 93 | 94 | 95 | #define MAXARG_A ((1<> 1) 99 | 100 | #define int2sC(i) ((i) + OFFSET_sC) 101 | #define sC2int(i) ((i) - OFFSET_sC) 102 | 103 | 104 | /* creates a mask with 'n' 1 bits at position 'p' */ 105 | #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p)) 106 | 107 | /* creates a mask with 'n' 0 bits at position 'p' */ 108 | #define MASK0(n,p) (~MASK1(n,p)) 109 | 110 | /* 111 | ** the following macros help to manipulate instructions 112 | */ 113 | 114 | #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) 115 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ 116 | ((cast(Instruction, o)<>(pos)) & MASK1(size,0))) 122 | #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ 123 | ((cast(Instruction, v)<> sC */ 242 | OP_SHLI,/* A B sC R[A] := sC << R[B] */ 243 | 244 | OP_ADD,/* A B C R[A] := R[B] + R[C] */ 245 | OP_SUB,/* A B C R[A] := R[B] - R[C] */ 246 | OP_MUL,/* A B C R[A] := R[B] * R[C] */ 247 | OP_MOD,/* A B C R[A] := R[B] % R[C] */ 248 | OP_POW,/* A B C R[A] := R[B] ^ R[C] */ 249 | OP_DIV,/* A B C R[A] := R[B] / R[C] */ 250 | OP_IDIV,/* A B C R[A] := R[B] // R[C] */ 251 | 252 | OP_BAND,/* A B C R[A] := R[B] & R[C] */ 253 | OP_BOR,/* A B C R[A] := R[B] | R[C] */ 254 | OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ 255 | OP_SHL,/* A B C R[A] := R[B] << R[C] */ 256 | OP_SHR,/* A B C R[A] := R[B] >> R[C] */ 257 | 258 | OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */ 259 | OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ 260 | OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ 261 | 262 | OP_UNM,/* A B R[A] := -R[B] */ 263 | OP_BNOT,/* A B R[A] := ~R[B] */ 264 | OP_NOT,/* A B R[A] := not R[B] */ 265 | OP_LEN,/* A B R[A] := #R[B] (length operator) */ 266 | 267 | OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ 268 | 269 | OP_CLOSE,/* A close all upvalues >= R[A] */ 270 | OP_TBC,/* A mark variable A "to be closed" */ 271 | OP_JMP,/* sJ pc += sJ */ 272 | OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ 273 | OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ 274 | OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ 275 | 276 | OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */ 277 | OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */ 278 | OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */ 279 | OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ 280 | OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ 281 | OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ 282 | 283 | OP_TEST,/* A k if (not R[A] == k) then pc++ */ 284 | OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */ 285 | 286 | OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ 287 | OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ 288 | 289 | OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */ 290 | OP_RETURN0,/* return */ 291 | OP_RETURN1,/* A return R[A] */ 292 | 293 | OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ 294 | OP_FORPREP,/* A Bx ; 295 | if not to run then pc+=Bx+1; */ 296 | 297 | OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ 298 | OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ 299 | OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ 300 | 301 | OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */ 302 | 303 | OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ 304 | 305 | OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ 306 | 307 | OP_VARARGPREP,/*A (adjust vararg parameters) */ 308 | 309 | OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ 310 | } OpCode; 311 | 312 | 313 | #define NUM_OPCODES ((int)(OP_EXTRAARG) + 1) 314 | 315 | 316 | 317 | /*=========================================================================== 318 | Notes: 319 | 320 | (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean 321 | value, in a code equivalent to (not cond ? false : true). (It 322 | produces false and skips the next instruction producing true.) 323 | 324 | (*) Opcodes OP_MMBIN and variants follow each arithmetic and 325 | bitwise opcode. If the operation succeeds, it skips this next 326 | opcode. Otherwise, this opcode calls the corresponding metamethod. 327 | 328 | (*) Opcode OP_TESTSET is used in short-circuit expressions that need 329 | both to jump and to produce a value, such as (a = b or c). 330 | 331 | (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then 332 | 'top' is set to last_result+1, so next open instruction (OP_CALL, 333 | OP_RETURN*, OP_SETLIST) may use 'top'. 334 | 335 | (*) In OP_VARARG, if (C == 0) then use actual number of varargs and 336 | set top (like in OP_CALL with C == 0). 337 | 338 | (*) In OP_RETURN, if (B == 0) then return up to 'top'. 339 | 340 | (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always 341 | OP_EXTRAARG. 342 | 343 | (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then 344 | real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the 345 | bits of C). 346 | 347 | (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a 348 | power of 2) plus 1, or zero for size zero. If not k, the array size 349 | is C. Otherwise, the array size is EXTRAARG _ C. 350 | 351 | (*) For comparisons, k specifies what condition the test should accept 352 | (true or false). 353 | 354 | (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped 355 | (the constant is the first operand). 356 | 357 | (*) All 'skips' (pc++) assume that next instruction is a jump. 358 | 359 | (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the 360 | function builds upvalues, which may need to be closed. C > 0 means 361 | the function is vararg, so that its 'func' must be corrected before 362 | returning; in this case, (C - 1) is its number of fixed parameters. 363 | 364 | (*) In comparisons with an immediate operand, C signals whether the 365 | original operand was a float. (It must be corrected in case of 366 | metamethods.) 367 | 368 | ===========================================================================*/ 369 | 370 | 371 | /* 372 | ** masks for instruction properties. The format is: 373 | ** bits 0-2: op mode 374 | ** bit 3: instruction set register A 375 | ** bit 4: operator is a test (next instruction must be a jump) 376 | ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) 377 | ** bit 6: instruction sets 'L->top' for next instruction (when C == 0) 378 | ** bit 7: instruction is an MM instruction (call a metamethod) 379 | */ 380 | 381 | LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) 382 | 383 | #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) 384 | #define testAMode(m) (luaP_opmodes[m] & (1 << 3)) 385 | #define testTMode(m) (luaP_opmodes[m] & (1 << 4)) 386 | #define testITMode(m) (luaP_opmodes[m] & (1 << 5)) 387 | #define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) 388 | #define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) 389 | 390 | /* "out top" (set top for next instruction) */ 391 | #define isOT(i) \ 392 | ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \ 393 | GET_OPCODE(i) == OP_TAILCALL) 394 | 395 | /* "in top" (uses top from previous instruction) */ 396 | #define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) 397 | 398 | #define opmode(mm,ot,it,t,a,m) \ 399 | (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) 400 | 401 | 402 | /* number of list items to accumulate before a SETLIST instruction */ 403 | #define LFIELDS_PER_FLUSH 50 404 | 405 | #endif 406 | -------------------------------------------------------------------------------- /include/Lua/lopnames.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopnames.h $ 3 | ** Opcode names 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #if !defined(lopnames_h) 8 | #define lopnames_h 9 | 10 | #include 11 | 12 | 13 | /* ORDER OP */ 14 | 15 | static const char *const opnames[] = { 16 | "MOVE", 17 | "LOADI", 18 | "LOADF", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADFALSE", 22 | "LFALSESKIP", 23 | "LOADTRUE", 24 | "LOADNIL", 25 | "GETUPVAL", 26 | "SETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "GETI", 30 | "GETFIELD", 31 | "SETTABUP", 32 | "SETTABLE", 33 | "SETI", 34 | "SETFIELD", 35 | "NEWTABLE", 36 | "SELF", 37 | "ADDI", 38 | "ADDK", 39 | "SUBK", 40 | "MULK", 41 | "MODK", 42 | "POWK", 43 | "DIVK", 44 | "IDIVK", 45 | "BANDK", 46 | "BORK", 47 | "BXORK", 48 | "SHRI", 49 | "SHLI", 50 | "ADD", 51 | "SUB", 52 | "MUL", 53 | "MOD", 54 | "POW", 55 | "DIV", 56 | "IDIV", 57 | "BAND", 58 | "BOR", 59 | "BXOR", 60 | "SHL", 61 | "SHR", 62 | "MMBIN", 63 | "MMBINI", 64 | "MMBINK", 65 | "UNM", 66 | "BNOT", 67 | "NOT", 68 | "LEN", 69 | "CONCAT", 70 | "CLOSE", 71 | "TBC", 72 | "JMP", 73 | "EQ", 74 | "LT", 75 | "LE", 76 | "EQK", 77 | "EQI", 78 | "LTI", 79 | "LEI", 80 | "GTI", 81 | "GEI", 82 | "TEST", 83 | "TESTSET", 84 | "CALL", 85 | "TAILCALL", 86 | "RETURN", 87 | "RETURN0", 88 | "RETURN1", 89 | "FORLOOP", 90 | "FORPREP", 91 | "TFORPREP", 92 | "TFORCALL", 93 | "TFORLOOP", 94 | "SETLIST", 95 | "CLOSURE", 96 | "VARARG", 97 | "VARARGPREP", 98 | "EXTRAARG", 99 | NULL 100 | }; 101 | 102 | #endif 103 | 104 | -------------------------------------------------------------------------------- /include/Lua/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression and variable descriptor. 17 | ** Code generation for variables and expressions can be delayed to allow 18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed 19 | ** variable/expression. It has a description of its "main" value plus a 20 | ** list of conditional jumps that can also produce its value (generated 21 | ** by short-circuit operators 'and'/'or'). 22 | */ 23 | 24 | /* kinds of variables/expressions */ 25 | typedef enum { 26 | VVOID, /* when 'expdesc' describes the last expression of a list, 27 | this kind means an empty list (so, no expression) */ 28 | VNIL, /* constant nil */ 29 | VTRUE, /* constant true */ 30 | VFALSE, /* constant false */ 31 | VK, /* constant in 'k'; info = index of constant in 'k' */ 32 | VKFLT, /* floating constant; nval = numerical float value */ 33 | VKINT, /* integer constant; ival = numerical integer value */ 34 | VKSTR, /* string constant; strval = TString address; 35 | (string is fixed by the lexer) */ 36 | VNONRELOC, /* expression has its value in a fixed register; 37 | info = result register */ 38 | VLOCAL, /* local variable; var.ridx = register index; 39 | var.vidx = relative index in 'actvar.arr' */ 40 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 41 | VCONST, /* compile-time variable; 42 | info = absolute index in 'actvar.arr' */ 43 | VINDEXED, /* indexed variable; 44 | ind.t = table register; 45 | ind.idx = key's R index */ 46 | VINDEXUP, /* indexed upvalue; 47 | ind.t = table upvalue; 48 | ind.idx = key's K index */ 49 | VINDEXI, /* indexed variable with constant integer; 50 | ind.t = table register; 51 | ind.idx = key's value */ 52 | VINDEXSTR, /* indexed variable with literal string; 53 | ind.t = table register; 54 | ind.idx = key's K index */ 55 | VJMP, /* expression is a test/comparison; 56 | info = pc of corresponding jump instruction */ 57 | VRELOC, /* expression can put result in any register; 58 | info = instruction pc */ 59 | VCALL, /* expression is a function call; info = instruction pc */ 60 | VVARARG /* vararg expression; info = instruction pc */ 61 | } expkind; 62 | 63 | 64 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) 65 | #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) 66 | 67 | 68 | typedef struct expdesc { 69 | expkind k; 70 | union { 71 | lua_Integer ival; /* for VKINT */ 72 | lua_Number nval; /* for VKFLT */ 73 | TString *strval; /* for VKSTR */ 74 | int info; /* for generic use */ 75 | struct { /* for indexed variables */ 76 | short idx; /* index (R or "long" K) */ 77 | lu_byte t; /* table (register or upvalue) */ 78 | } ind; 79 | struct { /* for local variables */ 80 | lu_byte ridx; /* register holding the variable */ 81 | unsigned short vidx; /* compiler index (in 'actvar.arr') */ 82 | } var; 83 | } u; 84 | int t; /* patch list of 'exit when true' */ 85 | int f; /* patch list of 'exit when false' */ 86 | } expdesc; 87 | 88 | 89 | /* kinds of variables */ 90 | #define VDKREG 0 /* regular */ 91 | #define RDKCONST 1 /* constant */ 92 | #define RDKTOCLOSE 2 /* to-be-closed */ 93 | #define RDKCTC 3 /* compile-time constant */ 94 | 95 | /* description of an active local variable */ 96 | typedef union Vardesc { 97 | struct { 98 | TValuefields; /* constant value (if it is a compile-time constant) */ 99 | lu_byte kind; 100 | lu_byte ridx; /* register holding the variable */ 101 | short pidx; /* index of the variable in the Proto's 'locvars' array */ 102 | TString *name; /* variable name */ 103 | } vd; 104 | TValue k; /* constant value (if any) */ 105 | } Vardesc; 106 | 107 | 108 | 109 | /* description of pending goto statements and label statements */ 110 | typedef struct Labeldesc { 111 | TString *name; /* label identifier */ 112 | int pc; /* position in code */ 113 | int line; /* line where it appeared */ 114 | lu_byte nactvar; /* number of active variables in that position */ 115 | lu_byte close; /* goto that escapes upvalues */ 116 | } Labeldesc; 117 | 118 | 119 | /* list of labels or gotos */ 120 | typedef struct Labellist { 121 | Labeldesc *arr; /* array */ 122 | int n; /* number of entries in use */ 123 | int size; /* array size */ 124 | } Labellist; 125 | 126 | 127 | /* dynamic structures used by the parser */ 128 | typedef struct Dyndata { 129 | struct { /* list of all active local variables */ 130 | Vardesc *arr; 131 | int n; 132 | int size; 133 | } actvar; 134 | Labellist gt; /* list of pending gotos */ 135 | Labellist label; /* list of active labels */ 136 | } Dyndata; 137 | 138 | 139 | /* control of blocks */ 140 | struct BlockCnt; /* defined in lparser.c */ 141 | 142 | 143 | /* state needed to generate code for a given function */ 144 | typedef struct FuncState { 145 | Proto *f; /* current function header */ 146 | struct FuncState *prev; /* enclosing function */ 147 | struct LexState *ls; /* lexical state */ 148 | struct BlockCnt *bl; /* chain of current blocks */ 149 | int pc; /* next position to code (equivalent to 'ncode') */ 150 | int lasttarget; /* 'label' of last 'jump label' */ 151 | int previousline; /* last line that was saved in 'lineinfo' */ 152 | int nk; /* number of elements in 'k' */ 153 | int np; /* number of elements in 'p' */ 154 | int nabslineinfo; /* number of elements in 'abslineinfo' */ 155 | int firstlocal; /* index of first local var (in Dyndata array) */ 156 | int firstlabel; /* index of first label (in 'dyd->label->arr') */ 157 | short ndebugvars; /* number of elements in 'f->locvars' */ 158 | lu_byte nactvar; /* number of active local variables */ 159 | lu_byte nups; /* number of upvalues */ 160 | lu_byte freereg; /* first free register */ 161 | lu_byte iwthabs; /* instructions issued since last absolute line info */ 162 | lu_byte needclose; /* function needs to close upvalues when returning */ 163 | } FuncState; 164 | 165 | 166 | LUAI_FUNC int luaY_nvarstack (FuncState *fs); 167 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 168 | Dyndata *dyd, const char *name, int firstchar); 169 | 170 | 171 | #endif 172 | -------------------------------------------------------------------------------- /include/Lua/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /include/Lua/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | /* 18 | ** Some notes about garbage-collected objects: All objects in Lua must 19 | ** be kept somehow accessible until being freed, so all objects always 20 | ** belong to one (and only one) of these lists, using field 'next' of 21 | ** the 'CommonHeader' for the link: 22 | ** 23 | ** 'allgc': all objects not marked for finalization; 24 | ** 'finobj': all objects marked for finalization; 25 | ** 'tobefnz': all objects ready to be finalized; 26 | ** 'fixedgc': all objects that are not to be collected (currently 27 | ** only small strings, such as reserved words). 28 | ** 29 | ** For the generational collector, some of these lists have marks for 30 | ** generations. Each mark points to the first element in the list for 31 | ** that particular generation; that generation goes until the next mark. 32 | ** 33 | ** 'allgc' -> 'survival': new objects; 34 | ** 'survival' -> 'old': objects that survived one collection; 35 | ** 'old1' -> 'reallyold': objects that became old in last collection; 36 | ** 'reallyold' -> NULL: objects old for more than one cycle. 37 | ** 38 | ** 'finobj' -> 'finobjsur': new objects marked for finalization; 39 | ** 'finobjsur' -> 'finobjold1': survived """"; 40 | ** 'finobjold1' -> 'finobjrold': just old """"; 41 | ** 'finobjrold' -> NULL: really old """". 42 | ** 43 | ** All lists can contain elements older than their main ages, due 44 | ** to 'luaC_checkfinalizer' and 'udata2finalize', which move 45 | ** objects between the normal lists and the "marked for finalization" 46 | ** lists. Moreover, barriers can age young objects in young lists as 47 | ** OLD0, which then become OLD1. However, a list never contains 48 | ** elements younger than their main ages. 49 | ** 50 | ** The generational collector also uses a pointer 'firstold1', which 51 | ** points to the first OLD1 object in the list. It is used to optimize 52 | ** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' 53 | ** and 'reallyold', but often the list has no OLD1 objects or they are 54 | ** after 'old1'.) Note the difference between it and 'old1': 55 | ** 'firstold1': no OLD1 objects before this point; there can be all 56 | ** ages after it. 57 | ** 'old1': no objects younger than OLD1 after this point. 58 | */ 59 | 60 | /* 61 | ** Moreover, there is another set of lists that control gray objects. 62 | ** These lists are linked by fields 'gclist'. (All objects that 63 | ** can become gray have such a field. The field is not the same 64 | ** in all objects, but it always has this name.) Any gray object 65 | ** must belong to one of these lists, and all objects in these lists 66 | ** must be gray (with two exceptions explained below): 67 | ** 68 | ** 'gray': regular gray objects, still waiting to be visited. 69 | ** 'grayagain': objects that must be revisited at the atomic phase. 70 | ** That includes 71 | ** - black objects got in a write barrier; 72 | ** - all kinds of weak tables during propagation phase; 73 | ** - all threads. 74 | ** 'weak': tables with weak values to be cleared; 75 | ** 'ephemeron': ephemeron tables with white->white entries; 76 | ** 'allweak': tables with weak keys and/or weak values to be cleared. 77 | ** 78 | ** The exceptions to that "gray rule" are: 79 | ** - TOUCHED2 objects in generational mode stay in a gray list (because 80 | ** they must be visited again at the end of the cycle), but they are 81 | ** marked black because assignments to them must activate barriers (to 82 | ** move them back to TOUCHED1). 83 | ** - Open upvales are kept gray to avoid barriers, but they stay out 84 | ** of gray lists. (They don't even have a 'gclist' field.) 85 | */ 86 | 87 | 88 | 89 | /* 90 | ** About 'nCcalls': This count has two parts: the lower 16 bits counts 91 | ** the number of recursive invocations in the C stack; the higher 92 | ** 16 bits counts the number of non-yieldable calls in the stack. 93 | ** (They are together so that we can change and save both with one 94 | ** instruction.) 95 | */ 96 | 97 | 98 | /* true if this thread does not have non-yieldable calls in the stack */ 99 | #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) 100 | 101 | /* real number of C calls */ 102 | #define getCcalls(L) ((L)->nCcalls & 0xffff) 103 | 104 | 105 | /* Increment the number of non-yieldable calls */ 106 | #define incnny(L) ((L)->nCcalls += 0x10000) 107 | 108 | /* Decrement the number of non-yieldable calls */ 109 | #define decnny(L) ((L)->nCcalls -= 0x10000) 110 | 111 | /* Non-yieldable call increment */ 112 | #define nyci (0x10000 | 1) 113 | 114 | 115 | 116 | 117 | struct lua_longjmp; /* defined in ldo.c */ 118 | 119 | 120 | /* 121 | ** Atomic type (relative to signals) to better ensure that 'lua_sethook' 122 | ** is thread safe 123 | */ 124 | #if !defined(l_signalT) 125 | #include 126 | #define l_signalT sig_atomic_t 127 | #endif 128 | 129 | 130 | /* 131 | ** Extra stack space to handle TM calls and some other extras. This 132 | ** space is not included in 'stack_last'. It is used only to avoid stack 133 | ** checks, either because the element will be promptly popped or because 134 | ** there will be a stack check soon after the push. Function frames 135 | ** never use this extra space, so it does not need to be kept clean. 136 | */ 137 | #define EXTRA_STACK 5 138 | 139 | 140 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 141 | 142 | #define stacksize(th) cast_int((th)->stack_last - (th)->stack) 143 | 144 | 145 | /* kinds of Garbage Collection */ 146 | #define KGC_INC 0 /* incremental gc */ 147 | #define KGC_GEN 1 /* generational gc */ 148 | 149 | 150 | typedef struct stringtable { 151 | TString **hash; 152 | int nuse; /* number of elements */ 153 | int size; 154 | } stringtable; 155 | 156 | 157 | /* 158 | ** Information about a call. 159 | ** About union 'u': 160 | ** - field 'l' is used only for Lua functions; 161 | ** - field 'c' is used only for C functions. 162 | ** About union 'u2': 163 | ** - field 'funcidx' is used only by C functions while doing a 164 | ** protected call; 165 | ** - field 'nyield' is used only while a function is "doing" an 166 | ** yield (from the yield until the next resume); 167 | ** - field 'nres' is used only while closing tbc variables when 168 | ** returning from a function; 169 | ** - field 'transferinfo' is used only during call/returnhooks, 170 | ** before the function starts or after it ends. 171 | */ 172 | typedef struct CallInfo { 173 | StkId func; /* function index in the stack */ 174 | StkId top; /* top for this function */ 175 | struct CallInfo *previous, *next; /* dynamic call link */ 176 | union { 177 | struct { /* only for Lua functions */ 178 | const Instruction *savedpc; 179 | volatile l_signalT trap; 180 | int nextraargs; /* # of extra arguments in vararg functions */ 181 | } l; 182 | struct { /* only for C functions */ 183 | lua_KFunction k; /* continuation in case of yields */ 184 | ptrdiff_t old_errfunc; 185 | lua_KContext ctx; /* context info. in case of yields */ 186 | } c; 187 | } u; 188 | union { 189 | int funcidx; /* called-function index */ 190 | int nyield; /* number of values yielded */ 191 | int nres; /* number of values returned */ 192 | struct { /* info about transferred values (for call/return hooks) */ 193 | unsigned short ftransfer; /* offset of first value transferred */ 194 | unsigned short ntransfer; /* number of values transferred */ 195 | } transferinfo; 196 | } u2; 197 | short nresults; /* expected number of results from this function */ 198 | unsigned short callstatus; 199 | } CallInfo; 200 | 201 | 202 | /* 203 | ** Bits in CallInfo status 204 | */ 205 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ 206 | #define CIST_C (1<<1) /* call is running a C function */ 207 | #define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ 208 | #define CIST_HOOKED (1<<3) /* call is running a debug hook */ 209 | #define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ 210 | #define CIST_TAIL (1<<5) /* call was tail called */ 211 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 212 | #define CIST_FIN (1<<7) /* function "called" a finalizer */ 213 | #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ 214 | #define CIST_CLSRET (1<<9) /* function is closing tbc variables */ 215 | /* Bits 10-12 are used for CIST_RECST (see below) */ 216 | #define CIST_RECST 10 217 | #if defined(LUA_COMPAT_LT_LE) 218 | #define CIST_LEQ (1<<13) /* using __lt for __le */ 219 | #endif 220 | 221 | 222 | /* 223 | ** Field CIST_RECST stores the "recover status", used to keep the error 224 | ** status while closing to-be-closed variables in coroutines, so that 225 | ** Lua can correctly resume after an yield from a __close method called 226 | ** because of an error. (Three bits are enough for error status.) 227 | */ 228 | #define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) 229 | #define setcistrecst(ci,st) \ 230 | check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ 231 | ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \ 232 | | ((st) << CIST_RECST))) 233 | 234 | 235 | /* active function is a Lua function */ 236 | #define isLua(ci) (!((ci)->callstatus & CIST_C)) 237 | 238 | /* call is running Lua code (not a hook) */ 239 | #define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) 240 | 241 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ 242 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) 243 | #define getoah(st) ((st) & CIST_OAH) 244 | 245 | 246 | /* 247 | ** 'global state', shared by all threads of this state 248 | */ 249 | typedef struct global_State { 250 | lua_Alloc frealloc; /* function to reallocate memory */ 251 | void *ud; /* auxiliary data to 'frealloc' */ 252 | l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 253 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 254 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 255 | lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ 256 | stringtable strt; /* hash table for strings */ 257 | TValue l_registry; 258 | TValue nilvalue; /* a nil value */ 259 | unsigned int seed; /* randomized seed for hashes */ 260 | lu_byte currentwhite; 261 | lu_byte gcstate; /* state of garbage collector */ 262 | lu_byte gckind; /* kind of GC running */ 263 | lu_byte gcstopem; /* stops emergency collections */ 264 | lu_byte genminormul; /* control for minor generational collections */ 265 | lu_byte genmajormul; /* control for major generational collections */ 266 | lu_byte gcstp; /* control whether GC is running */ 267 | lu_byte gcemergency; /* true if this is an emergency collection */ 268 | lu_byte gcpause; /* size of pause between successive GCs */ 269 | lu_byte gcstepmul; /* GC "speed" */ 270 | lu_byte gcstepsize; /* (log2 of) GC granularity */ 271 | GCObject *allgc; /* list of all collectable objects */ 272 | GCObject **sweepgc; /* current position of sweep in list */ 273 | GCObject *finobj; /* list of collectable objects with finalizers */ 274 | GCObject *gray; /* list of gray objects */ 275 | GCObject *grayagain; /* list of objects to be traversed atomically */ 276 | GCObject *weak; /* list of tables with weak values */ 277 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 278 | GCObject *allweak; /* list of all-weak tables */ 279 | GCObject *tobefnz; /* list of userdata to be GC */ 280 | GCObject *fixedgc; /* list of objects not to be collected */ 281 | /* fields for generational collector */ 282 | GCObject *survival; /* start of objects that survived one GC cycle */ 283 | GCObject *old1; /* start of old1 objects */ 284 | GCObject *reallyold; /* objects more than one cycle old ("really old") */ 285 | GCObject *firstold1; /* first OLD1 object in the list (if any) */ 286 | GCObject *finobjsur; /* list of survival objects with finalizers */ 287 | GCObject *finobjold1; /* list of old1 objects with finalizers */ 288 | GCObject *finobjrold; /* list of really old objects with finalizers */ 289 | struct lua_State *twups; /* list of threads with open upvalues */ 290 | lua_CFunction panic; /* to be called in unprotected errors */ 291 | struct lua_State *mainthread; 292 | TString *memerrmsg; /* message for memory-allocation errors */ 293 | TString *tmname[TM_N]; /* array with tag-method names */ 294 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 295 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ 296 | lua_WarnFunction warnf; /* warning function */ 297 | void *ud_warn; /* auxiliary data to 'warnf' */ 298 | } global_State; 299 | 300 | 301 | /* 302 | ** 'per thread' state 303 | */ 304 | struct lua_State { 305 | CommonHeader; 306 | lu_byte status; 307 | lu_byte allowhook; 308 | unsigned short nci; /* number of items in 'ci' list */ 309 | StkId top; /* first free slot in the stack */ 310 | global_State *l_G; 311 | CallInfo *ci; /* call info for current function */ 312 | StkId stack_last; /* end of stack (last element + 1) */ 313 | StkId stack; /* stack base */ 314 | UpVal *openupval; /* list of open upvalues in this stack */ 315 | StkId tbclist; /* list of to-be-closed variables */ 316 | GCObject *gclist; 317 | struct lua_State *twups; /* list of threads with open upvalues */ 318 | struct lua_longjmp *errorJmp; /* current error recover point */ 319 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 320 | volatile lua_Hook hook; 321 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 322 | l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ 323 | int oldpc; /* last pc traced */ 324 | int basehookcount; 325 | int hookcount; 326 | volatile l_signalT hookmask; 327 | }; 328 | 329 | 330 | #define G(L) (L->l_G) 331 | 332 | /* 333 | ** 'g->nilvalue' being a nil value flags that the state was completely 334 | ** build. 335 | */ 336 | #define completestate(g) ttisnil(&g->nilvalue) 337 | 338 | 339 | /* 340 | ** Union of all collectable objects (only for conversions) 341 | ** ISO C99, 6.5.2.3 p.5: 342 | ** "if a union contains several structures that share a common initial 343 | ** sequence [...], and if the union object currently contains one 344 | ** of these structures, it is permitted to inspect the common initial 345 | ** part of any of them anywhere that a declaration of the complete type 346 | ** of the union is visible." 347 | */ 348 | union GCUnion { 349 | GCObject gc; /* common header */ 350 | struct TString ts; 351 | struct Udata u; 352 | union Closure cl; 353 | struct Table h; 354 | struct Proto p; 355 | struct lua_State th; /* thread */ 356 | struct UpVal upv; 357 | }; 358 | 359 | 360 | /* 361 | ** ISO C99, 6.7.2.1 p.14: 362 | ** "A pointer to a union object, suitably converted, points to each of 363 | ** its members [...], and vice versa." 364 | */ 365 | #define cast_u(o) cast(union GCUnion *, (o)) 366 | 367 | /* macros to convert a GCObject into a specific value */ 368 | #define gco2ts(o) \ 369 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) 370 | #define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) 371 | #define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) 372 | #define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) 373 | #define gco2cl(o) \ 374 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) 375 | #define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) 376 | #define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) 377 | #define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) 378 | #define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) 379 | 380 | 381 | /* 382 | ** macro to convert a Lua object into a GCObject 383 | ** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) 384 | */ 385 | #define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) 386 | 387 | 388 | /* actual number of total bytes allocated */ 389 | #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) 390 | 391 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 392 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 393 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 394 | LUAI_FUNC void luaE_freeCI (lua_State *L); 395 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); 396 | LUAI_FUNC void luaE_checkcstack (lua_State *L); 397 | LUAI_FUNC void luaE_incCstack (lua_State *L); 398 | LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); 399 | LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); 400 | LUAI_FUNC int luaE_resetthread (lua_State *L, int status); 401 | 402 | 403 | #endif 404 | 405 | -------------------------------------------------------------------------------- /include/Lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | /* 16 | ** Memory-allocation error message must be preallocated (it cannot 17 | ** be created after memory is exhausted) 18 | */ 19 | #define MEMERRMSG "not enough memory" 20 | 21 | 22 | /* 23 | ** Size of a TString: Size of the header plus space for the string 24 | ** itself (including final '\0'). 25 | */ 26 | #define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) 27 | 28 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 29 | (sizeof(s)/sizeof(char))-1)) 30 | 31 | 32 | /* 33 | ** test whether a string is a reserved word 34 | */ 35 | #define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) 36 | 37 | 38 | /* 39 | ** equality for short strings, which are always internalized 40 | */ 41 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) 42 | 43 | 44 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 45 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 46 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 47 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 48 | LUAI_FUNC void luaS_clearcache (global_State *g); 49 | LUAI_FUNC void luaS_init (lua_State *L); 50 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 51 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); 52 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 53 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 54 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 55 | 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/Lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->u.next) 16 | 17 | 18 | /* 19 | ** Clear all bits of fast-access metamethods, which means that the table 20 | ** may have any of these metamethods. (First access that fails after the 21 | ** clearing will set the bit again.) 22 | */ 23 | #define invalidateTMcache(t) ((t)->flags &= ~maskflags) 24 | 25 | 26 | /* true when 't' is using 'dummynode' as its hash part */ 27 | #define isdummy(t) ((t)->lastfree == NULL) 28 | 29 | 30 | /* allocated size for hash nodes */ 31 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 32 | 33 | 34 | /* returns the Node, given the value of a table entry */ 35 | #define nodefromval(v) cast(Node *, (v)) 36 | 37 | 38 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 39 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 40 | TValue *value); 41 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 42 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 43 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 44 | LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, 45 | TValue *value); 46 | LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, 47 | TValue *value); 48 | LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, 49 | const TValue *slot, TValue *value); 50 | LUAI_FUNC Table *luaH_new (lua_State *L); 51 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 52 | unsigned int nhsize); 53 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 54 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 55 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 56 | LUAI_FUNC lua_Unsigned luaH_getn (Table *t); 57 | LUAI_FUNC unsigned int luaH_realasize (const Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy (const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/Lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Mask with 1 in all fast-access methods. A 1 in any of these bits 50 | ** in the flag of a (meta)table means the metatable does not have the 51 | ** corresponding metamethod field. (Bit 7 of the flag is used for 52 | ** 'isrealasize'.) 53 | */ 54 | #define maskflags (~(~0u << (TM_EQ + 1))) 55 | 56 | 57 | /* 58 | ** Test whether there is no tagmethod. 59 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 60 | */ 61 | #define notm(tm) ttisnil(tm) 62 | 63 | 64 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 65 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 66 | 67 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 68 | 69 | #define ttypename(x) luaT_typenames_[(x) + 1] 70 | 71 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) 72 | 73 | 74 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 75 | 76 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 77 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 78 | TMS event); 79 | LUAI_FUNC void luaT_init (lua_State *L); 80 | 81 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 82 | const TValue *p2, const TValue *p3); 83 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 84 | const TValue *p1, const TValue *p2, StkId p3); 85 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 86 | StkId res, TMS event); 87 | LUAI_FUNC void luaT_tryconcatTM (lua_State *L); 88 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 89 | const TValue *p2, int inv, StkId res, TMS event); 90 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 91 | int inv, StkId res, TMS event); 92 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 93 | const TValue *p2, TMS event); 94 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 95 | int inv, int isfloat, TMS event); 96 | 97 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 98 | struct CallInfo *ci, const Proto *p); 99 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 100 | StkId where, int wanted); 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /include/Lua/lua.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lua.h $ 3 | ** Lua - A Scripting 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 13 | #include 14 | 15 | 16 | #include "luaconf.h" 17 | 18 | 19 | #define LUA_VERSION_MAJOR "5" 20 | #define LUA_VERSION_MINOR "4" 21 | #define LUA_VERSION_RELEASE "4" 22 | 23 | #define LUA_VERSION_NUM 504 24 | #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) 25 | 26 | #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 27 | #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE 28 | #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio" 29 | #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" 30 | 31 | 32 | /* mark for precompiled code ('Lua') */ 33 | #define LUA_SIGNATURE "\x1bLua" 34 | 35 | /* option for multiple returns in 'lua_pcall' and 'lua_call' */ 36 | #define LUA_MULTRET (-1) 37 | 38 | 39 | /* 40 | ** Pseudo-indices 41 | ** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty 42 | ** space after that to help overflow detection) 43 | */ 44 | #define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) 45 | #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) 46 | 47 | 48 | /* thread status */ 49 | #define LUA_OK 0 50 | #define LUA_YIELD 1 51 | #define LUA_ERRRUN 2 52 | #define LUA_ERRSYNTAX 3 53 | #define LUA_ERRMEM 4 54 | #define LUA_ERRERR 5 55 | 56 | 57 | typedef struct lua_State lua_State; 58 | 59 | 60 | /* 61 | ** basic types 62 | */ 63 | #define LUA_TNONE (-1) 64 | 65 | #define LUA_TNIL 0 66 | #define LUA_TBOOLEAN 1 67 | #define LUA_TLIGHTUSERDATA 2 68 | #define LUA_TNUMBER 3 69 | #define LUA_TSTRING 4 70 | #define LUA_TTABLE 5 71 | #define LUA_TFUNCTION 6 72 | #define LUA_TUSERDATA 7 73 | #define LUA_TTHREAD 8 74 | 75 | #define LUA_NUMTYPES 9 76 | 77 | 78 | 79 | /* minimum Lua stack available to a C function */ 80 | #define LUA_MINSTACK 20 81 | 82 | 83 | /* predefined values in the registry */ 84 | #define LUA_RIDX_MAINTHREAD 1 85 | #define LUA_RIDX_GLOBALS 2 86 | #define LUA_RIDX_LAST LUA_RIDX_GLOBALS 87 | 88 | 89 | /* type of numbers in Lua */ 90 | typedef LUA_NUMBER lua_Number; 91 | 92 | 93 | /* type for integer functions */ 94 | typedef LUA_INTEGER lua_Integer; 95 | 96 | /* unsigned integer type */ 97 | typedef LUA_UNSIGNED lua_Unsigned; 98 | 99 | /* type for continuation-function contexts */ 100 | typedef LUA_KCONTEXT lua_KContext; 101 | 102 | 103 | /* 104 | ** Type for C functions registered with Lua 105 | */ 106 | typedef int (*lua_CFunction) (lua_State *L); 107 | 108 | /* 109 | ** Type for continuation functions 110 | */ 111 | typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); 112 | 113 | 114 | /* 115 | ** Type for functions that read/write blocks when loading/dumping Lua chunks 116 | */ 117 | typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 118 | 119 | typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); 120 | 121 | 122 | /* 123 | ** Type for memory-allocation functions 124 | */ 125 | typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 126 | 127 | 128 | /* 129 | ** Type for warning functions 130 | */ 131 | typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); 132 | 133 | 134 | 135 | 136 | /* 137 | ** generic extra include file 138 | */ 139 | #if defined(LUA_USER_H) 140 | #include LUA_USER_H 141 | #endif 142 | 143 | 144 | /* 145 | ** RCS ident string 146 | */ 147 | extern const char lua_ident[]; 148 | 149 | 150 | /* 151 | ** state manipulation 152 | */ 153 | LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 154 | LUA_API void (lua_close) (lua_State *L); 155 | LUA_API lua_State *(lua_newthread) (lua_State *L); 156 | LUA_API int (lua_resetthread) (lua_State *L); 157 | 158 | LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 159 | 160 | 161 | LUA_API lua_Number (lua_version) (lua_State *L); 162 | 163 | 164 | /* 165 | ** basic stack manipulation 166 | */ 167 | LUA_API int (lua_absindex) (lua_State *L, int idx); 168 | LUA_API int (lua_gettop) (lua_State *L); 169 | LUA_API void (lua_settop) (lua_State *L, int idx); 170 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); 171 | LUA_API void (lua_rotate) (lua_State *L, int idx, int n); 172 | LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); 173 | LUA_API int (lua_checkstack) (lua_State *L, int n); 174 | 175 | LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 176 | 177 | 178 | /* 179 | ** access functions (stack -> C) 180 | */ 181 | 182 | LUA_API int (lua_isnumber) (lua_State *L, int idx); 183 | LUA_API int (lua_isstring) (lua_State *L, int idx); 184 | LUA_API int (lua_iscfunction) (lua_State *L, int idx); 185 | LUA_API int (lua_isinteger) (lua_State *L, int idx); 186 | LUA_API int (lua_isuserdata) (lua_State *L, int idx); 187 | LUA_API int (lua_type) (lua_State *L, int idx); 188 | LUA_API const char *(lua_typename) (lua_State *L, int tp); 189 | 190 | LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); 191 | LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); 192 | LUA_API int (lua_toboolean) (lua_State *L, int idx); 193 | LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 194 | LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); 195 | LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 196 | LUA_API void *(lua_touserdata) (lua_State *L, int idx); 197 | LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 198 | LUA_API const void *(lua_topointer) (lua_State *L, int idx); 199 | 200 | 201 | /* 202 | ** Comparison and arithmetic functions 203 | */ 204 | 205 | #define LUA_OPADD 0 /* ORDER TM, ORDER OP */ 206 | #define LUA_OPSUB 1 207 | #define LUA_OPMUL 2 208 | #define LUA_OPMOD 3 209 | #define LUA_OPPOW 4 210 | #define LUA_OPDIV 5 211 | #define LUA_OPIDIV 6 212 | #define LUA_OPBAND 7 213 | #define LUA_OPBOR 8 214 | #define LUA_OPBXOR 9 215 | #define LUA_OPSHL 10 216 | #define LUA_OPSHR 11 217 | #define LUA_OPUNM 12 218 | #define LUA_OPBNOT 13 219 | 220 | LUA_API void (lua_arith) (lua_State *L, int op); 221 | 222 | #define LUA_OPEQ 0 223 | #define LUA_OPLT 1 224 | #define LUA_OPLE 2 225 | 226 | LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 227 | LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); 228 | 229 | 230 | /* 231 | ** push functions (C -> stack) 232 | */ 233 | LUA_API void (lua_pushnil) (lua_State *L); 234 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 235 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 236 | LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); 237 | LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); 238 | LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 239 | va_list argp); 240 | LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 241 | LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 242 | LUA_API void (lua_pushboolean) (lua_State *L, int b); 243 | LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 244 | LUA_API int (lua_pushthread) (lua_State *L); 245 | 246 | 247 | /* 248 | ** get functions (Lua -> stack) 249 | */ 250 | LUA_API int (lua_getglobal) (lua_State *L, const char *name); 251 | LUA_API int (lua_gettable) (lua_State *L, int idx); 252 | LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); 253 | LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); 254 | LUA_API int (lua_rawget) (lua_State *L, int idx); 255 | LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); 256 | LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); 257 | 258 | LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 259 | LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); 260 | LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 261 | LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); 262 | 263 | 264 | /* 265 | ** set functions (stack -> Lua) 266 | */ 267 | LUA_API void (lua_setglobal) (lua_State *L, const char *name); 268 | LUA_API void (lua_settable) (lua_State *L, int idx); 269 | LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 270 | LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); 271 | LUA_API void (lua_rawset) (lua_State *L, int idx); 272 | LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); 273 | LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); 274 | LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 275 | LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); 276 | 277 | 278 | /* 279 | ** 'load' and 'call' functions (load and run Lua code) 280 | */ 281 | LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, 282 | lua_KContext ctx, lua_KFunction k); 283 | #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) 284 | 285 | LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, 286 | lua_KContext ctx, lua_KFunction k); 287 | #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) 288 | 289 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 290 | const char *chunkname, const char *mode); 291 | 292 | LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); 293 | 294 | 295 | /* 296 | ** coroutine functions 297 | */ 298 | LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, 299 | lua_KFunction k); 300 | LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg, 301 | int *nres); 302 | LUA_API int (lua_status) (lua_State *L); 303 | LUA_API int (lua_isyieldable) (lua_State *L); 304 | 305 | #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) 306 | 307 | 308 | /* 309 | ** Warning-related functions 310 | */ 311 | LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); 312 | LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); 313 | 314 | 315 | /* 316 | ** garbage-collection function and options 317 | */ 318 | 319 | #define LUA_GCSTOP 0 320 | #define LUA_GCRESTART 1 321 | #define LUA_GCCOLLECT 2 322 | #define LUA_GCCOUNT 3 323 | #define LUA_GCCOUNTB 4 324 | #define LUA_GCSTEP 5 325 | #define LUA_GCSETPAUSE 6 326 | #define LUA_GCSETSTEPMUL 7 327 | #define LUA_GCISRUNNING 9 328 | #define LUA_GCGEN 10 329 | #define LUA_GCINC 11 330 | 331 | LUA_API int (lua_gc) (lua_State *L, int what, ...); 332 | 333 | 334 | /* 335 | ** miscellaneous functions 336 | */ 337 | 338 | LUA_API int (lua_error) (lua_State *L); 339 | 340 | LUA_API int (lua_next) (lua_State *L, int idx); 341 | 342 | LUA_API void (lua_concat) (lua_State *L, int n); 343 | LUA_API void (lua_len) (lua_State *L, int idx); 344 | 345 | LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); 346 | 347 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 348 | LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); 349 | 350 | LUA_API void (lua_toclose) (lua_State *L, int idx); 351 | LUA_API void (lua_closeslot) (lua_State *L, int idx); 352 | 353 | 354 | /* 355 | ** {============================================================== 356 | ** some useful macros 357 | ** =============================================================== 358 | */ 359 | 360 | #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) 361 | 362 | #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) 363 | #define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) 364 | 365 | #define lua_pop(L,n) lua_settop(L, -(n)-1) 366 | 367 | #define lua_newtable(L) lua_createtable(L, 0, 0) 368 | 369 | #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 370 | 371 | #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 372 | 373 | #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 374 | #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 375 | #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 376 | #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 377 | #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 378 | #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 379 | #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 380 | #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 381 | 382 | #define lua_pushliteral(L, s) lua_pushstring(L, "" s) 383 | 384 | #define lua_pushglobaltable(L) \ 385 | ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) 386 | 387 | #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 388 | 389 | 390 | #define lua_insert(L,idx) lua_rotate(L, (idx), 1) 391 | 392 | #define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) 393 | 394 | #define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) 395 | 396 | /* }============================================================== */ 397 | 398 | 399 | /* 400 | ** {============================================================== 401 | ** compatibility macros 402 | ** =============================================================== 403 | */ 404 | #if defined(LUA_COMPAT_APIINTCASTS) 405 | 406 | #define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 407 | #define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) 408 | #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) 409 | 410 | #endif 411 | 412 | #define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) 413 | #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) 414 | #define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) 415 | 416 | #define LUA_NUMTAGS LUA_NUMTYPES 417 | 418 | /* }============================================================== */ 419 | 420 | /* 421 | ** {====================================================================== 422 | ** Debug API 423 | ** ======================================================================= 424 | */ 425 | 426 | 427 | /* 428 | ** Event codes 429 | */ 430 | #define LUA_HOOKCALL 0 431 | #define LUA_HOOKRET 1 432 | #define LUA_HOOKLINE 2 433 | #define LUA_HOOKCOUNT 3 434 | #define LUA_HOOKTAILCALL 4 435 | 436 | 437 | /* 438 | ** Event masks 439 | */ 440 | #define LUA_MASKCALL (1 << LUA_HOOKCALL) 441 | #define LUA_MASKRET (1 << LUA_HOOKRET) 442 | #define LUA_MASKLINE (1 << LUA_HOOKLINE) 443 | #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 444 | 445 | typedef struct lua_Debug lua_Debug; /* activation record */ 446 | 447 | 448 | /* Functions to be called by the debugger in specific events */ 449 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 450 | 451 | 452 | LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); 453 | LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); 454 | LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); 455 | LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); 456 | LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); 457 | LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); 458 | 459 | LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); 460 | LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, 461 | int fidx2, int n2); 462 | 463 | LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); 464 | LUA_API lua_Hook (lua_gethook) (lua_State *L); 465 | LUA_API int (lua_gethookmask) (lua_State *L); 466 | LUA_API int (lua_gethookcount) (lua_State *L); 467 | 468 | LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); 469 | 470 | struct lua_Debug { 471 | int event; 472 | const char *name; /* (n) */ 473 | const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ 474 | const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ 475 | const char *source; /* (S) */ 476 | size_t srclen; /* (S) */ 477 | int currentline; /* (l) */ 478 | int linedefined; /* (S) */ 479 | int lastlinedefined; /* (S) */ 480 | unsigned char nups; /* (u) number of upvalues */ 481 | unsigned char nparams;/* (u) number of parameters */ 482 | char isvararg; /* (u) */ 483 | char istailcall; /* (t) */ 484 | unsigned short ftransfer; /* (r) index of first value transferred */ 485 | unsigned short ntransfer; /* (r) number of transferred values */ 486 | char short_src[LUA_IDSIZE]; /* (S) */ 487 | /* private part */ 488 | struct CallInfo *i_ci; /* active function */ 489 | }; 490 | 491 | /* }====================================================================== */ 492 | 493 | 494 | /****************************************************************************** 495 | * Copyright (C) 1994-2022 Lua.org, PUC-Rio. 496 | * 497 | * Permission is hereby granted, free of charge, to any person obtaining 498 | * a copy of this software and associated documentation files (the 499 | * "Software"), to deal in the Software without restriction, including 500 | * without limitation the rights to use, copy, modify, merge, publish, 501 | * distribute, sublicense, and/or sell copies of the Software, and to 502 | * permit persons to whom the Software is furnished to do so, subject to 503 | * the following conditions: 504 | * 505 | * The above copyright notice and this permission notice shall be 506 | * included in all copies or substantial portions of the Software. 507 | * 508 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 509 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 510 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 511 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 512 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 513 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 514 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 515 | ******************************************************************************/ 516 | 517 | 518 | #endif 519 | -------------------------------------------------------------------------------- /include/Lua/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /include/Lua/luaconf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luaconf.h $ 3 | ** Configuration file for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef luaconf_h 9 | #define luaconf_h 10 | 11 | #include 12 | #include 13 | 14 | 15 | /* 16 | ** =================================================================== 17 | ** General Configuration File for Lua 18 | ** 19 | ** Some definitions here can be changed externally, through the compiler 20 | ** (e.g., with '-D' options): They are commented out or protected 21 | ** by '#if !defined' guards. However, several other definitions 22 | ** should be changed directly here, either because they affect the 23 | ** Lua ABI (by making the changes here, you ensure that all software 24 | ** connected to Lua, such as C libraries, will be compiled with the same 25 | ** configuration); or because they are seldom changed. 26 | ** 27 | ** Search for "@@" to find all configurable definitions. 28 | ** =================================================================== 29 | */ 30 | 31 | 32 | /* 33 | ** {==================================================================== 34 | ** System Configuration: macros to adapt (if needed) Lua to some 35 | ** particular platform, for instance restricting it to C89. 36 | ** ===================================================================== 37 | */ 38 | 39 | /* 40 | @@ LUA_USE_C89 controls the use of non-ISO-C89 features. 41 | ** Define it if you want Lua to avoid the use of a few C99 features 42 | ** or Windows-specific features on Windows. 43 | */ 44 | /* #define LUA_USE_C89 */ 45 | 46 | 47 | /* 48 | ** By default, Lua on Windows use (some) specific Windows features 49 | */ 50 | #if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) 51 | #define LUA_USE_WINDOWS /* enable goodies for regular Windows */ 52 | #endif 53 | 54 | 55 | #if defined(LUA_USE_WINDOWS) 56 | #define LUA_DL_DLL /* enable support for DLL */ 57 | #define LUA_USE_C89 /* broadly, Windows is C89 */ 58 | #endif 59 | 60 | 61 | #if defined(LUA_USE_LINUX) 62 | #define LUA_USE_POSIX 63 | #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ 64 | #endif 65 | 66 | 67 | #if defined(LUA_USE_MACOSX) 68 | #define LUA_USE_POSIX 69 | #define LUA_USE_DLOPEN /* MacOS does not need -ldl */ 70 | #endif 71 | 72 | 73 | /* 74 | @@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. 75 | */ 76 | #define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) 77 | 78 | /* }================================================================== */ 79 | 80 | 81 | 82 | /* 83 | ** {================================================================== 84 | ** Configuration for Number types. These options should not be 85 | ** set externally, because any other code connected to Lua must 86 | ** use the same configuration. 87 | ** =================================================================== 88 | */ 89 | 90 | /* 91 | @@ LUA_INT_TYPE defines the type for Lua integers. 92 | @@ LUA_FLOAT_TYPE defines the type for Lua floats. 93 | ** Lua should work fine with any mix of these options supported 94 | ** by your C compiler. The usual configurations are 64-bit integers 95 | ** and 'double' (the default), 32-bit integers and 'float' (for 96 | ** restricted platforms), and 'long'/'double' (for C compilers not 97 | ** compliant with C99, which may not have support for 'long long'). 98 | */ 99 | 100 | /* predefined options for LUA_INT_TYPE */ 101 | #define LUA_INT_INT 1 102 | #define LUA_INT_LONG 2 103 | #define LUA_INT_LONGLONG 3 104 | 105 | /* predefined options for LUA_FLOAT_TYPE */ 106 | #define LUA_FLOAT_FLOAT 1 107 | #define LUA_FLOAT_DOUBLE 2 108 | #define LUA_FLOAT_LONGDOUBLE 3 109 | 110 | 111 | /* Default configuration ('long long' and 'double', for 64-bit Lua) */ 112 | #define LUA_INT_DEFAULT LUA_INT_LONGLONG 113 | #define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE 114 | 115 | 116 | /* 117 | @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. 118 | */ 119 | #define LUA_32BITS 0 120 | 121 | 122 | /* 123 | @@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for 124 | ** C89 ('long' and 'double'); Windows always has '__int64', so it does 125 | ** not need to use this case. 126 | */ 127 | #if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) 128 | #define LUA_C89_NUMBERS 1 129 | #else 130 | #define LUA_C89_NUMBERS 0 131 | #endif 132 | 133 | 134 | #if LUA_32BITS /* { */ 135 | /* 136 | ** 32-bit integers and 'float' 137 | */ 138 | #if LUAI_IS32INT /* use 'int' if big enough */ 139 | #define LUA_INT_TYPE LUA_INT_INT 140 | #else /* otherwise use 'long' */ 141 | #define LUA_INT_TYPE LUA_INT_LONG 142 | #endif 143 | #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT 144 | 145 | #elif LUA_C89_NUMBERS /* }{ */ 146 | /* 147 | ** largest types available for C89 ('long' and 'double') 148 | */ 149 | #define LUA_INT_TYPE LUA_INT_LONG 150 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE 151 | 152 | #else /* }{ */ 153 | /* use defaults */ 154 | 155 | #define LUA_INT_TYPE LUA_INT_DEFAULT 156 | #define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT 157 | 158 | #endif /* } */ 159 | 160 | 161 | /* }================================================================== */ 162 | 163 | 164 | 165 | /* 166 | ** {================================================================== 167 | ** Configuration for Paths. 168 | ** =================================================================== 169 | */ 170 | 171 | /* 172 | ** LUA_PATH_SEP is the character that separates templates in a path. 173 | ** LUA_PATH_MARK is the string that marks the substitution points in a 174 | ** template. 175 | ** LUA_EXEC_DIR in a Windows path is replaced by the executable's 176 | ** directory. 177 | */ 178 | #define LUA_PATH_SEP ";" 179 | #define LUA_PATH_MARK "?" 180 | #define LUA_EXEC_DIR "!" 181 | 182 | 183 | /* 184 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for 185 | ** Lua libraries. 186 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for 187 | ** C libraries. 188 | ** CHANGE them if your machine has a non-conventional directory 189 | ** hierarchy or if you want to install your libraries in 190 | ** non-conventional directories. 191 | */ 192 | 193 | #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 194 | #if defined(_WIN32) /* { */ 195 | /* 196 | ** In Windows, any exclamation mark ('!') in the path is replaced by the 197 | ** path of the directory of the executable file of the current process. 198 | */ 199 | #define LUA_LDIR "!\\lua\\" 200 | #define LUA_CDIR "!\\" 201 | #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" 202 | 203 | #if !defined(LUA_PATH_DEFAULT) 204 | #define LUA_PATH_DEFAULT \ 205 | LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ 206 | LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ 207 | LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ 208 | ".\\?.lua;" ".\\?\\init.lua" 209 | #endif 210 | 211 | #if !defined(LUA_CPATH_DEFAULT) 212 | #define LUA_CPATH_DEFAULT \ 213 | LUA_CDIR"?.dll;" \ 214 | LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ 215 | LUA_CDIR"loadall.dll;" ".\\?.dll" 216 | #endif 217 | 218 | #else /* }{ */ 219 | 220 | #define LUA_ROOT "/usr/local/" 221 | #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" 222 | #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" 223 | 224 | #if !defined(LUA_PATH_DEFAULT) 225 | #define LUA_PATH_DEFAULT \ 226 | LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ 227 | LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ 228 | "./?.lua;" "./?/init.lua" 229 | #endif 230 | 231 | #if !defined(LUA_CPATH_DEFAULT) 232 | #define LUA_CPATH_DEFAULT \ 233 | LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" 234 | #endif 235 | 236 | #endif /* } */ 237 | 238 | 239 | /* 240 | @@ LUA_DIRSEP is the directory separator (for submodules). 241 | ** CHANGE it if your machine does not use "/" as the directory separator 242 | ** and is not Windows. (On Windows Lua automatically uses "\".) 243 | */ 244 | #if !defined(LUA_DIRSEP) 245 | 246 | #if defined(_WIN32) 247 | #define LUA_DIRSEP "\\" 248 | #else 249 | #define LUA_DIRSEP "/" 250 | #endif 251 | 252 | #endif 253 | 254 | /* }================================================================== */ 255 | 256 | 257 | /* 258 | ** {================================================================== 259 | ** Marks for exported symbols in the C code 260 | ** =================================================================== 261 | */ 262 | 263 | /* 264 | @@ LUA_API is a mark for all core API functions. 265 | @@ LUALIB_API is a mark for all auxiliary library functions. 266 | @@ LUAMOD_API is a mark for all standard library opening functions. 267 | ** CHANGE them if you need to define those functions in some special way. 268 | ** For instance, if you want to create one Windows DLL with the core and 269 | ** the libraries, you may want to use the following definition (define 270 | ** LUA_BUILD_AS_DLL to get it). 271 | */ 272 | #if defined(LUA_BUILD_AS_DLL) /* { */ 273 | 274 | #if defined(LUA_CORE) || defined(LUA_LIB) /* { */ 275 | #define LUA_API __declspec(dllexport) 276 | #else /* }{ */ 277 | #define LUA_API __declspec(dllimport) 278 | #endif /* } */ 279 | 280 | #else /* }{ */ 281 | 282 | #define LUA_API extern 283 | 284 | #endif /* } */ 285 | 286 | 287 | /* 288 | ** More often than not the libs go together with the core. 289 | */ 290 | #define LUALIB_API LUA_API 291 | #define LUAMOD_API LUA_API 292 | 293 | 294 | /* 295 | @@ LUAI_FUNC is a mark for all extern functions that are not to be 296 | ** exported to outside modules. 297 | @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, 298 | ** none of which to be exported to outside modules (LUAI_DDEF for 299 | ** definitions and LUAI_DDEC for declarations). 300 | ** CHANGE them if you need to mark them in some special way. Elf/gcc 301 | ** (versions 3.2 and later) mark them as "hidden" to optimize access 302 | ** when Lua is compiled as a shared library. Not all elf targets support 303 | ** this attribute. Unfortunately, gcc does not offer a way to check 304 | ** whether the target offers that support, and those without support 305 | ** give a warning about it. To avoid these warnings, change to the 306 | ** default definition. 307 | */ 308 | #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ 309 | defined(__ELF__) /* { */ 310 | #define LUAI_FUNC __attribute__((visibility("internal"))) extern 311 | #else /* }{ */ 312 | #define LUAI_FUNC extern 313 | #endif /* } */ 314 | 315 | #define LUAI_DDEC(dec) LUAI_FUNC dec 316 | #define LUAI_DDEF /* empty */ 317 | 318 | /* }================================================================== */ 319 | 320 | 321 | /* 322 | ** {================================================================== 323 | ** Compatibility with previous versions 324 | ** =================================================================== 325 | */ 326 | 327 | /* 328 | @@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. 329 | ** You can define it to get all options, or change specific options 330 | ** to fit your specific needs. 331 | */ 332 | #if defined(LUA_COMPAT_5_3) /* { */ 333 | 334 | /* 335 | @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated 336 | ** functions in the mathematical library. 337 | ** (These functions were already officially removed in 5.3; 338 | ** nevertheless they are still available here.) 339 | */ 340 | #define LUA_COMPAT_MATHLIB 341 | 342 | /* 343 | @@ LUA_COMPAT_APIINTCASTS controls the presence of macros for 344 | ** manipulating other integer types (lua_pushunsigned, lua_tounsigned, 345 | ** luaL_checkint, luaL_checklong, etc.) 346 | ** (These macros were also officially removed in 5.3, but they are still 347 | ** available here.) 348 | */ 349 | #define LUA_COMPAT_APIINTCASTS 350 | 351 | 352 | /* 353 | @@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod 354 | ** using '__lt'. 355 | */ 356 | #define LUA_COMPAT_LT_LE 357 | 358 | 359 | /* 360 | @@ The following macros supply trivial compatibility for some 361 | ** changes in the API. The macros themselves document how to 362 | ** change your code to avoid using them. 363 | ** (Once more, these macros were officially removed in 5.3, but they are 364 | ** still available here.) 365 | */ 366 | #define lua_strlen(L,i) lua_rawlen(L, (i)) 367 | 368 | #define lua_objlen(L,i) lua_rawlen(L, (i)) 369 | 370 | #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 371 | #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) 372 | 373 | #endif /* } */ 374 | 375 | /* }================================================================== */ 376 | 377 | 378 | 379 | /* 380 | ** {================================================================== 381 | ** Configuration for Numbers (low-level part). 382 | ** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* 383 | ** satisfy your needs. 384 | ** =================================================================== 385 | */ 386 | 387 | /* 388 | @@ LUAI_UACNUMBER is the result of a 'default argument promotion' 389 | @@ over a floating number. 390 | @@ l_floatatt(x) corrects float attribute 'x' to the proper float type 391 | ** by prefixing it with one of FLT/DBL/LDBL. 392 | @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. 393 | @@ LUA_NUMBER_FMT is the format for writing floats. 394 | @@ lua_number2str converts a float to a string. 395 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. 396 | @@ l_floor takes the floor of a float. 397 | @@ lua_str2number converts a decimal numeral to a number. 398 | */ 399 | 400 | 401 | /* The following definitions are good for most cases here */ 402 | 403 | #define l_floor(x) (l_mathop(floor)(x)) 404 | 405 | #define lua_number2str(s,sz,n) \ 406 | l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) 407 | 408 | /* 409 | @@ lua_numbertointeger converts a float number with an integral value 410 | ** to an integer, or returns 0 if float is not within the range of 411 | ** a lua_Integer. (The range comparisons are tricky because of 412 | ** rounding. The tests here assume a two-complement representation, 413 | ** where MININTEGER always has an exact representation as a float; 414 | ** MAXINTEGER may not have one, and therefore its conversion to float 415 | ** may have an ill-defined value.) 416 | */ 417 | #define lua_numbertointeger(n,p) \ 418 | ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ 419 | (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ 420 | (*(p) = (LUA_INTEGER)(n), 1)) 421 | 422 | 423 | /* now the variable definitions */ 424 | 425 | #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ 426 | 427 | #define LUA_NUMBER float 428 | 429 | #define l_floatatt(n) (FLT_##n) 430 | 431 | #define LUAI_UACNUMBER double 432 | 433 | #define LUA_NUMBER_FRMLEN "" 434 | #define LUA_NUMBER_FMT "%.7g" 435 | 436 | #define l_mathop(op) op##f 437 | 438 | #define lua_str2number(s,p) strtof((s), (p)) 439 | 440 | 441 | #elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ 442 | 443 | #define LUA_NUMBER long double 444 | 445 | #define l_floatatt(n) (LDBL_##n) 446 | 447 | #define LUAI_UACNUMBER long double 448 | 449 | #define LUA_NUMBER_FRMLEN "L" 450 | #define LUA_NUMBER_FMT "%.19Lg" 451 | 452 | #define l_mathop(op) op##l 453 | 454 | #define lua_str2number(s,p) strtold((s), (p)) 455 | 456 | #elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ 457 | 458 | #define LUA_NUMBER double 459 | 460 | #define l_floatatt(n) (DBL_##n) 461 | 462 | #define LUAI_UACNUMBER double 463 | 464 | #define LUA_NUMBER_FRMLEN "" 465 | #define LUA_NUMBER_FMT "%.14g" 466 | 467 | #define l_mathop(op) op 468 | 469 | #define lua_str2number(s,p) strtod((s), (p)) 470 | 471 | #else /* }{ */ 472 | 473 | #error "numeric float type not defined" 474 | 475 | #endif /* } */ 476 | 477 | 478 | 479 | /* 480 | @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. 481 | @@ LUAI_UACINT is the result of a 'default argument promotion' 482 | @@ over a LUA_INTEGER. 483 | @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. 484 | @@ LUA_INTEGER_FMT is the format for writing integers. 485 | @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. 486 | @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. 487 | @@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. 488 | @@ lua_integer2str converts an integer to a string. 489 | */ 490 | 491 | 492 | /* The following definitions are good for most cases here */ 493 | 494 | #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" 495 | 496 | #define LUAI_UACINT LUA_INTEGER 497 | 498 | #define lua_integer2str(s,sz,n) \ 499 | l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) 500 | 501 | /* 502 | ** use LUAI_UACINT here to avoid problems with promotions (which 503 | ** can turn a comparison between unsigneds into a signed comparison) 504 | */ 505 | #define LUA_UNSIGNED unsigned LUAI_UACINT 506 | 507 | 508 | /* now the variable definitions */ 509 | 510 | #if LUA_INT_TYPE == LUA_INT_INT /* { int */ 511 | 512 | #define LUA_INTEGER int 513 | #define LUA_INTEGER_FRMLEN "" 514 | 515 | #define LUA_MAXINTEGER INT_MAX 516 | #define LUA_MININTEGER INT_MIN 517 | 518 | #define LUA_MAXUNSIGNED UINT_MAX 519 | 520 | #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ 521 | 522 | #define LUA_INTEGER long 523 | #define LUA_INTEGER_FRMLEN "l" 524 | 525 | #define LUA_MAXINTEGER LONG_MAX 526 | #define LUA_MININTEGER LONG_MIN 527 | 528 | #define LUA_MAXUNSIGNED ULONG_MAX 529 | 530 | #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ 531 | 532 | /* use presence of macro LLONG_MAX as proxy for C99 compliance */ 533 | #if defined(LLONG_MAX) /* { */ 534 | /* use ISO C99 stuff */ 535 | 536 | #define LUA_INTEGER long long 537 | #define LUA_INTEGER_FRMLEN "ll" 538 | 539 | #define LUA_MAXINTEGER LLONG_MAX 540 | #define LUA_MININTEGER LLONG_MIN 541 | 542 | #define LUA_MAXUNSIGNED ULLONG_MAX 543 | 544 | #elif defined(LUA_USE_WINDOWS) /* }{ */ 545 | /* in Windows, can use specific Windows types */ 546 | 547 | #define LUA_INTEGER __int64 548 | #define LUA_INTEGER_FRMLEN "I64" 549 | 550 | #define LUA_MAXINTEGER _I64_MAX 551 | #define LUA_MININTEGER _I64_MIN 552 | 553 | #define LUA_MAXUNSIGNED _UI64_MAX 554 | 555 | #else /* }{ */ 556 | 557 | #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ 558 | or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" 559 | 560 | #endif /* } */ 561 | 562 | #else /* }{ */ 563 | 564 | #error "numeric integer type not defined" 565 | 566 | #endif /* } */ 567 | 568 | /* }================================================================== */ 569 | 570 | 571 | /* 572 | ** {================================================================== 573 | ** Dependencies with C99 and other C details 574 | ** =================================================================== 575 | */ 576 | 577 | /* 578 | @@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. 579 | ** (All uses in Lua have only one format item.) 580 | */ 581 | #if !defined(LUA_USE_C89) 582 | #define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) 583 | #else 584 | #define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) 585 | #endif 586 | 587 | 588 | /* 589 | @@ lua_strx2number converts a hexadecimal numeral to a number. 590 | ** In C99, 'strtod' does that conversion. Otherwise, you can 591 | ** leave 'lua_strx2number' undefined and Lua will provide its own 592 | ** implementation. 593 | */ 594 | #if !defined(LUA_USE_C89) 595 | #define lua_strx2number(s,p) lua_str2number(s,p) 596 | #endif 597 | 598 | 599 | /* 600 | @@ lua_pointer2str converts a pointer to a readable string in a 601 | ** non-specified way. 602 | */ 603 | #define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) 604 | 605 | 606 | /* 607 | @@ lua_number2strx converts a float to a hexadecimal numeral. 608 | ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. 609 | ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will 610 | ** provide its own implementation. 611 | */ 612 | #if !defined(LUA_USE_C89) 613 | #define lua_number2strx(L,b,sz,f,n) \ 614 | ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) 615 | #endif 616 | 617 | 618 | /* 619 | ** 'strtof' and 'opf' variants for math functions are not valid in 620 | ** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the 621 | ** availability of these variants. ('math.h' is already included in 622 | ** all files that use these macros.) 623 | */ 624 | #if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) 625 | #undef l_mathop /* variants not available */ 626 | #undef lua_str2number 627 | #define l_mathop(op) (lua_Number)op /* no variant */ 628 | #define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) 629 | #endif 630 | 631 | 632 | /* 633 | @@ LUA_KCONTEXT is the type of the context ('ctx') for continuation 634 | ** functions. It must be a numerical type; Lua will use 'intptr_t' if 635 | ** available, otherwise it will use 'ptrdiff_t' (the nearest thing to 636 | ** 'intptr_t' in C89) 637 | */ 638 | #define LUA_KCONTEXT ptrdiff_t 639 | 640 | #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ 641 | __STDC_VERSION__ >= 199901L 642 | #include 643 | #if defined(INTPTR_MAX) /* even in C99 this type is optional */ 644 | #undef LUA_KCONTEXT 645 | #define LUA_KCONTEXT intptr_t 646 | #endif 647 | #endif 648 | 649 | 650 | /* 651 | @@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). 652 | ** Change that if you do not want to use C locales. (Code using this 653 | ** macro must include the header 'locale.h'.) 654 | */ 655 | #if !defined(lua_getlocaledecpoint) 656 | #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) 657 | #endif 658 | 659 | 660 | /* 661 | ** macros to improve jump prediction, used mostly for error handling 662 | ** and debug facilities. (Some macros in the Lua API use these macros. 663 | ** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your 664 | ** code.) 665 | */ 666 | #if !defined(luai_likely) 667 | 668 | #if defined(__GNUC__) && !defined(LUA_NOBUILTIN) 669 | #define luai_likely(x) (__builtin_expect(((x) != 0), 1)) 670 | #define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) 671 | #else 672 | #define luai_likely(x) (x) 673 | #define luai_unlikely(x) (x) 674 | #endif 675 | 676 | #endif 677 | 678 | 679 | #if defined(LUA_CORE) || defined(LUA_LIB) 680 | /* shorter names for Lua's own use */ 681 | #define l_likely(x) luai_likely(x) 682 | #define l_unlikely(x) luai_unlikely(x) 683 | #endif 684 | 685 | 686 | 687 | /* }================================================================== */ 688 | 689 | 690 | /* 691 | ** {================================================================== 692 | ** Language Variations 693 | ** ===================================================================== 694 | */ 695 | 696 | /* 697 | @@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some 698 | ** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from 699 | ** numbers to strings. Define LUA_NOCVTS2N to turn off automatic 700 | ** coercion from strings to numbers. 701 | */ 702 | /* #define LUA_NOCVTN2S */ 703 | /* #define LUA_NOCVTS2N */ 704 | 705 | 706 | /* 707 | @@ LUA_USE_APICHECK turns on several consistency checks on the C API. 708 | ** Define it as a help when debugging C code. 709 | */ 710 | #if defined(LUA_USE_APICHECK) 711 | #include 712 | #define luai_apicheck(l,e) assert(e) 713 | #endif 714 | 715 | /* }================================================================== */ 716 | 717 | 718 | /* 719 | ** {================================================================== 720 | ** Macros that affect the API and must be stable (that is, must be the 721 | ** same when you compile Lua and when you compile code that links to 722 | ** Lua). 723 | ** ===================================================================== 724 | */ 725 | 726 | /* 727 | @@ LUAI_MAXSTACK limits the size of the Lua stack. 728 | ** CHANGE it if you need a different limit. This limit is arbitrary; 729 | ** its only purpose is to stop Lua from consuming unlimited stack 730 | ** space (and to reserve some numbers for pseudo-indices). 731 | ** (It must fit into max(size_t)/32.) 732 | */ 733 | #if LUAI_IS32INT 734 | #define LUAI_MAXSTACK 1000000 735 | #else 736 | #define LUAI_MAXSTACK 15000 737 | #endif 738 | 739 | 740 | /* 741 | @@ LUA_EXTRASPACE defines the size of a raw memory area associated with 742 | ** a Lua state with very fast access. 743 | ** CHANGE it if you need a different size. 744 | */ 745 | #define LUA_EXTRASPACE (sizeof(void *)) 746 | 747 | 748 | /* 749 | @@ LUA_IDSIZE gives the maximum size for the description of the source 750 | @@ of a function in debug information. 751 | ** CHANGE it if you want a different size. 752 | */ 753 | #define LUA_IDSIZE 60 754 | 755 | 756 | /* 757 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. 758 | */ 759 | #define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) 760 | 761 | 762 | /* 763 | @@ LUAI_MAXALIGN defines fields that, when used in a union, ensure 764 | ** maximum alignment for the other items in that union. 765 | */ 766 | #define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l 767 | 768 | /* }================================================================== */ 769 | 770 | 771 | 772 | 773 | 774 | /* =================================================================== */ 775 | 776 | /* 777 | ** Local configuration. You can use this space to add your redefinitions 778 | ** without modifying the main part of the file. 779 | */ 780 | 781 | 782 | 783 | 784 | 785 | #endif 786 | 787 | -------------------------------------------------------------------------------- /include/Lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_MATHLIBNAME "math" 39 | LUAMOD_API int (luaopen_math) (lua_State *L); 40 | 41 | #define LUA_DBLIBNAME "debug" 42 | LUAMOD_API int (luaopen_debug) (lua_State *L); 43 | 44 | #define LUA_LOADLIBNAME "package" 45 | LUAMOD_API int (luaopen_package) (lua_State *L); 46 | 47 | 48 | /* open all previous libraries */ 49 | LUALIB_API void (luaL_openlibs) (lua_State *L); 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/Lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | /* 22 | ** Encode major-minor version in one byte, one nibble for each 23 | */ 24 | #define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ 25 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 26 | 27 | #define LUAC_FORMAT 0 /* this is the official format */ 28 | 29 | /* load one chunk; from lundump.c */ 30 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 31 | 32 | /* dump one chunk; from ldump.c */ 33 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 34 | void* data, int strip); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/Lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I F2Ieq 37 | #endif 38 | 39 | 40 | /* 41 | ** Rounding modes for float->integer coercion 42 | */ 43 | typedef enum { 44 | F2Ieq, /* no rounding; accepts only integral values */ 45 | F2Ifloor, /* takes the floor of the number */ 46 | F2Iceil /* takes the ceil of the number */ 47 | } F2Imod; 48 | 49 | 50 | /* convert an object to a float (including string coercion) */ 51 | #define tonumber(o,n) \ 52 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 53 | 54 | 55 | /* convert an object to a float (without string coercion) */ 56 | #define tonumberns(o,n) \ 57 | (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ 58 | (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) 59 | 60 | 61 | /* convert an object to an integer (including string coercion) */ 62 | #define tointeger(o,i) \ 63 | (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ 64 | : luaV_tointeger(o,i,LUA_FLOORN2I)) 65 | 66 | 67 | /* convert an object to an integer (without string coercion) */ 68 | #define tointegerns(o,i) \ 69 | (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ 70 | : luaV_tointegerns(o,i,LUA_FLOORN2I)) 71 | 72 | 73 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 74 | 75 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 76 | 77 | 78 | /* 79 | ** fast track for 'gettable': if 't' is a table and 't[k]' is present, 80 | ** return 1 with 'slot' pointing to 't[k]' (position of final result). 81 | ** Otherwise, return 0 (meaning it will have to check metamethod) 82 | ** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL 83 | ** (otherwise). 'f' is the raw get function to use. 84 | */ 85 | #define luaV_fastget(L,t,k,slot,f) \ 86 | (!ttistable(t) \ 87 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 88 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 89 | !isempty(slot))) /* result not empty? */ 90 | 91 | 92 | /* 93 | ** Special case of 'luaV_fastget' for integers, inlining the fast case 94 | ** of 'luaH_getint'. 95 | */ 96 | #define luaV_fastgeti(L,t,k,slot) \ 97 | (!ttistable(t) \ 98 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 99 | : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ 100 | ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ 101 | !isempty(slot))) /* result not empty? */ 102 | 103 | 104 | /* 105 | ** Finish a fast set operation (when fast get succeeds). In that case, 106 | ** 'slot' points to the place to put the value. 107 | */ 108 | #define luaV_finishfastset(L,t,slot,v) \ 109 | { setobj2t(L, cast(TValue *,slot), v); \ 110 | luaC_barrierback(L, gcvalue(t), v); } 111 | 112 | 113 | 114 | 115 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 116 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 117 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 118 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 119 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); 120 | LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, 121 | F2Imod mode); 122 | LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); 123 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 124 | StkId val, const TValue *slot); 125 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 126 | TValue *val, const TValue *slot); 127 | LUAI_FUNC void luaV_finishOp (lua_State *L); 128 | LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); 129 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 130 | LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); 131 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 132 | LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); 133 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 134 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 135 | 136 | #endif 137 | -------------------------------------------------------------------------------- /include/Lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/PluginSDK.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #ifndef __PLUGINSDK_H 16 | #define __PLUGINSDK_H 17 | 18 | #define PLUGIN_API_VERSION 0x1 19 | 20 | #ifdef _MSC_VER 21 | #define LIBRARY_EXPORT __declspec(dllexport) 22 | #define LIBRARY_IMPORT __declspec(dllimport) 23 | #define CALLING_CONVENTION __stdcall 24 | #else 25 | #define LIBRARY_EXPORT __attribute__ ((visibility ("default"))) 26 | #define LIBRARY_IMPORT __attribute__ ((visibility ("default"))) 27 | #define CALLING_CONVENTION 28 | #endif 29 | 30 | #ifdef __cplusplus 31 | #define SYMTAB_PREFIX extern "C" 32 | 33 | #else 34 | #define SYMTAB_PREFIX 35 | #endif 36 | 37 | // Functions exported to be called by the server 38 | #define EXPORT(t) SYMTAB_PREFIX LIBRARY_EXPORT t CALLING_CONVENTION 39 | 40 | // Functions imported to be called by the plugin 41 | #define IMPORT(t) SYMTAB_PREFIX LIBRARY_IMPORT t CALLING_CONVENTION 42 | 43 | #ifdef __cplusplus 44 | #include 45 | #else 46 | #include 47 | #include 48 | #include 49 | #endif 50 | 51 | #ifdef __cplusplus 52 | #include "sdk/LuaFunctionUtils.hpp" 53 | #include "sdk/LuaTable.hpp" 54 | #include "sdk/LuaFunction.hpp" 55 | #include "sdk/LuaValueLuaImpl.hpp" 56 | #include "sdk/PluginApi.hpp" 57 | #endif 58 | 59 | #define LUA_DEFINE(func) static int func(lua_State *L) 60 | #define LUA_FUNCTION(func) int func(lua_State *L) 61 | 62 | 63 | #endif /* __PLUGINSDK_H */ 64 | -------------------------------------------------------------------------------- /include/sdk/LuaFunction.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | 20 | namespace Lua 21 | { 22 | class LuaFunction 23 | { 24 | private: 25 | lua_State *_state; 26 | int _key; 27 | bool _valid = false; 28 | 29 | public: 30 | LuaFunction(lua_State *state) : _state(state) { } 31 | LuaFunction(lua_State *state, const char *name) : LuaFunction(state) 32 | { 33 | lua_getglobal(state, name); 34 | if (!lua_isfunction(state, 0)) 35 | { 36 | lua_pop(state, 0); 37 | return; 38 | } 39 | _key = luaL_ref(state, LUA_REGISTRYINDEX); 40 | _valid = true; 41 | } 42 | ~LuaFunction() 43 | { 44 | if (_valid) 45 | luaL_unref(_state, LUA_REGISTRYINDEX, _key); 46 | } 47 | 48 | LuaFunction(LuaFunction const &) = delete; 49 | LuaFunction &operator=(LuaFunction const &) = delete; 50 | 51 | LuaFunction(LuaFunction &&rhs) : 52 | _state(rhs._state), 53 | _key(rhs._key), 54 | _valid(rhs._valid) 55 | { } 56 | LuaFunction &operator=(LuaFunction &&rhs) 57 | { 58 | _state = rhs._state; 59 | _key = rhs._key; 60 | _valid = rhs._valid; 61 | return *this; 62 | } 63 | 64 | public: 65 | inline bool IsValid() const 66 | { 67 | return _valid; 68 | } 69 | 70 | void ParseFromLua(int index) 71 | { 72 | lua_pushvalue(_state, index); 73 | _key = luaL_ref(_state, LUA_REGISTRYINDEX); 74 | _valid = true; 75 | } 76 | 77 | void PushToLua(lua_State *state) const 78 | { 79 | if (state != _state) 80 | return; 81 | 82 | if (!_valid) 83 | return; 84 | 85 | lua_rawgeti(_state, LUA_REGISTRYINDEX, _key); 86 | } 87 | 88 | public: // static helper func 89 | template 90 | static inline LuaFunction_t Create(Args&& ...args) 91 | { 92 | return LuaFunction_t(new LuaFunction(std::forward(args)...)); 93 | } 94 | }; 95 | } 96 | -------------------------------------------------------------------------------- /include/sdk/LuaFunctionUtils.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | #include 18 | #include "LuaValue.hpp" 19 | #include "LuaTable.hpp" 20 | #include "LuaFunction.hpp" 21 | 22 | namespace Lua 23 | { 24 | struct FunctionCallInfo 25 | { 26 | std::string name, source; 27 | int line; 28 | }; 29 | 30 | inline void RegisterPluginFunction(lua_State *state, const char *function_name, 31 | lua_CFunction lua_function) 32 | { 33 | lua_pushcclosure(state, lua_function, 0); 34 | lua_setglobal(state, function_name); 35 | } 36 | 37 | static bool GetCurrentCallInfo(lua_State *state, FunctionCallInfo &info) 38 | { 39 | lua_Debug dbg; 40 | if (state == nullptr) 41 | return false; 42 | 43 | if (lua_getstack(state, 1, &dbg) != 1 44 | || lua_getinfo(state, "Sl", &dbg) == 0) 45 | { 46 | return false; 47 | } 48 | info.source = dbg.short_src; 49 | info.line = dbg.currentline; 50 | 51 | if (lua_getstack(state, 0, &dbg) != 1 52 | || lua_getinfo(state, "n", &dbg) == 0) 53 | { 54 | return false; 55 | } 56 | info.name = dbg.name; 57 | 58 | return true; 59 | } 60 | 61 | template 62 | LuaArgs_t BuildArgumentList(Args&&... args) 63 | { 64 | LuaArgs_t arg_list; 65 | BuildArgumentList(arg_list, std::forward(args)...); 66 | return arg_list; 67 | } 68 | 69 | template 70 | void BuildArgumentList(LuaArgs_t &arg_list, T value, Args&&... args) 71 | { 72 | arg_list.emplace_back(value); 73 | BuildArgumentList(arg_list, std::forward(args)...); 74 | } 75 | 76 | static void BuildArgumentList(LuaArgs_t &arg_list) 77 | { 78 | (void)arg_list; // unused 79 | } 80 | 81 | 82 | template 83 | void ParseArguments(lua_State *state, int &arg, Args&... args); 84 | template 85 | void ParseArguments(lua_State *state, unsigned int &arg, Args&... args); 86 | template 87 | void ParseArguments(lua_State *state, double &arg, Args&... args); 88 | template 89 | void ParseArguments(lua_State *state, float &arg, Args&... args); 90 | template 91 | void ParseArguments(lua_State *state, bool &arg, Args&... args); 92 | template 93 | void ParseArguments(lua_State *state, std::string &arg, Args&... args); 94 | template 95 | void ParseArguments(lua_State *state, LuaTable_t &arg, Args&... args); 96 | template 97 | void ParseArguments(lua_State *state, LuaFunction_t &arg, Args&... args); 98 | template 99 | void ParseArguments(lua_State *state, LuaArgs_t &arg); 100 | 101 | template 102 | void ParseArguments(lua_State *state) 103 | { 104 | (void)state; // unused 105 | } 106 | 107 | template 108 | void ParseArguments(lua_State *state, int &arg, Args&... args) 109 | { 110 | int type = lua_type(state, Idx); 111 | if (type == LUA_TNUMBER || type == LUA_TBOOLEAN || type == LUA_TSTRING) 112 | { 113 | arg = static_cast(lua_tointeger(state, Idx)); 114 | ParseArguments(state, args...); 115 | } 116 | else 117 | { 118 | luaL_argerror(state, Idx, "expected integer-like argument"); 119 | } 120 | } 121 | 122 | template 123 | void ParseArguments(lua_State *state, unsigned int &arg, Args&... args) 124 | { 125 | int type = lua_type(state, Idx); 126 | if (type == LUA_TNUMBER || type == LUA_TBOOLEAN || type == LUA_TSTRING) 127 | { 128 | arg = static_cast(lua_tointeger(state, Idx)); 129 | ParseArguments(state, args...); 130 | } 131 | else 132 | { 133 | luaL_argerror(state, Idx, "expected integer-like argument"); 134 | } 135 | } 136 | 137 | template 138 | void ParseArguments(lua_State *state, double &arg, Args&... args) 139 | { 140 | int type = lua_type(state, Idx); 141 | if (type == LUA_TNUMBER || type == LUA_TSTRING) 142 | { 143 | arg = static_cast(lua_tonumber(state, Idx)); 144 | ParseArguments(state, args...); 145 | } 146 | else 147 | { 148 | luaL_argerror(state, Idx, "expected number argument"); 149 | } 150 | } 151 | 152 | template 153 | void ParseArguments(lua_State *state, float &arg, Args&... args) 154 | { 155 | int type = lua_type(state, Idx); 156 | if (type == LUA_TNUMBER || type == LUA_TSTRING) 157 | { 158 | arg = static_cast(lua_tonumber(state, Idx)); 159 | ParseArguments(state, args...); 160 | } 161 | else 162 | { 163 | luaL_argerror(state, Idx, "expected number argument"); 164 | } 165 | } 166 | 167 | template 168 | void ParseArguments(lua_State *state, bool &arg, Args&... args) 169 | { 170 | int type = lua_type(state, Idx); 171 | if (type == LUA_TNUMBER || type == LUA_TBOOLEAN || type == LUA_TSTRING) 172 | { 173 | arg = lua_toboolean(state, Idx) != 0; 174 | ParseArguments(state, args...); 175 | } 176 | else 177 | { 178 | luaL_argerror(state, Idx, "expected boolean-like argument"); 179 | } 180 | } 181 | 182 | template 183 | void ParseArguments(lua_State *state, std::string &arg, Args&... args) 184 | { 185 | int type = lua_type(state, Idx); 186 | if (type == LUA_TSTRING || type == LUA_TNUMBER) 187 | { 188 | arg.assign(lua_tostring(state, Idx)); 189 | ParseArguments(state, args...); 190 | } 191 | else 192 | { 193 | luaL_argerror(state, Idx, "expected string-like argument"); 194 | } 195 | } 196 | 197 | template 198 | void ParseArguments(lua_State *state, LuaTable_t &arg, Args&... args) 199 | { 200 | if (lua_istable(state, Idx)) 201 | { 202 | if (!arg) 203 | arg.reset(new LuaTable); 204 | arg->ParseFromLua(state, Idx); 205 | ParseArguments(state, args...); 206 | } 207 | else 208 | { 209 | luaL_argerror(state, Idx, "expected table"); 210 | } 211 | } 212 | 213 | template 214 | void ParseArguments(lua_State *state, LuaFunction_t &arg, Args&... args) 215 | { 216 | if (lua_isfunction(state, Idx)) 217 | { 218 | if (!arg) 219 | arg.reset(new LuaFunction(state)); 220 | arg->ParseFromLua(Idx); 221 | ParseArguments(state, args...); 222 | } 223 | else 224 | { 225 | luaL_argerror(state, Idx, "expected function"); 226 | } 227 | } 228 | 229 | template 230 | void ParseArguments(lua_State *state, LuaArgs_t &arg) 231 | { 232 | int idx = Idx; 233 | while (lua_type(state, idx) != LUA_TNONE) 234 | arg.push_back(ParseValueFromLua(state, idx++)); 235 | } 236 | 237 | 238 | template 239 | bool ParseOptionalArguments(lua_State *state, int &arg, Args&... args); 240 | template 241 | bool ParseOptionalArguments(lua_State *state, unsigned int &arg, Args&... args); 242 | template 243 | bool ParseOptionalArguments(lua_State *state, double &arg, Args&... args); 244 | template 245 | bool ParseOptionalArguments(lua_State *state, float &arg, Args&... args); 246 | template 247 | bool ParseOptionalArguments(lua_State *state, bool &arg, Args&... args); 248 | template 249 | bool ParseOptionalArguments(lua_State *state, std::string &arg, Args&... args); 250 | template 251 | bool ParseOptionalArguments(lua_State *state, LuaTable_t &arg, Args&... args); 252 | template 253 | bool ParseOptionalArguments(lua_State *state, LuaFunction_t &arg, Args&... args); 254 | template 255 | bool ParseOptionalArguments(lua_State *state, LuaArgs_t &arg); 256 | 257 | template 258 | bool ParseOptionalArguments(lua_State *state) 259 | { 260 | (void)state; // unused 261 | return true; 262 | } 263 | 264 | template 265 | bool ParseOptionalArguments(lua_State *state, int &arg, Args&... args) 266 | { 267 | int type = lua_type(state, Idx); 268 | if (type == LUA_TNUMBER || type == LUA_TBOOLEAN || type == LUA_TSTRING) 269 | { 270 | arg = static_cast(lua_tointeger(state, Idx)); 271 | return ParseOptionalArguments(state, args...); 272 | } 273 | else if (type != LUA_TNONE) 274 | { 275 | luaL_argerror(state, Idx, "expected integer-like argument"); 276 | } 277 | return false; 278 | } 279 | 280 | template 281 | bool ParseOptionalArguments(lua_State *state, unsigned int &arg, Args&... args) 282 | { 283 | int type = lua_type(state, Idx); 284 | if (type == LUA_TNUMBER || type == LUA_TBOOLEAN || type == LUA_TSTRING) 285 | { 286 | arg = static_cast(lua_tointeger(state, Idx)); 287 | return ParseOptionalArguments(state, args...); 288 | } 289 | else if (type != LUA_TNONE) 290 | { 291 | luaL_argerror(state, Idx, "expected integer-like argument"); 292 | } 293 | return false; 294 | } 295 | 296 | template 297 | bool ParseOptionalArguments(lua_State *state, double &arg, Args&... args) 298 | { 299 | int type = lua_type(state, Idx); 300 | if (type == LUA_TNUMBER || type == LUA_TSTRING) 301 | { 302 | arg = static_cast(lua_tonumber(state, Idx)); 303 | return ParseOptionalArguments(state, args...); 304 | } 305 | else if (type != LUA_TNONE) 306 | { 307 | luaL_argerror(state, Idx, "expected number argument"); 308 | } 309 | return false; 310 | } 311 | 312 | template 313 | bool ParseOptionalArguments(lua_State *state, float &arg, Args&... args) 314 | { 315 | int type = lua_type(state, Idx); 316 | if (type == LUA_TNUMBER || type == LUA_TSTRING) 317 | { 318 | arg = static_cast(lua_tonumber(state, Idx)); 319 | return ParseOptionalArguments(state, args...); 320 | } 321 | else if (type != LUA_TNONE) 322 | { 323 | luaL_argerror(state, Idx, "expected number argument"); 324 | } 325 | return false; 326 | } 327 | 328 | template 329 | bool ParseOptionalArguments(lua_State *state, bool &arg, Args&... args) 330 | { 331 | int type = lua_type(state, Idx); 332 | if (type == LUA_TNUMBER || type == LUA_TBOOLEAN || type == LUA_TSTRING) 333 | { 334 | arg = static_cast(lua_toboolean(state, Idx)); 335 | return ParseOptionalArguments(state, args...); 336 | } 337 | else if (type != LUA_TNONE) 338 | { 339 | luaL_argerror(state, Idx, "expected boolean-like argument"); 340 | } 341 | return false; 342 | } 343 | 344 | template 345 | bool ParseOptionalArguments(lua_State *state, std::string &arg, Args&... args) 346 | { 347 | int type = lua_type(state, Idx); 348 | if (type == LUA_TSTRING || type == LUA_TNUMBER) 349 | { 350 | arg.assign(lua_tostring(state, Idx)); 351 | return ParseOptionalArguments(state, args...); 352 | } 353 | else if (type != LUA_TNONE) 354 | { 355 | luaL_argerror(state, Idx, "expected string-like argument"); 356 | } 357 | return false; 358 | } 359 | 360 | template 361 | bool ParseOptionalArguments(lua_State *state, LuaTable_t &arg, Args&... args) 362 | { 363 | if (lua_istable(state, Idx)) 364 | { 365 | if (!arg) 366 | arg.reset(new LuaTable); 367 | arg->ParseFromLua(state, Idx); 368 | return ParseOptionalArguments(state, args...); 369 | } 370 | else if (lua_isnone(state, Idx) == 0) 371 | { 372 | luaL_argerror(state, Idx, "expected table"); 373 | } 374 | return false; 375 | } 376 | 377 | template 378 | bool ParseOptionalArguments(lua_State *state, LuaFunction_t &arg, Args&... args) 379 | { 380 | if (lua_isfunction(state, Idx)) 381 | { 382 | if (!arg) 383 | arg.reset(new LuaFunction(state)); 384 | arg->ParseFromLua(Idx); 385 | return ParseOptionalArguments(state, args...); 386 | } 387 | else if (lua_isnone(state, Idx) == 0) 388 | { 389 | luaL_argerror(state, Idx, "expected function"); 390 | } 391 | return false; 392 | } 393 | 394 | template 395 | bool ParseOptionalArguments(lua_State *state, LuaArgs_t &arg) 396 | { 397 | ParseArguments(state, arg); 398 | return true; 399 | } 400 | 401 | static int ReturnValues(lua_State *state) 402 | { 403 | (void)state; // unused 404 | return 0; 405 | } 406 | 407 | template 408 | int ReturnValues(lua_State *state, int arg, Args&&... args) 409 | { 410 | lua_pushinteger(state, arg); 411 | return ReturnValues(state, std::forward(args)...) + 1; 412 | } 413 | 414 | template 415 | int ReturnValues(lua_State *state, unsigned int arg, Args&&... args) 416 | { 417 | lua_pushinteger(state, static_cast(arg)); 418 | return ReturnValues(state, std::forward(args)...) + 1; 419 | } 420 | 421 | template 422 | int ReturnValues(lua_State *state, long long arg, Args&&... args) 423 | { 424 | lua_pushinteger(state, arg); 425 | return ReturnValues(state, std::forward(args)...) + 1; 426 | } 427 | 428 | template 429 | int ReturnValues(lua_State *state, unsigned long long arg, Args&&... args) 430 | { 431 | lua_pushinteger(state, arg); 432 | return ReturnValues(state, std::forward(args)...) + 1; 433 | } 434 | 435 | template 436 | int ReturnValues(lua_State *state, double arg, Args&&... args) 437 | { 438 | lua_pushnumber(state, arg); 439 | return ReturnValues(state, std::forward(args)...) + 1; 440 | } 441 | 442 | template 443 | int ReturnValues(lua_State *state, bool arg, Args&&... args) 444 | { 445 | lua_pushboolean(state, arg); 446 | return ReturnValues(state, std::forward(args)...) + 1; 447 | } 448 | 449 | template 450 | int ReturnValues(lua_State *state, std::string const &arg, Args&&... args) 451 | { 452 | lua_pushlstring(state, arg.c_str(), arg.length()); 453 | return ReturnValues(state, std::forward(args)...) + 1; 454 | } 455 | 456 | template 457 | int ReturnValues(lua_State *state, const char *arg, Args&&... args) 458 | { 459 | lua_pushstring(state, arg); // pushes nil if arg is nullptr 460 | return ReturnValues(state, std::forward(args)...) + 1; 461 | } 462 | 463 | template 464 | int ReturnValues(lua_State *state, LuaTable_t const arg, Args&&... args) 465 | { 466 | arg->PushToLua(state); 467 | return ReturnValues(state, std::forward(args)...) + 1; 468 | } 469 | 470 | template 471 | int ReturnValues(lua_State *state, LuaFunction_t const arg, Args&&... args) 472 | { 473 | arg->PushToLua(state); 474 | return ReturnValues(state, std::forward(args)...) + 1; 475 | } 476 | 477 | template 478 | int ReturnValues(lua_State *state, LuaArgs_t const &arg, Args&&... args) 479 | { 480 | for (auto const &e : arg) 481 | PushValueToLua(e, state); 482 | return ReturnValues(state, std::forward(args)...) 483 | + static_cast(arg.size()); 484 | } 485 | } 486 | -------------------------------------------------------------------------------- /include/sdk/LuaTable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "LuaValue.hpp" 23 | 24 | 25 | namespace Lua 26 | { 27 | class LuaTable 28 | { 29 | private: 30 | std::unordered_map _table; 31 | 32 | public: 33 | template 34 | inline void Add(T key, U value) 35 | { 36 | _table[LuaValue(key)] = LuaValue(value); 37 | } 38 | 39 | template 40 | inline bool Exists(T key) 41 | { 42 | return _table.find(LuaValue(key)) != _table.end(); 43 | } 44 | 45 | template 46 | inline bool Remove(T key) 47 | { 48 | return _table.erase(LuaValue(key)) == 1; 49 | } 50 | 51 | inline int Count() 52 | { 53 | return static_cast(_table.size()); 54 | } 55 | 56 | template 57 | bool TryGet(T key, U &dest) 58 | { 59 | auto it = _table.find(LuaValue(key)); 60 | if (it == _table.end()) 61 | return false; 62 | 63 | return it->second.TryGetValue(dest); 64 | } 65 | 66 | inline void ForEach(std::function func) 67 | { 68 | for (auto const &e : _table) 69 | func(e.first, e.second); 70 | } 71 | 72 | void ParseFromLua(lua_State *state, int index) 73 | { 74 | static int recurse_counter = 0; 75 | if (recurse_counter > 3) 76 | return; 77 | 78 | recurse_counter++; 79 | _table.clear(); 80 | 81 | int counter = 0; 82 | lua_pushnil(state); 83 | while (lua_next(state, index) != 0) 84 | { 85 | _table[ParseValueFromLua(state, -2)] = ParseValueFromLua(state, -1); 86 | lua_pop(state, 1); 87 | if (++counter >= 1000) 88 | { 89 | lua_pop(state, 1); 90 | break; 91 | } 92 | } 93 | recurse_counter--; 94 | } 95 | 96 | void PushToLua(lua_State *state) const 97 | { 98 | lua_createtable(state, static_cast(_table.size()), 0); 99 | for (auto const &e : _table) 100 | { 101 | PushValueToLua(e.first, state); 102 | PushValueToLua(e.second, state); 103 | lua_rawset(state, -3); 104 | } 105 | } 106 | 107 | public: // static helper func 108 | static inline LuaTable_t Create() 109 | { 110 | return LuaTable_t(new LuaTable); 111 | } 112 | }; 113 | } 114 | -------------------------------------------------------------------------------- /include/sdk/LuaTypes.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | 20 | namespace Lua 21 | { 22 | using LuaFunction_t = std::shared_ptr; 23 | using LuaTable_t = std::shared_ptr; 24 | using LuaArgs_t = std::vector; 25 | } 26 | -------------------------------------------------------------------------------- /include/sdk/LuaValue.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | #include "LuaTypes.hpp" 18 | 19 | 20 | #ifdef _MSC_VER 21 | #pragma warning(push) 22 | // disable union "constructor/desructor not called implicitly" warning 23 | #pragma warning(disable: 4582 4583) 24 | #endif 25 | 26 | 27 | namespace Lua 28 | { 29 | class LuaValue 30 | { 31 | public: 32 | enum class Type 33 | { 34 | INVALID, 35 | NIL, 36 | INTEGER, 37 | NUMBER, 38 | BOOLEAN, 39 | STRING, 40 | TABLE, 41 | FUNCTION 42 | }; 43 | 44 | private: 45 | Type _type; 46 | union 47 | { 48 | lua_Integer Integer; 49 | lua_Number Number; 50 | bool Boolean; 51 | std::string String; 52 | LuaTable_t Table; 53 | LuaFunction_t Function; 54 | }; 55 | 56 | public: 57 | struct Hash 58 | { 59 | std::size_t operator()(LuaValue const &e) const 60 | { 61 | std::size_t hash = 0; 62 | 63 | using EnumType = std::underlying_type::type; 64 | hash ^= std::hash()(static_cast(e._type)) 65 | + 0x9e3779b9 + (hash << 6) + (hash >> 2); 66 | 67 | std::size_t value_hash = 0; 68 | bool skip_value = false; 69 | switch (e._type) 70 | { 71 | case Type::INTEGER: 72 | value_hash = std::hash()(e.Integer); 73 | break; 74 | case Type::NUMBER: 75 | value_hash = std::hash()(e.Number); 76 | break; 77 | case Type::BOOLEAN: 78 | value_hash = std::hash()(e.Boolean); 79 | break; 80 | case Type::STRING: 81 | value_hash = std::hash()(e.String); 82 | break; 83 | case Type::TABLE: 84 | value_hash = std::hash()(e.Table); 85 | break; 86 | case Type::FUNCTION: 87 | value_hash = std::hash()(e.Function); 88 | break; 89 | case Type::INVALID: 90 | case Type::NIL: 91 | // skip, the type hash is enough for these 92 | skip_value = true; 93 | break; 94 | } 95 | 96 | if (!skip_value) 97 | hash ^= value_hash + 0x9e3779b9 + (hash << 6) + (hash >> 2); 98 | return hash; 99 | } 100 | }; 101 | 102 | public: 103 | LuaValue() : _type(Type::INVALID) 104 | { } 105 | LuaValue(std::nullptr_t) : _type(Type::NIL) 106 | { } 107 | template::value && !std::is_same::value, int>::type = 0> 109 | LuaValue(T value) : 110 | _type(Type::INTEGER), 111 | Integer(static_cast(value)) 112 | { } 113 | LuaValue(double value) : 114 | _type(Type::NUMBER), 115 | Number(static_cast(value)) 116 | { } 117 | LuaValue(bool value) : 118 | _type(Type::BOOLEAN), 119 | Boolean(value) 120 | { } 121 | LuaValue(const char *value) 122 | { 123 | if (value == nullptr) 124 | { 125 | _type = Type::NIL; 126 | } 127 | else 128 | { 129 | _type = Type::STRING; 130 | new(&String) std::string(value); 131 | } 132 | } 133 | LuaValue(std::string value) : 134 | _type(Type::STRING) 135 | { 136 | new(&String) std::string(value); 137 | } 138 | LuaValue(LuaTable_t value) : 139 | _type(Type::TABLE) 140 | { 141 | new(&Table) std::shared_ptr(std::move(value)); 142 | } 143 | LuaValue(LuaFunction_t value) : 144 | _type(Type::FUNCTION) 145 | { 146 | new(&Function) std::shared_ptr(std::move(value)); 147 | } 148 | 149 | LuaValue(LuaValue const &rhs) : _type(rhs._type) 150 | { 151 | switch (_type) 152 | { 153 | case Type::NIL: 154 | // do nothing 155 | break; 156 | case Type::INTEGER: 157 | Integer = rhs.Integer; 158 | break; 159 | case Type::NUMBER: 160 | Number = rhs.Number; 161 | break; 162 | case Type::BOOLEAN: 163 | Boolean = rhs.Boolean; 164 | break; 165 | case Type::STRING: 166 | new(&String) std::string(rhs.String); 167 | break; 168 | case Type::TABLE: 169 | new(&Table) std::shared_ptr(rhs.Table); 170 | break; 171 | case Type::FUNCTION: 172 | new(&Function) std::shared_ptr(rhs.Function); 173 | break; 174 | case Type::INVALID: 175 | // skip 176 | break; 177 | } 178 | } 179 | LuaValue &operator=(LuaValue const &rhs) 180 | { 181 | _type = rhs._type; 182 | switch (_type) 183 | { 184 | case Type::NIL: 185 | // do nothing 186 | break; 187 | case Type::INTEGER: 188 | Integer = rhs.Integer; 189 | break; 190 | case Type::NUMBER: 191 | Number = rhs.Number; 192 | break; 193 | case Type::BOOLEAN: 194 | Boolean = rhs.Boolean; 195 | break; 196 | case Type::STRING: 197 | new(&String) std::string(rhs.String); 198 | break; 199 | case Type::TABLE: 200 | new(&Table) std::shared_ptr(rhs.Table); 201 | break; 202 | case Type::FUNCTION: 203 | new(&Function) std::shared_ptr(rhs.Function); 204 | break; 205 | case Type::INVALID: 206 | // skip 207 | break; 208 | } 209 | return *this; 210 | } 211 | 212 | LuaValue(LuaValue &&rhs) : _type(rhs._type) 213 | { 214 | switch (_type) 215 | { 216 | case Type::NIL: 217 | // do nothing 218 | break; 219 | case Type::INTEGER: 220 | Integer = rhs.Integer; 221 | break; 222 | case Type::NUMBER: 223 | Number = rhs.Number; 224 | break; 225 | case Type::BOOLEAN: 226 | Boolean = rhs.Boolean; 227 | break; 228 | case Type::STRING: 229 | new(&String) std::string(std::move(rhs.String)); 230 | break; 231 | case Type::TABLE: 232 | new(&Table) std::shared_ptr(std::move(rhs.Table)); 233 | break; 234 | case Type::FUNCTION: 235 | new(&Function) std::shared_ptr(std::move(rhs.Function)); 236 | break; 237 | case Type::INVALID: 238 | // skip 239 | break; 240 | } 241 | } 242 | LuaValue &operator=(LuaValue &&rhs) 243 | { 244 | _type = rhs._type; 245 | switch (_type) 246 | { 247 | case Type::NIL: 248 | // do nothing 249 | break; 250 | case Type::INTEGER: 251 | Integer = rhs.Integer; 252 | break; 253 | case Type::NUMBER: 254 | Number = rhs.Number; 255 | break; 256 | case Type::BOOLEAN: 257 | Boolean = rhs.Boolean; 258 | break; 259 | case Type::STRING: 260 | new(&String) std::string(std::move(rhs.String)); 261 | break; 262 | case Type::TABLE: 263 | new(&Table) std::shared_ptr(std::move(rhs.Table)); 264 | break; 265 | case Type::FUNCTION: 266 | new(&Function) std::shared_ptr(std::move(rhs.Function)); 267 | break; 268 | case Type::INVALID: 269 | // skip 270 | break; 271 | } 272 | return *this; 273 | } 274 | 275 | ~LuaValue() 276 | { 277 | if (_type == Type::STRING) 278 | String.~basic_string(); 279 | else if (_type == Type::TABLE) 280 | Table.~shared_ptr(); 281 | else if (_type == Type::FUNCTION) 282 | Function.~shared_ptr(); 283 | } 284 | 285 | bool operator==(LuaValue const &rhs) const 286 | { 287 | if (_type != rhs._type) 288 | return false; 289 | 290 | switch (_type) 291 | { 292 | case Type::NIL: 293 | return true; 294 | case Type::INTEGER: 295 | return Integer == rhs.Integer; 296 | case Type::NUMBER: 297 | return Number == rhs.Number; 298 | case Type::BOOLEAN: 299 | return Boolean == rhs.Boolean; 300 | case Type::STRING: 301 | return String.compare(rhs.String) == 0; 302 | case Type::TABLE: 303 | return Table == rhs.Table; 304 | case Type::FUNCTION: 305 | return false; // functions are not comparable 306 | case Type::INVALID: 307 | return false; // invalid type is not comparable 308 | } 309 | 310 | return false; 311 | } 312 | 313 | public: 314 | inline Type GetType() const 315 | { 316 | return _type; 317 | } 318 | 319 | inline bool IsNil() const 320 | { 321 | return _type == Type::NIL; 322 | } 323 | 324 | inline bool IsInteger() const 325 | { 326 | return _type == Type::INTEGER; 327 | } 328 | 329 | inline bool IsNumber() const 330 | { 331 | return _type == Type::NUMBER; 332 | } 333 | 334 | inline bool IsBoolean() const 335 | { 336 | return _type == Type::BOOLEAN; 337 | } 338 | 339 | inline bool IsString() const 340 | { 341 | return _type == Type::STRING; 342 | } 343 | 344 | inline bool IsTable() const 345 | { 346 | return _type == Type::TABLE; 347 | } 348 | 349 | inline bool IsFunction() const 350 | { 351 | return _type == Type::FUNCTION; 352 | } 353 | 354 | 355 | template::value && !std::is_same::value, int>::type = 0> 357 | bool TryGetValue(T &dest) const 358 | { 359 | if (_type != Type::INTEGER) 360 | return false; 361 | 362 | dest = static_cast(Integer); 363 | return true; 364 | } 365 | 366 | bool TryGetValue(double &dest) const 367 | { 368 | if (_type != Type::NUMBER) 369 | return false; 370 | 371 | dest = static_cast(Number); 372 | return true; 373 | } 374 | 375 | bool TryGetValue(float &dest) const 376 | { 377 | if (_type != Type::NUMBER) 378 | return false; 379 | 380 | dest = static_cast(Number); 381 | return true; 382 | } 383 | 384 | bool TryGetValue(bool &dest) const 385 | { 386 | if (_type != Type::BOOLEAN) 387 | return false; 388 | 389 | dest = Boolean; 390 | return true; 391 | } 392 | 393 | bool TryGetValue(std::string &dest) const 394 | { 395 | if (_type != Type::STRING) 396 | return false; 397 | 398 | dest = String; 399 | return true; 400 | } 401 | 402 | bool TryGetValue(LuaTable_t &dest) const 403 | { 404 | if (_type != Type::TABLE) 405 | return false; 406 | 407 | dest = Table; 408 | return true; 409 | } 410 | 411 | bool TryGetValue(LuaFunction_t &dest) const 412 | { 413 | if (_type != Type::FUNCTION) 414 | return false; 415 | 416 | dest = Function; 417 | return true; 418 | } 419 | 420 | template 421 | T GetValue() const 422 | { 423 | T value; 424 | TryGetValue(value); 425 | return value; 426 | } 427 | }; 428 | 429 | static void PushValueToLua(LuaValue const &value, lua_State *state); 430 | static LuaValue ParseValueFromLua(lua_State *state, int index); 431 | } 432 | 433 | 434 | #ifdef _MSC_VER 435 | #pragma warning(pop) 436 | #endif 437 | -------------------------------------------------------------------------------- /include/sdk/LuaValueLuaImpl.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | namespace Lua 18 | { 19 | static void PushValueToLua(LuaValue const &value, lua_State *state) 20 | { 21 | switch (value.GetType()) 22 | { 23 | case LuaValue::Type::NIL: 24 | lua_pushnil(state); 25 | break; 26 | case LuaValue::Type::INTEGER: 27 | lua_pushinteger(state, static_cast(value.GetValue())); 28 | break; 29 | case LuaValue::Type::NUMBER: 30 | lua_pushnumber(state, static_cast(value.GetValue())); 31 | break; 32 | case LuaValue::Type::BOOLEAN: 33 | lua_pushboolean(state, value.GetValue()); 34 | break; 35 | case LuaValue::Type::STRING: 36 | { 37 | auto str = value.GetValue(); 38 | lua_pushlstring(state, str.c_str(), str.length()); 39 | } break; 40 | case LuaValue::Type::TABLE: 41 | value.GetValue()->PushToLua(state); 42 | break; 43 | case LuaValue::Type::FUNCTION: 44 | value.GetValue()->PushToLua(state); 45 | break; 46 | case LuaValue::Type::INVALID: 47 | // do nothing 48 | break; 49 | } 50 | } 51 | 52 | static LuaValue ParseValueFromLua(lua_State *state, int index) 53 | { 54 | switch (lua_type(state, index)) 55 | { 56 | case LUA_TNIL: 57 | return LuaValue(nullptr); 58 | case LUA_TNUMBER: 59 | if (lua_isinteger(state, index)) 60 | return LuaValue(lua_tointeger(state, index)); 61 | else 62 | return LuaValue(lua_tonumber(state, index)); 63 | case LUA_TBOOLEAN: 64 | return LuaValue(lua_toboolean(state, index) != 0); 65 | case LUA_TSTRING: 66 | return LuaValue(lua_tostring(state, index)); 67 | case LUA_TTABLE: 68 | { 69 | LuaTable_t table(new LuaTable); 70 | if (index < 0) 71 | index += lua_gettop(state) + 1; 72 | table->ParseFromLua(state, index); 73 | return LuaValue(table); 74 | } 75 | case LUA_TFUNCTION: 76 | { 77 | LuaFunction_t func(new LuaFunction(state)); 78 | func->ParseFromLua(index); 79 | return LuaValue(func); 80 | } 81 | default: 82 | luaL_error(state, "unsupported type %s in table", 83 | lua_typename(state, lua_type(state, index))); 84 | } 85 | return LuaValue(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /include/sdk/PluginApi.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 Blue Mountains GmbH 3 | 4 | This program is free software: you can redistribute it and/or modify it under the terms of the Onset 5 | Open Source License as published by Blue Mountains GmbH. 6 | 7 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 8 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | See the Onset Open Source License for more details. 10 | 11 | You should have received a copy of the Onset Open Source License along with this program. If not, 12 | see https://bluemountains.io/Onset_OpenSourceSoftware_License.txt 13 | */ 14 | 15 | #pragma once 16 | 17 | #include "LuaTypes.hpp" 18 | 19 | namespace Onset 20 | { 21 | class IBaseInterface 22 | { 23 | public: 24 | }; 25 | 26 | class IServerPlugin : public IBaseInterface 27 | { 28 | public: 29 | // Print message to console and server log file 30 | virtual void Log(const char *format, ...) = 0; 31 | 32 | // Get passed seconds since server start 33 | virtual double GetTimeSeconds() = 0; 34 | 35 | // Get server frame time 36 | virtual float GetDeltaSeconds() = 0; 37 | 38 | // Call an event in Lua which was defined by AddEvent 39 | virtual bool CallEvent(const char *EventName, Lua::LuaArgs_t *Arguments = nullptr) = 0; 40 | 41 | // Call a remote event 42 | virtual void CallRemoteEvent(const char *EventName, Lua::LuaArgs_t *Arguments = nullptr) = 0; 43 | 44 | virtual ~IServerPlugin() { } 45 | }; 46 | 47 | class Plugin 48 | { 49 | protected: 50 | static IServerPlugin *_instance; 51 | 52 | public: 53 | Plugin() = default; 54 | ~Plugin() = default; 55 | 56 | inline static void Init(IBaseInterface *base) 57 | { 58 | _instance = static_cast(base); 59 | } 60 | 61 | inline static IServerPlugin *Get() 62 | { 63 | return _instance; 64 | } 65 | 66 | inline static void Destroy() 67 | { 68 | if (_instance != nullptr) 69 | { 70 | delete _instance; 71 | _instance = nullptr; 72 | } 73 | } 74 | }; 75 | } 76 | -------------------------------------------------------------------------------- /lib/Lua.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlueMountainsIO/OnsetSDK/16d7b2ad5aa3ea1e41c5bc79be600a8bd45eaad8/lib/Lua.lib -------------------------------------------------------------------------------- /lib/libluaplugin.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlueMountainsIO/OnsetSDK/16d7b2ad5aa3ea1e41c5bc79be600a8bd45eaad8/lib/libluaplugin.a --------------------------------------------------------------------------------