release 0.1.13
[liba.git] / lua / src / lua.c
blobd7ad2fa1c979776d2b860efdbfd9aca9d8371799
1 #include "a.h"
3 void *lua_alloc(lua_State *L, void const *ptr_, size_t siz)
5 void *ud = (void *)(a_uptr)ptr_; // NOLINT
6 void *ptr = (void *)(a_uptr)ptr_; // NOLINT
7 return lua_getallocf(L, &ud)(ud, ptr, 0, siz);
10 void lua_registry_get(lua_State *L, int (*fn)(lua_State *))
12 lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)(a_uptr)fn); // NOLINT
15 void lua_registry_set(lua_State *L, int (*fn)(lua_State *))
17 lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)(a_uptr)fn); // NOLINT
20 void lua_fun_set(lua_State *L, int idx, char const *name, lua_CFunction func)
22 /* table[name]=func */
23 lua_pushstring(L, name);
24 lua_pushcclosure(L, func, 0);
25 lua_rawset(L, idx < 0 ? idx - 2 : idx);
28 void lua_fun_reg(lua_State *L, int idx, lua_fun const *tab, size_t len)
30 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
32 /* table[name]=func */
33 lua_pushstring(L, tab->name);
34 lua_pushcclosure(L, tab->func, 0);
35 lua_rawset(L, idx);
39 void lua_str_set(lua_State *L, int idx, char const *name, char const *data)
41 /* table[name]=data */
42 lua_pushstring(L, name);
43 lua_pushstring(L, data);
44 lua_rawset(L, idx < 0 ? idx - 2 : idx);
47 char const *lua_str_get(lua_State *L, int idx, char const *name)
49 /* data=table[name] */
50 lua_pushstring(L, name);
51 lua_rawget(L, idx < 0 ? idx - 1 : idx);
52 char const *s = lua_tostring(L, -1);
53 lua_pop(L, 1);
54 return s;
57 void lua_str_reg(lua_State *L, int idx, lua_str const *tab, size_t len)
59 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
61 /* table[name]=data */
62 lua_pushstring(L, tab->name);
63 lua_pushstring(L, tab->data);
64 lua_rawset(L, idx);
68 void lua_int_set(lua_State *L, int idx, char const *name, LUA_INT data)
70 /* table[name]=data */
71 lua_pushstring(L, name);
72 lua_pushinteger(L, (lua_Integer)data);
73 lua_rawset(L, idx < 0 ? idx - 2 : idx);
76 LUA_INT lua_int_get(lua_State *L, int idx, char const *name)
78 /* data=table[name] */
79 lua_pushstring(L, name);
80 lua_rawget(L, idx < 0 ? idx - 1 : idx);
81 LUA_INT x = (LUA_INT)lua_tointeger(L, -1);
82 lua_pop(L, 1);
83 return x;
86 void lua_int_reg(lua_State *L, int idx, lua_int const *tab, size_t len)
88 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
90 /* table[name]=data */
91 lua_pushstring(L, tab->name);
92 lua_pushinteger(L, (lua_Integer)tab->data);
93 lua_rawset(L, idx);
97 void lua_num_set(lua_State *L, int idx, char const *name, LUA_NUM data)
99 /* table[name]=data */
100 lua_pushstring(L, name);
101 lua_pushnumber(L, (lua_Number)data);
102 lua_rawset(L, idx < 0 ? idx - 2 : idx);
105 LUA_NUM lua_num_get(lua_State *L, int idx, char const *name)
107 /* data=table[name] */
108 lua_pushstring(L, name);
109 lua_rawget(L, idx < 0 ? idx - 1 : idx);
110 LUA_NUM x = (LUA_NUM)lua_tonumber(L, -1);
111 lua_pop(L, 1);
112 return x;
115 void lua_num_reg(lua_State *L, int idx, lua_num const *tab, size_t len)
117 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
119 /* table[name]=data */
120 lua_pushstring(L, tab->name);
121 lua_pushnumber(L, (lua_Number)tab->data);
122 lua_rawset(L, idx);
126 void lua_array_str_get(lua_State *L, int idx, char const **ptr, unsigned int num)
128 size_t len = (size_t)lua_rawlen(L, idx);
129 if (num > len) { num = (unsigned int)len; }
130 for (unsigned int i = num; i--; num = i)
132 lua_rawgeti(L, idx, (int)num);
133 ptr[i] = lua_tostring(L, -1);
134 lua_pop(L, 1);
138 void lua_array_str_set(lua_State *L, int idx, char const *const *ptr, unsigned int num)
140 idx = idx < 0 ? idx - 1 : idx;
141 for (unsigned int i = num; i--; num = i)
143 lua_pushstring(L, ptr[i]);
144 lua_rawseti(L, idx, (int)num);
148 void lua_array_str_new(lua_State *L, char const *const *ptr, unsigned int num)
150 lua_createtable(L, (int)num, 0);
151 for (unsigned int i = num; i--; num = i)
153 lua_pushstring(L, ptr[i]);
154 lua_rawseti(L, -2, (int)num);
158 void lua_array_int_get(lua_State *L, int idx, LUA_INT *ptr, unsigned int num)
160 size_t len = (size_t)lua_rawlen(L, idx);
161 if (num > len) { num = (unsigned int)len; }
162 for (unsigned int i = num; i--; num = i)
164 lua_rawgeti(L, idx, (int)num);
165 ptr[i] = (LUA_INT)lua_tointeger(L, -1);
166 lua_pop(L, 1);
170 void lua_array_int_set(lua_State *L, int idx, LUA_INT const *ptr, unsigned int num)
172 idx = idx < 0 ? idx - 1 : idx;
173 for (unsigned int i = num; i--; num = i)
175 lua_pushinteger(L, (lua_Integer)ptr[i]);
176 lua_rawseti(L, idx, (int)num);
180 void lua_array_int_new(lua_State *L, LUA_INT const *ptr, unsigned int num)
182 lua_createtable(L, (int)num, 0);
183 for (unsigned int i = num; i--; num = i)
185 lua_pushinteger(L, (lua_Integer)ptr[i]);
186 lua_rawseti(L, -2, (int)num);
190 void lua_array_num_get(lua_State *L, int idx, LUA_NUM *ptr, unsigned int num)
192 size_t len = (size_t)lua_rawlen(L, idx);
193 if (num > len) { num = (unsigned int)len; }
194 for (unsigned int i = num; i--; num = i)
196 lua_rawgeti(L, idx, (int)num);
197 ptr[i] = (LUA_NUM)lua_tonumber(L, -1);
198 lua_pop(L, 1);
202 void lua_array_num_set(lua_State *L, int idx, LUA_NUM const *ptr, unsigned int num)
204 idx = idx < 0 ? idx - 1 : idx;
205 for (unsigned int i = num; i--; num = i)
207 lua_pushnumber(L, (lua_Number)ptr[i]);
208 lua_rawseti(L, idx, (int)num);
212 void lua_array_num_new(lua_State *L, LUA_NUM const *ptr, unsigned int num)
214 lua_createtable(L, (int)num, 0);
215 for (unsigned int i = num; i--; num = i)
217 lua_pushnumber(L, (lua_Number)ptr[i]);
218 lua_rawseti(L, -2, (int)num);
222 size_t lua_table_num_len(lua_State *L, int idx);
223 size_t lua_table_num_len(lua_State *L, int idx) // NOLINT(misc-no-recursion)
225 size_t num = 0;
226 size_t n = (size_t)lua_rawlen(L, idx);
227 for (unsigned int i = 0; i++ != n;)
229 lua_rawgeti(L, idx, (int)i);
230 int e = lua_type(L, -1);
231 if (e == LUA_TNUMBER) { ++num; }
232 else if (e == LUA_TTABLE)
234 num += lua_table_num_len(L, -1);
236 lua_pop(L, 1);
238 return num;
241 LUA_NUM *lua_table_num_ptr(lua_State *L, int idx, LUA_NUM *ptr);
242 LUA_NUM *lua_table_num_ptr(lua_State *L, int idx, LUA_NUM *ptr) // NOLINT(misc-no-recursion)
244 size_t n = (size_t)lua_rawlen(L, idx);
245 for (unsigned int i = 0; i++ != n;)
247 lua_rawgeti(L, idx, (int)i);
248 int e = lua_type(L, -1);
249 if (e == LUA_TNUMBER)
251 *ptr++ = (LUA_NUM)lua_tonumber(L, -1);
253 else if (e == LUA_TTABLE)
255 ptr = lua_table_num_ptr(L, -1, ptr);
257 lua_pop(L, 1);
259 return ptr;
262 LUA_NUM *lua_table_num_get(lua_State *L, int idx, LUA_NUM const *ptr, size_t *num)
264 LUA_NUM *ret = (LUA_NUM *)(intptr_t)ptr; // NOLINT(performance-no-int-to-ptr)
265 if (lua_type(L, idx) == LUA_TTABLE)
267 size_t num_ = 0;
268 num = num ? num : &num_;
269 *num = lua_table_num_len(L, idx);
270 if (*num)
272 ret = (LUA_NUM *)lua_alloc(L, ret, sizeof(LUA_NUM) * *num);
273 lua_table_num_ptr(L, idx, ret);
276 return ret;
279 #include <stdio.h>
280 #include <stdlib.h>
281 #include <inttypes.h>
282 #if defined(_MSC_VER) && (_MSC_VER < 1900)
283 #define snprintf sprintf_s
284 #endif /* _MSC_VER */
286 void lua_u8_new(lua_State *L, a_u8 value)
288 lua_pushinteger(L, (lua_Integer)value);
291 a_u8 lua_u8_get(lua_State *L, int idx)
293 a_u8 value = 0;
294 switch (lua_type(L, idx))
296 default: A_FALLTHROUGH;
297 case LUA_TNUMBER: value = (a_u8)luaL_checkinteger(L, idx); break;
298 case LUA_TSTRING: value = (a_u8)strtoul(lua_tostring(L, idx), NULL, 0);
300 return value;
303 void lua_u16_new(lua_State *L, a_u16 value)
305 lua_pushinteger(L, (lua_Integer)value);
308 a_u16 lua_u16_get(lua_State *L, int idx)
310 a_u16 value = 0;
311 switch (lua_type(L, idx))
313 default: A_FALLTHROUGH;
314 case LUA_TNUMBER: value = (a_u16)luaL_checkinteger(L, idx); break;
315 case LUA_TSTRING: value = (a_u16)strtoul(lua_tostring(L, idx), NULL, 0);
317 return value;
320 void lua_u32_new(lua_State *L, a_u32 value)
322 char buf[8 + 3];
323 (void)snprintf(buf, sizeof(buf), "0x%08" PRIX32, value);
324 lua_pushstring(L, buf);
327 a_u32 lua_u32_get(lua_State *L, int idx)
329 a_u32 value = 0;
330 switch (lua_type(L, idx))
332 default: A_FALLTHROUGH;
333 case LUA_TNUMBER: value = (a_u32)luaL_checkinteger(L, idx); break;
334 case LUA_TSTRING: value = (a_u32)strtoul(lua_tostring(L, idx), NULL, 0);
336 return value;
339 void lua_u64_new(lua_State *L, a_u64 value)
341 char buf[16 + 3];
342 (void)snprintf(buf, sizeof(buf), "0x%016" PRIX64, value);
343 lua_pushstring(L, buf);
346 a_u64 lua_u64_get(lua_State *L, int idx)
348 a_u64 value = 0;
349 switch (lua_type(L, idx))
351 default: A_FALLTHROUGH;
352 case LUA_TNUMBER: value = (a_u64)luaL_checkinteger(L, idx); break;
353 #if ULONG_MAX > A_U32_MAX
354 case LUA_TSTRING: value = (a_u64)strtoul(lua_tostring(L, idx), NULL, 0);
355 #else /* >long */
356 case LUA_TSTRING: value = (a_u64)strtoull(lua_tostring(L, idx), NULL, 0);
357 #endif /* long */
359 return value;
362 void lua_array_u8_new(lua_State *L, a_u8 const *ptr, unsigned int num)
364 lua_createtable(L, (int)num, 0);
365 for (unsigned int i = num; i--; num = i)
367 lua_u8_new(L, ptr[i]);
368 lua_rawseti(L, -2, (int)num);
372 void lua_array_u16_new(lua_State *L, a_u16 const *ptr, unsigned int num)
374 lua_createtable(L, (int)num, 0);
375 for (unsigned int i = num; i--; num = i)
377 lua_u16_new(L, ptr[i]);
378 lua_rawseti(L, -2, (int)num);
382 void lua_array_u32_new(lua_State *L, a_u32 const *ptr, unsigned int num)
384 lua_createtable(L, (int)num, 0);
385 for (unsigned int i = num; i--; num = i)
387 lua_u32_new(L, ptr[i]);
388 lua_rawseti(L, -2, (int)num);
392 void lua_array_u64_new(lua_State *L, a_u64 const *ptr, unsigned int num)
394 lua_createtable(L, (int)num, 0);
395 for (unsigned int i = num; i--; num = i)
397 lua_u64_new(L, ptr[i]);
398 lua_rawseti(L, -2, (int)num);
402 #if defined(LUA_STACK)
403 void lua_stack_view(lua_State *L, unsigned int line)
405 int const n = lua_gettop(L);
406 printf("%u:", line);
407 for (int i = 0; i++ != n;)
409 putchar(' ');
410 switch (lua_type(L, i))
412 case LUA_TBOOLEAN:
413 printf("%s", lua_toboolean(L, i) ? "true" : "false");
414 break;
415 case LUA_TLIGHTUSERDATA:
416 printf("ptr:%p", lua_topointer(L, i));
417 break;
418 case LUA_TNUMBER:
419 printf("%g", lua_tonumber(L, i));
420 break;
421 case LUA_TSTRING:
422 printf("\"%s\"", lua_tostring(L, i));
423 break;
424 case LUA_TTABLE:
425 printf("tab:%p", lua_topointer(L, i));
426 break;
427 case LUA_TFUNCTION:
429 union
431 int (*fn)(lua_State *);
432 void *p;
433 } func = {lua_tocfunction(L, i)};
434 printf("func:%p", func.p);
435 break;
437 case LUA_TUSERDATA:
438 printf("data:%p", lua_touserdata(L, i));
439 break;
440 case LUA_TTHREAD:
441 printf("thrd:%p", (void *)lua_tothread(L, i));
442 break;
443 default: printf("%s", "nil");
446 putchar('\n');
448 #endif /* LUA_STACK */