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
|
/*
* TOOLS.H
*/
#ifndef TOOLS_H
#define TOOLS_H
#include "lauxlib.h"
#include "threading.h"
#include "deep.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)
#define LUA_OK 0
#define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value
void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources
#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
#ifndef lua_equal // already defined when compatibility is active in luaconf.h
#define lua_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ)
#endif // lua_equal
#ifndef lua_lessthan // already defined when compatibility is active in luaconf.h
#define lua_lessthan( L, a, b) lua_compare( L, a, b, LUA_OPLT)
#endif // lua_lessthan
#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0)
#endif // LUA_VERSION_NUM == 502
// For some reason, LuaJIT 64bits doesn't support lua_newstate()
// If you build specifically for this situation, change value to 0
#define PROPAGATE_ALLOCF 1
#if PROPAGATE_ALLOCF
#define PROPAGATE_ALLOCF_PREP( L) void* allocUD; lua_Alloc allocF = lua_getallocf( L, &allocUD)
#define PROPAGATE_ALLOCF_ALLOC() lua_newstate( allocF, allocUD)
#else // PROPAGATE_ALLOCF
#define PROPAGATE_ALLOCF_PREP( L)
#define PROPAGATE_ALLOCF_ALLOC() luaL_newstate()
#endif // PROPAGATE_ALLOCF
#define USE_DEBUG_SPEW 0
#if USE_DEBUG_SPEW
extern char const* debugspew_indent;
extern int debugspew_indent_depth;
#define INDENT_BEGIN "%.*s "
#define INDENT_END , debugspew_indent_depth, debugspew_indent
#define DEBUGSPEW_CODE(_code) _code
#else // USE_DEBUG_SPEW
#define DEBUGSPEW_CODE(_code)
#endif // USE_DEBUG_SPEW
#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*/
#else
void ASSERT_IMPL( lua_State* L, bool_t cond_, char const* file_, int const line_, char const* text_);
#define _ASSERT_L(lua,c) ASSERT_IMPL( lua, (c) != 0, __FILE__, __LINE__, #c)
//
#define STACK_CHECK(L) { int const _oldtop_##L = lua_gettop( L)
#define STACK_MID(L,change) \
do \
{ \
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__ ); \
} while( 0)
#define STACK_END(L,change) STACK_MID(L,change); }
#define STACK_DUMP( L) luaG_dump( L)
#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))
void luaG_dump( lua_State* L );
lua_State* luaG_newstate( lua_State* _from, char const* libs);
void luaG_copy_one_time_settings( lua_State* L, lua_State* L2, char const* name_);
typedef struct {
volatile int refcount;
void *deep;
} DEEP_PRELUDE;
enum eLookupMode
{
eLM_LaneBody, // send the lane body directly from the source to the destination lane
eLM_ToKeeper, // send a function from a lane to a keeper state
eLM_FromKeeper // send a function from a keeper state to a lane
};
void luaG_push_proxy( lua_State *L, luaG_IdFunction idfunc, DEEP_PRELUDE *deep_userdata);
void luaG_inter_copy_package( lua_State* L, lua_State* L2, int _idx, enum eLookupMode mode_);
int luaG_inter_copy( lua_State *L, lua_State *L2, uint_t n, enum eLookupMode mode_);
int luaG_inter_move( lua_State *L, lua_State *L2, uint_t n, enum eLookupMode mode_);
int luaG_nameof( lua_State* L);
int luaG_new_require( 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);
int initialize_on_state_create( lua_State *L);
extern MUTEX_T require_cs;
// for verbose errors
extern bool_t GVerboseErrors;
extern char const* const CONFIG_REGKEY;
extern char const* const LOOKUP_REGKEY;
#endif // TOOLS_H
|