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

View file

@ -15,11 +15,13 @@ where
let (abs_path, rel_path) = ctx.resolve_paths(source, path)?; let (abs_path, rel_path) = ctx.resolve_paths(source, path)?;
// 1. Try to require the exact path // 1. Try to require the exact path
println!("1. REQUIRE: Exact");
if let Ok(res) = require_inner(ctx, &abs_path, &rel_path).await { if let Ok(res) = require_inner(ctx, &abs_path, &rel_path).await {
return Ok(res); return Ok(res);
} }
// 2. Try to require the path with an added "luau" extension // 2. Try to require the path with an added "luau" extension
println!("2. REQUIRE: Luau extension");
let (luau_abs_path, luau_rel_path) = ( let (luau_abs_path, luau_rel_path) = (
append_extension(&abs_path, "luau"), append_extension(&abs_path, "luau"),
append_extension(&rel_path, "luau"), append_extension(&rel_path, "luau"),
@ -29,6 +31,7 @@ where
} }
// 2. Try to require the path with an added "lua" extension // 2. Try to require the path with an added "lua" extension
println!("3. REQUIRE: Lua extension");
let (lua_abs_path, lua_rel_path) = ( let (lua_abs_path, lua_rel_path) = (
append_extension(&abs_path, "lua"), append_extension(&abs_path, "lua"),
append_extension(&rel_path, "lua"), append_extension(&rel_path, "lua"),
@ -38,6 +41,7 @@ where
} }
// Nothing left to try, throw an error // Nothing left to try, throw an error
println!("4. REQUIRE: Error");
Err(LuaError::runtime(format!( Err(LuaError::runtime(format!(
"No file exist at the path '{}'", "No file exist at the path '{}'",
rel_path.display() rel_path.display()
@ -56,10 +60,13 @@ where
let rel_path = rel_path.as_ref(); let rel_path = rel_path.as_ref();
if ctx.is_cached(abs_path)? { if ctx.is_cached(abs_path)? {
println!("Found cached, fetching from cache");
ctx.get_from_cache(abs_path) ctx.get_from_cache(abs_path)
} else if ctx.is_pending(abs_path)? { } else if ctx.is_pending(abs_path)? {
println!("Found pending, waiting for cache");
ctx.wait_for_cache(&abs_path).await ctx.wait_for_cache(&abs_path).await
} else { } else {
println!("No cached, loading new");
ctx.load_with_caching(&abs_path, &rel_path).await 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 // Send results of resuming this thread to any listeners
if let Some(sender) = self.thread_senders.borrow_mut().remove(&thread_id) { if let Some(sender) = self.thread_senders.borrow_mut().remove(&thread_id) {
if sender.receiver_count() > 0 { 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 sender
.send(res.map(|v| { .send(stored)
Arc::new(
self.lua
.create_registry_value(v.into_vec())
.expect("Failed to store return values in registry"),
)
}))
.expect("Failed to broadcast return values of thread"); .expect("Failed to broadcast return values of thread");
} }
} }
@ -115,7 +117,9 @@ where
loop { loop {
// 1. Run lua threads until exit or there are none left, // 1. Run lua threads until exit or there are none left,
// if any thread was resumed it may have spawned futures // if any thread was resumed it may have spawned futures
println!("Resuming lua");
let resumed_lua = self.run_lua_threads(); let resumed_lua = self.run_lua_threads();
println!("Resumed lua");
// 2. If we got a manual exit code from lua we should // 2. If we got a manual exit code from lua we should
// not try to wait for any pending futures to complete // 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 // 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 // resume, or until we don't have any futures left to wait for
println!("Resuming futures");
let resumed_fut = self.run_futures().await; let resumed_fut = self.run_futures().await;
println!("Resumed futures");
// 4. If we did not resume any lua threads, and we have no 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 // remaining either, we have now run the scheduler until completion

View file

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