mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Fix require not throwing syntax error (#168)
This commit is contained in:
parent
8220216893
commit
3f79756f70
1 changed files with 48 additions and 30 deletions
|
@ -1,6 +1,7 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use mlua::prelude::*;
|
||||
use mlua::Error::ExternalError;
|
||||
|
||||
use super::context::*;
|
||||
|
||||
|
@ -27,26 +28,33 @@ where
|
|||
'lua: 'ctx,
|
||||
{
|
||||
// 1. Try to require the exact path
|
||||
if let Ok(res) = require_inner(lua, ctx, &abs_path, &rel_path).await {
|
||||
return Ok(res);
|
||||
match require_inner(lua, ctx, &abs_path, &rel_path).await {
|
||||
Ok(res) => return Ok(res),
|
||||
Err(err) => {
|
||||
if !is_file_not_found_error(&err) {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to require the path with an added "luau" extension
|
||||
let (luau_abs_path, luau_rel_path) = (
|
||||
append_extension(&abs_path, "luau"),
|
||||
append_extension(&rel_path, "luau"),
|
||||
);
|
||||
if let Ok(res) = require_inner(lua, ctx, &luau_abs_path, &luau_rel_path).await {
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
// 3. Try to require the path with an added "lua" extension
|
||||
let (lua_abs_path, lua_rel_path) = (
|
||||
append_extension(&abs_path, "lua"),
|
||||
append_extension(&rel_path, "lua"),
|
||||
);
|
||||
if let Ok(res) = require_inner(lua, ctx, &lua_abs_path, &lua_rel_path).await {
|
||||
return Ok(res);
|
||||
for extension in ["luau", "lua"] {
|
||||
match require_inner(
|
||||
lua,
|
||||
ctx,
|
||||
&append_extension(&abs_path, extension),
|
||||
&append_extension(&rel_path, extension),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(res) => return Ok(res),
|
||||
Err(err) => {
|
||||
if !is_file_not_found_error(&err) {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We didn't find any direct file paths, look
|
||||
|
@ -55,21 +63,23 @@ where
|
|||
let rel_init = rel_path.join("init");
|
||||
|
||||
// 4. Try to require the init path with an added "luau" extension
|
||||
let (luau_abs_init, luau_rel_init) = (
|
||||
append_extension(&abs_init, "luau"),
|
||||
append_extension(&rel_init, "luau"),
|
||||
);
|
||||
if let Ok(res) = require_inner(lua, ctx, &luau_abs_init, &luau_rel_init).await {
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
// 5. Try to require the init path with an added "lua" extension
|
||||
let (lua_abs_init, lua_rel_init) = (
|
||||
append_extension(&abs_init, "lua"),
|
||||
append_extension(&rel_init, "lua"),
|
||||
);
|
||||
if let Ok(res) = require_inner(lua, ctx, &lua_abs_init, &lua_rel_init).await {
|
||||
return Ok(res);
|
||||
for extension in ["luau", "lua"] {
|
||||
match require_inner(
|
||||
lua,
|
||||
ctx,
|
||||
&append_extension(&abs_init, extension),
|
||||
&append_extension(&rel_init, extension),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(res) => return Ok(res),
|
||||
Err(err) => {
|
||||
if !is_file_not_found_error(&err) {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing left to try, throw an error
|
||||
|
@ -109,3 +119,11 @@ fn append_extension(path: impl Into<PathBuf>, ext: &'static str) -> PathBuf {
|
|||
};
|
||||
new
|
||||
}
|
||||
|
||||
fn is_file_not_found_error(err: &LuaError) -> bool {
|
||||
if let ExternalError(err) = err {
|
||||
err.as_ref().downcast_ref::<std::io::Error>().is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue