summaryrefslogtreecommitdiff
path: root/include/BLua.h
blob: f745f552458774ed45f7205374dbec1c582ae522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 *  Baltisot
 *  Copyright (C) 1999-2007 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* $Id: BLua.h,v 1.38 2007-05-31 13:26:52 pixel Exp $ */

#ifndef __BLUA_H__
#define __BLUA_H__

struct lua_State;

extern "C" {
    void do_lua_lock(lua_State *);
    void do_lua_unlock(lua_State *);
}

#define lua_lock(L) do_lua_lock(L)
#define lua_unlock(L) do_lua_unlock(L)

#include <lua.h>
#include <lauxlib.h>
#include <map>
#include <Exceptions.h>
#include <Handle.h>

#undef isnumber

class LuaPrinter : public Base {
  public:
    virtual void puts(const char *);
};

//!Basic LUA engine.
/*!
  This will create an LUA context, and provide mechanisms to interact with it.
*/
class Lua : public Base {
  public:
      Lua();
      Lua(const Lua &) throw (GeneralException);
      virtual ~Lua();
    void open_base();
    void open_table();
    void open_io(bool safe = true);
    void open_string();
    void open_math();
    void open_debug();
    void open_dir();
    void declarefunc(const String &, lua_CFunction, int = LUA_GLOBALSINDEX);
    void call(const String &, int = LUA_GLOBALSINDEX, int = 0, int = 0);
    void call(int = 0, int = 0) throw (GeneralException);
    void push();
    void push(lua_Number);
    void push(const String &);
    void push(bool);
    void push(char *, int size = -1);
    void push(void *);
    void push(lua_CFunction, int = 0);
    void pop(int = 1);
    void copy(int = -1);
    void remove(int = 1);
    void insert(int = 1);
    void replace(int = 1);
    void newtable();
    void * newuser(size_t);
    void settable(int = -3, bool raw = false);
    void gettable(int = -2, bool raw = false);
    void setvar();
    int gettop();
    void getglobal(const String &) throw (GeneralException);
    void push_lua_context();
    void error(const String &);
    int status();
    int type(int = -1);
    bool isnil(int = -1);
    bool isboolean(int = -1);
    bool isnumber(int = -1);
    bool isstring(int = -1);
    bool istable(int = -1);
    bool isfunction(int = -1);
    bool iscfunction(int = -1);
    bool isuserdata(int = -1);
    bool islightuserdata(int = -1);
    bool isobject(int = -1);
    bool toboolean(int = -1);
    lua_Number tonumber(int = -1);
    String tostring(int = -1);
    lua_CFunction tocfunction(int = -1);
    void * touserdata(int = -1);
    Lua * tothread(int = -1);
    String escape_string(const String &);
    void load(Handle *, bool docall = true) throw (GeneralException);
    void load(const String &, bool docall = true) throw (GeneralException);
    void dump(Handle *, bool strip = true);
    void dumpvars(Handle *, const String &, int = -1);
    Lua * thread(bool saveit = true);
    Lua * thread(const String &, int nargs = 0, bool saveit = true);
    Lua * thread(Handle *, int nargs = 0, bool saveit = true);
    int yield(int nresults = 0);
    void resume(int nargs = 0) throw (GeneralException);
    void resume(const String &, int nargs = 0);
    void resume(Handle *, int nargs = 0);
    static Lua * find(lua_State *) throw (GeneralException);
    void showstack(int level = M_INFO);
    void showerror();
    int getmetatable(int = -1);
    int setmetatable(int = -2);
    int sethook(lua_Hook func, int mask, int count);
    
    void do_break();
    
    virtual void lock();
    virtual void unlock();

    bool is_protected();
    void openlib(const String & libname, const struct luaL_reg *l, int nup);

    void setgcthreshold(int = 0);
    int getgcthreshold();
    int getgccount();
    
    Lua * Father();
    
    void SetPrinter(LuaPrinter *);
    void puts(const char * msg);

  protected:
    virtual Lua * spawn_from_thread(lua_State *);
      Lua(lua_State *);

  private:
    void setup_state(lua_State *);
    lua_State * L;
    static std::map<lua_State *, Lua *> lualist;
    bool _protected, _is_thread;
    void dumpvars_r(Handle *, int, int = 0) throw (GeneralException);
    Lua * father;
    LuaPrinter * lprinter;

