-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestions for simplifying module creation #424
Comments
Regarding the first point, functions like |
Cool! One more reason to look forward to v0.10! |
I just gave v0.10.0-beta.2 a spin, but it didn't work like you suggested. It did cleanup one line of code: -let demo: LuaFunction = lua.create_function(demo).unwrap();
+let demo = Function::wrap(demo); But what |
The commit I referred to, was made after beta.2 version. |
Oh my bad, somehow I assumed it was older and just didn't even think to look at the beta tag point! I'm working on an experimental branch mostly so I might as well give master a shake. |
I gave it a shake with Git HEAD (main branch), and got a little further. However I struggled a but to figure out how it was going to handle errors and the result does not seem satisfactory: // Can NOT be wrapped (what I actually want)
type Result<T> = anyhow::Result<T>;
fn demo() -> Result<String>
// Can NOT be wrapped (what I tried next when anyhow failed)
fn demo() -> Result<String, std::error:Error>
// Can NOT be wrapped
fn demo() -> Result<String>
// Can be wrapped (but not useful as a Rust function outside of the Lua module wrapper)
fn demo() -> Result<String, LuaError> Am I missing something obvious here? |
Can you try |
That works to wrap Would it be helpful to work up a MWE for this? |
Can you try the latest |
Why are modules and vendored exclusive features anyway? Exposing an API for Lua scripts via modules is arguably the standard approach to bindings (see for example the sol library or OpenMW). As it is, it seems like if you use the vendored feature you're encouraged to expose the API through global table assignment (this is also what all examples in the readme do), which is less than ideal as most existing tooling for lua expects things to be exposed with modules that can be required instead. |
@Calandiel I'm not sure I understand your question.
|
I'm not sure I understand @Calandiel's comment either, but there does seem to be a little bit of a catch-22. For example I have a project that both builds an app and a module (possibly for use together, but also possibly separate). Of course having the module linking from the host apps Lua VM makes sense (not static), but my host app does get built with This whole topic should probably be a separate issue though, this is starting to wander from the initial topic which has been largely addressed already with v0.10. |
Could you specify which bit is problematic? It's difficult to clarify a message as a whole without knowing what's not clear ^^ I see Lua bindings through the lense of game development, maybe that explains the miscommunication. In such projects you want to expose an API for modders to write scripts with, ideally without polluting the global namespace. I listed some existing projects using such architecture. In essence, you want a script you can require by, say, Let me know if that helps, but @alerque , I think, has a similar problem to mine 🙂 |
@Calandiel Please open this as a new issue, you can use the top right menu on your first comment to "reference in new issue" and we can continue there. |
Doesn't seem to me like it's necessarily a separate issue - to quote your original post:
And the final note in it:
These are more or less my questions too 🙂 |
Just to clarify:
Apps build in |
It seems like the |
Modules are not linked either statically or dynamically with Lua library. They use dynamic lookup for undefined symbols (see The fact that modules are not linked with Lua library allow any option for host apps: static builds and dynamic builds. Host apps can be dynamically linked with Lua library with any name, like PS Windows is a bit different and does not support dynamic lookup (I don't have much experience with windows and this area for me is a gray zone). |
I'm having some trouble conceptualizing how to setup a project and am looking for any suggestions for how to keep it simple so I don't end up with 4 implementations of every function to maintain. I've had great success both using mlua as a runtime and for using it to create Lua modules. This projects needs to both at the same time and it's a bit of a mind bender.
The real project is SILE and my WIP is here, but I'll try to describe the relevant bits in MWE fashion here.
The first piece –the main project crate– is the CLI with a mlua based VM that loads a bunch of Lua code.
One complicating factor is that this needs to compile in several modes: with or without being built against system Lua sources (so the
vendored
feature), and also dynamic or static linking, and with the static option also a variant that has all the Lua resources (script and C modules) embedded in the binary. All that works already, but we have two more layers before things start getting weird.I would also like to write code in Rust in the main Rust crate that is then exposed to in the end user in the Lua API. Again this is relatively easy to do directly ... some Rust functions and inject them into the Lua VM after instantiating it. In fact I started down just that read in this WIP. But it turns out this particular function was viable only because it could also be achieved on the Lua side with other dependencies. Because of the last requirement in this mess...
The last requirement is that the functions written in Rust and provided into Lua also need to be available if the Lua code is run from a system native Lua VM rather than the mlua based CLI wrapper. The whole thing needs to work as a Lua library module as well even without the CLI wrapper and mlua VM it provides.
In a naive attempt, I tried to simply add the
module
feature and export a Lua module from the existing crate lib.rs. That was a no-go for ugly linking reasons. Then I realized that was going to be a no-go anyway because of this limitation:So my next move was to split it into 2 crates: one for the main app with the VM, and one that just repackages Rust functions and exports them into a Lua module. That isolates the CLI runtime with the Lua VM in one crate and a loadable Lua module in another. If running the application's Lua code base as a library loaded into Lua the Lua module will be able to provide all the expected API surface area.
So now we get to the actual question!
This layout has me creating two functions with two function signatures and hand converting the types for each side. For example in the POC of worked up, in the main crate's library we might have a function that returns a string:
Simple enough. But we also need a similar function for the Lua module in the other crate:
In this particular case it isn't too complex with no input argument types to convert and only a
Result<String>
to turn into aLuaResult<LuaString>
, but I can see this getting more complex (or at least verbose) in a hurry. As soon as each function has a few different input args and possible multiple return arguments this is going to be some verbose casting boilerplate code.Then we need to actually stuff that in a Lua module, so each function will also have to have an extra line in something like this:
Is there something more ergonomic that I'm missing in any of this layout to perhaps do this in one crate, to somehow derive the right types for re-exporting to the Lua side, or automatically collate them in the module?
The text was updated successfully, but these errors were encountered: