lua: make FFI types CamelCase

This commit is contained in:
Riccardo Mazzarini 2024-10-01 18:25:57 +08:00
parent 95bfc3c4eb
commit 3ff136338b
No known key found for this signature in database
GPG key ID: 38165222613796F5
23 changed files with 125 additions and 175 deletions

View file

@ -71,7 +71,7 @@ impl FromObject for Buffer {
impl Poppable for Buffer {
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> std::result::Result<Self, lua::Error> {
BufHandle::pop(lstate).map(Into::into)
}
@ -80,7 +80,7 @@ impl Poppable for Buffer {
impl Pushable for Buffer {
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> std::result::Result<std::ffi::c_int, lua::Error> {
self.0.push(lstate)
}

View file

@ -83,7 +83,7 @@ extern "C" {
strict_indexing: bool,
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
arena: *mut Arena,
lstate: *mut luajit::ffi::lua_State,
lstate: *mut luajit::ffi::State,
err: *mut Error,
) -> Array;
@ -124,7 +124,7 @@ extern "C" {
opts: *const GetTextOpts,
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
arena: *mut Arena,
lstate: *mut luajit::ffi::lua_State,
lstate: *mut luajit::ffi::State,
err: *mut Error,
) -> Array;

View file

@ -46,7 +46,7 @@ impl From<TabPage> for Object {
impl Poppable for TabPage {
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> std::result::Result<Self, lua::Error> {
TabHandle::pop(lstate).map(Into::into)
}
@ -55,7 +55,7 @@ impl Poppable for TabPage {
impl Pushable for TabPage {
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> std::result::Result<std::ffi::c_int, lua::Error> {
self.0.push(lstate)
}

View file

@ -47,7 +47,7 @@ impl FromObject for AutocmdCallbackArgs {
impl luajit::Poppable for AutocmdCallbackArgs {
unsafe fn pop(
lstate: *mut luajit::ffi::lua_State,
lstate: *mut luajit::ffi::State,
) -> Result<Self, luajit::Error> {
let obj = Object::pop(lstate)?;

View file

@ -58,7 +58,7 @@ impl FromObject for CommandArgs {
impl luajit::Poppable for CommandArgs {
unsafe fn pop(
lstate: *mut luajit::ffi::lua_State,
lstate: *mut luajit::ffi::State,
) -> Result<Self, luajit::Error> {
let obj = Object::pop(lstate)?;

View file

@ -65,7 +65,7 @@ impl FromObject for Window {
impl Poppable for Window {
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> std::result::Result<Self, lua::Error> {
WinHandle::pop(lstate).map(Into::into)
}
@ -74,7 +74,7 @@ impl Poppable for Window {
impl Pushable for Window {
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> std::result::Result<std::ffi::c_int, lua::Error> {
self.0.push(lstate)
}

View file

@ -42,7 +42,7 @@ impl crate::ProperLayout for uv_timer_t {}
extern "C" {
// https://github.com/luvit/luv/blob/master/src/luv.c#L751
pub(crate) fn luv_loop(
lua_state: *mut luajit::ffi::lua_State,
lua_state: *mut luajit::ffi::State,
) -> *mut uv_loop_t;
pub(crate) fn uv_async_init(

View file

@ -1,6 +1,6 @@
use core::cell::OnceCell;
use luajit::ffi::lua_State;
use luajit::ffi::State;
use crate::ffi;
@ -13,7 +13,7 @@ thread_local! {
/// NOTE: this function **must** be called before calling any other function
/// exposed by this crate or there will be segfaults.
#[doc(hidden)]
pub unsafe fn init(lua_state: *mut lua_State) {
pub unsafe fn init(lua_state: *mut State) {
LOOP.with(|uv_loop| uv_loop.set(ffi::luv_loop(lua_state)))
.unwrap_unchecked();
}

View file

@ -54,7 +54,7 @@ impl Error {
}
pub unsafe fn pop_wrong_type_at_idx<T>(
lstate: *mut crate::ffi::lua_State,
lstate: *mut crate::ffi::State,
idx: std::ffi::c_int,
) -> Self {
let expected = std::any::type_name::<T>();

View file

@ -1,12 +1,10 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
use std::ffi::{c_char, c_double, c_int, c_void};
use std::marker::{PhantomData, PhantomPinned};
#[repr(C)]
#[doc(hidden)]
pub struct lua_State {
pub struct State {
_data: [u8; 0],
/// This marker ensures the struct is not `Send`, `Sync` and `Unpin` (the
@ -43,13 +41,13 @@ pub const LUA_TUSERDATA: c_int = 7;
pub const LUA_TTHREAD: c_int = 8;
// https://www.lua.org/manual/5.1/manual.html#lua_CFunction
pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int;
pub type CFunction = unsafe extern "C" fn(L: *mut State) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_Integer
pub type lua_Integer = isize;
pub type Integer = isize;
// https://www.lua.org/manual/5.1/manual.html#lua_Number
pub type lua_Number = c_double;
pub type Number = c_double;
#[cfg_attr(
all(target_os = "windows", target_env = "msvc"),
@ -57,7 +55,7 @@ pub type lua_Number = c_double;
)]
extern "C" {
// https://www.lua.org/manual/5.1/manual.html#lua_call
pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
pub fn lua_call(L: *mut State, nargs: c_int, nresults: c_int);
/// Binding to [`lua_createtable()`] (-0, +1).
///
@ -70,134 +68,134 @@ extern "C" {
/// `lua_newtable`.
///
/// [`lua_createtable`]: https://www.lua.org/manual/5.1/manual.html#lua_createtable
pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
pub fn lua_createtable(L: *mut State, narr: c_int, nrec: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_error
pub fn lua_error(L: *mut lua_State) -> !;
pub fn lua_error(L: *mut State) -> !;
// https://www.lua.org/manual/5.1/manual.html#lua_call
pub fn lua_getfield(L: *mut lua_State, index: c_int, k: *const c_char);
pub fn lua_getfield(L: *mut State, index: c_int, k: *const c_char);
// https://www.lua.org/manual/5.1/manual.html#lua_getmetatable
pub fn lua_getmetatable(L: *mut lua_State, index: c_int) -> c_int;
pub fn lua_getmetatable(L: *mut State, index: c_int) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_gettop
pub fn lua_gettop(L: *mut lua_State) -> c_int;
pub fn lua_gettop(L: *mut State) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_newuserdata
pub fn lua_newuserdata(L: *mut lua_State, size: usize) -> *mut c_void;
pub fn lua_newuserdata(L: *mut State, size: usize) -> *mut c_void;
// https://www.lua.org/manual/5.1/manual.html#lua_next
pub fn lua_next(L: *mut lua_State, index: c_int) -> c_int;
pub fn lua_next(L: *mut State, index: c_int) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_objlen
pub fn lua_objlen(L: *mut lua_State, index: c_int) -> usize;
pub fn lua_objlen(L: *mut State, index: c_int) -> usize;
// https://www.lua.org/manual/5.1/manual.html#lua_pcall
pub fn lua_pcall(
L: *mut lua_State,
L: *mut State,
nargs: c_int,
nresults: c_int,
errorfunc: c_int,
) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
pub fn lua_pushboolean(L: *mut lua_State, n: lua_Integer);
pub fn lua_pushboolean(L: *mut State, n: Integer);
// https://www.lua.org/manual/5.1/manual.html#lua_pushcclosure
pub fn lua_pushcclosure(L: *mut lua_State, r#fn: lua_CFunction, n: c_int);
pub fn lua_pushcclosure(L: *mut State, r#fn: CFunction, n: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
pub fn lua_pushinteger(L: *mut State, n: Integer);
// https://www.lua.org/manual/5.1/manual.html#lua_pushlightuserdata
pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
pub fn lua_pushlightuserdata(L: *mut State, p: *mut c_void);
// https://www.lua.org/manual/5.1/manual.html#lua_pushlstring
pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, len: usize);
pub fn lua_pushlstring(L: *mut State, s: *const c_char, len: usize);
// https://www.lua.org/manual/5.1/manual.html#lua_pushnil
pub fn lua_pushnil(L: *mut lua_State);
pub fn lua_pushnil(L: *mut State);
// https://www.lua.org/manual/5.1/manual.html#lua_pushnumber
pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
pub fn lua_pushnumber(L: *mut State, n: Number);
// https://www.lua.org/manual/5.1/manual.html#lua_pushstring
pub fn lua_pushstring(L: *mut lua_State, s: *const c_char);
pub fn lua_pushstring(L: *mut State, s: *const c_char);
// https://www.lua.org/manual/5.1/manual.html#lua_pushvalue
pub fn lua_pushvalue(L: *mut lua_State, index: c_int);
pub fn lua_pushvalue(L: *mut State, index: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_rawgeti
pub fn lua_rawgeti(L: *mut lua_State, index: c_int, n: c_int);
pub fn lua_rawgeti(L: *mut State, index: c_int, n: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_rawset
pub fn lua_rawset(L: *mut lua_State, index: c_int);
pub fn lua_rawset(L: *mut State, index: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_rawseti
pub fn lua_rawseti(L: *mut lua_State, index: c_int, n: c_int);
pub fn lua_rawseti(L: *mut State, index: c_int, n: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_settop
pub fn lua_settop(L: *mut lua_State, index: c_int);
pub fn lua_settop(L: *mut State, index: c_int);
// https://www.lua.org/manual/5.1/manual.html#lua_toboolean
pub fn lua_toboolean(L: *mut lua_State, index: c_int) -> c_int;
pub fn lua_toboolean(L: *mut State, index: c_int) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_tointeger
pub fn lua_tointeger(L: *mut lua_State, index: c_int) -> lua_Integer;
pub fn lua_tointeger(L: *mut State, index: c_int) -> Integer;
// https://www.lua.org/manual/5.1/manual.html#lua_tolstring
pub fn lua_tolstring(
L: *mut lua_State,
L: *mut State,
index: c_int,
len: *mut usize,
) -> *const c_char;
// https://www.lua.org/manual/5.1/manual.html#lua_tonumber
pub fn lua_tonumber(L: *mut lua_State, index: c_int) -> lua_Number;
pub fn lua_tonumber(L: *mut State, index: c_int) -> Number;
// https://www.lua.org/manual/5.1/manual.html#lua_touserdata
pub fn lua_touserdata(L: *mut lua_State, index: c_int) -> *mut c_void;
pub fn lua_touserdata(L: *mut State, index: c_int) -> *mut c_void;
// https://www.lua.org/manual/5.1/manual.html#lua_type
pub fn lua_type(L: *mut lua_State, index: c_int) -> c_int;
pub fn lua_type(L: *mut State, index: c_int) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#lua_typename
pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
pub fn lua_typename(L: *mut State, tp: c_int) -> *const c_char;
// Lua auxiliary library.
// https://www.lua.org/manual/5.1/manual.html#luaL_error
pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !;
pub fn luaL_error(L: *mut State, fmt: *const c_char, ...) -> !;
// https://www.lua.org/manual/5.1/manual.html#luaL_ref
pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int;
pub fn luaL_ref(L: *mut State, t: c_int) -> c_int;
// https://www.lua.org/manual/5.1/manual.html#luaL_unref
pub fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int);
pub fn luaL_unref(L: *mut State, t: c_int, r#ref: c_int);
}
// https://www.lua.org/manual/5.1/manual.html#lua_getglobal
pub unsafe fn lua_getglobal(L: *mut lua_State, name: *const c_char) {
pub unsafe fn lua_getglobal(L: *mut State, name: *const c_char) {
lua_getfield(L, LUA_GLOBALSINDEX, name)
}
// https://www.lua.org/manual/5.1/manual.html#lua_pop
pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
pub unsafe fn lua_pop(L: *mut State, n: c_int) {
lua_settop(L, -n - 1)
}
// https://www.lua.org/manual/5.1/manual.html#lua_pushcfunction
pub unsafe fn lua_pushcfunction(L: *mut lua_State, r#fn: lua_CFunction) {
pub unsafe fn lua_pushcfunction(L: *mut State, r#fn: CFunction) {
lua_pushcclosure(L, r#fn, 0)
}
// https://www.lua.org/manual/5.1/manual.html#lua_tostring
pub unsafe fn lua_tostring(L: *mut lua_State, index: c_int) -> *const c_char {
pub unsafe fn lua_tostring(L: *mut State, index: c_int) -> *const c_char {
lua_tolstring(L, index, std::ptr::null_mut())
}
// https://www.lua.org/manual/5.1/manual.html#luaL_typename
pub unsafe fn luaL_typename(L: *mut lua_State, index: c_int) -> *const c_char {
pub unsafe fn luaL_typename(L: *mut State, index: c_int) -> *const c_char {
lua_typename(L, lua_type(L, index))
}

View file

@ -3,7 +3,7 @@ use std::ffi::{c_int, CStr};
use std::mem;
use std::ptr;
use crate::ffi::{self, lua_State};
use crate::ffi::{self, State};
use crate::{utils, IntoResult, Poppable, Pushable};
/// Stores a function in the Lua registry, returning its ref.
@ -16,9 +16,9 @@ where
R::Error: Error + 'static,
{
type Callback =
Box<dyn Fn(*mut lua_State) -> Result<c_int, crate::Error> + 'static>;
Box<dyn Fn(*mut State) -> Result<c_int, crate::Error> + 'static>;
unsafe extern "C" fn c_fun(lstate: *mut lua_State) -> c_int {
unsafe extern "C" fn c_fun(lstate: *mut State) -> c_int {
let fun = {
let idx = ffi::lua_upvalueindex(1);
let upv = ffi::lua_touserdata(lstate, idx) as *mut Callback;

View file

@ -8,12 +8,12 @@ use crate::Error;
/// Trait implemented for types that can be popped off the Lua stack.
pub trait Poppable: Sized {
/// Pops the value at the top of the stack.
unsafe fn pop(lua_state: *mut lua_State) -> Result<Self, Error>;
unsafe fn pop(lua_state: *mut State) -> Result<Self, Error>;
}
impl Poppable for () {
#[inline(always)]
unsafe fn pop(state: *mut lua_State) -> Result<Self, crate::Error> {
unsafe fn pop(state: *mut State) -> Result<Self, crate::Error> {
if lua_gettop(state) == 0 {
Ok(())
} else if lua_type(state, -1) == LUA_TNIL {
@ -26,7 +26,7 @@ impl Poppable for () {
}
impl Poppable for bool {
unsafe fn pop(state: *mut lua_State) -> Result<Self, Error> {
unsafe fn pop(state: *mut State) -> Result<Self, Error> {
if lua_gettop(state) == 0 {
return Err(Error::PopEmptyStack);
}
@ -42,8 +42,8 @@ impl Poppable for bool {
}
}
impl Poppable for lua_Integer {
unsafe fn pop(state: *mut lua_State) -> Result<Self, crate::Error> {
impl Poppable for Integer {
unsafe fn pop(state: *mut State) -> Result<Self, crate::Error> {
if lua_gettop(state) == 0 {
return Err(Error::PopEmptyStack);
}
@ -60,14 +60,12 @@ impl Poppable for lua_Integer {
}
/// Implements `Poppable` for a integer types that implement
/// `TryFrom<lua_Integer>`.
/// `TryFrom<Integer>`.
macro_rules! pop_try_from_integer {
($integer:ty) => {
impl Poppable for $integer {
unsafe fn pop(
lstate: *mut lua_State,
) -> Result<Self, crate::Error> {
lua_Integer::pop(lstate)?
unsafe fn pop(lstate: *mut State) -> Result<Self, crate::Error> {
Integer::pop(lstate)?
.try_into()
.map_err(Error::pop_error_from_err::<Self, _>)
}
@ -85,8 +83,8 @@ pop_try_from_integer!(i64);
pop_try_from_integer!(u64);
pop_try_from_integer!(usize);
impl Poppable for lua_Number {
unsafe fn pop(state: *mut lua_State) -> Result<Self, crate::Error> {
impl Poppable for Number {
unsafe fn pop(state: *mut State) -> Result<Self, crate::Error> {
if lua_gettop(state) == 0 {
return Err(Error::PopEmptyStack);
}
@ -103,13 +101,13 @@ impl Poppable for lua_Number {
}
impl Poppable for f32 {
unsafe fn pop(state: *mut lua_State) -> Result<Self, crate::Error> {
lua_Number::pop(state).map(|n| n as f32)
unsafe fn pop(state: *mut State) -> Result<Self, crate::Error> {
Number::pop(state).map(|n| n as f32)
}
}
impl Poppable for String {
unsafe fn pop(state: *mut lua_State) -> Result<Self, Error> {
unsafe fn pop(state: *mut State) -> Result<Self, Error> {
if lua_gettop(state) == 0 {
return Err(Error::PopEmptyStack);
}
@ -139,7 +137,7 @@ impl<T> Poppable for Option<T>
where
T: Poppable,
{
unsafe fn pop(state: *mut lua_State) -> Result<Self, Error> {
unsafe fn pop(state: *mut State) -> Result<Self, Error> {
if lua_gettop(state) == 0 {
return Ok(None);
}
@ -158,7 +156,7 @@ impl<T> Poppable for Vec<T>
where
T: Poppable,
{
unsafe fn pop(state: *mut lua_State) -> Result<Self, Error> {
unsafe fn pop(state: *mut State) -> Result<Self, Error> {
if lua_gettop(state) == 0 {
return Err(Error::PopEmptyStack);
}
@ -192,7 +190,7 @@ where
K: Poppable + Eq + Hash,
V: Poppable,
{
unsafe fn pop(state: *mut lua_State) -> Result<Self, Error> {
unsafe fn pop(state: *mut State) -> Result<Self, Error> {
if lua_gettop(state) == 0 {
return Err(Error::PopEmptyStack);
}
@ -239,7 +237,7 @@ macro_rules! pop_tuple {
$($name: Poppable,)*
{
#[allow(non_snake_case)]
unsafe fn pop(state: *mut lua_State) -> Result<Self, crate::Error> {
unsafe fn pop(state: *mut State) -> Result<Self, crate::Error> {
crate::utils::grow_stack(state, count!($($name)*));
pop_reverse!(state, $($name)*);
Ok(($($name,)*))

View file

@ -1,6 +1,6 @@
use std::ffi::{c_char, c_int};
use crate::ffi::{self, lua_Integer, lua_Number, lua_State};
use crate::ffi::{self, Integer, Number, State};
use crate::macros::count;
use crate::utils;
@ -8,52 +8,40 @@ use crate::utils;
pub trait Pushable {
/// Pushes all its values on the Lua stack, returning the number of values
/// that it pushed.
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error>;
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error>;
}
impl Pushable for () {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
ffi::lua_pushnil(lstate);
Ok(1)
}
}
impl Pushable for bool {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
ffi::lua_pushboolean(lstate, self as _);
Ok(1)
}
}
impl Pushable for lua_Integer {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
impl Pushable for Integer {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
ffi::lua_pushinteger(lstate, self);
Ok(1)
}
}
/// Implements `LuaPushable` for an integer type that implements
/// `Into<lua_Integer>`.
/// `Into<Integer>`.
macro_rules! push_into_integer {
($integer:ty) => {
impl Pushable for $integer {
unsafe fn push(
self,
lstate: *mut lua_State,
lstate: *mut State,
) -> Result<c_int, crate::Error> {
let n: lua_Integer = self.into();
let n: Integer = self.into();
n.push(lstate)
}
}
@ -61,15 +49,15 @@ macro_rules! push_into_integer {
}
/// Implements `LuaPushable` for an integer type that implements
/// `TryInto<lua_Integer>`.
/// `TryInto<Integer>`.
macro_rules! push_try_into_integer {
($integer:ty) => {
impl Pushable for $integer {
unsafe fn push(
self,
lstate: *mut lua_State,
lstate: *mut State,
) -> Result<c_int, crate::Error> {
let n: lua_Integer = self.try_into().map_err(
let n: Integer = self.try_into().map_err(
|err: std::num::TryFromIntError| {
crate::Error::push_error(
std::any::type_name::<$integer>(),
@ -93,30 +81,21 @@ push_try_into_integer!(i64);
push_try_into_integer!(u64);
push_try_into_integer!(usize);
impl Pushable for lua_Number {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
impl Pushable for Number {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
ffi::lua_pushnumber(lstate, self);
Ok(1)
}
}
impl Pushable for f32 {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
(self as lua_Number).push(lstate)
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
(self as Number).push(lstate)
}
}
impl Pushable for String {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
ffi::lua_pushlstring(
lstate,
self.as_ptr() as *const c_char,
@ -130,10 +109,7 @@ impl<T> Pushable for Option<T>
where
T: Pushable,
{
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
match self {
Some(t) => t.push(lstate),
None => ().push(lstate),
@ -145,10 +121,7 @@ impl<T> Pushable for Vec<T>
where
T: Pushable,
{
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
ffi::lua_createtable(lstate, self.len() as _, 0);
for (i, obj) in self.into_iter().enumerate() {
@ -166,10 +139,7 @@ where
E: core::fmt::Display,
{
#[inline]
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, crate::Error> {
match self {
Ok(value) => value.push(lstate),
Err(err) => utils::push_error(&err, lstate),
@ -187,7 +157,7 @@ macro_rules! push_tuple {
#[allow(non_snake_case)]
unsafe fn push(
self,
lstate: *mut lua_State,
lstate: *mut State,
) -> Result<c_int, crate::Error> {
let ($($name,)*) = self;
$($name.push(lstate)?;)*

View file

@ -1,16 +1,16 @@
use core::cell::OnceCell;
use crate::ffi::lua_State;
use crate::ffi::State;
thread_local! {
static LUA: OnceCell<*mut lua_State> = const { OnceCell::new() };
static LUA: OnceCell<*mut State> = const { OnceCell::new() };
}
/// Initializes the Lua state.
///
/// NOTE: this function **must** be called before calling any other function
/// exposed by this crate or there will be segfaults.
pub unsafe fn init(lstate: *mut lua_State) {
pub unsafe fn init(lstate: *mut State) {
LUA.with(|lua| lua.set(lstate).unwrap_unchecked());
}
@ -20,7 +20,7 @@ pub unsafe fn init(lstate: *mut lua_State) {
/// calling [`init`].
pub unsafe fn with_state<F, R>(fun: F) -> R
where
F: FnOnce(*mut lua_State) -> R,
F: FnOnce(*mut State) -> R,
{
LUA.with(move |lstate| fun(*lstate.get().unwrap()))
}

View file

@ -1,11 +1,11 @@
use std::ffi::{c_int, CStr};
use std::fmt::Display;
use crate::ffi::{self, lua_State};
use crate::ffi::{self, State};
/// Does nothing if the stack is already taller than `n`, grows the stack
/// height to `n` by adding `nil`s if it's not.
pub unsafe fn grow_stack(lstate: *mut lua_State, n: c_int) {
pub unsafe fn grow_stack(lstate: *mut State, n: c_int) {
if ffi::lua_gettop(lstate) < n {
ffi::lua_settop(lstate, n);
}
@ -13,10 +13,7 @@ pub unsafe fn grow_stack(lstate: *mut lua_State, n: c_int) {
/// Returns a displayable representation of the Lua value at a given stack
/// index.
pub unsafe fn debug_value(
lstate: *mut lua_State,
n: c_int,
) -> Box<dyn Display> {
pub unsafe fn debug_value(lstate: *mut State, n: c_int) -> Box<dyn Display> {
match ffi::lua_type(lstate, n) {
ffi::LUA_TNONE | ffi::LUA_TNIL => Box::new("()"),
@ -34,7 +31,7 @@ pub unsafe fn debug_value(
/// Assumes that the value at index `index` is a table and returns whether it's
/// an array table (as opposed to a dictionary table).
pub unsafe fn is_table_array(lstate: *mut lua_State, index: c_int) -> bool {
pub unsafe fn is_table_array(lstate: *mut State, index: c_int) -> bool {
ffi::lua_pushnil(lstate);
if ffi::lua_next(lstate, index - 1) == 0 {
@ -52,12 +49,12 @@ pub unsafe fn is_table_array(lstate: *mut lua_State, index: c_int) -> bool {
}
/// Returns the type of the Lua value at a given stack index.
pub unsafe fn debug_type(lstate: *mut lua_State, n: c_int) -> impl Display {
pub unsafe fn debug_type(lstate: *mut State, n: c_int) -> impl Display {
CStr::from_ptr(ffi::luaL_typename(lstate, n)).to_string_lossy()
}
/// Pretty prints the contents of the Lua stack to the Neovim message area.
pub unsafe fn debug_stack(lstate: *mut lua_State) {
pub unsafe fn debug_stack(lstate: *mut State) {
let height = ffi::lua_gettop(lstate);
let stack_pp = (1..height + 1)
@ -75,7 +72,7 @@ pub unsafe fn debug_stack(lstate: *mut lua_State) {
pub unsafe fn push_error<E: core::fmt::Display + ?Sized>(
err: &E,
lstate: *mut lua_State,
lstate: *mut State,
) -> ! {
let msg = err.to_string();
ffi::lua_pushlstring(lstate, msg.as_ptr() as *const _, msg.len());

View file

@ -24,7 +24,7 @@ pub fn plugin(attr: TokenStream, item: TokenStream) -> TokenStream {
#[no_mangle]
unsafe extern "C" fn #lua_module(
state: *mut #nvim_oxi::lua::ffi::lua_State,
state: *mut #nvim_oxi::lua::ffi::State,
) -> ::core::ffi::c_int {
#nvim_oxi::entrypoint::entrypoint(state, #plugin_name)
}

View file

@ -116,9 +116,7 @@ impl core::iter::FusedIterator for ArrayIterator {}
impl lua::Poppable for Array {
#[inline]
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
) -> Result<Self, lua::Error> {
unsafe fn pop(lstate: *mut lua::ffi::State) -> Result<Self, lua::Error> {
use lua::ffi::*;
if lua_gettop(lstate) == 0 {
@ -150,7 +148,7 @@ impl lua::Pushable for Array {
#[inline]
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> Result<core::ffi::c_int, lua::Error> {
use lua::ffi::*;

View file

@ -267,9 +267,7 @@ impl core::iter::FusedIterator for DictIterMut<'_> {}
impl lua::Poppable for Dictionary {
#[inline]
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
) -> Result<Self, lua::Error> {
unsafe fn pop(lstate: *mut lua::ffi::State) -> Result<Self, lua::Error> {
use lua::ffi::*;
if lua_gettop(lstate) == 0 {
@ -306,7 +304,7 @@ impl lua::Pushable for Dictionary {
#[inline]
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> Result<core::ffi::c_int, lua::Error> {
use lua::ffi::*;

View file

@ -41,9 +41,7 @@ where
}
impl<A, R> Poppable for Function<A, R> {
unsafe fn pop(
state: *mut lua::ffi::lua_State,
) -> Result<Self, lua::Error> {
unsafe fn pop(state: *mut lua::ffi::State) -> Result<Self, lua::Error> {
if ffi::lua_gettop(state) == 0 {
return Err(lua::Error::PopEmptyStack);
}
@ -66,7 +64,7 @@ impl<A, R> Poppable for Function<A, R> {
impl<A, R> Pushable for Function<A, R> {
unsafe fn push(
self,
state: *mut lua::ffi::lua_State,
state: *mut lua::ffi::State,
) -> Result<c_int, lua::Error> {
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, self.lua_ref);
Ok(1)

View file

@ -462,7 +462,7 @@ where
}
impl Pushable for Object {
unsafe fn push(self, lstate: *mut lua_State) -> Result<c_int, lua::Error> {
unsafe fn push(self, lstate: *mut State) -> Result<c_int, lua::Error> {
match self.kind() {
ObjectKind::Nil => ().push(lstate),
ObjectKind::Boolean => self.as_boolean_unchecked().push(lstate),
@ -483,7 +483,7 @@ impl Pushable for Object {
}
impl Poppable for Object {
unsafe fn pop(lstate: *mut lua_State) -> Result<Self, lua::Error> {
unsafe fn pop(lstate: *mut State) -> Result<Self, lua::Error> {
if lua_gettop(lstate) == 0 {
return Ok(Self::nil());
}
@ -494,11 +494,11 @@ impl Poppable for Object {
LUA_TBOOLEAN => bool::pop(lstate).map(Into::into),
LUA_TNUMBER => {
let n = lua_Number::pop(lstate)?;
let n = Number::pop(lstate)?;
// This checks that the number is in the range (i32::MIN,
// i32::MAX) andd that it has no fractional component.
if n == (n as c_int) as lua_Number {
if n == (n as c_int) as Number {
Ok(Object::from(n as c_int))
} else {
Ok(Object::from(n))

View file

@ -237,7 +237,7 @@ impl lua::Pushable for String {
#[inline]
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> Result<ffi::c_int, lua::Error> {
lua::ffi::lua_pushlstring(lstate, self.as_ptr(), self.len());
Ok(1)
@ -246,9 +246,7 @@ impl lua::Pushable for String {
impl lua::Poppable for String {
#[inline]
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
) -> Result<Self, lua::Error> {
unsafe fn pop(lstate: *mut lua::ffi::State) -> Result<Self, lua::Error> {
use lua::ffi::*;
if lua_gettop(lstate) < 0 {

View file

@ -61,9 +61,7 @@ impl ToObject for Car {
}
impl lua::Poppable for Car {
unsafe fn pop(
lstate: *mut lua::ffi::lua_State,
) -> Result<Self, lua::Error> {
unsafe fn pop(lstate: *mut lua::ffi::State) -> Result<Self, lua::Error> {
let obj = Object::pop(lstate)?;
Self::from_object(obj)
.map_err(lua::Error::pop_error_from_err::<Self, _>)
@ -73,7 +71,7 @@ impl lua::Poppable for Car {
impl lua::Pushable for Car {
unsafe fn push(
self,
lstate: *mut lua::ffi::lua_State,
lstate: *mut lua::ffi::State,
) -> Result<std::ffi::c_int, lua::Error> {
self.to_object()
.map_err(lua::Error::push_error_from_err::<Self, _>)?

View file

@ -1,16 +1,13 @@
use core::ffi::c_int;
use luajit::{ffi::lua_State, Pushable};
use luajit::{ffi::State, Pushable};
/// The entrypoint of the plugin.
///
/// Initializes the Lua state, executes the entrypoint function and pushes the
/// result on the stack.
#[inline(always)]
pub unsafe fn entrypoint<T>(
lua_state: *mut lua_State,
body: fn() -> T,
) -> c_int
pub unsafe fn entrypoint<T>(lua_state: *mut State, body: fn() -> T) -> c_int
where
T: Pushable,
{