[5] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/lib/lua.hh"
|
---|
| 8 |
|
---|
| 9 | #if defined( NV_LUA_DYNAMIC )
|
---|
| 10 |
|
---|
| 11 | #include "nv/library.hh"
|
---|
| 12 |
|
---|
[164] | 13 | #if NV_LUA_VERSION == NV_LUA_52
|
---|
| 14 | # undef luaL_loadfile
|
---|
| 15 | # undef luaL_loadbuffer
|
---|
| 16 | # undef luaL_prepbuffer
|
---|
| 17 | # undef lua_tonumber
|
---|
| 18 | # undef lua_tointeger
|
---|
| 19 | # undef lua_tounsigned
|
---|
| 20 | # undef lua_call
|
---|
| 21 | # undef lua_pcall
|
---|
| 22 | # undef lua_yield
|
---|
| 23 | #endif
|
---|
[5] | 24 |
|
---|
[164] | 25 | #define NV_LUA_FUN( rtype, fname, fparams ) rtype (*fname) fparams = nullptr;
|
---|
| 26 | #if NV_LUA_VERSION == NV_LUA_52
|
---|
| 27 | # define NV_LUA_FUN_51( rtype, fname, fparams )
|
---|
| 28 | # define NV_LUA_FUN_52 NV_LUA_FUN
|
---|
[165] | 29 | #elif NV_LUA_VERSION == NV_LUA_51
|
---|
| 30 | # define NV_LUA_FUN_51 NV_LUA_FUN
|
---|
| 31 | # define NV_LUA_FUN_52( rtype, fname, fparams )
|
---|
[164] | 32 | #else
|
---|
[165] | 33 | # define NV_LUA_FUN_51( rtype, fname, fparams )
|
---|
| 34 | # define NV_LUA_FUN_52( rtype, fname, fparams )
|
---|
[164] | 35 | #endif
|
---|
[5] | 36 |
|
---|
[164] | 37 | #include <nv/lib/detail/lua_functions.inc>
|
---|
[5] | 38 |
|
---|
[164] | 39 | #undef NV_LUA_FUN
|
---|
| 40 | #undef NV_LUA_FUN_51
|
---|
| 41 | #undef NV_LUA_FUN_52
|
---|
[5] | 42 |
|
---|
[166] | 43 | #if NV_LUA_VERSION == NV_LUA_5C
|
---|
| 44 | # define NV_LUA_COMPAT_FUN( rt, fn, fp,u1,u2,u3,u4,u5 ) rt (*fn) fp = nullptr;
|
---|
| 45 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 46 | # undef NV_LUA_COMPAT_FUN
|
---|
[172] | 47 | int LUA_UPVALUEINDEX = 0;
|
---|
[166] | 48 | int LUA_REGISTRYINDEX = 0;
|
---|
| 49 | int LUA_VERSION_NUM = 0;
|
---|
| 50 | size_t (*lua_rawlen) (lua_State *L, int idx) = nullptr;
|
---|
| 51 | int (*lua_absindex) (lua_State *L, int idx) = nullptr;
|
---|
| 52 | void (*lua_getglobal) (lua_State *L, const char *var) = nullptr;
|
---|
| 53 | void (*lua_setglobal) (lua_State *L, const char *var) = nullptr;
|
---|
| 54 | void (*luaL_requiref) (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr;
|
---|
| 55 | void (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr;
|
---|
| 56 | void* (*luaL_testudata) (lua_State *L, int ud, const char *tname) = nullptr;
|
---|
| 57 | void (*lua_copy) (lua_State *L, int fromidx, int toidx) = nullptr;
|
---|
| 58 | int (*lua_compare) (lua_State *L, int idx1, int idx2, int op) = nullptr;
|
---|
| 59 | void (*lua_rawgetp) (lua_State *L, int idx, const void *p) = nullptr;
|
---|
| 60 | void (*lua_rawsetp) (lua_State *L, int idx, const void *p) = nullptr;
|
---|
| 61 | void (*lua_pushglobaltable)(lua_State* L) = nullptr;
|
---|
| 62 | void (*luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup) = nullptr;
|
---|
| 63 | int (*luaL_getsubtable) (lua_State *L, int idx, const char *fname) = nullptr;
|
---|
| 64 |
|
---|
| 65 | const lua_Number* (*lua_version) (lua_State *L) = nullptr;
|
---|
| 66 |
|
---|
| 67 | // only loaded in 5.1 mode to implement lua_compare
|
---|
| 68 | int (*lua_equal) (lua_State *L, int idx1, int idx2) = nullptr;
|
---|
| 69 | int (*lua_lessthan) (lua_State *L, int idx1, int idx2) = nullptr;
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | # define NV_LUA_COMPAT_FUN( u1,u2,u3,rt2,fn2,fp2,u4,u5 ) static rt2 (*fn2##_compat) fp2 = nullptr;
|
---|
| 73 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 74 | # undef NV_LUA_COMPAT_FUN
|
---|
| 75 |
|
---|
| 76 | # define NV_LUA_COMPAT_FUN( rt, fn, fp, rt2, fn2, fp2,arg,ret ) \
|
---|
| 77 | rt call_##fn2##_compat fp { ret fn2##_compat arg; }
|
---|
| 78 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 79 | # undef NV_LUA_COMPAT_FUN
|
---|
| 80 |
|
---|
| 81 | # define LUAI_MAXSTACK_52 1000000
|
---|
| 82 | # define LUAI_FIRSTPSEUDOIDX_52 (-LUAI_MAXSTACK_52 - 1000)
|
---|
| 83 | # define LUA_REGISTRYINDEX_52 LUAI_FIRSTPSEUDOIDX_52
|
---|
| 84 | # define LUA_GLOBALSINDEX_51 (-10002)
|
---|
[173] | 85 | # define LUA_GLOBALSINDEX_52 2
|
---|
[166] | 86 | # define LUA_REGISTRYINDEX_51 (-10000)
|
---|
| 87 | # define lua_upvalueindex_51(i) (LUA_GLOBALSINDEX_51-(i))
|
---|
| 88 | # define lua_upvalueindex_52(i) (LUA_REGISTRYINDEX_52-(i))
|
---|
| 89 |
|
---|
| 90 | int lua_absindex_51 (lua_State *L, int idx) { return (idx > 0 ? idx : idx + lua_gettop(L) + 1); };
|
---|
| 91 | void lua_getglobal_51 (lua_State *L, const char *var) { lua_getfield(L, LUA_GLOBALSINDEX_51, var ); };
|
---|
| 92 | void lua_setglobal_51 (lua_State *L, const char *var) { lua_setfield(L, LUA_GLOBALSINDEX_51, var ); };
|
---|
| 93 | void luaL_requiref_51 (lua_State *L, const char *modname, lua_CFunction openf, int glb)
|
---|
| 94 | {
|
---|
| 95 | lua_pushcfunction(L, openf);
|
---|
| 96 | lua_pushstring(L, modname);
|
---|
| 97 | lua_call(L, 1, 1);
|
---|
| 98 | if (glb != 0)
|
---|
| 99 | {
|
---|
| 100 | lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
|
---|
| 101 | lua_pushvalue(L, -2);
|
---|
| 102 | lua_setfield(L, -2, modname);
|
---|
| 103 | lua_pop(L, 1);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void luaL_setmetatable_51 (lua_State *L, const char *tname)
|
---|
| 108 | {
|
---|
| 109 | luaL_getmetatable(L, tname);
|
---|
| 110 | lua_setmetatable(L, -2);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void *luaL_testudata_51 (lua_State *L, int ud, const char *tname)
|
---|
| 114 | {
|
---|
| 115 | void *p = lua_touserdata(L, ud);
|
---|
| 116 | if (p != NULL)
|
---|
| 117 | {
|
---|
| 118 | if (lua_getmetatable(L, ud))
|
---|
| 119 | {
|
---|
| 120 | luaL_getmetatable(L, tname);
|
---|
| 121 | if ( !lua_rawequal(L, -1, -2))
|
---|
| 122 | p = NULL;
|
---|
| 123 | lua_pop(L, 2);
|
---|
| 124 | return p;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | return NULL;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | const lua_Number *lua_version_51 (lua_State*)
|
---|
| 131 | {
|
---|
| 132 | static const lua_Number version = (lua_Number)LUA_VERSION_NUM;
|
---|
| 133 | return &version;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void lua_copy_51 (lua_State *L, int fromidx, int toidx)
|
---|
| 137 | {
|
---|
| 138 | toidx = lua_absindex( L, toidx );
|
---|
| 139 | lua_pushvalue( L, fromidx );
|
---|
| 140 | lua_replace( L, toidx );
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | int lua_compare_51(lua_State *L, int idx1, int idx2, int op)
|
---|
| 144 | {
|
---|
| 145 | switch (op)
|
---|
| 146 | {
|
---|
| 147 | case LUA_OPEQ : return lua_equal( L, idx1, idx2 );
|
---|
| 148 | case LUA_OPLT : return lua_lessthan( L, idx1, idx2 );
|
---|
| 149 | case LUA_OPLE : return lua_lessthan( L, idx1, idx2 ) || lua_equal( L, idx1, idx2 );
|
---|
[172] | 150 | default:
|
---|
[166] | 151 | return 0;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void lua_rawgetp_51(lua_State *L, int idx, const void *p)
|
---|
| 156 | {
|
---|
| 157 | idx = lua_absindex( L, idx );
|
---|
| 158 | void* pp = const_cast<void *>(p); // EVIL
|
---|
| 159 | lua_pushlightuserdata( L, pp );
|
---|
| 160 | lua_rawget( L, idx );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void lua_rawsetp_51(lua_State *L, int idx, const void *p)
|
---|
| 164 | {
|
---|
| 165 | idx = lua_absindex( L, idx );
|
---|
| 166 | void* pp = const_cast<void *>(p); // EVIL
|
---|
| 167 | lua_pushlightuserdata( L, pp );
|
---|
| 168 | lua_insert( L, -1 );
|
---|
| 169 | lua_rawset( L, idx );
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | int luaL_getsubtable_51(lua_State *L, int idx, const char *fname)
|
---|
| 173 | {
|
---|
| 174 | lua_getfield(L, idx, fname);
|
---|
| 175 | if ( lua_istable(L, -1) ) return 1;
|
---|
| 176 | else {
|
---|
| 177 | idx = lua_absindex(L, idx);
|
---|
| 178 | lua_pop(L, 1);
|
---|
| 179 | lua_newtable(L);
|
---|
| 180 | lua_pushvalue(L, -1);
|
---|
| 181 | lua_setfield(L, idx, fname);
|
---|
| 182 | return 0;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | void luaL_setfuncs_51(lua_State *L, const luaL_Reg *l, int nup)
|
---|
| 187 | {
|
---|
| 188 | luaL_checkstack(L, nup, "too many upvalues");
|
---|
| 189 | for (; l->name != NULL; l++)
|
---|
| 190 | {
|
---|
| 191 | for (int i = 0; i < nup; i++)
|
---|
| 192 | {
|
---|
| 193 | lua_pushvalue(L, -nup);
|
---|
| 194 | }
|
---|
| 195 | lua_pushcclosure(L, l->func, nup);
|
---|
| 196 | lua_setfield(L, -(nup + 2), l->name);
|
---|
| 197 | }
|
---|
| 198 | lua_pop(L, nup);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | void lua_pushglobaltable_51(lua_State* L)
|
---|
| 202 | {
|
---|
| 203 | lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[173] | 206 | void lua_pushglobaltable_52(lua_State* L)
|
---|
| 207 | {
|
---|
| 208 | lua_rawgeti( L, LUA_REGISTRYINDEX_52, LUA_GLOBALSINDEX_52 );
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[166] | 211 | #endif
|
---|
| 212 |
|
---|
[5] | 213 | bool nv::load_lua_library( const char* path )
|
---|
| 214 | {
|
---|
[109] | 215 | static nv::library lua_library;
|
---|
| 216 | if ( lua_library.is_open() ) return true;
|
---|
[166] | 217 | #if NV_LUA_VERSION == NV_LUA_5C
|
---|
| 218 | if ( path == nullptr )
|
---|
| 219 | {
|
---|
| 220 | if (!lua_library.try_open( NV_LUA_PATH_JIT ) &&
|
---|
| 221 | !lua_library.try_open( NV_LUA_PATH_52 ) )
|
---|
| 222 | {
|
---|
| 223 | lua_library.open( NV_LUA_PATH_51 );
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | else
|
---|
| 227 | #else
|
---|
| 228 | lua_library.open( path );
|
---|
| 229 | #endif
|
---|
[5] | 230 |
|
---|
[164] | 231 | # define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname);
|
---|
| 232 | # if NV_LUA_VERSION == NV_LUA_52
|
---|
| 233 | # define NV_LUA_FUN_51( rtype, fname, fparams )
|
---|
| 234 | # define NV_LUA_FUN_52 NV_LUA_FUN
|
---|
[165] | 235 | # elif NV_LUA_VERSION == NV_LUA_51
|
---|
[164] | 236 | # define NV_LUA_FUN_51 NV_LUA_FUN
|
---|
| 237 | # define NV_LUA_FUN_52( rtype, fname, fparams )
|
---|
[165] | 238 | # else
|
---|
| 239 | # define NV_LUA_FUN_51( rtype, fname, fparams )
|
---|
| 240 | # define NV_LUA_FUN_52( rtype, fname, fparams )
|
---|
[164] | 241 | # endif
|
---|
[109] | 242 |
|
---|
[165] | 243 | # include <nv/lib/detail/lua_functions.inc>
|
---|
[5] | 244 |
|
---|
[165] | 245 | # undef NV_LUA_FUN
|
---|
| 246 | # undef NV_LUA_FUN_51
|
---|
| 247 | # undef NV_LUA_FUN_52
|
---|
[166] | 248 |
|
---|
| 249 | #if NV_LUA_VERSION == NV_LUA_5C
|
---|
| 250 | # define NV_LUA_LOAD( fname ) *(void **) (&fname) = lua_library.get(#fname);
|
---|
| 251 | # define NV_LUA_LOAD_AS( fname,fname2 ) *(void **) (&fname) = lua_library.get(#fname2);
|
---|
| 252 | bool version_52 = lua_library.try_get("luaL_checkversion_") != nullptr;
|
---|
| 253 | if (version_52)
|
---|
| 254 | {
|
---|
| 255 | # define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, fn2, u5, u6, u7 ) \
|
---|
| 256 | *(void **) (&(fn2##_compat)) = lua_library.get(#fn2); \
|
---|
| 257 | fn = call_##fn2##_compat;
|
---|
| 258 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 259 | # undef NV_LUA_COMPAT_FUN
|
---|
| 260 | NV_LUA_LOAD( lua_rawlen );
|
---|
| 261 | NV_LUA_LOAD( lua_absindex );
|
---|
| 262 | NV_LUA_LOAD( lua_getglobal );
|
---|
| 263 | NV_LUA_LOAD( lua_setglobal );
|
---|
| 264 | NV_LUA_LOAD( luaL_setmetatable );
|
---|
| 265 | NV_LUA_LOAD( luaL_testudata );
|
---|
| 266 | NV_LUA_LOAD( lua_version );
|
---|
| 267 | NV_LUA_LOAD( lua_copy );
|
---|
| 268 | NV_LUA_LOAD( lua_compare );
|
---|
| 269 | NV_LUA_LOAD( lua_rawgetp );
|
---|
| 270 | NV_LUA_LOAD( lua_rawsetp );
|
---|
| 271 | NV_LUA_LOAD( luaL_setfuncs );
|
---|
| 272 | NV_LUA_LOAD( luaL_getsubtable );
|
---|
[173] | 273 | NV_LUA_LOAD( luaL_requiref );
|
---|
[166] | 274 |
|
---|
[173] | 275 | lua_pushglobaltable = lua_pushglobaltable_52;
|
---|
| 276 |
|
---|
[172] | 277 | LUA_UPVALUEINDEX = LUA_REGISTRYINDEX_52;
|
---|
[166] | 278 | LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_52;
|
---|
| 279 | LUA_VERSION_NUM = 502;
|
---|
| 280 | }
|
---|
| 281 | else
|
---|
| 282 | {
|
---|
| 283 | # define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, u4, u5, u6, u7 ) \
|
---|
| 284 | *(void **) (&fn) = lua_library.get(#fn);
|
---|
| 285 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 286 | # undef NV_LUA_COMPAT_FUN
|
---|
| 287 | NV_LUA_LOAD_AS( lua_rawlen, lua_objlen )
|
---|
| 288 | lua_absindex = lua_absindex_51;
|
---|
| 289 | lua_getglobal = lua_getglobal_51;
|
---|
| 290 | lua_setglobal = lua_setglobal_51;
|
---|
| 291 | luaL_setmetatable = luaL_setmetatable_51;
|
---|
| 292 | luaL_testudata = luaL_testudata_51;
|
---|
| 293 | lua_version = lua_version_51;
|
---|
| 294 | lua_copy = lua_copy_51;
|
---|
| 295 | lua_rawgetp = lua_rawgetp_51;
|
---|
| 296 | lua_rawsetp = lua_rawsetp_51;
|
---|
| 297 | lua_pushglobaltable = lua_pushglobaltable_51;
|
---|
| 298 | luaL_setfuncs = luaL_setfuncs_51;
|
---|
| 299 | luaL_getsubtable = luaL_getsubtable_51;
|
---|
[173] | 300 | luaL_requiref = luaL_requiref_51;
|
---|
[166] | 301 |
|
---|
| 302 | NV_LUA_LOAD( lua_lessthan );
|
---|
| 303 | NV_LUA_LOAD( lua_equal );
|
---|
| 304 | lua_compare = lua_compare_51;
|
---|
[172] | 305 | LUA_UPVALUEINDEX = LUA_GLOBALSINDEX_51;
|
---|
[166] | 306 | LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_51;
|
---|
| 307 | LUA_VERSION_NUM = 501;
|
---|
| 308 | }
|
---|
| 309 | # undef NV_LUA_LOAD
|
---|
| 310 | #endif
|
---|
| 311 |
|
---|
[5] | 312 | return true;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | #endif
|
---|