This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpllua.c
177 lines (146 loc) · 4.6 KB
/
pllua.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* pllua.c: PL/Lua call handler, trusted
* Author: Luis Carvalho <lexcarvalho at gmail.com>
* Please check copyright notice at the bottom of pllua.h
* $Id: pllua.c,v 1.15 2008/03/29 02:49:55 carvalho Exp $
*/
#include "pllua.h"
#include "pllua_errors.h"
PG_MODULE_MAGIC;
static lua_State *LuaVM[2] = {NULL, NULL}; /* Lua VMs */
LVMInfo lvm_info[2];
static void init_vmstructs(){
LVMInfo lvm0;
LVMInfo lvm1;
lvm0.name = "untrusted";
lvm0.hasTraceback = 0;
lvm1.name = "trusted";
lvm1.hasTraceback = 0;
lvm_info[0] = lvm0;
lvm_info[1] = lvm1;
}
PGDLLEXPORT Datum _PG_init(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum _PG_fini(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum pllua_validator(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum pllua_call_handler(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum plluau_validator(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum plluau_call_handler(PG_FUNCTION_ARGS);
#if PG_VERSION_NUM >= 90000
PGDLLEXPORT Datum pllua_inline_handler(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum plluau_inline_handler(PG_FUNCTION_ARGS);
#endif
#include "pllua_xact_cleanup.h"
PG_FUNCTION_INFO_V1(_PG_init);
Datum _PG_init(PG_FUNCTION_ARGS) {
init_vmstructs();
pllua_init_common_ctx();
LuaVM[0] = luaP_newstate(0); /* untrusted */
LuaVM[1] = luaP_newstate(1); /* trusted */
RegisterXactCallback(pllua_xact_cb, NULL);
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(_PG_fini);
Datum _PG_fini(PG_FUNCTION_ARGS) {
luaP_close(LuaVM[0]);
luaP_close(LuaVM[1]);
pllua_delete_common_ctx();
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(plluau_validator);
Datum plluau_validator(PG_FUNCTION_ARGS) {
return luaP_validator(LuaVM[0], PG_GETARG_OID(0));
}
PG_FUNCTION_INFO_V1(plluau_call_handler);
Datum plluau_call_handler(PG_FUNCTION_ARGS) {
lvm_info[0].hasTraceback = false;
return luaP_callhandler(LuaVM[0], fcinfo);
}
PG_FUNCTION_INFO_V1(pllua_validator);
Datum pllua_validator(PG_FUNCTION_ARGS) {
return luaP_validator(LuaVM[1], PG_GETARG_OID(0));
}
PG_FUNCTION_INFO_V1(pllua_call_handler);
Datum pllua_call_handler(PG_FUNCTION_ARGS) {
lvm_info[1].hasTraceback = false;
return luaP_callhandler(LuaVM[1], fcinfo);
}
#if PG_VERSION_NUM >= 90000
#define CODEBLOCK \
((InlineCodeBlock *) DatumGetPointer(PG_GETARG_DATUM(0)))->source_text
PG_FUNCTION_INFO_V1(plluau_inline_handler);
Datum plluau_inline_handler(PG_FUNCTION_ARGS) {
lvm_info[0].hasTraceback = false;
return luaP_inlinehandler(LuaVM[0], CODEBLOCK);
}
PG_FUNCTION_INFO_V1(pllua_inline_handler);
Datum pllua_inline_handler(PG_FUNCTION_ARGS) {
lvm_info[1].hasTraceback = false;
return luaP_inlinehandler(LuaVM[1], CODEBLOCK);
}
#endif
/* p_lua_mem_cxt and p_lua_master_state addresses used as keys for storing
* values in lua states. In case when this functions are equal then
* visual studio compiler with default settings "optimize" it and as
* result both functions have equal addresses which make them unusable as keys,
* thats why return 1; return 2;
*/
int p_lua_mem_cxt(void){return 2;}
int p_lua_master_state(void){return 1;}
//------------------------------------------------------------------------------------------------------
MemoryContext luaP_getmemctxt(lua_State *L) {
MemoryContext mcxt;
lua_pushlightuserdata(L, p_lua_mem_cxt);
lua_rawget(L, LUA_REGISTRYINDEX);
mcxt = lua_touserdata(L, -1);
lua_pop(L, 1);
return mcxt;
}
lua_State * pllua_getmaster(lua_State *L) {
lua_State *master;
lua_pushlightuserdata(L, p_lua_master_state);
lua_rawget(L, LUA_REGISTRYINDEX);
master = (lua_State *) lua_touserdata(L, -1);
lua_pop(L, 1);
return master;
}
int pllua_getmaster_index(lua_State *L) {
if (pllua_getmaster(L) == LuaVM[0])
return 0;
return 1;
}
#if LUA_VERSION_NUM < 502
void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif
Oid pg_to_regtype(const char *typ_name)
{
Oid result;
int32 typmod;
/*
* Invoke the full parser to deal with special cases such as array syntax.
*/
#if PG_VERSION_NUM < 90400
PG_TRY();
{
parseTypeString(typ_name, &result, &typmod);
}
PG_CATCH();
{
result = InvalidOid;
}
PG_END_TRY();
#else
parseTypeString(typ_name, &result, &typmod, true);
#endif
return result;
}