aboutsummaryrefslogtreecommitdiff
path: root/src/tools.h
blob: 43582c56229898975d5fc6fea9996d2328bf730c (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
/*
* TOOLS.H
*/
#ifndef TOOLS_H
#define TOOLS_H

#include "lauxlib.h"
#include "threading.h"
    // MUTEX_T

#include <assert.h>

// M$ compiler doesn't support 'inline' keyword in C files...
#if defined( _MSC_VER)
#define inline __inline
#endif

// code is now using Lua 5.2 API
// add Lua 5.2 API when building for Lua 5.1
#if LUA_VERSION_NUM == 501
#define lua_absindex( L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) +1)
#define lua_pushglobaltable(L) lua_pushvalue( L, LUA_GLOBALSINDEX)
#define lua_setuservalue lua_setfenv
#define lua_getuservalue lua_getfenv
#define lua_rawlen lua_objlen
#define luaG_registerlibfuncs( L, _funcs) luaL_register( L, NULL, _funcs)
#endif // LUA_VERSION_NUM == 501

// wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way
#if LUA_VERSION_NUM == 502
#define lua_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ)
#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0)
#endif // LUA_VERSION_NUM == 502

#ifdef NDEBUG
  #define _ASSERT_L(lua,c)  /*nothing*/
  #define STACK_CHECK(L)    /*nothing*/
  #define STACK_MID(L,c)    /*nothing*/
  #define STACK_END(L,c)    /*nothing*/
  #define STACK_DUMP(L)    /*nothing*/
  #define DEBUG()   /*nothing*/
  #define DEBUGEXEC(_code) {}  /*nothing*/
#else
  #define _ASSERT_L(lua,c)  do { if (!(c)) luaL_error( lua, "ASSERT failed: %s:%d '%s'", __FILE__, __LINE__, #c ); } while( 0)
  //
  #define STACK_CHECK(L)     { int _oldtop_##L = lua_gettop(L);
  #define STACK_MID(L,change)  { int a= lua_gettop(L)-_oldtop_##L; int b= (change); \
                               if (a != b) luaL_error( L, "STACK ASSERT failed (%d not %d): %s:%d", a, b, __FILE__, __LINE__ ); }
  #define STACK_END(L,change)  STACK_MID(L,change) }

  #define STACK_DUMP(L)    luaG_dump(L);
  #define DEBUG()   fprintf( stderr, "<<%s %d>>\n", __FILE__, __LINE__ );
  #define DEBUGEXEC(_code) {_code;}  /*nothing*/
#endif
#define ASSERT_L(c) _ASSERT_L(L,c)

#define STACK_GROW(L,n) do { if (!lua_checkstack(L,n)) luaL_error( L, "Cannot grow stack!" ); } while( 0)

#define LUAG_FUNC( func_name ) static int LG_##func_name( lua_State *L )

#define luaG_optunsigned(L,i,d) ((uint_t) luaL_optinteger(L,i,d))
#define luaG_tounsigned(L,i) ((uint_t) lua_tointeger(L,i))

#define luaG_isany(L,i)  (!lua_isnil(L,i))

typedef void (*luaG_IdFunction)( lua_State *L, char const * const which);

void luaG_dump( lua_State* L );

lua_State* luaG_newstate( char const* libs, lua_CFunction _on_state_create);

int luaG_deep_userdata( lua_State *L, luaG_IdFunction idfunc);
void *luaG_todeep( lua_State *L, luaG_IdFunction idfunc, int index );

typedef struct {
    volatile int refcount;
    void *deep;
} DEEP_PRELUDE;

void luaG_push_proxy( lua_State *L, luaG_IdFunction idfunc, DEEP_PRELUDE *deep_userdata );

int luaG_inter_copy( lua_State *L, lua_State *L2, uint_t n);
int luaG_inter_move( lua_State *L, lua_State *L2, uint_t n);

int luaG_nameof( lua_State* L);

// Lock for reference counter inc/dec locks (to be initialized by outside code)
//
extern MUTEX_T deep_lock;
extern MUTEX_T mtid_lock;

void populate_func_lookup_table( lua_State *L, int _i, char const *_name);
void serialize_require( lua_State *L);
extern MUTEX_T require_cs;

#endif
    // TOOLS_H