mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Add Lua::load_file function

Open LordMZTE opened this issue 2 years ago • 2 comments

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

LordMZTE avatar Apr 10 '22 20:04 LordMZTE

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();

khvzak avatar Apr 13 '22 23:04 khvzak

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 for Path conflicts with the current blanket impl on AsRef<[u8]>, as path also implements that, but we can't use the blanket impl, since Path must be treated differently.
  • Changing the source field of Chunk 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 a return).
    • This change to Chunk also requires changes in AsChunk, so that the source method returns this enum type

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!

LordMZTE avatar Apr 13 '22 23:04 LordMZTE

Any chance of getting this merged/what's needed? This would be really helpful for a project I'm working on. 😅

sondr3 avatar Dec 21 '22 14:12 sondr3

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();

khvzak avatar Dec 21 '22 15:12 khvzak