-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluascript.c
More file actions
205 lines (126 loc) · 4.08 KB
/
luascript.c
File metadata and controls
205 lines (126 loc) · 4.08 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "luascript.h"
int luascript_do_filex(lua_State *L, const char *filename, const char *mode, int *thread_status)
{
int luaL_loadfilex_return = luaL_loadfilex(L, filename, mode);
if (luaL_loadfilex_return != LUA_OK)
{
return luaL_loadfilex_return;
}
int lua_pcall_return = lua_pcall(L, 0, LUA_MULTRET, 0);
if (thread_status != NULL)
{
*thread_status = lua_pcall_return;
}
return LUA_OK;
}
int luascript_error_messagex(lua_State *L, int status, bool can_exit, const char *file, int line)
{
if (status != LUA_OK && status != LUA_YIELD) /* or status > LUA_YIELD */
{
if (lua_isstring(L, -1) == 1)
{
const char *error_type = NULL;
switch (status)
{
case LUA_ERRRUN:
error_type = "Runtime Error";
break;
case LUA_ERRSYNTAX:
error_type = "Syntax Error";
break;
case LUA_ERRMEM:
error_type = "Memory Allocation Error";
break;
case LUA_ERRGCMM:
error_type = "Garbage Collector Error";
break;
case LUA_ERRERR:
error_type = "Error While Handling Error";
break;
case LUA_ERRFILE:
error_type = "File Error";
break;
}
const char *error_message = lua_tostring(L, -1);
fprintf(
stderr,
"[lua] %s from FILE %s on LINE %d: \"%s\".\n",
error_type,
file,
line,
error_message
);
}
if (can_exit)
{
exit(EXIT_FAILURE);
}
lua_pop(L, 1);
}
return status;
}
int luascript_serialize_pairs(lua_State *L, luascript_pair_descriptor_t *pairs, buffer_t *buffer, size_t *error_count, buffer_t *error_buffer)
{
for (luascript_pair_descriptor_t *pair_descriptor = pairs; pair_descriptor->type != LUA_TNONE; ++pair_descriptor)
{
if (pair_descriptor->field)
{
lua_getfield(L, -1, pair_descriptor->name);
}
else
{
lua_getglobal(L, pair_descriptor->name);
}
if (lua_type(L, -1) != pair_descriptor->type)
{
lua_pop(L, 1);
assert(0 && "TODO: update error_count and error_buffer.");
continue;
}
switch (pair_descriptor->type)
{
case LUA_TBOOLEAN:
{
bool bool_ = lua_toboolean(L, -1) == 1 ? true : false;
buffer_append(buffer, sizeof(bool), (char *)&bool_);
}
break;
case LUA_TNUMBER:
if (pair_descriptor->array_table_number_integer)
{
lua_Integer Integer = lua_tointeger(L, -1);
buffer_append(buffer, sizeof(lua_Integer), (char *)&Integer);
}
else
{
lua_Number Number = lua_tonumber(L, -1);
buffer_append(buffer, sizeof(lua_Number), (char *)&Number);
}
break;
case LUA_TSTRING:
{
const char *lua_string = lua_tostring(L, -1);
size_t lua_string_size = strlen(lua_string) + 1;
char *string = malloc(lua_string_size);
memcpy(string, lua_string, lua_string_size);
buffer_append(buffer, sizeof(char *), (char *)&string);
}
break;
case LUA_TTABLE:
if (pair_descriptor->array_table_number_integer)
{
assert(0 && "TODO: .");
}
else
{
luascript_serialize_pairs(L, pair_descriptor->pairs, buffer, error_count, error_buffer);
}
break;
}
lua_pop(L, 1);
}
return LUA_OK;
}
int luascript_deserialize_pairs(lua_State *L, luascript_pair_descriptor_t *pairs, buffer_t *buffer)
{
}