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

Last change on this file since 172 was 172, checked in by epyon, 12 years ago
  • sdl - missing 2.0 sdl_functions
  • lua - compat implementation of lua_upvalueindex
  • position - rectangle single argument constructor made explicit
  • gl_window - fix for SDL 2.0 ascii code of key press
  • lua/glm - coord as a separate class (behaves like ivec2)
File size: 9.2 KB
RevLine 
[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]47int LUA_UPVALUEINDEX  = 0;
[166]48int LUA_REGISTRYINDEX = 0;
49int LUA_VERSION_NUM   = 0;
50size_t (*lua_rawlen)        (lua_State *L, int idx) = nullptr;
51int    (*lua_absindex)      (lua_State *L, int idx) = nullptr;
52void   (*lua_getglobal)     (lua_State *L, const char *var) = nullptr;
53void   (*lua_setglobal)     (lua_State *L, const char *var) = nullptr;
54void   (*luaL_requiref)     (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr;
55void   (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr;
56void*  (*luaL_testudata)    (lua_State *L, int ud, const char *tname) = nullptr;
57void   (*lua_copy)          (lua_State *L, int fromidx, int toidx) = nullptr;
58int    (*lua_compare)       (lua_State *L, int idx1, int idx2, int op) = nullptr;
59void   (*lua_rawgetp)       (lua_State *L, int idx, const void *p) = nullptr;
60void   (*lua_rawsetp)       (lua_State *L, int idx, const void *p) = nullptr;
61void   (*lua_pushglobaltable)(lua_State* L) = nullptr;
62void   (*luaL_setfuncs)     (lua_State *L, const luaL_Reg *l, int nup) = nullptr;
63int    (*luaL_getsubtable)  (lua_State *L, int idx, const char *fname) = nullptr;
64
65const lua_Number* (*lua_version) (lua_State *L) = nullptr;
66
67// only loaded in 5.1 mode to implement lua_compare
68int    (*lua_equal)        (lua_State *L, int idx1, int idx2) = nullptr;
69int    (*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)
85#       define LUA_REGISTRYINDEX_51     (-10000)
86#       define lua_upvalueindex_51(i)   (LUA_GLOBALSINDEX_51-(i))
87#       define lua_upvalueindex_52(i)   (LUA_REGISTRYINDEX_52-(i))
88
89int lua_absindex_51 (lua_State *L, int idx) { return (idx > 0 ? idx : idx + lua_gettop(L) + 1); };
90void lua_getglobal_51 (lua_State *L, const char *var) { lua_getfield(L, LUA_GLOBALSINDEX_51, var ); };
91void lua_setglobal_51 (lua_State *L, const char *var) { lua_setfield(L, LUA_GLOBALSINDEX_51, var ); };
92void luaL_requiref_51 (lua_State *L, const char *modname, lua_CFunction openf, int glb)
93{
94        lua_pushcfunction(L, openf);
95        lua_pushstring(L, modname);
96        lua_call(L, 1, 1);
97        if (glb != 0)
98        {
99                lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
100                lua_pushvalue(L, -2);
101                lua_setfield(L, -2, modname);
102                lua_pop(L, 1);
103        }
104}
105
106void luaL_setmetatable_51 (lua_State *L, const char *tname)
107{
108        luaL_getmetatable(L, tname);
109        lua_setmetatable(L, -2);
110}
111
112void *luaL_testudata_51 (lua_State *L, int ud, const char *tname)
113{
114        void *p = lua_touserdata(L, ud);
115        if (p != NULL)
116        { 
117                if (lua_getmetatable(L, ud))
118                {
119                        luaL_getmetatable(L, tname);
120                        if ( !lua_rawequal(L, -1, -2))
121                                p = NULL; 
122                        lua_pop(L, 2);
123                        return p;
124                }
125        }
126        return NULL;
127}
128
129const lua_Number *lua_version_51 (lua_State*)
130{
131        static const lua_Number version = (lua_Number)LUA_VERSION_NUM;
132        return &version;
133}
134
135void lua_copy_51 (lua_State *L, int fromidx, int toidx)
136{
137        toidx = lua_absindex( L, toidx );
138        lua_pushvalue( L, fromidx );
139        lua_replace( L, toidx );
140}
141
142int lua_compare_51(lua_State *L, int idx1, int idx2, int op)
143{
144        switch (op)
145        {
146        case LUA_OPEQ : return lua_equal( L, idx1, idx2 );
147        case LUA_OPLT : return lua_lessthan( L, idx1, idx2 );
148        case LUA_OPLE : return lua_lessthan( L, idx1, idx2 ) || lua_equal( L, idx1, idx2 );
[172]149        default:
[166]150                return 0;
151        }
152}
153
154void lua_rawgetp_51(lua_State *L, int idx, const void *p)
155{
156        idx = lua_absindex( L, idx );
157        void* pp = const_cast<void *>(p); // EVIL
158        lua_pushlightuserdata( L, pp );
159        lua_rawget( L, idx );
160}
161
162void lua_rawsetp_51(lua_State *L, int idx, const void *p)
163{
164        idx = lua_absindex( L, idx );
165        void* pp = const_cast<void *>(p); // EVIL
166        lua_pushlightuserdata( L, pp );
167        lua_insert( L, -1 );
168        lua_rawset( L, idx );
169}
170
171int luaL_getsubtable_51(lua_State *L, int idx, const char *fname)
172{
173        lua_getfield(L, idx, fname);
174        if ( lua_istable(L, -1) ) return 1;
175        else {
176                idx = lua_absindex(L, idx);
177                lua_pop(L, 1);
178                lua_newtable(L);
179                lua_pushvalue(L, -1);
180                lua_setfield(L, idx, fname);
181                return 0;
182        }
183}
184
185void luaL_setfuncs_51(lua_State *L, const luaL_Reg *l, int nup)
186{
187        luaL_checkstack(L, nup, "too many upvalues");
188        for (; l->name != NULL; l++)
189        {
190                for (int i = 0; i < nup; i++)
191                {
192                        lua_pushvalue(L, -nup);
193                }
194                lua_pushcclosure(L, l->func, nup);
195                lua_setfield(L, -(nup + 2), l->name);
196        }
197        lua_pop(L, nup);
198}
199
200void lua_pushglobaltable_51(lua_State* L)
201{
202        lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
203}
204
205#endif
206
[5]207bool nv::load_lua_library( const char* path )
208{
[109]209        static nv::library lua_library;
210        if ( lua_library.is_open() ) return true;
[166]211#if NV_LUA_VERSION == NV_LUA_5C
212        if ( path == nullptr )
213        {
214                if (!lua_library.try_open( NV_LUA_PATH_JIT ) &&
215                        !lua_library.try_open( NV_LUA_PATH_52 ) )
216                {
217                        lua_library.open( NV_LUA_PATH_51 );
218                }
219        }
220        else
221#else
222                lua_library.open( path );
223#endif
[5]224
[164]225#       define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname);
226#       if NV_LUA_VERSION == NV_LUA_52
227#               define NV_LUA_FUN_51( rtype, fname, fparams )
228#               define NV_LUA_FUN_52 NV_LUA_FUN
[165]229#       elif NV_LUA_VERSION == NV_LUA_51
[164]230#               define NV_LUA_FUN_51 NV_LUA_FUN
231#               define NV_LUA_FUN_52( rtype, fname, fparams )
[165]232#       else
233#               define NV_LUA_FUN_51( rtype, fname, fparams )
234#               define NV_LUA_FUN_52( rtype, fname, fparams )
[164]235#       endif
[109]236
[165]237#       include <nv/lib/detail/lua_functions.inc>
[5]238
[165]239#       undef NV_LUA_FUN
240#       undef NV_LUA_FUN_51
241#       undef NV_LUA_FUN_52
[166]242
243#if NV_LUA_VERSION == NV_LUA_5C
244#       define NV_LUA_LOAD( fname ) *(void **) (&fname) = lua_library.get(#fname);
245#       define NV_LUA_LOAD_AS( fname,fname2 ) *(void **) (&fname) = lua_library.get(#fname2);
246        bool version_52 = lua_library.try_get("luaL_checkversion_") != nullptr;
247        if (version_52)
248        {
249#       define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, fn2, u5, u6, u7 ) \
250        *(void **) (&(fn2##_compat)) = lua_library.get(#fn2); \
251        fn = call_##fn2##_compat;
252#       include <nv/lib/detail/lua_functions_compat.inc>
253#       undef NV_LUA_COMPAT_FUN
254                NV_LUA_LOAD( lua_rawlen );
255                NV_LUA_LOAD( lua_absindex );
256                NV_LUA_LOAD( lua_getglobal );
257                NV_LUA_LOAD( lua_setglobal );
258                NV_LUA_LOAD( luaL_setmetatable );
259                NV_LUA_LOAD( luaL_testudata );
260                NV_LUA_LOAD( lua_version );
261                NV_LUA_LOAD( lua_copy );
262                NV_LUA_LOAD( lua_compare );
263                NV_LUA_LOAD( lua_rawgetp );
264                NV_LUA_LOAD( lua_rawsetp );
265                NV_LUA_LOAD( lua_pushglobaltable );
266                NV_LUA_LOAD( luaL_setfuncs );
267                NV_LUA_LOAD( luaL_getsubtable );
268
[172]269                LUA_UPVALUEINDEX  = LUA_REGISTRYINDEX_52;
[166]270                LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_52;
271                LUA_VERSION_NUM   = 502;
272        }
273        else
274        {
275#       define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, u4, u5, u6, u7 ) \
276                *(void **) (&fn) = lua_library.get(#fn);
277#       include <nv/lib/detail/lua_functions_compat.inc>
278#       undef NV_LUA_COMPAT_FUN
279                NV_LUA_LOAD_AS( lua_rawlen, lua_objlen )
280                lua_absindex        = lua_absindex_51;
281                lua_getglobal       = lua_getglobal_51;
282                lua_setglobal       = lua_setglobal_51;
283                luaL_setmetatable   = luaL_setmetatable_51;
284                luaL_testudata      = luaL_testudata_51;
285                lua_version         = lua_version_51;
286                lua_copy            = lua_copy_51;
287                lua_rawgetp         = lua_rawgetp_51;
288                lua_rawsetp         = lua_rawsetp_51;
289                lua_pushglobaltable = lua_pushglobaltable_51;
290                luaL_setfuncs       = luaL_setfuncs_51;
291                luaL_getsubtable    = luaL_getsubtable_51;
292
293                NV_LUA_LOAD( lua_lessthan );
294                NV_LUA_LOAD( lua_equal );
295                lua_compare       = lua_compare_51;
[172]296                LUA_UPVALUEINDEX  = LUA_GLOBALSINDEX_51;
[166]297                LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_51;
298                LUA_VERSION_NUM   = 501;
299        }
300#       undef NV_LUA_LOAD
301#endif
302
[5]303        return true;
304}
305
306#endif
Note: See TracBrowser for help on using the repository browser.