1 #if !defined _GNU_SOURCE && defined(__linux__)
3 #endif /* _GNU_SOURCE */
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);
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] */
54 lua_getfield(L
, idx
, name
);
55 s
= lua_tostring(L
, -1);
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
);
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] */
83 lua_getfield(L
, idx
, name
);
84 x
= lua_tointeger(L
, -1);
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
);
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] */
112 lua_getfield(L
, idx
, name
);
113 x
= lua_tonumber(L
, -1);
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
);
129 unsigned int lua_array_len(lua_State
*L
, int idx
)
131 if (luaL_callmeta(L
, idx
, "__len"))
134 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 501)
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 */
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);
162 void lua_array_str_set(lua_State
*L
, int idx
, char const *const *ptr
, unsigned int num
)
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 */
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
)
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 */
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);
206 void lua_array_int_set(lua_State
*L
, int idx
, LUA_INT
const *ptr
, unsigned int num
)
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 */
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
)
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 */
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
;)
244 lua_geti(L
, idx
, (lua_Integer
)i
);
246 if (e
== LUA_TNUMBER
) { ++num
; }
247 else if (e
== LUA_TTABLE
&& dim
> 0)
249 num
+= lua_array_num_len(L
, -1, dim
);
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
;)
262 lua_geti(L
, idx
, (lua_Integer
)i
);
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
);
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_
; }
284 p
= (LUA_NUM
*)lua_alloc(L
, p
, sizeof(LUA_NUM
) * n
);
286 lua_array_num_ptr(L
, idx
, p
, dim
);
291 void lua_array_num_set(lua_State
*L
, int idx
, LUA_NUM
const *ptr
, unsigned int num
)
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 */
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
)
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 */
317 #endif /* LUA_VERSION_NUM */
318 lua_pushnumber(L
, (lua_Number
)ptr
[i
]);
319 lua_rawseti(L
, -2, n
);
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
)
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);
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
)
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);
363 void lua_u32_new(lua_State
*L
, a_u32 value
)
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
)
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);
382 void lua_u64_new(lua_State
*L
, a_u64 value
)
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
)
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);
403 case LUA_TSTRING
: value
= (a_u64
)strtoull(lua_tostring(L
, idx
), NULL
, 0);
409 void lua_array_u8_new(lua_State
*L
, a_u8
const *ptr
, unsigned int num
)
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 */
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
)
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 */
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
)
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 */
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
)
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 */
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
)
477 int const n
= lua_gettop(L
);
479 for (i
= 0; i
++ != n
;)
482 switch (lua_type(L
, i
))
485 printf("%s", lua_toboolean(L
, i
) ? "true" : "false");
487 case LUA_TLIGHTUSERDATA
:
488 printf("ptr:%p", lua_topointer(L
, i
));
491 printf("%g", lua_tonumber(L
, i
));
494 printf("\"%s\"", lua_tostring(L
, i
));
497 printf("tab:%p", lua_topointer(L
, i
));
503 int (*fn
)(lua_State
*);
505 } func
= {lua_tocfunction(L
, i
)};
506 printf("func:%p", func
.p
);
510 printf("data:%p", lua_touserdata(L
, i
));
513 printf("thrd:%p", (void *)lua_tothread(L
, i
));
515 default: printf("%s", "nil");
520 #endif /* LUA_STACK */