forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua-utils.cpp
More file actions
179 lines (150 loc) · 4.8 KB
/
Copy pathlua-utils.cpp
File metadata and controls
179 lines (150 loc) · 4.8 KB
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
178
179
#include "config.h"
#include "lua-utils.hpp"
#include "format.hpp"
extern "C"
{
#include <lauxlib.h>
}
#include <cassert>
// The lua_getextraspace() function is only available from Lua 5.3. For
// earlier versions we fall back to storing the context pointer in the
// Lua registry which is somewhat more effort so will be slower.
#if LUA_VERSION_NUM >= 503
void luaX_set_context(lua_State *lua_state, void *ptr) noexcept
{
assert(lua_state);
assert(ptr);
*static_cast<void **>(lua_getextraspace(lua_state)) = ptr;
}
void *luaX_get_context(lua_State *lua_state) noexcept
{
assert(lua_state);
return *static_cast<void **>(lua_getextraspace(lua_state));
}
#else
// Unique key for lua registry
static char const *osm2pgsql_output_flex = "osm2pgsql_output_flex";
void luaX_set_context(lua_State *lua_state, void *ptr) noexcept
{
assert(lua_state);
assert(ptr);
lua_pushlightuserdata(lua_state, (void *)osm2pgsql_output_flex);
lua_pushlightuserdata(lua_state, ptr);
lua_settable(lua_state, LUA_REGISTRYINDEX);
}
void *luaX_get_context(lua_State *lua_state) noexcept
{
assert(lua_state);
lua_pushlightuserdata(lua_state, (void *)osm2pgsql_output_flex);
lua_gettable(lua_state, LUA_REGISTRYINDEX);
auto *const ptr = lua_touserdata(lua_state, -1);
assert(ptr);
lua_pop(lua_state, 1);
return ptr;
}
#endif
void luaX_add_table_str(lua_State *lua_state, char const *key,
char const *value) noexcept
{
lua_pushstring(lua_state, key);
lua_pushstring(lua_state, value);
lua_rawset(lua_state, -3);
}
void luaX_add_table_str(lua_State *lua_state, char const *key,
char const *value, std::size_t size) noexcept
{
lua_pushstring(lua_state, key);
lua_pushlstring(lua_state, value, size);
lua_rawset(lua_state, -3);
}
void luaX_add_table_int(lua_State *lua_state, char const *key,
int64_t value) noexcept
{
lua_pushstring(lua_state, key);
lua_pushinteger(lua_state, value);
lua_rawset(lua_state, -3);
}
void luaX_add_table_num(lua_State *lua_state, char const *key,
double value) noexcept
{
lua_pushstring(lua_state, key);
lua_pushnumber(lua_state, value);
lua_rawset(lua_state, -3);
}
void luaX_add_table_bool(lua_State *lua_state, char const *key,
bool value) noexcept
{
lua_pushstring(lua_state, key);
lua_pushboolean(lua_state, value);
lua_rawset(lua_state, -3);
}
void luaX_add_table_func(lua_State *lua_state, char const *key,
lua_CFunction func) noexcept
{
lua_pushstring(lua_state, key);
lua_pushcfunction(lua_state, func);
lua_rawset(lua_state, -3);
}
char const *luaX_get_table_string(lua_State *lua_state, char const *key,
int table_index, char const *error_msg)
{
assert(lua_state);
assert(key);
assert(error_msg);
lua_getfield(lua_state, table_index, key);
if (!lua_isstring(lua_state, -1)) {
throw std::runtime_error{
"{} must contain a '{}' string field"_format(error_msg, key)};
}
return lua_tostring(lua_state, -1);
}
bool luaX_get_table_bool(lua_State *lua_state, char const *key, int table_index,
char const *error_msg, bool default_value)
{
assert(lua_state);
assert(key);
assert(error_msg);
lua_getfield(lua_state, table_index, key);
auto const type = lua_type(lua_state, -1);
if (type == LUA_TNIL) {
return default_value;
}
if (type == LUA_TBOOLEAN) {
return lua_toboolean(lua_state, -1);
}
throw std::runtime_error{
"{} must contain a '{}' boolean field"_format(error_msg, key)};
}
// Lua 5.1 doesn't support luaL_traceback, unless LuaJIT is used
#if LUA_VERSION_NUM < 502 && !defined(HAVE_LUAJIT)
int luaX_pcall(lua_State *lua_state, int narg, int nres)
{
return lua_pcall(lua_state, narg, nres, 0);
}
#else
static int pcall_error_traceback_handler(lua_State *lua_state)
{
assert(lua_state);
char const *msg = lua_tostring(lua_state, 1);
if (msg == nullptr) {
if (luaL_callmeta(lua_state, 1, "__tostring") &&
lua_type(lua_state, -1) == LUA_TSTRING) {
return 1;
}
msg = lua_pushfstring(lua_state, "(error object is a %s value)",
luaL_typename(lua_state, 1));
}
luaL_traceback(lua_state, lua_state, msg, 1);
return 1;
}
/// Wrapper function for lua_pcall() showing a stack trace on error.
int luaX_pcall(lua_State *lua_state, int narg, int nres)
{
int const base = lua_gettop(lua_state) - narg;
lua_pushcfunction(lua_state, pcall_error_traceback_handler);
lua_insert(lua_state, base);
int const status = lua_pcall(lua_state, narg, nres, base);
lua_remove(lua_state, base);
return status;
}
#endif