mlua
mlua copied to clipboard
Add Lua::load_file function
I had a go at implementing a load_file
function that uses lua's native luaL_loadfile
function. This makes it easier to load files.
Feedback would be greatly appreciated!
closes #139
Thanks for the PR!
I was thinking about it and probably will prefer a bit more flixible approach by having AsChunk
implemented for Path
.
I see it requires some changes in the trait and in other parts but eventually it would be a win by providing a unified interface to load chunks, as:
lua.load(Path::new("./abc.lua")).exec();
I think that's a great idea, and it was actually what I tried initially, however, I ran into a few issues with this:
- The
AsChunk
implementation forPath
conflicts with the current blanket impl onAsRef<[u8]>
, as path also implements that, but we can't use the blanket impl, sincePath
must be treated differently. - Changing the
source
field ofChunk
to an enum type with variants for a path and source code would require major refactoring, as a lot of code assumes it to be source code, and does stuff with that code (such as prepending areturn
).- This change to
Chunk
also requires changes inAsChunk
, so that thesource
method returns this enum type
- This change to
So that's quite a lot of breaking changes. Please let me know how I should proceed on these, and I'll get to work!
Any chance of getting this merged/what's needed? This would be really helpful for a project I'm working on. 😅
Any chance of getting this merged/what's needed? This would be really helpful for a project I'm working on. 😅
Oh, this is already supported since v0.8, you can use lua.load(std::path::Path::new("file.lua")).exec().unwrap();