diff --git a/docs/modules/time.md b/docs/modules/time.md index 80d2690f..7a8afe28 100644 --- a/docs/modules/time.md +++ b/docs/modules/time.md @@ -5,4 +5,8 @@ label: time ### `time.time()` -Returns the current time in seconds since the epoch as a floating point number. \ No newline at end of file +Returns the current time in seconds since the epoch as a floating point number. + +### `time.sleep(secs)` + +Suspend execution of the calling thread for the given number of seconds. \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index f97e6295..a2b6072b 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -797,6 +797,17 @@ inline void add_module_time(VM* vm){ auto now = std::chrono::high_resolution_clock::now(); return VAR(std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); }); + + vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) { + f64 seconds = FLOAT(args[0]); + auto begin = std::chrono::high_resolution_clock::now(); + while(true){ + auto now = std::chrono::high_resolution_clock::now(); + f64 elapsed = std::chrono::duration_cast(now - begin).count() / 1000000.0; + if(elapsed >= seconds) break; + } + return vm->None; + }); } inline void add_module_sys(VM* vm){