fix atan2,rename {hypot,hypot3} to {norm2,norm3}
[liba.git] / lua / src / lua.c
blobe74e02da9abd9a2ef0f6b0cbc6a6b0a1c64882f0
1 #if !defined _GNU_SOURCE && defined(__linux__)
2 #define _GNU_SOURCE 1
3 #endif /* _GNU_SOURCE */
4 #include "a.h"
6 void *lua_alloc(lua_State *L, void const *ptr_, size_t siz)
8 void *ud = (void *)(a_uptr)ptr_; /* NOLINT */
9 void *ptr = (void *)(a_uptr)ptr_; /* NOLINT */
10 return lua_getallocf(L, &ud)(ud, ptr, 0, siz);
13 void lua_registry_get(lua_State *L, int (*fn)(lua_State *))
15 lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)(a_uptr)fn); /* NOLINT */
18 void lua_registry_set(lua_State *L, int (*fn)(lua_State *))
20 lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)(a_uptr)fn); /* NOLINT */
23 void lua_fun_set(lua_State *L, int idx, char const *name, lua_CFunction func)
25 /* table[name]=func */
26 lua_pushstring(L, name);
27 lua_pushcclosure(L, func, 0);
28 lua_rawset(L, idx < 0 ? idx - 2 : idx);
31 void lua_fun_reg(lua_State *L, int idx, lua_fun const *tab, size_t len)
33 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
35 /* table[name]=func */
36 lua_pushstring(L, tab->name);
37 lua_pushcclosure(L, tab->func, 0);
38 lua_rawset(L, idx);
42 void lua_str_set(lua_State *L, int idx, char const *name, char const *data)
44 /* table[name]=data */
45 lua_pushstring(L, name);
46 lua_pushstring(L, data);
47 lua_rawset(L, idx < 0 ? idx - 2 : idx);
50 char const *lua_str_get(lua_State *L, int idx, char const *name)
52 /* data=table[name] */
53 char const *s;
54 lua_getfield(L, idx, name);
55 s = lua_tostring(L, -1);
56 lua_pop(L, 1);
57 return s;
60 void lua_str_reg(lua_State *L, int idx, lua_str const *tab, size_t len)
62 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
64 /* table[name]=data */
65 lua_pushstring(L, tab->name);
66 lua_pushstring(L, tab->data);
67 lua_rawset(L, idx);
71 void lua_int_set(lua_State *L, int idx, char const *name, LUA_INT data)
73 /* table[name]=data */
74 lua_pushstring(L, name);
75 lua_pushinteger(L, (lua_Integer)data);
76 lua_rawset(L, idx < 0 ? idx - 2 : idx);
79 LUA_INT lua_int_get(lua_State *L, int idx, char const *name)
81 /* data=table[name] */
82 lua_Integer x;
83 lua_getfield(L, idx, name);
84 x = lua_tointeger(L, -1);
85 lua_pop(L, 1);
86 return (LUA_INT)x;
89 void lua_int_reg(lua_State *L, int idx, lua_int const *tab, size_t len)
91 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
93 /* table[name]=data */
94 lua_pushstring(L, tab->name);
95 lua_pushinteger(L, (lua_Integer)tab->data);
96 lua_rawset(L, idx);
100 void lua_num_set(lua_State *L, int idx, char const *name, LUA_NUM data)
102 /* table[name]=data */
103 lua_pushstring(L, name);
104 lua_pushnumber(L, (lua_Number)data);
105 lua_rawset(L, idx < 0 ? idx - 2 : idx);
108 LUA_NUM lua_num_get(lua_State *L, int idx, char const *name)
110 /* data=table[name] */
111 lua_Number x;
112 lua_getfield(L, idx, name);
113 x = lua_tonumber(L, -1);
114 lua_pop(L, 1);
115 return (LUA_NUM)x;
118 void lua_num_reg(lua_State *L, int idx, lua_num const *tab, size_t len)
120 for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
122 /* table[name]=data */
123 lua_pushstring(L, tab->name);
124 lua_pushnumber(L, (lua_Number)tab->data);
125 lua_rawset(L, idx);
129 unsigned int lua_array_len(lua_State *L, int idx)
131 if (luaL_callmeta(L, idx, "__len"))
133 unsigned int n;
134 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 501)
135 int isnum;
136 n = (unsigned int)lua_tointegerx(L, -1, &isnum);
137 if (A_UNLIKELY(!isnum))
139 luaL_error(L, "object length is not an integer");
141 #else /* !LUA_VERSION_NUM */
142 n = (unsigned int)lua_tointeger(L, -1);
143 #endif /* LUA_VERSION_NUM */
144 lua_pop(L, 1);
145 return n;
147 return (unsigned int)lua_rawlen(L, idx);
150 void lua_array_str_get(lua_State *L, int idx, char const **ptr, unsigned int num)
152 unsigned int i, n = lua_array_len(L, idx);
153 if (num < n) { n = num; }
154 for (i = n; i--; n = i)
156 lua_geti(L, idx, (lua_Integer)n);
157 ptr[i] = lua_tostring(L, -1);
158 lua_pop(L, 1);
162 void lua_array_str_set(lua_State *L, int idx, char const *const *ptr, unsigned int num)
164 unsigned int i;
165 idx = idx < 0 ? idx - 1 : idx;
166 for (i = num; i--; num = i)
168 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
169 lua_Integer n = (lua_Integer)num;
170 #else /* !LUA_VERSION_NUM */
171 int n = (int)num;
172 #endif /* LUA_VERSION_NUM */
173 lua_pushstring(L, ptr[i]);
174 lua_rawseti(L, idx, n);
178 void lua_array_str_new(lua_State *L, char const *const *ptr, unsigned int num)
180 unsigned int i;
181 lua_createtable(L, (int)num, 0);
182 for (i = num; i--; num = i)
184 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
185 lua_Integer n = (lua_Integer)num;
186 #else /* !LUA_VERSION_NUM */
187 int n = (int)num;
188 #endif /* LUA_VERSION_NUM */
189 lua_pushstring(L, ptr[i]);
190 lua_rawseti(L, -2, n);
194 void lua_array_int_get(lua_State *L, int idx, LUA_INT *ptr, unsigned int num)
196 unsigned int i, n = lua_array_len(L, idx);
197 if (num < n) { n = num; }
198 for (i = n; i--; n = i)
200 lua_geti(L, idx, (lua_Integer)n);
201 ptr[i] = (LUA_INT)lua_tointeger(L, -1);
202 lua_pop(L, 1);
206 void lua_array_int_set(lua_State *L, int idx, LUA_INT const *ptr, unsigned int num)
208 unsigned int i;
209 idx = idx < 0 ? idx - 1 : idx;
210 for (i = num; i--; num = i)
212 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
213 lua_Integer n = (lua_Integer)num;
214 #else /* !LUA_VERSION_NUM */
215 int n = (int)num;
216 #endif /* LUA_VERSION_NUM */
217 lua_pushinteger(L, (lua_Integer)ptr[i]);
218 lua_rawseti(L, idx, n);
222 void lua_array_int_new(lua_State *L, LUA_INT const *ptr, unsigned int num)
224 unsigned int i;
225 lua_createtable(L, (int)num, 0);
226 for (i = num; i--; num = i)
228 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
229 lua_Integer n = (lua_Integer)num;
230 #else /* !LUA_VERSION_NUM */
231 int n = (int)num;
232 #endif /* LUA_VERSION_NUM */
233 lua_pushinteger(L, (lua_Integer)ptr[i]);
234 lua_rawseti(L, -2, n);
238 unsigned int lua_array_num_len(lua_State *L, int idx, int dim) /* NOLINT(misc-no-recursion) */
240 unsigned int i = 0, n = lua_array_len(L, idx), num = 0;
241 for (--dim; i++ != n;)
243 int e;
244 lua_geti(L, idx, (lua_Integer)i);
245 e = lua_type(L, -1);
246 if (e == LUA_TNUMBER) { ++num; }
247 else if (e == LUA_TTABLE && dim > 0)
249 num += lua_array_num_len(L, -1, dim);
251 lua_pop(L, 1);
253 return num;
256 LUA_NUM *lua_array_num_ptr(lua_State *L, int idx, LUA_NUM *ptr, int dim) /* NOLINT(misc-no-recursion) */
258 unsigned int i = 0, n = lua_array_len(L, idx);
259 for (--dim; i++ != n;)
261 int e;
262 lua_geti(L, idx, (lua_Integer)i);
263 e = lua_type(L, -1);
264 if (e == LUA_TNUMBER)
266 *ptr++ = (LUA_NUM)lua_tonumber(L, -1);
268 else if (e == LUA_TTABLE && dim > 0)
270 ptr = lua_array_num_ptr(L, -1, ptr, dim);
272 lua_pop(L, 1);
274 return ptr;
277 LUA_NUM *lua_array_num_get(lua_State *L, int idx, LUA_NUM const *ptr, unsigned int *num, int dim)
279 LUA_NUM *p = (LUA_NUM *)(a_uptr)ptr; /* NOLINT(performance-no-int-to-ptr) */
280 unsigned int n = lua_array_num_len(L, idx, dim), n_ = 0;
281 if (!num) { num = &n_; }
282 if (n > *num)
284 p = (LUA_NUM *)lua_alloc(L, p, sizeof(LUA_NUM) * n);
286 lua_array_num_ptr(L, idx, p, dim);
287 *num = n;
288 return p;
291 void lua_array_num_set(lua_State *L, int idx, LUA_NUM const *ptr, unsigned int num)
293 unsigned int i;
294 idx = idx < 0 ? idx - 1 : idx;
295 for (i = num; i--; num = i)
297 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
298 lua_Integer n = (lua_Integer)num;
299 #else /* !LUA_VERSION_NUM */
300 int n = (int)num;
301 #endif /* LUA_VERSION_NUM */
302 lua_pushnumber(L, (lua_Number)ptr[i]);
303 lua_rawseti(L, idx, n);
307 void lua_array_num_new(lua_State *L, LUA_NUM const *ptr, unsigned int num)
309 unsigned int i;
310 lua_createtable(L, (int)num, 0);
311 for (i = num; i--; num = i)
313 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
314 lua_Integer n = (lua_Integer)num;
315 #else /* !LUA_VERSION_NUM */
316 int n = (int)num;
317 #endif /* LUA_VERSION_NUM */
318 lua_pushnumber(L, (lua_Number)ptr[i]);
319 lua_rawseti(L, -2, n);
323 #include <stdio.h>
324 #include <stdlib.h>
325 #if defined(_MSC_VER) && (_MSC_VER < 1900)
326 #define snprintf sprintf_s
327 #endif /* _MSC_VER */
329 void lua_u8_new(lua_State *L, a_u8 value)
331 lua_pushinteger(L, (lua_Integer)value);
334 a_u8 lua_u8_get(lua_State *L, int idx)
336 a_u8 value = 0;
337 switch (lua_type(L, idx))
339 default: A_FALLTHROUGH;
340 case LUA_TNUMBER: value = (a_u8)luaL_checkinteger(L, idx); break;
341 case LUA_TSTRING: value = (a_u8)strtoul(lua_tostring(L, idx), NULL, 0);
343 return value;
346 void lua_u16_new(lua_State *L, a_u16 value)
348 lua_pushinteger(L, (lua_Integer)value);
351 a_u16 lua_u16_get(lua_State *L, int idx)
353 a_u16 value = 0;
354 switch (lua_type(L, idx))
356 default: A_FALLTHROUGH;
357 case LUA_TNUMBER: value = (a_u16)luaL_checkinteger(L, idx); break;
358 case LUA_TSTRING: value = (a_u16)strtoul(lua_tostring(L, idx), NULL, 0);
360 return value;
363 void lua_u32_new(lua_State *L, a_u32 value)
365 char buf[8 + 3];
366 (void)snprintf(buf, sizeof(buf), "0x%08" A_PRI32 "X", value);
367 lua_pushstring(L, buf);
370 a_u32 lua_u32_get(lua_State *L, int idx)
372 a_u32 value = 0;
373 switch (lua_type(L, idx))
375 default: A_FALLTHROUGH;
376 case LUA_TNUMBER: value = (a_u32)luaL_checkinteger(L, idx); break;
377 case LUA_TSTRING: value = (a_u32)strtoul(lua_tostring(L, idx), NULL, 0);
379 return value;
382 void lua_u64_new(lua_State *L, a_u64 value)
384 char buf[16 + 3];
385 (void)snprintf(buf, sizeof(buf), "0x%016" A_PRI64 "X", value);
386 lua_pushstring(L, buf);
389 a_u64 lua_u64_get(lua_State *L, int idx)
391 a_u64 value = 0;
392 switch (lua_type(L, idx))
394 default: A_FALLTHROUGH;
395 case LUA_TNUMBER: value = (a_u64)luaL_checkinteger(L, idx); break;
396 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
397 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
398 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
399 case LUA_TSTRING: value = _strtoui64(lua_tostring(L, idx), NULL, 0);
400 #elif ULONG_MAX > 0xFFFFFFFFUL
401 case LUA_TSTRING: value = (a_u64)strtoul(lua_tostring(L, idx), NULL, 0);
402 #else /* >long */
403 case LUA_TSTRING: value = (a_u64)strtoull(lua_tostring(L, idx), NULL, 0);
404 #endif /* long */
406 return value;
409 void lua_array_u8_new(lua_State *L, a_u8 const *ptr, unsigned int num)
411 unsigned int i;
412 lua_createtable(L, (int)num, 0);
413 for (i = num; i--; num = i)
415 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
416 lua_Integer n = (lua_Integer)num;
417 #else /* !LUA_VERSION_NUM */
418 int n = (int)num;
419 #endif /* LUA_VERSION_NUM */
420 lua_u8_new(L, ptr[i]);
421 lua_rawseti(L, -2, n);
425 void lua_array_u16_new(lua_State *L, a_u16 const *ptr, unsigned int num)
427 unsigned int i;
428 lua_createtable(L, (int)num, 0);
429 for (i = num; i--; num = i)
431 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
432 lua_Integer n = (lua_Integer)num;
433 #else /* !LUA_VERSION_NUM */
434 int n = (int)num;
435 #endif /* LUA_VERSION_NUM */
436 lua_u16_new(L, ptr[i]);
437 lua_rawseti(L, -2, n);
441 void lua_array_u32_new(lua_State *L, a_u32 const *ptr, unsigned int num)
443 unsigned int i;
444 lua_createtable(L, (int)num, 0);
445 for (i = num; i--; num = i)
447 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
448 lua_Integer n = (lua_Integer)num;
449 #else /* !LUA_VERSION_NUM */
450 int n = (int)num;
451 #endif /* LUA_VERSION_NUM */
452 lua_u32_new(L, ptr[i]);
453 lua_rawseti(L, -2, n);
457 void lua_array_u64_new(lua_State *L, a_u64 const *ptr, unsigned int num)
459 unsigned int i;
460 lua_createtable(L, (int)num, 0);
461 for (i = num; i--; num = i)
463 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 502)
464 lua_Integer n = (lua_Integer)num;
465 #else /* !LUA_VERSION_NUM */
466 int n = (int)num;
467 #endif /* LUA_VERSION_NUM */
468 lua_u64_new(L, ptr[i]);
469 lua_rawseti(L, -2, n);
473 #if defined(LUA_STACK)
474 void lua_stack_view(lua_State *L, unsigned int line)
476 int i;
477 int const n = lua_gettop(L);
478 printf("%u:", line);
479 for (i = 0; i++ != n;)
481 putchar(' ');
482 switch (lua_type(L, i))
484 case LUA_TBOOLEAN:
485 printf("%s", lua_toboolean(L, i) ? "true" : "false");
486 break;
487 case LUA_TLIGHTUSERDATA:
488 printf("ptr:%p", lua_topointer(L, i));
489 break;
490 case LUA_TNUMBER:
491 printf("%g", lua_tonumber(L, i));
492 break;
493 case LUA_TSTRING:
494 printf("\"%s\"", lua_tostring(L, i));
495 break;
496 case LUA_TTABLE:
497 printf("tab:%p", lua_topointer(L, i));
498 break;
499 case LUA_TFUNCTION:
501 union
503 int (*fn)(lua_State *);
504 void *p;
505 } func = {lua_tocfunction(L, i)};
506 printf("func:%p", func.p);
507 break;
509 case LUA_TUSERDATA:
510 printf("data:%p", lua_touserdata(L, i));
511 break;
512 case LUA_TTHREAD:
513 printf("thrd:%p", (void *)lua_tothread(L, i));
514 break;
515 default: printf("%s", "nil");
518 putchar('\n');
520 #endif /* LUA_STACK */