[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[5] | 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 |
|
---|
[319] | 11 | #include "nv/core/library.hh"
|
---|
[5] | 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
|
---|
[198] | 68 | static int (*lua_equal) (lua_State *L, int idx1, int idx2) = nullptr;
|
---|
| 69 | static int (*lua_lessthan) (lua_State *L, int idx1, int idx2) = nullptr;
|
---|
[166] | 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 ) \
|
---|
[198] | 77 | static rt call_##fn2##_compat fp { ret fn2##_compat arg; }
|
---|
[166] | 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 |
|
---|
[198] | 88 | static int lua_absindex_51 (lua_State *L, int idx) { return (idx > 0 ? idx : idx + lua_gettop(L) + 1); };
|
---|
| 89 | static void lua_getglobal_51 (lua_State *L, const char *var) { lua_getfield(L, LUA_GLOBALSINDEX_51, var ); };
|
---|
| 90 | static void lua_setglobal_51 (lua_State *L, const char *var) { lua_setfield(L, LUA_GLOBALSINDEX_51, var ); };
|
---|
| 91 | static void luaL_requiref_51 (lua_State *L, const char *modname, lua_CFunction openf, int glb)
|
---|
[166] | 92 | {
|
---|
| 93 | lua_pushcfunction(L, openf);
|
---|
| 94 | lua_pushstring(L, modname);
|
---|
| 95 | lua_call(L, 1, 1);
|
---|
| 96 | if (glb != 0)
|
---|
| 97 | {
|
---|
| 98 | lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
|
---|
| 99 | lua_pushvalue(L, -2);
|
---|
| 100 | lua_setfield(L, -2, modname);
|
---|
| 101 | lua_pop(L, 1);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[198] | 105 | static void luaL_setmetatable_51 (lua_State *L, const char *tname)
|
---|
[166] | 106 | {
|
---|
| 107 | luaL_getmetatable(L, tname);
|
---|
| 108 | lua_setmetatable(L, -2);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[198] | 111 | static void *luaL_testudata_51 (lua_State *L, int ud, const char *tname)
|
---|
[166] | 112 | {
|
---|
| 113 | void *p = lua_touserdata(L, ud);
|
---|
| 114 | if (p != NULL)
|
---|
| 115 | {
|
---|
| 116 | if (lua_getmetatable(L, ud))
|
---|
| 117 | {
|
---|
| 118 | luaL_getmetatable(L, tname);
|
---|
| 119 | if ( !lua_rawequal(L, -1, -2))
|
---|
| 120 | p = NULL;
|
---|
| 121 | lua_pop(L, 2);
|
---|
| 122 | return p;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | return NULL;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[198] | 128 | static const lua_Number *lua_version_51 (lua_State*)
|
---|
[166] | 129 | {
|
---|
| 130 | static const lua_Number version = (lua_Number)LUA_VERSION_NUM;
|
---|
| 131 | return &version;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[198] | 134 | static void lua_copy_51 (lua_State *L, int fromidx, int toidx)
|
---|
[166] | 135 | {
|
---|
| 136 | toidx = lua_absindex( L, toidx );
|
---|
| 137 | lua_pushvalue( L, fromidx );
|
---|
| 138 | lua_replace( L, toidx );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[198] | 141 | static int lua_compare_51(lua_State *L, int idx1, int idx2, int op)
|
---|
[166] | 142 | {
|
---|
| 143 | switch (op)
|
---|
| 144 | {
|
---|
| 145 | case LUA_OPEQ : return lua_equal( L, idx1, idx2 );
|
---|
| 146 | case LUA_OPLT : return lua_lessthan( L, idx1, idx2 );
|
---|
| 147 | case LUA_OPLE : return lua_lessthan( L, idx1, idx2 ) || lua_equal( L, idx1, idx2 );
|
---|
[172] | 148 | default:
|
---|
[166] | 149 | return 0;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[198] | 153 | static void lua_rawgetp_51(lua_State *L, int idx, const void *p)
|
---|
[166] | 154 | {
|
---|
| 155 | idx = lua_absindex( L, idx );
|
---|
| 156 | void* pp = const_cast<void *>(p); // EVIL
|
---|
| 157 | lua_pushlightuserdata( L, pp );
|
---|
| 158 | lua_rawget( L, idx );
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[198] | 161 | static void lua_rawsetp_51(lua_State *L, int idx, const void *p)
|
---|
[166] | 162 | {
|
---|
| 163 | idx = lua_absindex( L, idx );
|
---|
| 164 | void* pp = const_cast<void *>(p); // EVIL
|
---|
| 165 | lua_pushlightuserdata( L, pp );
|
---|
| 166 | lua_insert( L, -1 );
|
---|
| 167 | lua_rawset( L, idx );
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[198] | 170 | static int luaL_getsubtable_51(lua_State *L, int idx, const char *fname)
|
---|
[166] | 171 | {
|
---|
| 172 | lua_getfield(L, idx, fname);
|
---|
| 173 | if ( lua_istable(L, -1) ) return 1;
|
---|
| 174 | else {
|
---|
| 175 | idx = lua_absindex(L, idx);
|
---|
| 176 | lua_pop(L, 1);
|
---|
| 177 | lua_newtable(L);
|
---|
| 178 | lua_pushvalue(L, -1);
|
---|
| 179 | lua_setfield(L, idx, fname);
|
---|
| 180 | return 0;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[198] | 184 | static void luaL_setfuncs_51(lua_State *L, const luaL_Reg *l, int nup)
|
---|
[166] | 185 | {
|
---|
| 186 | luaL_checkstack(L, nup, "too many upvalues");
|
---|
| 187 | for (; l->name != NULL; l++)
|
---|
| 188 | {
|
---|
| 189 | for (int i = 0; i < nup; i++)
|
---|
| 190 | {
|
---|
| 191 | lua_pushvalue(L, -nup);
|
---|
| 192 | }
|
---|
| 193 | lua_pushcclosure(L, l->func, nup);
|
---|
| 194 | lua_setfield(L, -(nup + 2), l->name);
|
---|
| 195 | }
|
---|
| 196 | lua_pop(L, nup);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[198] | 199 | static void lua_pushglobaltable_51(lua_State* L)
|
---|
[166] | 200 | {
|
---|
| 201 | lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[198] | 204 | static void lua_pushglobaltable_52(lua_State* L)
|
---|
[173] | 205 | {
|
---|
| 206 | lua_rawgeti( L, LUA_REGISTRYINDEX_52, LUA_GLOBALSINDEX_52 );
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[166] | 209 | #endif
|
---|
| 210 |
|
---|
[5] | 211 | bool nv::load_lua_library( const char* path )
|
---|
| 212 | {
|
---|
[109] | 213 | static nv::library lua_library;
|
---|
| 214 | if ( lua_library.is_open() ) return true;
|
---|
[166] | 215 | #if NV_LUA_VERSION == NV_LUA_5C
|
---|
| 216 | if ( path == nullptr )
|
---|
| 217 | {
|
---|
| 218 | if (!lua_library.try_open( NV_LUA_PATH_JIT ) &&
|
---|
| 219 | !lua_library.try_open( NV_LUA_PATH_52 ) )
|
---|
| 220 | {
|
---|
| 221 | lua_library.open( NV_LUA_PATH_51 );
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | else
|
---|
| 225 | #else
|
---|
| 226 | lua_library.open( path );
|
---|
| 227 | #endif
|
---|
[5] | 228 |
|
---|
[164] | 229 | # define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname);
|
---|
| 230 | # if NV_LUA_VERSION == NV_LUA_52
|
---|
| 231 | # define NV_LUA_FUN_51( rtype, fname, fparams )
|
---|
| 232 | # define NV_LUA_FUN_52 NV_LUA_FUN
|
---|
[165] | 233 | # elif NV_LUA_VERSION == NV_LUA_51
|
---|
[164] | 234 | # define NV_LUA_FUN_51 NV_LUA_FUN
|
---|
| 235 | # define NV_LUA_FUN_52( rtype, fname, fparams )
|
---|
[165] | 236 | # else
|
---|
| 237 | # define NV_LUA_FUN_51( rtype, fname, fparams )
|
---|
| 238 | # define NV_LUA_FUN_52( rtype, fname, fparams )
|
---|
[164] | 239 | # endif
|
---|
[109] | 240 |
|
---|
[165] | 241 | # include <nv/lib/detail/lua_functions.inc>
|
---|
[5] | 242 |
|
---|
[165] | 243 | # undef NV_LUA_FUN
|
---|
| 244 | # undef NV_LUA_FUN_51
|
---|
| 245 | # undef NV_LUA_FUN_52
|
---|
[166] | 246 |
|
---|
| 247 | #if NV_LUA_VERSION == NV_LUA_5C
|
---|
| 248 | # define NV_LUA_LOAD( fname ) *(void **) (&fname) = lua_library.get(#fname);
|
---|
| 249 | # define NV_LUA_LOAD_AS( fname,fname2 ) *(void **) (&fname) = lua_library.get(#fname2);
|
---|
| 250 | bool version_52 = lua_library.try_get("luaL_checkversion_") != nullptr;
|
---|
| 251 | if (version_52)
|
---|
| 252 | {
|
---|
| 253 | # define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, fn2, u5, u6, u7 ) \
|
---|
| 254 | *(void **) (&(fn2##_compat)) = lua_library.get(#fn2); \
|
---|
| 255 | fn = call_##fn2##_compat;
|
---|
| 256 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 257 | # undef NV_LUA_COMPAT_FUN
|
---|
| 258 | NV_LUA_LOAD( lua_rawlen );
|
---|
| 259 | NV_LUA_LOAD( lua_absindex );
|
---|
| 260 | NV_LUA_LOAD( lua_getglobal );
|
---|
| 261 | NV_LUA_LOAD( lua_setglobal );
|
---|
| 262 | NV_LUA_LOAD( luaL_setmetatable );
|
---|
| 263 | NV_LUA_LOAD( luaL_testudata );
|
---|
| 264 | NV_LUA_LOAD( lua_version );
|
---|
| 265 | NV_LUA_LOAD( lua_copy );
|
---|
| 266 | NV_LUA_LOAD( lua_compare );
|
---|
| 267 | NV_LUA_LOAD( lua_rawgetp );
|
---|
| 268 | NV_LUA_LOAD( lua_rawsetp );
|
---|
| 269 | NV_LUA_LOAD( luaL_setfuncs );
|
---|
| 270 | NV_LUA_LOAD( luaL_getsubtable );
|
---|
[173] | 271 | NV_LUA_LOAD( luaL_requiref );
|
---|
[166] | 272 |
|
---|
[173] | 273 | lua_pushglobaltable = lua_pushglobaltable_52;
|
---|
| 274 |
|
---|
[172] | 275 | LUA_UPVALUEINDEX = LUA_REGISTRYINDEX_52;
|
---|
[166] | 276 | LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_52;
|
---|
| 277 | LUA_VERSION_NUM = 502;
|
---|
| 278 | }
|
---|
| 279 | else
|
---|
| 280 | {
|
---|
| 281 | # define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, u4, u5, u6, u7 ) \
|
---|
| 282 | *(void **) (&fn) = lua_library.get(#fn);
|
---|
| 283 | # include <nv/lib/detail/lua_functions_compat.inc>
|
---|
| 284 | # undef NV_LUA_COMPAT_FUN
|
---|
| 285 | NV_LUA_LOAD_AS( lua_rawlen, lua_objlen )
|
---|
| 286 | lua_absindex = lua_absindex_51;
|
---|
| 287 | lua_getglobal = lua_getglobal_51;
|
---|
| 288 | lua_setglobal = lua_setglobal_51;
|
---|
| 289 | luaL_setmetatable = luaL_setmetatable_51;
|
---|
| 290 | luaL_testudata = luaL_testudata_51;
|
---|
| 291 | lua_version = lua_version_51;
|
---|
| 292 | lua_copy = lua_copy_51;
|
---|
| 293 | lua_rawgetp = lua_rawgetp_51;
|
---|
| 294 | lua_rawsetp = lua_rawsetp_51;
|
---|
| 295 | lua_pushglobaltable = lua_pushglobaltable_51;
|
---|
| 296 | luaL_setfuncs = luaL_setfuncs_51;
|
---|
| 297 | luaL_getsubtable = luaL_getsubtable_51;
|
---|
[173] | 298 | luaL_requiref = luaL_requiref_51;
|
---|
[166] | 299 |
|
---|
| 300 | NV_LUA_LOAD( lua_lessthan );
|
---|
| 301 | NV_LUA_LOAD( lua_equal );
|
---|
| 302 | lua_compare = lua_compare_51;
|
---|
[172] | 303 | LUA_UPVALUEINDEX = LUA_GLOBALSINDEX_51;
|
---|
[166] | 304 | LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_51;
|
---|
| 305 | LUA_VERSION_NUM = 501;
|
---|
| 306 | }
|
---|
| 307 | # undef NV_LUA_LOAD
|
---|
| 308 | #endif
|
---|
| 309 |
|
---|
[5] | 310 | return true;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | #endif
|
---|