diff --git a/crates/lb/Cargo.toml b/crates/lb/Cargo.toml index e51a74c..25da4a5 100644 --- a/crates/lb/Cargo.toml +++ b/crates/lb/Cargo.toml @@ -8,7 +8,7 @@ homepage.workspace = true repository.workspace = true [features] -task = [] +task = ["tokio/time"] fs = ["tokio/fs"] net = ["tokio/net"] diff --git a/crates/lb/src/task.lua b/crates/lb/src/task.lua index 9969a72..bca5aef 100644 --- a/crates/lb/src/task.lua +++ b/crates/lb/src/task.lua @@ -1,6 +1,10 @@ +-- include task functions in the global scope local task = require("lb:task") --- include `lb_tasklib::spawn` in the global scope +function sleep(ms) + return task:sleep(ms) +end + function spawn(f, ...) return task:spawn(f, ...) end diff --git a/crates/lb/src/task.rs b/crates/lb/src/task.rs index ea7919b..1aec2b4 100644 --- a/crates/lb/src/task.rs +++ b/crates/lb/src/task.rs @@ -14,7 +14,11 @@ impl lb_tasklib { Self } - pub extern "Lua" fn spawn(self, f: function, ...) { + pub async extern "Lua-C" fn sleep(&self, ms: f64) { + sleep(Duration::from_secs_f64(ms / 1000.)).await; + } + + pub extern "Lua" fn spawn(&self, f: function, ...) { // pack the function and its arguments into a table and pass its ref to rust self.__spawn(__ref(__tpack(f, variadic!()))) }