    friend class LuaStatics;
};

class LuaObject : public Base {
  public:
      LuaObject() : wantdestruct(false), pushed(false) {}
    virtual void push(Lua *) throw (GeneralException);
    static void * getme(Lua *, int = 1);
    void pushdestruct(Lua *) throw (GeneralException);
  protected:
    virtual void pushmembers(Lua *) = 0;
    void pushme(Lua *, void * = 0, const String & = "", bool = true);
    static void pushit(Lua *, const String &, lua_CFunction);
    static void pushmeta(Lua *, const String &, lua_CFunction);
    bool wantdestruct, pushed;
};

class LuaException : public GeneralException {
  public:
      LuaException(String);
  protected:
      LuaException();
};

enum Lua_types_t {
    LUA_OBJECT      = 0x01,
    LUA_TABLE       = 0x02,
    LUA_BOOLEAN     = 0x04,
    LUA_NUMBER      = 0x08,
    LUA_STRING      = 0x10,
    LUA_FUNCTION    = 0x20,
    LUA_NIL         = 0x40,
    LUA_ANY         = 0x7f,
};

#define MAX_TYPE 7

#define MAXARGS 32

struct lua_functypes_t {
    int number;
    char * name;
    int minargs, maxargs;
    int argtypes[MAXARGS];
};

#define DECLARE_METHOD(classname, enumvar) static int method_##enumvar(lua_State * L) { \
    return LuaHelpers<classname>::method_multiplex( \
        enumvar, \
        L, \
        sLua_##classname::classname##_proceed, \
        0, \
        classname##_methods, \
        true); \
    }

#define DECLARE_FUNCTION(classname, enumvar) static int function_##enumvar(lua_State * L) { \
    return LuaHelpers<classname>::method_multiplex( \
        enumvar, \
        L, \
        0, \
        sLua_##classname::classname##_proceed_statics, \
        classname##_functions, \
        false); \
    }

#define PUSH_METHOD(classname, enumvar) pushit( \
    L, \
    classname##_methods[enumvar].name, \
    sLua_##classname::method_##enumvar)

#define PUSH_METAMETHOD(classname, enumvar) pushmeta( \
    L, \
    String("__") + classname##_methods[enumvar].name, \
    sLua_##classname::method_##enumvar)

#define PUSH_FUNCTION(classname, enumvar) L->declarefunc( \
    classname##_functions[enumvar].name, \
    sLua_##classname::function_##enumvar)


#define CHECK_METHODS(classname) { \
    int i = 0; \
    while (classname##_methods[i].number != -1) { \
        if (i != classname##_methods[i].number) { \
            throw GeneralException("Data of " #classname "_methods inconsistants!"); \
        } \
        i++; \
    } \
}

#define CHECK_FUNCTIONS(classname) { \
    int i = 0; \
    while (classname##_functions[i].number != -1) { \
        if (i != classname##_functions[i].number) { \
            throw GeneralException("Data of " #classname "_functions inconsistants!"); \
        } \
        i++; \
    } \
}

#include <typeinfo>


