summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-04-21 10:46:50 +0200
committerJo-Philipp Wich <jo@mein.io>2021-04-21 10:46:50 +0200
commitf360350bd874aeec0806c8df02c7a20a54c44406 (patch)
tree57b6502dab584fc535298bf33aa08eb2a1016929 /lib.c
parent7f0ff9143159cfa76408ed3dfedb2d730b17fb46 (diff)
lib: implement sleep(ms) function
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/lib.c b/lib.c
index ea48e34..a4cfdea 100644
--- a/lib.c
+++ b/lib.c
@@ -2393,6 +2393,26 @@ uc_proto(uc_vm *vm, size_t nargs)
return ref ? uc_value_get(ref->header.jso) : NULL;
}
+static json_object *
+uc_sleep(uc_vm *vm, size_t nargs)
+{
+ json_object *duration = uc_get_arg(0);
+ struct timeval tv;
+ int64_t ms;
+
+ ms = uc_cast_int64(duration);
+
+ if (errno != 0 || ms <= 0)
+ return xjs_new_boolean(false);
+
+ tv.tv_sec = ms / 1000;
+ tv.tv_usec = (ms % 1000) * 1000;
+
+ select(0, NULL, NULL, NULL, &tv);
+
+ return xjs_new_boolean(true);
+}
+
static const uc_cfunction_list functions[] = {
{ "chr", uc_chr },
{ "delete", uc_delete },
@@ -2441,7 +2461,8 @@ static const uc_cfunction_list functions[] = {
{ "warn", uc_warn },
{ "system", uc_system },
{ "trace", uc_trace },
- { "proto", uc_proto }
+ { "proto", uc_proto },
+ { "sleep", uc_sleep }
};