source: trunk/src/lib/lua.cc @ 166

Last change on this file since 166 was 166, checked in by epyon, 12 years ago
  • library - try_open and try_get functions added for non-throwing access
  • lua - compatibility layer mode added - the whole system can bind to lua 5.1, 5.2 or LuaJIT as a *RUNTIME* choice
File size: 9.1 KB
Line 
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
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
24
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
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 )
32#else
33#       define NV_LUA_FUN_51( rtype, fname, fparams )
34#       define NV_LUA_FUN_52( rtype, fname, fparams )
35#endif
36
37#include <nv/lib/detail/lua_functions.inc>
38
39#undef NV_LUA_FUN
40#undef NV_LUA_FUN_51
41#undef NV_LUA_FUN_52
42
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
47int LUA_REGISTRYINDEX = 0;
48int LUA_VERSION_NUM   = 0;
49size_t (*lua_rawlen)        (lua_State *L, int idx) = nullptr;
50int    (*lua_absindex)      (lua_State *L, int idx) = nullptr;
51void   (*lua_getglobal)     (lua_State *L, const char *var) = nullptr;
52void   (*lua_setglobal)     (lua_State *L, const char *var) = nullptr;
53void   (*luaL_requiref)     (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr;
54void   (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr;
55void*  (*luaL_testudata)    (lua_State *L, int ud, const char *tname) = nullptr;
56void   (*lua_copy)          (lua_State *L, int fromidx, int toidx) = nullptr;
57int    (*lua_compare)       (lua_State *L, int idx1, int idx2, int op) = nullptr;
58void   (*lua_rawgetp)       (lua_State *L, int idx, const void *p) = nullptr;
59void   (*lua_rawsetp)       (lua_State *L, int idx, const void *p) = nullptr;
60void   (*lua_pushglobaltable)(lua_State* L) = nullptr;
61void   (*luaL_setfuncs)     (lua_State *L, const luaL_Reg *l, int nup) = nullptr;
62int    (*luaL_getsubtable)  (lua_State *L, int idx, const char *fname) = nullptr;
63
64const lua_Number* (*lua_version) (lua_State *L) = nullptr;
65
66// only loaded in 5.1 mode to implement lua_compare
67int    (*lua_equal)        (lua_State *L, int idx1, int idx2) = nullptr;
68int    (*lua_lessthan)     (lua_State *L, int idx1, int idx2) = nullptr;
69
70
71#       define NV_LUA_COMPAT_FUN( u1,u2,u3,rt2,fn2,fp2,u4,u5 ) static rt2 (*fn2##_compat) fp2 = nullptr;
72#       include <nv/lib/detail/lua_functions_compat.inc>
73#       undef NV_LUA_COMPAT_FUN
74
75#       define NV_LUA_COMPAT_FUN( rt, fn, fp, rt2, fn2, fp2,arg,ret ) \
76                rt call_##fn2##_compat fp { ret fn2##_compat arg; }
77#       include <nv/lib/detail/lua_functions_compat.inc>
78#       undef NV_LUA_COMPAT_FUN
79
80#       define LUAI_MAXSTACK_52             1000000
81#       define LUAI_FIRSTPSEUDOIDX_52   (-LUAI_MAXSTACK_52 - 1000)
82#       define LUA_REGISTRYINDEX_52         LUAI_FIRSTPSEUDOIDX_52
83#       define LUA_GLOBALSINDEX_51      (-10002)
84#       define LUA_REGISTRYINDEX_51     (-10000)
85#       define lua_upvalueindex_51(i)   (LUA_GLOBALSINDEX_51-(i))
86#       define lua_upvalueindex_52(i)   (LUA_REGISTRYINDEX_52-(i))
87
88int lua_absindex_51 (lua_State *L, int idx) { return (idx > 0 ? idx : idx + lua_gettop(L) + 1); };
89void lua_getglobal_51 (lua_State *L, const char *var) { lua_getfield(L, LUA_GLOBALSINDEX_51, var ); };
90void lua_setglobal_51 (lua_State *L, const char *var) { lua_setfield(L, LUA_GLOBALSINDEX_51, var ); };
91void luaL_requiref_51 (lua_State *L, const char *modname, lua_CFunction openf, int glb)
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
105void luaL_setmetatable_51 (lua_State *L, const char *tname)
106{
107        luaL_getmetatable(L, tname);
108        lua_setmetatable(L, -2);
109}
110
111void *luaL_testudata_51 (lua_State *L, int ud, const char *tname)
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
128const lua_Number *lua_version_51 (lua_State*)
129{
130        static const lua_Number version = (lua_Number)LUA_VERSION_NUM;
131        return &version;
132}
133
134void lua_copy_51 (lua_State *L, int fromidx, int toidx)
135{
136        toidx = lua_absindex( L, toidx );
137        lua_pushvalue( L, fromidx );
138        lua_replace( L, toidx );
139}
140
141int lua_compare_51(lua_State *L, int idx1, int idx2, int op)
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 );
148        default:
149                return 0;
150        }
151}
152
153void lua_rawgetp_51(lua_State *L, int idx, const void *p)
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
161void lua_rawsetp_51(lua_State *L, int idx, const void *p)
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
170int luaL_getsubtable_51(lua_State *L, int idx, const char *fname)
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
184void luaL_setfuncs_51(lua_State *L, const luaL_Reg *l, int nup)
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
199void lua_pushglobaltable_51(lua_State* L)
200{
201        lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
202}
203
204#endif
205
206bool nv::load_lua_library( const char* path )
207{
208        static nv::library lua_library;
209        if ( lua_library.is_open() ) return true;
210#if NV_LUA_VERSION == NV_LUA_5C
211        if ( path == nullptr )
212        {
213                if (!lua_library.try_open( NV_LUA_PATH_JIT ) &&
214                        !lua_library.try_open( NV_LUA_PATH_52 ) )
215                {
216                        lua_library.open( NV_LUA_PATH_51 );
217                }
218        }
219        else
220#else
221                lua_library.open( path );
222#endif
223
224#       define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname);
225#       if NV_LUA_VERSION == NV_LUA_52
226#               define NV_LUA_FUN_51( rtype, fname, fparams )
227#               define NV_LUA_FUN_52 NV_LUA_FUN
228#       elif NV_LUA_VERSION == NV_LUA_51
229#               define NV_LUA_FUN_51 NV_LUA_FUN
230#               define NV_LUA_FUN_52( rtype, fname, fparams )
231#       else
232#               define NV_LUA_FUN_51( rtype, fname, fparams )
233#               define NV_LUA_FUN_52( rtype, fname, fparams )
234#       endif
235
236#       include <nv/lib/detail/lua_functions.inc>
237
238#       undef NV_LUA_FUN
239#       undef NV_LUA_FUN_51
240#       undef NV_LUA_FUN_52
241
242#if NV_LUA_VERSION == NV_LUA_5C
243#       define NV_LUA_LOAD( fname ) *(void **) (&fname) = lua_library.get(#fname);
244#       define NV_LUA_LOAD_AS( fname,fname2 ) *(void **) (&fname) = lua_library.get(#fname2);
245        bool version_52 = lua_library.try_get("luaL_checkversion_") != nullptr;
246        if (version_52)
247        {
248#       define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, fn2, u5, u6, u7 ) \
249        *(void **) (&(fn2##_compat)) = lua_library.get(#fn2); \
250        fn = call_##fn2##_compat;
251#       include <nv/lib/detail/lua_functions_compat.inc>
252#       undef NV_LUA_COMPAT_FUN
253                NV_LUA_LOAD( lua_rawlen );
254                NV_LUA_LOAD( lua_absindex );
255                NV_LUA_LOAD( lua_getglobal );
256                NV_LUA_LOAD( lua_setglobal );
257                NV_LUA_LOAD( luaL_setmetatable );
258                NV_LUA_LOAD( luaL_testudata );
259                NV_LUA_LOAD( lua_version );
260                NV_LUA_LOAD( lua_copy );
261                NV_LUA_LOAD( lua_compare );
262                NV_LUA_LOAD( lua_rawgetp );
263                NV_LUA_LOAD( lua_rawsetp );
264                NV_LUA_LOAD( lua_pushglobaltable );
265                NV_LUA_LOAD( luaL_setfuncs );
266                NV_LUA_LOAD( luaL_getsubtable );
267
268                LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_52;
269                LUA_VERSION_NUM   = 502;
270        }
271        else
272        {
273#       define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, u4, u5, u6, u7 ) \
274                *(void **) (&fn) = lua_library.get(#fn);
275#       include <nv/lib/detail/lua_functions_compat.inc>
276#       undef NV_LUA_COMPAT_FUN
277                NV_LUA_LOAD_AS( lua_rawlen, lua_objlen )
278                lua_absindex        = lua_absindex_51;
279                lua_getglobal       = lua_getglobal_51;
280                lua_setglobal       = lua_setglobal_51;
281                luaL_setmetatable   = luaL_setmetatable_51;
282                luaL_testudata      = luaL_testudata_51;
283                lua_version         = lua_version_51;
284                lua_copy            = lua_copy_51;
285                lua_rawgetp         = lua_rawgetp_51;
286                lua_rawsetp         = lua_rawsetp_51;
287                lua_pushglobaltable = lua_pushglobaltable_51;
288                luaL_setfuncs       = luaL_setfuncs_51;
289                luaL_getsubtable    = luaL_getsubtable_51;
290
291                NV_LUA_LOAD( lua_lessthan );
292                NV_LUA_LOAD( lua_equal );
293                lua_compare       = lua_compare_51;
294                LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_51;
295                LUA_VERSION_NUM   = 501;
296        }
297#       undef NV_LUA_LOAD
298#endif
299
300        return true;
301}
302
303#endif
Note: See TracBrowser for help on using the repository browser.