aboutsummaryrefslogtreecommitdiff
path: root/src/lua.h
blob: 2dc8ffb1b86f16c634ab7c07d3f36635c6a3191b (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
#include <lua5.4/lua.h>
#include <lua5.4/lualib.h>
#include <lua5.4/lauxlib.h>
#include <stdlib.h>

void* __malloc_(size_t);
void __free_(void*);

void i_dcopy(lua_State* src, lua_State* dest, void*);

//generic macro that takes other macros (see below)
#define _tset_b(L, Tidx, K, V, F)\
    lua_pushvalue(L, Tidx);\
    lua_pushstring(L, K);\
    F(L, V);\
    lua_settable(L, Tidx);

//some macros to make batch adding easier (may switch to arrays for this later)
#define luaI_tseti(L, Tidx, K, V)\
    _tset_b(L, Tidx, K, V, lua_pushinteger)
#define luaI_tsetb(L, Tidx, K, V)\
  _tset_b(L, Tidx, K, V, lua_pushboolean)
#define luaI_tsets(L, Tidx, K, V)\
    _tset_b(L, Tidx, K, V, lua_pushstring)
#define luaI_tsetv(L, Tidx, K, V)\
    _tset_b(L, Tidx, K, V, lua_pushvalue)
#define luaI_tsetcf(L, Tidx, K, V)\
    _tset_b(L, Tidx, K, V, lua_pushcfunction)

int writer(lua_State*, const void*, size_t, void*);

#if LUA_VERSION_NUM == 504
    #define lreg(N, FN)\
        lua_pushstring(L, N);\
        luaL_register(L, NULL, FN);\
        lua_settable(L, -3);

    
    #define requiref( L, modname, f, glob ) \
        { luaL_requiref( L, modname, f, glob ); lua_pop( L, 1 ); }

    #define lua_objlen(L,i) lua_rawlen(L,(i))
    #define luaL_register(L, M, F) luaL_newlib(L, F);
#else
    #define lreg(N, FN)\
        lua_newtable(L);\
        luaL_register(L, NULL, FN);\
        lua_setfield(L, 2, N);
    
    //taken straight from luaproc
    #define requiref(L, modname, f, glob){\
      lua_pushcfunction(L, f);\
      lua_pushstring(L, modname); \
      lua_call(L, 1, 1);\
      lua_getfield(L, LUA_GLOBALSINDEX, LUA_LOADLIBNAME);\
      if(lua_type(L, -1) == LUA_TTABLE){\
        lua_getfield(L, -1, "loaded");\
        if(lua_type(L, -1) == LUA_TTABLE){\
          lua_getfield(L, -1, modname);\
          if(lua_type(L, -1) == LUA_TNIL) {\
            lua_pushvalue(L, 1);\
            lua_setfield(L, -3, modname);\
          }\
        lua_pop(L, 1);\
        }\
      lua_pop(L, 1);\
      }\
      lua_pop(L, 1);\
      if(glob){\
        lua_setglobal(L, modname);\
      }else{\
        lua_pop(L, 1);\
      }\
    }
#endif