47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use crate::runtime::spawn;
|
|
use luaffi::{cdef, metatype};
|
|
use std::{ffi::c_int, process};
|
|
use tokio::task::JoinHandle;
|
|
|
|
#[cdef]
|
|
pub struct lb_libtask;
|
|
|
|
#[metatype]
|
|
impl lb_libtask {
|
|
#[new]
|
|
extern "Lua-C" fn new() -> Self {
|
|
Self
|
|
}
|
|
|
|
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!())))
|
|
}
|
|
|
|
extern "Lua-C" fn __spawn(&self, key: c_int) -> lb_task {
|
|
let handle = spawn(async move |s| {
|
|
// SAFETY: key is always unique, created by __ref above
|
|
let arg = unsafe { s.new_ref_unchecked(key) };
|
|
s.resize(0);
|
|
s.push(arg);
|
|
let narg = s.unpack(1, 1, None) - 1;
|
|
println!("{s:?}");
|
|
if let Err(_err) = s.call_async(narg, 0).await {
|
|
process::exit(1)
|
|
}
|
|
println!("{s:?}");
|
|
});
|
|
|
|
lb_task { handle }
|
|
}
|
|
}
|
|
|
|
#[cdef]
|
|
pub struct lb_task {
|
|
#[opaque]
|
|
handle: JoinHandle<()>,
|
|
}
|
|
|
|
#[metatype]
|
|
impl lb_task {}
|