Implement task sleep function

This commit is contained in:
lumi 2025-06-26 18:47:43 +10:00
parent 9b7dbcc141
commit 01f459ffaf
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
3 changed files with 11 additions and 3 deletions

View File

@ -8,7 +8,7 @@ homepage.workspace = true
repository.workspace = true repository.workspace = true
[features] [features]
task = [] task = ["tokio/time"]
fs = ["tokio/fs"] fs = ["tokio/fs"]
net = ["tokio/net"] net = ["tokio/net"]

View File

@ -1,6 +1,10 @@
-- include task functions in the global scope
local task = require("lb:task") local task = require("lb:task")
-- include `lb_tasklib::spawn` in the global scope function sleep(ms)
return task:sleep(ms)
end
function spawn(f, ...) function spawn(f, ...)
return task:spawn(f, ...) return task:spawn(f, ...)
end end

View File

@ -14,7 +14,11 @@ impl lb_tasklib {
Self 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 // pack the function and its arguments into a table and pass its ref to rust
self.__spawn(__ref(__tpack(f, variadic!()))) self.__spawn(__ref(__tpack(f, variadic!())))
} }