Subversion Repositories pspware

Rev

Rev 345 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
345 Oobles 1
#ifndef __LUAMODULE_H
2
#define __LUAMODULE_H
3
 
4
#include <stdlib.h>
5
#include <tdefs.h>
6
 
7
#include "lua.h"
8
#include "lualib.h"
9
#include "lauxlib.h"
10
extern void luaC_collectgarbage (lua_State *L);
11
 
12
#define MAX(a, b) ((a) > (b) ? (a) : (b))
13
#define CLAMP(val, min, max) ((val)>(max)?(max):((val)<(min)?(min):(val)))
14
 
15
#define UserdataStubs(HANDLE, DATATYPE) \
16
DATATYPE *to##HANDLE (lua_State *L, int index) \
17
{ \
18
  DATATYPE* handle  = (DATATYPE*)lua_touserdata(L, index); \
19
  if (handle == NULL) luaL_typerror(L, index, #HANDLE); \
20
  return handle; \
21
} \
22
DATATYPE* push##HANDLE(lua_State *L) { \
23
        DATATYPE * newvalue = (DATATYPE*)lua_newuserdata(L, sizeof(DATATYPE)); \
24
        luaL_getmetatable(L, #HANDLE); \
25
        lua_pushvalue(L, -1); \
26
        lua_setmetatable(L, -3); \
27
        lua_pushstring(L, "__index"); \
28
        lua_pushstring(L, #HANDLE); \
29
        lua_gettable(L, LUA_GLOBALSINDEX); \
30
        lua_settable(L, -3); \
31
        lua_pop(L, 1); \
32
        return newvalue; \
33
}
34
 
35
#define UserdataRegister(HANDLE, METHODS, METAMETHODS) \
36
int HANDLE##_register(lua_State *L) { \
37
        luaL_newmetatable(L, #HANDLE);  /* create new metatable for file handles */ \
38
        lua_pushliteral(L, "__index"); \
39
        lua_pushvalue(L, -2);  /* push metatable */ \
40
        lua_rawset(L, -3);  /* metatable.__index = metatable */ \
41
        \
42
        luaL_openlib(L, 0, METAMETHODS, 0); \
43
        luaL_openlib(L, #HANDLE, METHODS, 0); \
44
        \
45
        lua_pushstring(L, #HANDLE); \
46
        lua_gettable(L, LUA_GLOBALSINDEX); \
47
        luaL_getmetatable(L, #HANDLE); \
48
        lua_setmetatable(L, -2); \
49
        return 1; \
50
}
51
 
52
 
53
#endif
54