Add some debugging for require

This commit is contained in:
Filip Tibell 2023-08-20 13:44:52 -05:00
parent 4eb7d5ab8b
commit ee21921601
4 changed files with 33 additions and 15 deletions

View file

@ -131,12 +131,14 @@ impl<'lua> RequireContext<'lua> {
.expect("Path does not exist in results cache");
match cached {
Err(e) => Err(e.clone()),
Ok(key) => {
Ok(k) => {
let multi_vec = self
.lua
.registry_value::<Vec<LuaValue>>(key)
.registry_value::<Vec<LuaValue>>(k)
.expect("Missing require result in lua registry");
Ok(LuaMultiValue::from_vec(multi_vec))
let multi = LuaMultiValue::from_vec(multi_vec);
println!("Got multi value from cache: {multi:?}");
Ok(multi)
}
}
}
@ -196,8 +198,9 @@ impl<'lua> RequireContext<'lua> {
// Return the result of the thread, storing any lua value(s) in the registry
match thread_res {
Err(e) => Err(e),
Ok(multi) => {
let multi_vec = multi.into_vec();
Ok(v) => {
println!("Got multi value from require: {v:?}");
let multi_vec = v.into_vec();
let multi_key = self
.lua
.create_registry_value(multi_vec)
@ -255,9 +258,9 @@ impl<'lua> RequireContext<'lua> {
.expect("RequireContext may not be used from multiple threads")
.remove(abs_path)
.expect("Pending require broadcaster was unexpectedly removed");
broadcast_tx
.send(())
.expect("Failed to send require broadcast");
broadcast_tx.send(()).ok();
println!("Got return value from require: {load_val:?}");
load_val
}

View file

@ -15,11 +15,13 @@ where
let (abs_path, rel_path) = ctx.resolve_paths(source, path)?;
// 1. Try to require the exact path
println!("1. REQUIRE: Exact");
if let Ok(res) = require_inner(ctx, &abs_path, &rel_path).await {
return Ok(res);
}
// 2. Try to require the path with an added "luau" extension
println!("2. REQUIRE: Luau extension");
let (luau_abs_path, luau_rel_path) = (
append_extension(&abs_path, "luau"),
append_extension(&rel_path, "luau"),
@ -29,6 +31,7 @@ where
}
// 2. Try to require the path with an added "lua" extension
println!("3. REQUIRE: Lua extension");
let (lua_abs_path, lua_rel_path) = (
append_extension(&abs_path, "lua"),
append_extension(&rel_path, "lua"),
@ -38,6 +41,7 @@ where
}
// Nothing left to try, throw an error
println!("4. REQUIRE: Error");
Err(LuaError::runtime(format!(
"No file exist at the path '{}'",
rel_path.display()
@ -56,10 +60,13 @@ where
let rel_path = rel_path.as_ref();
if ctx.is_cached(abs_path)? {
println!("Found cached, fetching from cache");
ctx.get_from_cache(abs_path)
} else if ctx.is_pending(abs_path)? {
println!("Found pending, waiting for cache");
ctx.wait_for_cache(&abs_path).await
} else {
println!("No cached, loading new");
ctx.load_with_caching(&abs_path, &rel_path).await
}
}

View file

@ -59,14 +59,16 @@ where
// Send results of resuming this thread to any listeners
if let Some(sender) = self.thread_senders.borrow_mut().remove(&thread_id) {
if sender.receiver_count() > 0 {
let stored = match res {
Err(e) => Err(e),
Ok(v) => Ok(Arc::new(
self.lua
.create_registry_value(v.into_vec())
.expect("Failed to store return values in registry"),
)),
};
sender
.send(res.map(|v| {
Arc::new(
self.lua
.create_registry_value(v.into_vec())
.expect("Failed to store return values in registry"),
)
}))
.send(stored)
.expect("Failed to broadcast return values of thread");
}
}
@ -115,7 +117,9 @@ where
loop {
// 1. Run lua threads until exit or there are none left,
// if any thread was resumed it may have spawned futures
println!("Resuming lua");
let resumed_lua = self.run_lua_threads();
println!("Resumed lua");
// 2. If we got a manual exit code from lua we should
// not try to wait for any pending futures to complete
@ -125,7 +129,9 @@ where
// 3. Keep resuming futures until we get a new lua thread to
// resume, or until we don't have any futures left to wait for
println!("Resuming futures");
let resumed_fut = self.run_futures().await;
println!("Resumed futures");
// 4. If we did not resume any lua threads, and we have no futures
// remaining either, we have now run the scheduler until completion

View file

@ -1,6 +1,8 @@
local task = require("@lune/task")
print("Requiring 1")
local module1 = require("./modules/async")
print("Requiring 2")
local module2 = require("./modules/async")
task.wait(1)