Modules should be registered in luby not lb
This commit is contained in:
@@ -16,7 +16,3 @@ sysexits = "0.9.0"
|
||||
thiserror = "2.0.12"
|
||||
tokio = { version = "1.45.1", features = ["rt", "time", "fs", "net", "process", "signal", "tracing"] }
|
||||
tracing = "0.1.41"
|
||||
|
||||
[dev-dependencies]
|
||||
luaify = { path = "../luaify" }
|
||||
tokio = { version = "1.45.1", features = ["full"] }
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
local task = require("lb:task")
|
||||
|
||||
function spawn(f, ...)
|
||||
return task:spawn(f, ...)
|
||||
end
|
||||
@@ -1,4 +1,3 @@
|
||||
use crate::{chan::lb_chanlib, fs::lb_fslib, net::lb_netlib, task::lb_tasklib};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use luaffi::{Registry, Type};
|
||||
use luajit::{Chunk, State};
|
||||
@@ -15,15 +14,13 @@ pub struct Builder {
|
||||
|
||||
impl Builder {
|
||||
pub fn new() -> Self {
|
||||
let mut registry = Registry::new();
|
||||
Self {
|
||||
registry: Registry::new(),
|
||||
}
|
||||
}
|
||||
|
||||
registry
|
||||
.preload::<lb_tasklib>("lb:task")
|
||||
.preload::<lb_chanlib>("lb:channel")
|
||||
.preload::<lb_fslib>("lb:fs")
|
||||
.preload::<lb_netlib>("lb:net");
|
||||
|
||||
Self { registry }
|
||||
pub fn registry(&self) -> &Registry {
|
||||
&self.registry
|
||||
}
|
||||
|
||||
pub fn module<T: Type>(&mut self, name: impl Display) -> &mut Self {
|
||||
@@ -31,18 +28,11 @@ impl Builder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn registry(&self) -> &Registry {
|
||||
&self.registry
|
||||
}
|
||||
|
||||
pub fn build(&self) -> luajit::Result<Runtime> {
|
||||
Ok(Runtime {
|
||||
state: {
|
||||
let mut s = State::new()?;
|
||||
let mut chunk = Chunk::new(self.registry.done());
|
||||
chunk.extend(include_bytes!("./runtime.lua"));
|
||||
// println!("{chunk}");
|
||||
s.eval(chunk.path("[luby]"), 0, 0)?;
|
||||
s.eval(Chunk::new(self.registry.build()).path("[luby]"), 0, 0)?;
|
||||
s
|
||||
},
|
||||
tasks: LocalSet::new(),
|
||||
@@ -73,6 +63,9 @@ impl Runtime {
|
||||
}
|
||||
|
||||
pub fn spawn<T: 'static>(f: impl AsyncFnOnce(&mut State) -> T + 'static) -> JoinHandle<T> {
|
||||
// SAFETY: `new_thread` must be called inside `spawn_local` because this free-standing spawn
|
||||
// function may be called via ffi from lua, and it is not safe to access the lua state within
|
||||
// ffi calls.
|
||||
spawn_local(async move { f(&mut STATE.with(|s| s.new_thread())).await })
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
use lb::runtime;
|
||||
use luaify::luaify;
|
||||
use luajit::{Chunk, LoadMode};
|
||||
use tokio::test;
|
||||
|
||||
async fn run_lua(s: &'static str) {
|
||||
let rt = runtime::Builder::new().build().unwrap();
|
||||
let task = rt.spawn(async move |state| {
|
||||
println!("executing test chunk: {s}");
|
||||
|
||||
state
|
||||
.load(Chunk::new(s).mode(LoadMode::TEXT))
|
||||
.unwrap_or_else(|err| panic!("{err}"));
|
||||
|
||||
state
|
||||
.call_async(0, 0)
|
||||
.await
|
||||
.unwrap_or_else(|err| panic!("{err}"));
|
||||
});
|
||||
|
||||
rt.await;
|
||||
task.await.unwrap_or_else(|err| panic!("{err}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
async fn ipaddr() {
|
||||
run_lua(luaify!({
|
||||
let net = require("lb:net");
|
||||
print(net.ipaddr("127.0.0.1"));
|
||||
}))
|
||||
.await
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
use lb::runtime;
|
||||
use luaify::luaify;
|
||||
use luajit::{Chunk, LoadMode};
|
||||
use tokio::test;
|
||||
|
||||
async fn run_lua(s: &'static str) {
|
||||
let rt = runtime::Builder::new().build().unwrap();
|
||||
let task = rt.spawn(async move |state| {
|
||||
println!("executing test chunk: {s}");
|
||||
|
||||
state
|
||||
.load(Chunk::new(s).mode(LoadMode::TEXT))
|
||||
.unwrap_or_else(|err| panic!("{err}"));
|
||||
|
||||
state
|
||||
.call_async(0, 0)
|
||||
.await
|
||||
.unwrap_or_else(|err| panic!("{err}"));
|
||||
});
|
||||
|
||||
rt.await;
|
||||
task.await.unwrap_or_else(|err| panic!("{err}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
async fn task_test() {
|
||||
run_lua(luaify!({
|
||||
let thing = spawn(|| {
|
||||
print("spawn callback!!!!!!!!!!!!!");
|
||||
});
|
||||
print("thing is", thing);
|
||||
//
|
||||
}))
|
||||
.await
|
||||
}
|
||||
Reference in New Issue
Block a user