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

Last change on this file since 109 was 109, checked in by epyon, 12 years ago
  • prevent multiple loadings of the same library
File size: 15.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/* State manipulation */
14lua_State *        (*lua_newstate) (lua_Alloc f, void *ud) = nullptr;
15void               (*lua_close) (lua_State *L) = nullptr;
16lua_State *        (*lua_newthread) (lua_State *L) = nullptr;
17lua_CFunction      (*lua_atpanic) (lua_State *L, lua_CFunction panicf) = nullptr;
18const lua_Number * (*lua_version) (lua_State *L) = nullptr;
19
20/* Basic stack manipulation */
21int   (*lua_absindex) (lua_State *L, int idx) = nullptr;
22int   (*lua_gettop) (lua_State *L) = nullptr;
23void  (*lua_settop) (lua_State *L, int idx) = nullptr;
24void  (*lua_pushvalue) (lua_State *L, int idx) = nullptr;
25void  (*lua_remove) (lua_State *L, int idx) = nullptr;
26void  (*lua_insert) (lua_State *L, int idx) = nullptr;
27void  (*lua_replace) (lua_State *L, int idx) = nullptr;
28void  (*lua_copy) (lua_State *L, int fromidx, int toidx) = nullptr;
29int   (*lua_checkstack) (lua_State *L, int sz) = nullptr;
30void  (*lua_xmove) (lua_State *from, lua_State *to, int n) = nullptr;
31
32/* Access functions (stack -> C) */
33int             (*lua_isnumber) (lua_State *L, int idx) = nullptr;
34int             (*lua_isstring) (lua_State *L, int idx) = nullptr;
35int             (*lua_iscfunction) (lua_State *L, int idx) = nullptr;
36int             (*lua_isuserdata) (lua_State *L, int idx) = nullptr;
37int             (*lua_type) (lua_State *L, int idx) = nullptr;
38const char*     (*lua_typename) (lua_State *L, int tp) = nullptr;
39
40lua_Number      (*lua_tonumberx) (lua_State *L, int idx, int *isnum) = nullptr;
41lua_Integer     (*lua_tointegerx) (lua_State *L, int idx, int *isnum) = nullptr;
42lua_Unsigned    (*lua_tounsignedx) (lua_State *L, int idx, int *isnum) = nullptr;
43int             (*lua_toboolean) (lua_State *L, int idx) = nullptr;
44const char*     (*lua_tolstring) (lua_State *L, int idx, size_t *len) = nullptr;
45size_t          (*lua_rawlen) (lua_State *L, int idx) = nullptr;
46lua_CFunction   (*lua_tocfunction) (lua_State *L, int idx) = nullptr;
47void*           (*lua_touserdata) (lua_State *L, int idx) = nullptr;
48lua_State*      (*lua_tothread) (lua_State *L, int idx) = nullptr;
49const void*     (*lua_topointer) (lua_State *L, int idx) = nullptr;
50
51/* Comparison and arithmetic functions */
52void  (*lua_arith) (lua_State *L, int op) = nullptr;
53int   (*lua_rawequal) (lua_State *L, int idx1, int idx2) = nullptr;
54int   (*lua_compare) (lua_State *L, int idx1, int idx2, int op) = nullptr;
55
56/* Push functions (C -> stack) */
57void         (*lua_pushnil) (lua_State *L) = nullptr;
58void         (*lua_pushnumber) (lua_State *L, lua_Number n) = nullptr;
59void         (*lua_pushinteger) (lua_State *L, lua_Integer n) = nullptr;
60void         (*lua_pushunsigned) (lua_State *L, lua_Unsigned n) = nullptr;
61const char*  (*lua_pushlstring) (lua_State *L, const char *s, size_t l) = nullptr;
62const char*  (*lua_pushstring) (lua_State *L, const char *s) = nullptr;
63const char*  (*lua_pushvfstring) (lua_State *L, const char *fmt,
64                                                      va_list argp) = nullptr;
65const char * (*lua_pushfstring) (lua_State *L, const char *fmt, ...) = nullptr;
66void  (*lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n) = nullptr;
67void  (*lua_pushboolean) (lua_State *L, int b) = nullptr;
68void  (*lua_pushlightuserdata) (lua_State *L, void *p) = nullptr;
69int   (*lua_pushthread) (lua_State *L) = nullptr;
70
71/* Get functions (Lua -> stack) */
72void  (*lua_getglobal) (lua_State *L, const char *var) = nullptr;
73void  (*lua_gettable) (lua_State *L, int idx) = nullptr;
74void  (*lua_getfield) (lua_State *L, int idx, const char *k) = nullptr;
75void  (*lua_rawget) (lua_State *L, int idx) = nullptr;
76void  (*lua_rawgeti) (lua_State *L, int idx, int n) = nullptr;
77void  (*lua_rawgetp) (lua_State *L, int idx, const void *p) = nullptr;
78void  (*lua_createtable) (lua_State *L, int narr, int nrec) = nullptr;
79void* (*lua_newuserdata) (lua_State *L, size_t sz) = nullptr;
80int   (*lua_getmetatable) (lua_State *L, int objindex) = nullptr;
81void  (*lua_getuservalue) (lua_State *L, int idx) = nullptr;
82
83/* Set functions (stack -> Lua) */
84void  (*lua_setglobal) (lua_State *L, const char *var) = nullptr;
85void  (*lua_settable) (lua_State *L, int idx) = nullptr;
86void  (*lua_setfield) (lua_State *L, int idx, const char *k) = nullptr;
87void  (*lua_rawset) (lua_State *L, int idx) = nullptr;
88void  (*lua_rawseti) (lua_State *L, int idx, int n) = nullptr;
89void  (*lua_rawsetp) (lua_State *L, int idx, const void *p) = nullptr;
90int   (*lua_setmetatable) (lua_State *L, int objindex) = nullptr;
91void  (*lua_setuservalue) (lua_State *L, int idx) = nullptr;
92
93/* 'load' and 'call' functions (load and run Lua code) */
94void  (*lua_callk) (lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k) = nullptr;
95int   (*lua_getctx) (lua_State *L, int *ctx) = nullptr;
96int   (*lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k) = nullptr;
97int   (*lua_load) (lua_State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode) = nullptr;
98int   (*lua_dump) (lua_State *L, lua_Writer writer, void *data) = nullptr;
99
100/* Coroutine functions */
101int  (*lua_yieldk) (lua_State *L, int nresults, int ctx, lua_CFunction k) = nullptr;
102int  (*lua_resume) (lua_State *L, lua_State *from, int narg) = nullptr;
103int  (*lua_status) (lua_State *L) = nullptr;
104
105/* Garbage-collection function and options */
106int (*lua_gc) (lua_State *L, int what, int data) = nullptr;
107
108/* Miscellaneous functions */
109int   (*lua_error) (lua_State *L) = nullptr;
110int   (*lua_next) (lua_State *L, int idx) = nullptr;
111void  (*lua_concat) (lua_State *L, int n) = nullptr;
112void  (*lua_len)    (lua_State *L, int idx) = nullptr;
113lua_Alloc (*lua_getallocf) (lua_State *L, void **ud) = nullptr;
114void      (*lua_setallocf) (lua_State *L, lua_Alloc f, void *ud) = nullptr;
115
116/* Debug API */
117int (*lua_getstack) (lua_State *L, int level, lua_Debug *ar) = nullptr;
118int (*lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar) = nullptr;
119const char * (*lua_getlocal) (lua_State *L, const lua_Debug *ar, int n) = nullptr;
120const char * (*lua_setlocal) (lua_State *L, const lua_Debug *ar, int n) = nullptr;
121const char * (*lua_getupvalue) (lua_State *L, int funcindex, int n) = nullptr;
122const char * (*lua_setupvalue) (lua_State *L, int funcindex, int n) = nullptr;
123void * (*lua_upvalueid) (lua_State *L, int fidx, int n) = nullptr;
124void   (*lua_upvaluejoin) (lua_State *L, int fidx1, int n1, int fidx2, int n2) = nullptr;
125int (*lua_sethook) (lua_State *L, lua_Hook func, int mask, int count) = nullptr;
126lua_Hook (*lua_gethook) (lua_State *L) = nullptr;
127int (*lua_gethookmask) (lua_State *L) = nullptr;
128int (*lua_gethookcount) (lua_State *L) = nullptr;
129
130/* lualib API */
131int (*luaopen_base) (lua_State *L) = nullptr;
132int (*luaopen_coroutine) (lua_State *L) = nullptr;
133int (*luaopen_table) (lua_State *L) = nullptr;
134int (*luaopen_io) (lua_State *L) = nullptr;
135int (*luaopen_os) (lua_State *L) = nullptr;
136int (*luaopen_string) (lua_State *L) = nullptr;
137int (*luaopen_bit32) (lua_State *L) = nullptr;
138int (*luaopen_math) (lua_State *L) = nullptr;
139int (*luaopen_debug) (lua_State *L) = nullptr;
140int (*luaopen_package) (lua_State *L) = nullptr;
141int (*luaL_openlibs) (lua_State *L) = nullptr;
142
143/* lauxlib API */
144void   (*luaL_checkversion_) (lua_State *L, lua_Number ver) = nullptr;
145int  (*luaL_getmetafield) (lua_State *L, int obj, const char *e) = nullptr;
146int  (*luaL_callmeta) (lua_State *L, int obj, const char *e) = nullptr;
147const char * (*luaL_tolstring) (lua_State *L, int idx, size_t *len) = nullptr;
148int  (*luaL_argerror) (lua_State *L, int numarg, const char *extramsg) = nullptr;
149const char * (*luaL_checklstring) (lua_State *L, int numArg, size_t *l) = nullptr;
150const char * (*luaL_optlstring) (lua_State *L, int numArg, const char *def, size_t *l) = nullptr;
151lua_Number  (*luaL_checknumber) (lua_State *L, int numArg) = nullptr;
152lua_Number  (*luaL_optnumber) (lua_State *L, int nArg, lua_Number def) = nullptr;
153lua_Integer  (*luaL_checkinteger) (lua_State *L, int numArg) = nullptr;
154lua_Integer  (*luaL_optinteger) (lua_State *L, int nArg, lua_Integer def) = nullptr;
155lua_Unsigned  (*luaL_checkunsigned) (lua_State *L, int numArg) = nullptr;
156lua_Unsigned  (*luaL_optunsigned) (lua_State *L, int numArg, lua_Unsigned def) = nullptr;
157void  (*luaL_checkstack) (lua_State *L, int sz, const char *msg) = nullptr;
158void  (*luaL_checktype) (lua_State *L, int narg, int t) = nullptr;
159void  (*luaL_checkany) (lua_State *L, int narg) = nullptr;
160int    (*luaL_newmetatable) (lua_State *L, const char *tname) = nullptr;
161void   (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr;
162void * (*luaL_testudata) (lua_State *L, int ud, const char *tname) = nullptr;
163void * (*luaL_checkudata) (lua_State *L, int ud, const char *tname) = nullptr;
164void  (*luaL_where) (lua_State *L, int lvl) = nullptr;
165int  (*luaL_error) (lua_State *L, const char *fmt, ...) = nullptr;
166int  (*luaL_checkoption) (lua_State *L, int narg, const char *def, const char *const lst[]) = nullptr;
167int  (*luaL_fileresult) (lua_State *L, int stat, const char *fname) = nullptr;
168int  (*luaL_execresult) (lua_State *L, int stat) = nullptr;
169
170int  (*luaL_ref) (lua_State *L, int t) = nullptr;
171void  (*luaL_unref) (lua_State *L, int t, int ref) = nullptr;
172int  (*luaL_loadfilex) (lua_State *L, const char *filename, const char *mode) = nullptr;
173int  (*luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) = nullptr;
174int  (*luaL_loadstring) (lua_State *L, const char *s) = nullptr;
175lua_State * (*luaL_newstate)  (void) = nullptr;
176int  (*luaL_len) (lua_State *L, int idx) = nullptr;
177const char * (*luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r) = nullptr;
178void  (*luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup) = nullptr;
179int  (*luaL_getsubtable) (lua_State *L, int idx, const char *fname) = nullptr;
180void  (*luaL_traceback) (lua_State *L, lua_State *L1, const char *msg, int level) = nullptr;
181void  (*luaL_requiref) (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr;
182
183bool nv::load_lua_library( const char* path )
184{
185#       define NV_LUA_LOAD( symbol ) *(void **) (&symbol) = lua_library.get(#symbol);
186
187        static nv::library lua_library;
188        if ( lua_library.is_open() ) return true;
189        lua_library.open( path );
190
191
192/* State manipulation */
193        NV_LUA_LOAD(lua_newstate);
194        NV_LUA_LOAD(lua_close);
195        NV_LUA_LOAD(lua_newthread);
196        NV_LUA_LOAD(lua_atpanic);
197        NV_LUA_LOAD(lua_version);
198
199/* Basic stack manipulation */
200        NV_LUA_LOAD(lua_absindex);
201        NV_LUA_LOAD(lua_gettop);
202        NV_LUA_LOAD(lua_settop);
203        NV_LUA_LOAD(lua_pushvalue);
204        NV_LUA_LOAD(lua_remove);
205        NV_LUA_LOAD(lua_insert);
206        NV_LUA_LOAD(lua_replace);
207        NV_LUA_LOAD(lua_copy);
208        NV_LUA_LOAD(lua_checkstack);
209        NV_LUA_LOAD(lua_xmove);
210
211/* Access functions (stack -> C) */
212        NV_LUA_LOAD(lua_isnumber);
213        NV_LUA_LOAD(lua_isstring);
214        NV_LUA_LOAD(lua_iscfunction);
215        NV_LUA_LOAD(lua_isuserdata);
216        NV_LUA_LOAD(lua_type);
217        NV_LUA_LOAD(lua_typename);
218
219        NV_LUA_LOAD(lua_tonumberx);
220        NV_LUA_LOAD(lua_tointegerx);
221        NV_LUA_LOAD(lua_tounsignedx);
222        NV_LUA_LOAD(lua_toboolean);
223        NV_LUA_LOAD(lua_tolstring);
224        NV_LUA_LOAD(lua_rawlen);
225        NV_LUA_LOAD(lua_tocfunction);
226        NV_LUA_LOAD(lua_touserdata);
227        NV_LUA_LOAD(lua_tothread);
228        NV_LUA_LOAD(lua_topointer);
229
230/* Comparison and arithmetic functions */
231        NV_LUA_LOAD(lua_arith);
232        NV_LUA_LOAD(lua_rawequal);
233        NV_LUA_LOAD(lua_compare);
234
235/* Push functions (C -> stack) */
236        NV_LUA_LOAD(lua_pushnil);
237        NV_LUA_LOAD(lua_pushnumber);
238        NV_LUA_LOAD(lua_pushinteger);
239        NV_LUA_LOAD(lua_pushunsigned);
240        NV_LUA_LOAD(lua_pushlstring);
241        NV_LUA_LOAD(lua_pushstring);
242        NV_LUA_LOAD(lua_pushvfstring);
243        NV_LUA_LOAD(lua_pushfstring);
244        NV_LUA_LOAD(lua_pushcclosure);
245        NV_LUA_LOAD(lua_pushboolean);
246        NV_LUA_LOAD(lua_pushlightuserdata);
247        NV_LUA_LOAD(lua_pushthread);
248
249/* Get functions (Lua -> stack) */
250        NV_LUA_LOAD(lua_getglobal);
251        NV_LUA_LOAD(lua_gettable);
252        NV_LUA_LOAD(lua_getfield);
253        NV_LUA_LOAD(lua_rawget);
254        NV_LUA_LOAD(lua_rawgeti);
255        NV_LUA_LOAD(lua_rawgetp);
256        NV_LUA_LOAD(lua_createtable);
257        NV_LUA_LOAD(lua_newuserdata);
258        NV_LUA_LOAD(lua_getmetatable);
259        NV_LUA_LOAD(lua_getuservalue);
260
261/* Set functions (stack -> Lua) */
262        NV_LUA_LOAD(lua_setglobal);
263        NV_LUA_LOAD(lua_settable);
264        NV_LUA_LOAD(lua_setfield);
265        NV_LUA_LOAD(lua_rawset);
266        NV_LUA_LOAD(lua_rawseti);
267        NV_LUA_LOAD(lua_rawsetp);
268        NV_LUA_LOAD(lua_setmetatable);
269        NV_LUA_LOAD(lua_setuservalue);
270
271/* 'load' and 'call' functions (load and run Lua code) */
272        NV_LUA_LOAD(lua_callk);
273        NV_LUA_LOAD(lua_getctx);
274        NV_LUA_LOAD(lua_pcallk);
275        NV_LUA_LOAD(lua_load);
276        NV_LUA_LOAD(lua_dump);
277
278/* Coroutine functions */
279        NV_LUA_LOAD(lua_yieldk);
280        NV_LUA_LOAD(lua_resume);
281        NV_LUA_LOAD(lua_status);
282
283/* Garbage-collection function and options */
284        NV_LUA_LOAD(lua_gc);
285
286/* Miscellaneous functions */
287        NV_LUA_LOAD(lua_error);
288        NV_LUA_LOAD(lua_next);
289        NV_LUA_LOAD(lua_concat);
290        NV_LUA_LOAD(lua_len);
291        NV_LUA_LOAD(lua_getallocf);
292        NV_LUA_LOAD(lua_setallocf);
293
294/* Debug API */
295        NV_LUA_LOAD(lua_getstack);
296        NV_LUA_LOAD(lua_getinfo);
297        NV_LUA_LOAD(lua_getlocal);
298        NV_LUA_LOAD(lua_setlocal);
299        NV_LUA_LOAD(lua_getupvalue);
300        NV_LUA_LOAD(lua_setupvalue);
301        NV_LUA_LOAD(lua_upvalueid);
302        NV_LUA_LOAD(lua_upvaluejoin);
303        NV_LUA_LOAD(lua_sethook);
304        NV_LUA_LOAD(lua_gethook);
305        NV_LUA_LOAD(lua_gethookmask);
306        NV_LUA_LOAD(lua_gethookcount);
307
308/* lualib API */
309        NV_LUA_LOAD(luaopen_base);
310        NV_LUA_LOAD(luaopen_coroutine);
311        NV_LUA_LOAD(luaopen_table);
312        NV_LUA_LOAD(luaopen_io);
313        NV_LUA_LOAD(luaopen_os);
314        NV_LUA_LOAD(luaopen_string);
315        NV_LUA_LOAD(luaopen_bit32);
316        NV_LUA_LOAD(luaopen_math);
317        NV_LUA_LOAD(luaopen_debug);
318        NV_LUA_LOAD(luaopen_package);
319        NV_LUA_LOAD(luaL_openlibs);
320
321/* lauxlib API */
322        NV_LUA_LOAD(luaL_checkversion_);
323        NV_LUA_LOAD(luaL_getmetafield);
324        NV_LUA_LOAD(luaL_callmeta);
325        NV_LUA_LOAD(luaL_tolstring);
326        NV_LUA_LOAD(luaL_argerror);
327        NV_LUA_LOAD(luaL_checklstring);
328        NV_LUA_LOAD(luaL_optlstring);
329        NV_LUA_LOAD(luaL_checknumber);
330        NV_LUA_LOAD(luaL_optnumber);
331        NV_LUA_LOAD(luaL_checkinteger);
332        NV_LUA_LOAD(luaL_optinteger);
333        NV_LUA_LOAD(luaL_checkunsigned);
334        NV_LUA_LOAD(luaL_optunsigned);
335        NV_LUA_LOAD(luaL_checkstack);
336        NV_LUA_LOAD(luaL_checktype);
337        NV_LUA_LOAD(luaL_checkany);
338        NV_LUA_LOAD(luaL_newmetatable);
339        NV_LUA_LOAD(luaL_setmetatable);
340        NV_LUA_LOAD(luaL_testudata);
341        NV_LUA_LOAD(luaL_checkudata);
342        NV_LUA_LOAD(luaL_where);
343        NV_LUA_LOAD(luaL_error);
344        NV_LUA_LOAD(luaL_checkoption);
345        NV_LUA_LOAD(luaL_fileresult);
346        NV_LUA_LOAD(luaL_execresult);
347
348        NV_LUA_LOAD(luaL_ref);
349        NV_LUA_LOAD(luaL_unref);
350        NV_LUA_LOAD(luaL_loadfilex);
351        NV_LUA_LOAD(luaL_loadbufferx);
352        NV_LUA_LOAD(luaL_loadstring);
353        NV_LUA_LOAD(luaL_newstate);
354        NV_LUA_LOAD(luaL_len);
355        NV_LUA_LOAD(luaL_gsub);
356        NV_LUA_LOAD(luaL_setfuncs);
357        NV_LUA_LOAD(luaL_getsubtable);
358        NV_LUA_LOAD(luaL_traceback);
359        NV_LUA_LOAD(luaL_requiref);
360
361#       undef NV_LUA_LOAD
362        return true;
363}
364
365#endif
Note: See TracBrowser for help on using the repository browser.