-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Automatically generate definition files #392
Comments
I started playing with an implementation for writing lua language server definition files. Maybe it could be used here. I was thinking it could be an opt-in crate separate from this crate, but it would be interesting to discuss various options! |
Unfortunately my attempt, while possible to do, proved to perhaps not provide enough value as it does not have a way to type check the annotations (or to generate them from rust code). Such an approach came up in discussion here: wez/wezterm#3132 (comment) The idea, as I understand it, is to write a new feature to mlua that would allow exposing metadata about modules, functions, variables, etc. I think the best thing would be to be able to generate this at compile time with e.g. macros, so no runtime cost would have to be made. Then the feature could maybe be completely opt-in. Does this sound like a possible approach? |
I can only speak for myself but generating on compile time sounds like a good idea. |
I ran into what look like promising existing toolkits fit for the purpose of generating some sort of FFI (foreign function interface) bindings from rust to other languages:
Here are some suggestions for possible solutions to this problem:
|
Run into the very same question :) I am looking to integrate lua via mlua into game framework and my strict requirement is to provide vscode intellisense and (less strictly) debugging capabilities. The first one is supplied by Lua Definition Files, so I am having my eyes open here. As of API design, I would recommend looking into serde's schemars - it is designed around macros, and docs-comments around actual struct- and methods definitions and it allows generating schema via macro, which can be then deployed into specific file as part of build- or runtime process. We actually use that in runtime, as we can supplement it with additional data from the current project files. Any progress on this feature? |
From my side, I looked at all the options I listed in my previous comment, and it seems they could kind of work. All of them are designed for languages that are compiled into runtime code, which is not the case here (types are not executed at runtime). This is likely not a big issue, but in some cases it means we have to conform to a common api that is not designed for lua. Another interesting finding is that these FFI generation packages are designed to work based on either rust functions and data types (with some special restrictions), or an additional schema definition that's separate from rust source code. I felt my goal of having easy type definitions with minimal code changes by users (developers) cannot be achieved so easily. Some more random notes:
I also have some ideas towards potential solutions: Option 1: "fancy solution"In this option, the types are automatically generated from the rust code. @Shelim, your idea about a schemars like api seems like the best option. In addition to data, functions would have to be supported. I think this could be supported in a backward compatible manner if the user opts in to using it. It might work if they move their mlua invocations under a new trait whose methods have a signature that is compatible with whatever code generation tool is picked. Maybe the method calls are optimized away automatically to prevent a runtime cost, or maybe this can be done by inlining - I'm not sure. Option 2: "simple solution"This option is extremely simple:
|
Option 1 is a dream-like scenario. I believe it can be implemented as separate crate, atop of mlua (and just call some imperative mlua api); The cost of indirect function calls would remain through (or maybe not, if for some clever usage of Rust macros...?) |
Just going to drop this here then: https://github.com/lenscas/tealr should support: A: Generating a site containing any documentation and signatures of exposed functions Sadly, I don't have as much time to work on it as I would like, but I happily accept PR's and it should be a good starting point if someone wants to get more things working. |
I have also done some work related to this, though mostly for simpler "data-only" types: Which is more meant to be used with the A crate I've been looking at maybe adding luals support to is https://github.com/specta-rs/specta but again that'd maybe more in conjunction with the |
It may be interesting to have some mechanism to autogenerate definition files to be consumed by lua-language-server from rust code:
https://luals.github.io/wiki/definition-files
A usecase for example: exposed a function from rust, and get type hints when editing lua code that uses this function.
The text was updated successfully, but these errors were encountered: