-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlextra.c
69 lines (57 loc) · 1.13 KB
/
lextra.c
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
/*
* Callisto - standalone scripting platform for Lua 5.4
* Copyright (c) 2023-2024 Jeremy Baxter.
*/
/***
* Extra functions that don't fit into any other category.
*
* This module does not provide its own table;
* all functions here are found in the global table.
*
* @module extra
*/
#include <time.h>
#include <lua/lauxlib.h>
#include <lua/lua.h>
/***
* Waits the specified amount of seconds.
*
* @function sleep
* @usage
local minutes = 5
sleep(minutes * 60) -- 5 minutes
* @tparam number seconds The amount of seconds to wait.
*/
static int
extra_sleep(lua_State *L)
{
/* Implementation from luasocket */
double n;
struct timespec t, r;
n = luaL_checknumber(L, 1);
if (n < 0.0)
n = 0.0;
if (n > INT_MAX)
n = INT_MAX;
t.tv_sec = (int)n;
n -= t.tv_sec;
t.tv_nsec = (int)(n * 1000000000);
if (t.tv_nsec >= 1000000000)
t.tv_nsec = 999999999;
while (nanosleep(&t, &r) != 0) {
t.tv_sec = r.tv_sec;
t.tv_nsec = r.tv_nsec;
}
return 0;
}
/* clang-format off */
static const luaL_Reg extlib[] = {
{"sleep", extra_sleep},
{NULL, NULL}
};
int
luaopen_extra(lua_State *L)
{
luaL_newlib(L, extlib);
return 1;
}