template <class T>
class LuaHelpers : public Base {
  public:
    static int method_multiplex(int caller, lua_State * __L, int (*proceed)(Lua * L, int n, T * obj, int caller), int (*proceed_static)(Lua * L, int n, int caller), lua_functypes_t * tab, bool method) {
        Lua * L = Lua::find(__L);
        int add = method ? 1 : 0;
        int n = L->gettop() - add;
        T * obj = 0;
        int i, j, mask;
        bool invalid = false, arg_valid;

        if (method)
            obj = (T *) LuaObject::getme(L);

        if ((n < tab[caller].minargs) || (n > tab[caller].maxargs)) {
            invalid = true;
        } else {
            for (i = 0; i < tab[caller].maxargs && !invalid; i++) {
                if (n >= (i + 1)) {
                    arg_valid = false;
                    for (j = 0; j < MAX_TYPE && !arg_valid; j++) {
                        mask = 1 << j;
                        if (tab[caller].argtypes[i] & mask) {
                            switch(mask) {
                                case LUA_OBJECT:
                                    if (L->istable(i + 1 + add)) {
                                        L->push("__obj");
                                        L->gettable(i + 1 + add);
                                        arg_valid = L->isuserdata();
                                        L->pop();
                                    } else {
                                        arg_valid = L->isnil(i + 1 + add);
                                    }
                                    break;
                                case LUA_TABLE:
                                    arg_valid = L->istable(i + 1 + add);
                                    break;
                                case LUA_BOOLEAN:
                                    arg_valid = L->isboolean(i + 1 + add);
                                    break;
                                case LUA_NUMBER:
                                    arg_valid = L->isnumber(i + 1 + add);
                                    break;
                                case LUA_STRING:
                                    arg_valid = L->isstring(i + 1 + add);
                                    break;
                                case LUA_FUNCTION:
                                    arg_valid = L->isfunction(i + 1 + add);
                                    break;
                                case LUA_NIL:
                                    arg_valid = L->isnil(i + 1 + add);
                                    break;
                            }
                        }
                    }
                    invalid = !arg_valid;
                }
            }
        }

        if (invalid) {
            if (method) {
                L->error(String("Invalid arguments to method `") + typeid(T).name() + "::" + tab[caller].name + "'");
            } else {
                L->error(String("Invalid arguments to function `") + typeid(T).name() + " " + tab[caller].name + "'");
            }
        }

        if (method) {
            return proceed(L, n, obj, caller);
        } else {
            return proceed_static(L, n, caller);
        }
    }
};


 /*******************************\
|**  Let's have a sample of use **|
 \*******************************/

#ifdef THIS_IS_A_SAMPLE_WHICH_DOES_NOT_COMPILE
Luacdfile::Luacdfile(cdfile * h) : LuaHandle(h) { }

enum cdfile_methods_t {
    CDFILE_XXX = 0,
    CDFILE_YYY
};

enum cdfile_functions_t {
    CDFILE_NEWCDFILE = 0,
};

struct lua_functypes_t cdutils_methods[] = {
    { CDFILE_XXX, "xxx", 1, 1, {LUA_OBJECT} },
    { CDFILE_YYY, "yyy", 0, 2, {LUA_NUMBER, LUA_NUMBER} },
    { -1, 0, 0, 0, 0 }
};

struct lua_functypes_t cdfile_functions[] = {
    { CDFILE_NEWCDFILE, "cdfile", 1, 4, {LUA_OBJECT, LUA_ANY, LUA_NUMBER, LUA_NUMBER} },
    { -1, 0, 0, 0, 0 }
};

class sLua_cdfile : public Base {
  public:
    static int newcdfile(lua_State * L);
    DECLARE_METHOD(cdfile, CDFILE_XXX);
    DECLARE_METHOD(cdfile, CDFILE_YYY);
    DECLARE_FUNCTION(cdfile, CDFILE_NEWCDFILE);
  private:
    static int cdfile_proceed(Lua * L, int n, cdfile * obj, int caller);
    static int cdfile_proceed_statics(Lua * L, int n, int caller);
};

void Luacdfile::pushmembers(Lua * L) {
    {
    LuaHandle::pushmembers(L);
        or
    pushme(L, SomeObject);
    }
    PUSH_METHOD(cdfile, CDFILE_XXX);
    PUSH_METHOD(cdfile, CDFILE_YYY);
}

void Luacdfile::pushstatics(Lua * L) {
    CHECK_METHODS(cdfile);
    CHECK_FUNCTIONS(cdfile);

    PUSH_FUNCTION(cdfile, CDFILE_NEWCDFILE);
}

int sLua_cdfile::cdfile_proceed(Lua * L, int n, cdfile * cdfile, int caller) {
    int r = 0;
    SomeObj * obj;
    int arg1 = DEFAULT1, arg2 = DEFAULT2;

    switch(caller) {
    case CDFILE_XXX:
        obj = (SomeObj *) LuaObject::getme(L, 2);
        cdfile->xxx(obj);
        break;
    case CDFILE_YYY:
        if (n >= 1)
            arg1 = L->tonumber(2);
        if (n >= 2)
            arg2 = L->tonumber(3);
        L->push((lua_Number) cdfile->yyy(arg1, arg2));
        r = 1;
        break;
    }

    return r;
}

int sLua_cdfile::cdfile_proceed_statics(Lua * L, int n, int caller) {
    int r = 0;

    switch(caller) {
    case CDFILE_NEWCDFILE:
        /****TODO****/
        break;
    }

    return r;
}

#endif

#endif