feat: add mlua integration

This commit is contained in:
noib3 2022-08-08 00:28:29 +02:00
parent 456d44793f
commit 71e8f28ad6
No known key found for this signature in database
GPG key ID: 7AF92216C504A017
9 changed files with 67 additions and 3 deletions

View file

@ -0,0 +1,11 @@
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
[target.aarch64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

11
examples/mlua/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "lua"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
nvim-oxi = { path = "../../nvim-oxi", features = ["mlua"] }
mlua = { version = "0.8", features = ["luajit"] }

4
examples/mlua/README.md Normal file
View file

@ -0,0 +1,4 @@
# mlua
`require("lua").greetings()` will print `"Hello from Rust.."` followed by
`"..and goodbye from Lua!"`.

17
examples/mlua/src/lib.rs Normal file
View file

@ -0,0 +1,17 @@
use mlua::prelude::LuaFunction;
use nvim_oxi::{self as oxi, Dictionary, Function};
fn greetings(_: ()) -> oxi::Result<()> {
oxi::print!("Hello from Rust..");
let lua = oxi::mlua::lua();
let print = lua.globals().get::<_, LuaFunction>("print")?;
print.call("..and goodbye from Lua!")?;
Ok(())
}
#[oxi::module]
fn lua() -> oxi::Result<Dictionary> {
Ok(Dictionary::from_iter([("greetings", Function::from_fn(greetings))]))
}

View file

@ -41,9 +41,11 @@
buildInputs = lib.lists.optionals stdenv.isDarwin [ pkgs.libiconv ];
packages = with pkgs; [
gcc
(rust-bin.selectLatestNightlyWith (toolchain: toolchain.default))
gcc
luajit
neovim
pkg-config
];
}
);

View file

@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
libuv-rs = ["loop", "dep:libuv"]
loop = ["dep:libuv-sys2"]
mlua = ["dep:mlua"]
nightly = []
test = ["dep:oxi-test"]
@ -26,6 +27,7 @@ derive_builder = "0.11"
libc = "0.2"
libuv-sys2 = { version = "1.43", optional = true }
libuv = { version = "2.4", optional = true }
mlua = { version = "0.8", optional = true }
nvim-types = { version = "0.1", path = "../nvim-types", features = ["serde"] }
once_cell = "1.12"
oxi-module = { version = "0.1", path = "../oxi-module" }

View file

@ -6,7 +6,8 @@ use serde::{de, ser};
pub type Result<T> = std::result::Result<T, Error>;
/// Error returned by `nvim-oxi` functions.
#[derive(thiserror::Error, Debug, Eq, PartialEq)]
#[derive(thiserror::Error, Debug)]
#[cfg_attr(not(feature = "mlua"), derive(Eq, PartialEq))]
pub enum Error {
#[error(transparent)]
NvimError(#[from] nvim_types::Error),
@ -40,6 +41,10 @@ pub enum Error {
#[error("{0}")]
Other(String),
#[cfg(feature = "mlua")]
#[error(transparent)]
MluaError(#[from] mlua::Error),
}
impl ser::Error for Error {

View file

@ -42,6 +42,19 @@ pub mod types {
#[cfg_attr(docsrs, doc(cfg(feature = "loop")))]
pub mod libuv;
#[cfg(feature = "mlua")]
#[cfg_attr(docsrs, doc(cfg(feature = "mlua")))]
pub mod mlua {
/// Returns a static reference to a
/// [`mlua::Lua`](https://docs.rs/mlua/latest/mlua/struct.Lua.html) object
/// to be able to interact with other Lua plugins.
pub fn lua() -> &'static mlua::Lua {
crate::lua::with_state(|lstate| unsafe {
mlua::Lua::init_from_ptr(lstate as *mut _).into_static()
})
}
}
// Re-exports.
pub use error::{Error, Result};
pub use lua::Function;

View file

@ -48,7 +48,6 @@ macro_rules! dbg {
};
}
/// Prints a message to the Neovim message area.
#[doc(hidden)]
pub fn __print(text: impl Into<String>) {