diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-04-21 10:46:50 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-04-21 10:46:50 +0200 |
commit | f360350bd874aeec0806c8df02c7a20a54c44406 (patch) | |
tree | 57b6502dab584fc535298bf33aa08eb2a1016929 | |
parent | 7f0ff9143159cfa76408ed3dfedb2d730b17fb46 (diff) |
lib: implement sleep(ms) function
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | lib.c | 23 |
2 files changed, 27 insertions, 1 deletions
@@ -1085,3 +1085,8 @@ When invoked with a second prototype argument, the given `proto` value is set as prototype on the array or object in `val`. Throws an exception if the given prototype value is not an object. + +#### 6.55. `sleep(milliseconds)` + +Pause execution for the given amount of milliseconds. Returns `false` if +an invalid value was passed, otherwise `true`. @@ -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 } }; |