IVogel 2 年 前
コミット
1ad2a8fab6
3 ファイル変更65 行追加49 行削除
  1. 7 2
      src/dump.rs
  2. 51 44
      src/lib.rs
  3. 7 3
      src/loadx.rs

+ 7 - 2
src/dump.rs

@@ -10,7 +10,12 @@ pub unsafe fn dump<WRITER>(
 where
     WRITER: std::io::Write,
 {
-    unsafe extern "C" fn writer_callback<WRITER>(_: lua_State, data: *const u8, size: usize, userdata: *mut c_void) -> i32
+    unsafe extern "C" fn writer_callback<WRITER>(
+        _: lua_State,
+        data: *const u8,
+        size: usize,
+        userdata: *mut c_void,
+    ) -> i32
     where
         WRITER: std::io::Write,
     {
@@ -28,4 +33,4 @@ where
         0 => Ok(()),
         any @ _ => Err(LError::DumpError(any)),
     }
-}
+}

+ 51 - 44
src/lib.rs

@@ -103,7 +103,7 @@ extern "C" {
 
     /// Creates a new Lua state.
     /// It calls `lua_newstate` with an allocator based on the standard C realloc function and then sets a panic function that prints an error message to the standard error output in case of fatal errors.
-    /// 
+    ///
     /// Returns the new state, or `NULL` if there is a memory allocation error.
     #[link_name = "luaL_newstate"]
     pub fn newstate() -> lua_State;
@@ -119,7 +119,7 @@ extern "C" {
     pub fn newthread(state: lua_State) -> lua_State;
 
     // basic stack manipulation
-    
+
     /// Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the number of elements in the stack; in particular, 0 means an empty stack.
     #[link_name = "lua_gettop"]
     pub fn gettop(state: lua_State);
@@ -223,7 +223,7 @@ extern "C" {
     pub fn topointer(state: lua_State, index: i32) -> *const c_void;
 
     // push functions (C -> stack)
-    
+
     /// Pushes a nil value onto the stack.
     #[link_name = "lua_pushnil"]
     pub fn pushnil(state: lua_State);
@@ -243,12 +243,12 @@ extern "C" {
     #[link_name = "lua_pushstring"]
     pub fn pushstring(state: lua_State, str: *const u8);
     /// Pushes a new C closure onto the stack.
-    /// 
+    ///
     /// When a C function is created, it is possible to associate some values with it, thus creating a C closure (see [§3.4](https://www.lua.org/manual/5.1/manual.html#3.4)); these values are then accessible to the function whenever it is called.
     /// To associate values with a C function, first these values should be pushed onto the stack (when there are multiple values, the first value is pushed first).
     /// Then [`pushcclosure`] (`lua_pushcclosure`)  is called to create and push the C function onto the stack, with the argument n telling how many values should be associated with the function.
     /// [`pushcclosure`] (`lua_pushcclosure`) also pops these values from the stack.
-    /// 
+    ///
     /// The maximum value for `upvalues` is 255.
     #[link_name = "lua_pushcclosure"]
     pub fn pushcclosure(state: lua_State, func: lua_CFunction, upvalues: i32);
@@ -256,7 +256,7 @@ extern "C" {
     #[link_name = "lua_pushboolean"]
     pub fn pushboolean(state: lua_State, bool: i32);
     /// Pushes a light userdata onto the stack.
-    /// 
+    ///
     /// Userdata represent C values in Lua
     /// A _light userdata_ represents a pointer.
     /// It is a value (like a number): you do not create it, it has no individual metatable, and it is not collected (as it was never created).
@@ -268,9 +268,9 @@ extern "C" {
     pub fn pushthread(state: lua_State) -> i32;
 
     // get functions (Lua -> stack)
-    
+
     /// Pushes onto the stack the value `t[k]`, where `t` is the value at the given valid index and `k` is the value at the top of the stack.
-    /// 
+    ///
     /// This function pops the key from the stack (putting the resulting value in its place).
     /// As in Lua, this function may trigger a metamethod for the "index" event (see [§2.8](https://www.lua.org/manual/5.1/manual.html#2.8)).
     #[link_name = "lua_gettable"]
@@ -290,12 +290,12 @@ extern "C" {
     #[link_name = "lua_createtable"]
     pub fn createtable(state: lua_State, array: i32, hash: i32);
     /// This function allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address.
-    /// 
+    ///
     /// Userdata represent C values in Lua.
     /// A _full userdata_ represents a block of memory.
     /// It is an object (like a table): you must create it, it can have its own metatable, and you can detect when it is being collected.
     /// A full userdata is only equal to itself (under raw equality).
-    /// 
+    ///
     /// When Lua collects a full userdata with a [`__gc`](https://www.lua.org/manual/5.1/manual.html#2.10.1) metamethod, Lua calls the metamethod and marks the userdata as finalized.
     /// When this userdata is collected again then Lua frees its corresponding memory.
     #[link_name = "lua_newuserdata"]
@@ -310,12 +310,12 @@ extern "C" {
     // set functions (stack -> Lua)
 
     /// Does the equivalent to `t[k] = v`, where `t` is the value at the given valid index, `v` is the value at the top of the stack, and `k` is the value just below the top.
-    /// 
+    ///
     /// This function pops both the key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event (see [§2.8](https://www.lua.org/manual/5.1/manual.html#2.8)).
     #[link_name = "lua_settable"]
     pub fn settable(state: lua_State, index: i32);
     /// Does the equivalent to `t[k] = v`, where `t` is the value at the given valid index and `v` is the value at the top of the stack.
-    /// 
+    ///
     /// This function pops the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event (see [§2.8](https://www.lua.org/manual/5.1/manual.html#2.8)).
     #[link_name = "lua_setfield"]
     pub fn setfield(state: lua_State, index: i32, str: *const u8);
@@ -323,21 +323,21 @@ extern "C" {
     #[link_name = "lua_rawset"]
     pub fn rawset(state: lua_State, index: i32);
     /// Does the equivalent of `t[n] = v`, where `t` is the value at the given valid index and `v` is the value at the top of the stack.
-    /// 
+    ///
     /// This function pops the value from the stack. The assignment is raw; that is, it does not invoke metamethods.
     #[link_name = "lua_rawseti"]
     pub fn rawseti(state: lua_State, index: i32, slot: i32);
-    /// Pops a table from the stack and sets it as the new metatable for the value at the given acceptable index. 
+    /// Pops a table from the stack and sets it as the new metatable for the value at the given acceptable index.
     #[link_name = "lua_setmetatable"]
     pub fn setmetatable(state: lua_State, index: i32) -> i32;
-    /// Pops a table from the stack and sets it as the new environment for the value at the given index. If the value at the given index is neither a function nor a thread nor a userdata, [`setfenv`] (`lua_setfenv`) returns 0. Otherwise it returns 1. 
+    /// Pops a table from the stack and sets it as the new environment for the value at the given index. If the value at the given index is neither a function nor a thread nor a userdata, [`setfenv`] (`lua_setfenv`) returns 0. Otherwise it returns 1.
     #[link_name = "lua_setfenv"]
     pub fn setfenv(state: lua_State, index: i32) -> i32;
 
     // `load' and `call' functions (load and run Lua code)
-    
-    /// Calls a function. 
-    /// 
+
+    /// Calls a function.
+    ///
     /// To call a function you must use the following protocol: first, the function to be called is pushed onto the stack; then, the arguments to the function are pushed in direct order; that is, the first argument is pushed first.
     /// Finally you call [`call`] (`lua_call`); `nargs` is the number of arguments that you pushed onto the stack.
     /// All arguments and the function value are popped from the stack when the function is called.
@@ -345,18 +345,18 @@ extern "C" {
     /// The number of results is adjusted to `nrets`, unless `nrets` is `LUA_MULTRET`.
     /// In this case, _all_ results from the function are pushed.
     /// Lua takes care that the returned values fit into the stack space.
-    /// The function results are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on the top of the stack. 
-    /// 
-    /// Any error inside the called function is propagated upwards (with a `longjmp`). 
+    /// The function results are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on the top of the stack.
+    ///
+    /// Any error inside the called function is propagated upwards (with a `longjmp`).
     #[link_name = "lua_call"]
     pub fn call(state: lua_State, nargs: i32, nrets: i32);
     /// Calls a function in protected mode.
-    /// 
+    ///
     /// Both nargs and nresults have the same meaning as in [`call`] (`lua_call`).
     /// If there are no errors during the call, [`pcall`] (`lua_pcall`) behaves exactly like [`call`] (`lua_call`).
     /// However, if there is any error, [`pcall`] (`lua_pcall`) catches it, pushes a single value on the stack (the error message), and returns an error code.
     /// Like [`call`] (`lua_call`), [`pcall`] (`lua_pcall`) always removes the function and its arguments from the stack.
-    /// 
+    ///
     /// If `errfunc` is 0, then the error message returned on the stack is exactly the original error message.
     /// Otherwise, `errfunc` is the stack index of an _error handler function_. (In the current implementation, this index cannot be a pseudo-index.)
     /// In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by [`pcall`] (`lua_pcall`).
@@ -371,9 +371,9 @@ extern "C" {
     ) -> Status;
     fn lua_dump(state: lua_State, writer: lua_Writer, userdata: *mut c_void) -> i32;
 
-    // miscellaneous functions 
+    // miscellaneous functions
 
-    /// Generates a Lua error. The error message (which can actually be a Lua value of any type) must be on the stack top. This function does a long jump, and therefore never returns. (see [`luaL_error`](https://www.lua.org/manual/5.1/manual.html#luaL_error)). 
+    /// Generates a Lua error. The error message (which can actually be a Lua value of any type) must be on the stack top. This function does a long jump, and therefore never returns. (see [`luaL_error`](https://www.lua.org/manual/5.1/manual.html#luaL_error)).
     #[link_name = "lua_error"]
     pub fn error(state: lua_State) -> i32;
     // #[link_name = "lua_next"]
@@ -384,13 +384,13 @@ extern "C" {
     // lauxlib
 
     /// Checks whether the function argument `index` is a string and returns this string; if `length` is not NULL fills `*length` with the string's length.
-    /// 
-    /// This function uses [`tolstring`] (`lua_tolstring`) to get its result, so all conversions and caveats of that function apply here. 
+    ///
+    /// This function uses [`tolstring`] (`lua_tolstring`) to get its result, so all conversions and caveats of that function apply here.
     #[link_name = "luaL_checklstring"]
     pub fn Lchecklstring(state: lua_State, index: i32, length: &mut usize) -> *const u8;
     /// If the function argument `index` is a string, returns this string. If this argument is absent or is **nil**, returns `default`. Otherwise, raises an error.
-    /// 
-    /// If `length` is not `NULL`, fills the position `*length` with the results's length. 
+    ///
+    /// If `length` is not `NULL`, fills the position `*length` with the results's length.
     #[link_name = "luaL_optlstring"]
     pub fn Loptlstring(
         state: lua_State,
@@ -398,23 +398,23 @@ extern "C" {
         default: *const u8,
         length: &mut usize,
     ) -> *const u8;
-    /// Checks whether the function argument `index` is a number and returns this number. 
+    /// Checks whether the function argument `index` is a number and returns this number.
     #[link_name = "luaL_checknumber"]
     pub fn Lchecknumber(state: lua_State, index: i32) -> f64;
     /// If the function argument `index` is a number, returns this number. If this argument is absent or is **nil**, returns `default`. Otherwise, raises an error.
     #[link_name = "luaL_optnumber"]
     pub fn Loptnumber(state: lua_State, index: i32, default: f64) -> f64;
-    /// Checks whether the function argument `index` is a number and returns this number cast to a [`lua_Integer`](https://www.lua.org/manual/5.1/manual.html#lua_Integer). 
+    /// Checks whether the function argument `index` is a number and returns this number cast to a [`lua_Integer`](https://www.lua.org/manual/5.1/manual.html#lua_Integer).
     #[link_name = "luaL_checkinteger"]
     pub fn Lcheckinteger(state: lua_State, index: i32) -> isize;
     /// If the function argument `index` is a number, returns this number cast to a [`lua_Integer`](https://www.lua.org/manual/5.1/manual.html#lua_Integer).
-    /// If this argument is absent or is **nil**, returns `default`. Otherwise, raises an error. 
+    /// If this argument is absent or is **nil**, returns `default`. Otherwise, raises an error.
     #[link_name = "luaL_optinteger"]
     pub fn Loptinteger(state: lua_State, index: i32, default: isize) -> isize;
     /// Grows the stack size to `top + size` elements, raising an error if the stack cannot grow to that size. `msg` is an additional text to go into the error message.
     #[link_name = "luaL_checkstack"]
     pub fn Lcheckstack(state: lua_State, size: i32, msg: *const u8);
-    /// Checks whether the function argument `index` has type `typ`. See [`lua_type`](https://www.lua.org/manual/5.1/manual.html#lua_type) for the encoding of types for `typ`. 
+    /// Checks whether the function argument `index` has type `typ`. See [`lua_type`](https://www.lua.org/manual/5.1/manual.html#lua_type) for the encoding of types for `typ`.
     #[link_name = "luaL_checktype"]
     pub fn Lchecktype(state: lua_State, index: i32, typ: i32);
     /// Checks whether the function has an argument of any type (including **nil**) at position `index`.
@@ -422,11 +422,11 @@ extern "C" {
     pub fn Lcheckany(state: lua_State, index: i32);
 
     /// If the registry already has the key `type_name`, returns `false`. Otherwise, creates a new table to be used as a metatable for userdata, adds it to the registry with key `type_name`, and returns `true`.
-    /// 
-    /// In both cases pushes onto the stack the final value associated with `type_name` in the registry. 
+    ///
+    /// In both cases pushes onto the stack the final value associated with `type_name` in the registry.
     #[link_name = "luaL_newmetatable"]
     pub fn Lnewmetatable(state: lua_State, type_name: *const u8) -> bool;
-    /// Checks whether the function argument `index` is a userdata of the type `type_name` (see [`Lnewmetatable`]). 
+    /// Checks whether the function argument `index` is a userdata of the type `type_name` (see [`Lnewmetatable`]).
     #[link_name = "luaL_checkudata"]
     pub fn Lcheckudata(state: lua_State, index: i32, type_name: *const u8) -> *mut c_void;
 
@@ -435,14 +435,14 @@ extern "C" {
     ///     chunkname:currentline:
     /// ```
     /// Level 0 is the running function, level 1 is the function that called the running function, etc.
-    /// 
-    /// This function is used to build a prefix for error messages. 
+    ///
+    /// This function is used to build a prefix for error messages.
     #[link_name = "luaL_where"]
     pub fn Lpush_where(state: lua_State, level: i32);
     /// Raises an error. The error message format is given by `fmt` plus any extra arguments, following the same rules of [`lua_pushfstring`](https://www.lua.org/manual/5.1/manual.html#lua_pushfstring).
     /// It also adds at the beginning of the message the file name and the line number where the error occurred, if this information is available.
-    /// 
-    /// This function never returns, but it is an idiom to use it in C functions as `return luaL_error(args)`. 
+    ///
+    /// This function never returns, but it is an idiom to use it in C functions as `return luaL_error(args)`.
     #[link_name = "luaL_error"]
     pub fn Lerror(state: lua_State, fmt: *const u8, ...) -> i32;
 }
@@ -463,7 +463,7 @@ extern "C" {
 /// }
 /// pushfunction(state, test_function);
 /// setfield(state, -2, cstr!("test_immutable_function"));
-/// 
+///
 /// let mut counter = 0;
 /// pushfunction_mut(state, move |_| {
 ///     println!("Here is yout counter!: {}", counter);
@@ -482,13 +482,21 @@ where
     where
         FUNC: 'static + FnMut(lua_State) -> Result,
     {
-        let callback_ptr = if std::mem::size_of::<FUNC>() > 0 {touserdata(state, upvalueindex!(1))} else {null_mut()};
+        let callback_ptr = if std::mem::size_of::<FUNC>() > 0 {
+            touserdata(state, upvalueindex!(1))
+        } else {
+            null_mut()
+        };
         match (&mut *callback_ptr.cast::<FUNC>())(state) {
             Ok(nrets) => nrets,
             Err(err) => {
                 let error_str = err.to_string();
                 std::mem::drop(err);
-                pushlstring(state, error_str.as_bytes().as_ptr(), error_str.as_bytes().len());
+                pushlstring(
+                    state,
+                    error_str.as_bytes().as_ptr(),
+                    error_str.as_bytes().len(),
+                );
                 std::mem::drop(error_str);
                 error(state);
                 unreachable_unchecked();
@@ -496,7 +504,6 @@ where
         }
     }
 
-    
     if std::mem::size_of::<FUNC>() > 0 {
         let udata_ptr = newuserdata(state, std::mem::size_of::<FUNC>()).cast::<FUNC>();
         udata_ptr.write(callback);

+ 7 - 3
src/loadx.rs

@@ -1,6 +1,6 @@
 use std::{ffi::c_void, hint::unreachable_unchecked};
 
-use crate::{lua_State, LError, lua_loadx, Status, tolstring, pop};
+use crate::{lua_State, lua_loadx, pop, tolstring, LError, Status};
 
 pub unsafe fn loadx<READER>(
     state: lua_State,
@@ -18,7 +18,11 @@ where
         buffer: [u8; 4096],
         reader: &'a mut READER,
     }
-    unsafe extern "C" fn reader_callback<READER>(_: lua_State, userdata: *mut c_void, size: &mut usize) -> *const u8
+    unsafe extern "C" fn reader_callback<READER>(
+        _: lua_State,
+        userdata: *mut c_void,
+        size: &mut usize,
+    ) -> *const u8
     where
         READER: std::io::Read,
     {
@@ -60,4 +64,4 @@ where
         }
         _ => unreachable_unchecked(),
     }
-}
+}