diff options
Diffstat (limited to 'src/lua/libraries/luax/luax.h')
-rw-r--r-- | src/lua/libraries/luax/luax.h | 449 |
1 files changed, 0 insertions, 449 deletions
diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h deleted file mode 100644 index 0055176..0000000 --- a/src/lua/libraries/luax/luax.h +++ /dev/null @@ -1,449 +0,0 @@ -/** -* Copyright (C) 2018 chai -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is furnished -* to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ -/** -* include following 3 files before this -* lua.h -* lauxlib.h -* lualib.h -*/ -#ifndef __LUAX_H -#define __LUAX_H -// only for lua 5.1 -#if LUA_VERSION_NUM == 501 - -#define LUAX_VERSION "0.1.0" - -#define luax_newstate luaL_newstate -#define luax_close lua_close -#define luax_openlibs luaL_openlibs -// load chunk but dont run it -#define luax_loadbuffer luaL_loadbuffer -#define luax_dostring luaL_dostring -#define luax_pcall lua_pcall -#define luax_setglobal lua_setglobal -#define luax_setglobali(L, i, name)\ - lua_pushvalue(L, i);\ - lua_setglobal(L, name); -#define luax_pop lua_pop -#define luax_remove lua_remove -#define luax_newtable lua_newtable -#define luax_getglobal lua_getglobal - -#define luax_clearstack(L) lua_settop(L, 0) -/** -* -*/ -#define luax_setglobalstring(L, n, v) (lua_pushstring(L, v), lua_setglobal(L, n)) - -/** -* Get number of args -*/ -#define luax_gettop lua_gettop - -#define luax_gettable lua_gettable - -#define luax_settable lua_settable - -/** -* Check userdata type. -*/ -#define luax_checktype luaL_checkudata -#define luax_checknumber luaL_checknumber -#define luax_checkinteger luaL_checkinteger -#define luax_checkstring luaL_checkstring -#define luax_checklstring luaL_checklstring -#define luax_touserdata lua_touserdata -#define luax_tolightuserdata lua_touserdata -//#define luax_checkbool luaL_checkinteger -inline bool luax_checkbool(lua_State *L, int numArg) -{ - bool b = false; - if (lua_type(L, numArg) == LUA_TBOOLEAN) - { - b = lua_toboolean(L, numArg); - } - else - { - luaL_typerror(L, numArg, lua_typename(L, LUA_TBOOLEAN)); - } - return b; -} - -/** -* Oprating tables. -*/ -/* get value and leaves it on top of stack */ -#define luax_rawgetnumber(L, i, k) (lua_rawgeti(L,i, k), lua_tonumber(L, -1)) -inline float luax_rawgetnumberthenpop(lua_State* L, int i, int k) -{ - float n = luax_rawgetnumber(L, i, k); - luax_pop(L, 1); - return n; -} - -#define luax_rawgeti lua_rawgeti - -/** -* -*/ -#define luax_typerror luaL_typerror - - -/** -* Error checking -*/ -#define luax_error luaL_error - -/** -* Push value on the top of stack. -*/ -#define luax_pushnumber lua_pushnumber -#define luax_pushstring lua_pushstring -#define luax_pushlstring lua_pushlstring -#define luax_pushinteger lua_pushinteger -#define luax_pushboolean lua_pushboolean -#define luax_pushlightuserdata lua_pushlightuserdata -#define luax_pushnil lua_pushnil -#define luax_pushvalue lua_pushvalue -#define luax_pushliteral lua_pushliteral - -#define luax_setmetatable lua_setmetatable - -//inline void luax_pushuserdata(lua_State* L, void* p) -//{ -// /** -// * https://stackoverflow.com/questions/15038796/lua-c-api-push-existing-pointers -// */ -// void** box = (void**)lua_newuserdata(L, sizeof(p)); -// *box = p; -// -//} - -#define luax_rawseti lua_rawseti - -/** -* Set field -*/ -#define luax_setfield lua_setfield -#define luax_setfield_(T, L, k, v)\ - do { lua_push##T(L, v); lua_setfield(L, -2, k); } while (0) - -#define luax_setfieldnumber(L, k, v) luax_setfield_(number, L, k, v) -#define luax_setfieldinteger(L, k, v) luax_setfield_(integer, L, k, v) -#define luax_setfieldstring(L, k, v) luax_setfield_(string, L, k, v) -#define luax_setfieldbool(L, k, v) luax_setfield_(boolean, L, k, v) -#define luax_setfieldudata(L, k, v) luax_setfield_(lightuserdata, L, k, v) -#define luax_setfieldcfunc(L, k, v) luax_setfield_(cfunction, L, k, v) -#define luax_setfieldfstring(L, k, ...)\ - do { lua_pushfstring(L, __VA_ARGS__); lua_setfield(L, -2, k); } while (0) - -/** -* If nosuch field push a nil at the top of stack. -*/ -#define luax_getfield(L, I, N) lua_getfield(L, I, N) -inline float luax_getfieldnumber(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - float n = luax_checknumber(L, -1); - return n; -} -inline int luax_getfieldinteger(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - int bin = luax_checkinteger(L, -1); - return bin; -} -inline const char* luax_getfieldstring(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - const char* str = luax_checkstring(L, -1); - return str; -} -inline char luax_getfieldbool(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - char bin = lua_toboolean(L, -1); - return bin; -} -/** -* -*/ -#define luax_call lua_call - -/** -* Set raw -*/ -#define luax_setraw(T, L, idx, i, v)\ - (lua_push##T(L, v), lua_rawseti(L, idx, i)) - -#define luax_setrawstring(L, idx, i, v) luax_setraw(string, L, idx, i, v) -#define luax_setrawnumber(L, idx, i, v) luax_setraw(number, L, idx, i, v) -#define luax_setrawbool(L, idx, i, v) luax_setraw(boolean, L, idx, i, v) - -/** -* -*/ -#define luax_optboolean(L, i, x)\ - (!lua_isnoneornil(L, i) ? lua_toboolean(L, i) : (x)) -#define luax_optudata(L, i, name, x)\ - (!lua_isnoneornil(L, i) ? luaL_checkudata(L, i, name) : (x)) -#define luax_optnumber luaL_optnumber -#define luax_optinteger luaL_optinteger - -inline int luax_newlib(lua_State* L, const luaL_Reg* f) -{ - lua_createtable(L, 0, sizeof(f)); - for (; f && f->name; ++f) - { - lua_pushcfunction(L, f->func); - lua_setfield(L, -2, f->name); - } - return 1; // leave lib table on top of stack -} - -/** -* Register a userdefined lua type with given type name. -*/ -inline void luax_newtype(lua_State* L, const char* tname, const luaL_Reg* f) -{ - luaL_newmetatable(L, tname); - - // m.__index = m - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - - if (f != 0) - luaL_register(L, 0, f); - - lua_pop(L, 1); // Pops metatable. -} - -/** -* Instance of a type. -*/ -inline void* luax_newinstance(lua_State* L, const char* tname, int size) -{ - void* p = lua_newuserdata(L, size); - - luaL_newmetatable(L, tname); - lua_setmetatable(L, -2); - - return p; -} - -/** -* require a module -*/ -inline int luax_require(lua_State* L, const char* mname, lua_CFunction openf, int glb) -{ - openf(L); - if (glb) - { - lua_setglobal(L, mname); - return 0; - } - return 1; -} - -/** -* Return 1 if match. -*/ -inline int luax_istype(lua_State* L, int idx, const char* tname) -{ - if (lua_getmetatable(L, idx)) - { - /* get metatable called tname */ - lua_getfield(L, LUA_REGISTRYINDEX, tname); - if (lua_rawequal(L, -1, -2)) - { - lua_pop(L, 2); // pop both metatables - return 1; - } - lua_pop(L, 2); - return 0; - } - /* value at idx has no metatable */ - return 0; -} - -#define luax_is(T, L, i) (lua_is##T(L, i)) -#define luax_isnumber(L, i) luax_is(number, L, i) -#define luax_isstring(L, i) luax_is(string, L, i) -#define luax_istable(L, i) luax_is(table, L, i) -#define luax_isnil(L, i) luax_is(nil, L, i) -#define luax_isboolean(L, i) luax_is(boolean, L, i) -#define luax_isfunction(L, i) luax_is(function, L, i) -#define luax_isuserdata lua_isuserdata -#define luax_islightuserdata lua_islightuserdata -inline int luax_isinteger(lua_State* L, int i) -{ - if (!luax_isnumber(L, i)) - return 0; - return lua_tonumber(L, i) == lua_tointeger(L, i); -} -inline int luax_isfloat(lua_State* L, int i) -{ - if (!luax_isnumber(L, i)) - return 0; - return lua_tonumber(L, i) != lua_tointeger(L, i); -} - -#define luax_isnumberstrict(L, i) (lua_type(L, i) == LUA_TNUMBER) -#define luax_isstringstrict(L, i) (lua_type(L, i) == LUA_TSTRING) -#define luax_isbooleanstrict(L, i) (lua_type(L, i) == LUA_TBOOLEAN) -#define luax_istablestrict(L, i) (lua_type(L, i) == LUA_TTABLE) -#define luax_isnilstrict(L, i) (lua_type(L, i) == LUA_TNIL) -inline int luax_isintegerstrict(lua_State* L, int i) -{ - if (!luax_isnumberstrict(L, i)) - return 0; - return lua_tonumber(L, i) == lua_tointeger(L, i); -} -inline int luax_isfloatstrict(lua_State* L, int i) -{ - if (!luax_isnumberstrict(L, i)) - return 0; - return lua_tonumber(L, i) != lua_tointeger(L, i); -} - -/** -* To userdata. -*/ -inline void* luax_toudata(lua_State* L, int idx) -{ - void* p = lua_touserdata(L, idx); - if (p == NULL) - luaL_typerror(L, idx, "userdata"); // if p is not userdata - return p; -} - -/** -* Get table index size. -*/ -inline int luax_tableidxlen(lua_State* L, int i) -{ - return lua_objlen(L, i); -} - -/** -* Get table hash size -inline int luax_tbalehashlen(lua_State* L, int i) -{ - -} -*/ - -/** -* Get table hash and index size -inline int luax_tablelen(lua_State* L, int i) -{ - -} -*/ - -/* create a global tbale and stay it on the top of stack */ -#define luax_globaltable(L, name)\ -lua_newtable(L);\ -lua_pushvalue(L, 1);\ -lua_setglobal(L, name); - -inline int luax_tableinsert(lua_State * L, int tindex, int vindex, int pos) -{ - if (tindex < 0) - tindex = lua_gettop(L) + 1 + tindex; - if (vindex < 0) - vindex = lua_gettop(L) + 1 + vindex; - if (pos == -1) - { - lua_pushvalue(L, vindex); - lua_rawseti(L, tindex, lua_objlen(L, tindex) + 1); - return 0; - } - else if (pos < 0) - pos = lua_objlen(L, tindex) + 1 + pos; - for (int i = lua_objlen(L, tindex) + 1; i > pos; --i) - { - lua_rawgeti(L, tindex, i - 1); - lua_rawseti(L, tindex, i); - } - lua_pushvalue(L, vindex); - lua_rawseti(L, tindex, pos); - return 0; -} - -/** -* Add the package loader to the package.loaders table. -*/ -inline int luax_registersearcher(lua_State * L, lua_CFunction f, int pos) -{ - lua_getglobal(L, "package"); - - if (lua_isnil(L, -1)) - return luaL_error(L, "Can't register searcher: package table does not exist."); - - lua_getfield(L, -1, "loaders"); - - if (lua_isnil(L, -1)) - return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); - - lua_pushcfunction(L, f); - luax_tableinsert(L, -2, -1, pos); - lua_pop(L, 3); - return 0; -} - -typedef struct luax_Str -{ - const char* name; - const char* value; -} luax_Str; - -inline void luax_setfieldstrings(lua_State* L, const luax_Str* strs) -{ - for (int i = 0; strs[i].name != 0; ++i) - { - luax_setfieldstring(L, strs[i].name, strs[i].value); - } -} - -typedef struct luax_Num -{ - const char* name; - float number; -}; - -inline void luax_setfieldnumbers(lua_State* L, const luax_Num* strs) -{ - for (int i = 0; strs[i].name != 0; ++i) - { - luax_setfieldnumber(L, strs[i].name, strs[i].number); - } -} - -typedef luaL_Reg luax_Reg; - -#define luax_ref luaL_ref - -#define luax_unref luaL_unref - -#endif // #if LUA_VERSION_NUM == 501 - -#endif // __LUAX_H
\ No newline at end of file |