-
Notifications
You must be signed in to change notification settings - Fork 9
/
ipc.c
124 lines (107 loc) · 3.26 KB
/
ipc.c
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
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "ipc.h"
IPC_LOCAL FILE* ipc_checkfile( lua_State* L, int idx ) {
#if LUA_VERSION_NUM == 501 /* Lua 5.1 / LuaJIT */
FILE** fp = luaL_checkudata( L, idx, LUA_FILEHANDLE );
if( *fp == NULL )
luaL_error( L, "attempt to use closed file" );
return *fp;
#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 503
luaL_Stream* s = luaL_checkudata( L, idx, LUA_FILEHANDLE );
if( s->closef == 0 || s->f == NULL )
luaL_error( L, "attempt to use closed file" );
return s->f;
#else
#error unsupported Lua version
#endif
}
IPC_LOCAL FILE* ipc_testfile( lua_State* L, int idx ) {
#if LUA_VERSION_NUM == 501 /* Lua 5.1 / LuaJIT */
FILE** fp = luaL_testudata( L, idx, LUA_FILEHANDLE );
if( fp == NULL )
return NULL;
else if( *fp == NULL )
luaL_error( L, "attempt to use closed filed" );
return *fp;
#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 503
luaL_Stream* s = luaL_testudata( L, idx, LUA_FILEHANDLE );
if( s == NULL )
return NULL;
else if( s->closef == 0 || s->f == NULL )
luaL_error( L, "attempt to use closed filed" );
return s->f;
#else
#error unsupported Lua version
#endif
}
IPC_LOCAL int ipc_getuservaluefield( lua_State* L, int idx,
char const* name ) {
luaL_checkstack( L, 2, "not enough stack space" );
lua_getuservalue( L, idx );
if( lua_type( L, -1 ) == LUA_TTABLE )
lua_getfield( L, -1, name );
else
lua_pushnil( L );
lua_replace( L, -2 );
return lua_type( L, -1 );
}
IPC_LOCAL void ipc_setuservaluefield( lua_State* L, int idx,
char const* name ) {
luaL_checkstack( L, 2, "not enough stack space" );
lua_getuservalue( L, idx );
if( lua_type( L, -1 ) != LUA_TTABLE )
luaL_error( L, "attempt to set field of non-table uservalue" );
lua_pushvalue( L, -2 );
lua_setfield( L, -2, name );
lua_pop( L, 2 );
}
IPC_LOCAL int ipc_err( char const* file, int line, char const* func,
int code ) {
if( code != 0 ) {
if( func != NULL )
fprintf( stderr, "[%s:%d] error return (%d) in function '%s'\n",
file, line, code, func );
else
fprintf( stderr, "[%s:%d]: error return (%d)\n",
file, line, code );
fflush( stderr );
}
return code;
}
/* implementation of compatibility functions */
#if LUA_VERSION_NUM == 501
IPC_LOCAL int ipc_absindex( lua_State* L, int idx ) {
if( idx < 0 && idx > LUA_REGISTRYINDEX )
idx += lua_gettop( L )+1;
return idx;
}
IPC_LOCAL void* ipc_testudata( lua_State* L, int idx,
char const* name ) {
void* p = lua_touserdata( L, idx );
if( p == NULL || !lua_getmetatable( L, idx ) )
return NULL;
else {
int res = 0;
luaL_getmetatable( L, name );
res = lua_rawequal( L, -1, -2 );
lua_pop( L, 2 );
if( !res )
p = NULL;
}
return p;
}
#endif /* LUA_VERSION_NUM == 501 */
#ifdef _WIN32
/* LuaRocks with MSVC can't really handle multiple modules in a single
* DLL, so we have to export the luaopen_ functions ourself, and let
* LuaRocks think that the ipc.dll contains the ipc module: */
IPC_API int luaopen_ipc( lua_State* L ) {
(void)L;
return 0;
}
#endif