Start work on improved module loader

This commit is contained in:
Filip Tibell 2023-03-20 09:43:01 +01:00
parent 0f5f173488
commit 1876a25922
No known key found for this signature in database

View file

@ -44,7 +44,9 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
let require_get_abs_rel_paths = lua
.create_function(
|_, (require_pwd, require_source, require_path): (String, String, String)| {
// TODO: Special case for @lune prefix here
if require_path.starts_with('@') {
return Ok((require_path.clone(), require_path));
}
let path_relative_to_pwd = PathBuf::from(
&require_source
.trim_start_matches("[string \"")
@ -76,7 +78,22 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
// were async then one lua script may require a module during the file reading process
let require_get_loaded_file = lua.create_function(
|lua: &Lua, (path_absolute, path_relative): (String, String)| {
// TODO: Check for @lune prefix here and try to load a builtin module
// Check if we got a special require path starting with an @
if path_absolute == path_relative && path_absolute.starts_with('@') {
return match path_absolute {
p if p.starts_with("@lune/") => {
let _module_name = p.strip_prefix("@lune/").unwrap();
// TODO: Return builtin module
Err(LuaError::RuntimeError(
"Builtin require paths prefixed by '@' are not yet supported"
.to_string(),
))
}
_ => Err(LuaError::RuntimeError(
"Custom require paths prefixed by '@' are not yet supported".to_string(),
)),
};
}
// Use a name without extensions for loading the chunk, the
// above code assumes the require path is without extensions
let path_relative_no_extension = path_relative