Comments (4)
You can make a Rust async call (function created using Lua::create_async_function
) directly from neovim without blocking it. You need to have a (global) tokio runtime that you can enter inside async func:
static TOKIO: Lazy<runtime::Runtime> = Lazy::new(|| {
runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("cannot start tokio runtime")
});
let my_func = lua.create_async_function(lua, |lua, args| async move {
let _guard = TOKIO.enter();
<async code>
})?;
Then you can poll the function through coroutine checking for Lua::poll_pending()
value.
When polling you can return control back to neovim / libuv scheduler.
It would be better and safer approach.
Take a look to #76 for some early stage ideas / examples.
Now I probably would try to incorporate some event listener to signal eventloop that execution is finished.
from mlua.
Hey thanks for the help, the comments there look a bit more indirect / verbose than what I have now, currently I don't really have problems but I will dive a bit deeper into those concepts (lua coroutine etc) this weekend, and see if it can improve my code a bit.
Something else, I have some simple macros (I don't think they are production ready yet) to implement IntoLua and FromLua on rust structs, maybe they can be interesting for your project. I will slowly improve them a over time but in a lot of use cases they work already.
https://github.com/Norlock/neo-api-rs/blob/main/crates/macros/src/into_table.rs
https://github.com/Norlock/neo-api-rs/blob/main/crates/macros/src/from_table.rs
from mlua.
Is there a way I can maybe create a new Lua context from inside the app? I would like to be able to generate one
You can try Lua::init_from_ptr(raw_state)
which will return previously initialized state.
If this will not help, could you explain the issue more detailed? Eg. what lock you have and how you use it.
from mlua.
Yes but where do I get the raw_state? I will try to explain my issue (for a shared library):
I have made bindings for Neovim in the neo-api-rs repository, I'm currently building a fuzzy finder.
In the code there is a text changed event for Neovim that will be called whenever the text changes.
https://github.com/Norlock/neo-api-rs/blob/bad84fba8c3114c00d26bb8927b5bc01fc12090f/src/fuzzy.rs#L591
On that event I retrieve the text:
let search_query = NeoApi::get_current_line(lua)?;
And then I want to spawn a task asynchronously that will run the 'fd' and 'ripgrep' processes from rust. It can take in some cases around 120~ ms. For performance reasons I don't want to block the whole Lua thread so I will not block that task (which means no more access to the lua object).
I store the results of those processes in a lazy static object from once_cell, where I can retrieve that data later.
static CONTAINER: Lazy<FuzzyContainer> = Lazy::new(|| FuzzyContainer {
all_lines: RwLock::new(String::new()),
cached_lines: RwLock::new(vec![]),
fuzzy: RwLock::new(None),
rt: tokio::runtime::Runtime::new().unwrap(),
query_meta: RwLock::new(QueryMeta {
update_results: false,
last_search: "".to_string(),
}),
preview: RwLock::new(Vec::new()),
});
Then I have created an interval from the buildin neovim api (bindings to libUv https://neovim.io/doc/user/luvref.html) .
In that interval:
https://github.com/Norlock/neo-api-rs/blob/bad84fba8c3114c00d26bb8927b5bc01fc12090f/src/fuzzy.rs#L491
I have now the try_lock
functionality which is better anyway for an interval. However if I would change that to await
, and type quickly it can eventually create the: Can't leave the thread C boundary
error. Or it would deadlock.
So basically try_lock is the correct way of doing it in an interval, but I would like to know is there a way if I leave the lua context (the part where I will run 'fd' and 'ripgrep'), I can get back into the Lua context? To update some stuff inside my plugin.
Then I can ditch the interval all together and run that specific code after the processes have ran. I hope this makes it a bit more clear.
If your last answer is still correct, where do i retrieve the lua_state (raw_state) pointer:
Lua::init_from_ptr(raw_state)
from mlua.
Related Issues (20)
- Lua 5.1: cannot create a Lua reference, out of auxiliary stack space HOT 1
- NixOS breaks luajit compilation HOT 2
- Ability to create Luau buffer from bytes / get bytes contents HOT 4
- Hello, is it possible for mlua to write a dll module for standard lua5.3 or lua5.4. HOT 2
- Soundness bug in mlua_sys's luau_compile HOT 2
- Lua::compile is not idiomatic HOT 7
- Lack of Floored Division Support for Luau HOT 2
- Serializing from `serde_json::Value` to `mlua::Value` when the `arbitrary_precision` feature is enabled HOT 2
- `AnyUserData::is` panics with unreachable code after being destructed with `AnyUserData::take` HOT 1
- Automatically generate definition files HOT 4
- Can't re export lua_module in own lib
- Can module mode works with async? HOT 2
- Difficulty creating a wrapper type HOT 1
- Support Audio manipulation HOT 2
- Support UI HOT 2
- Error compiling module to wasm32-unknown-emscripten
- Terra integration?
- Lifetime Issues with Scope HOT 3
- Serialization: Recursive table detection yields false positives HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mlua.