mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 15e353a7f8 | |||
| 765117c2bb | |||
| 573d71345f | |||
| 72de17bf47 | |||
| 5a96e80266 | |||
| bfdb4087b8 | |||
| eb84284824 | |||
| 34679e105d | |||
| bc194981fc | |||
| c9715aa5d9 | |||
| c108dc8213 | |||
| e86ef9d755 | |||
| 05eb20f9c6 | |||
| 9716918517 | |||
| c88417a3b6 | |||
| 6e95386f30 | |||
| 6807dfa22e | |||
| f27c49f931 |
@@ -14,7 +14,7 @@ jobs:
|
||||
|
||||
- name: Generate coverage report
|
||||
run: |
|
||||
cargo tarpaulin --verbose --features lua54,vendored,async,send,serialize,macros --out xml --exclude-files benches --exclude-files build --exclude-files mlua_derive --exclude-files src/ffi --exclude-files tests
|
||||
cargo tarpaulin --out xml --tests --exclude-files benches/* --exclude-files src/ffi/*/*
|
||||
|
||||
- name: Upload report to codecov.io
|
||||
uses: codecov/codecov-action@v3
|
||||
|
||||
@@ -1,3 +1,25 @@
|
||||
## v0.8.9
|
||||
|
||||
- Update minimal (vendored) Lua 5.4 to 5.4.6
|
||||
- Use `lua_closethread` instead of `lua_resetthread` in vendored mode (Lua 5.4.6)
|
||||
- Allow deserializing Lua null into unit (`()`) or unit struct.
|
||||
|
||||
## v0.8.8
|
||||
|
||||
- Fix potential deadlock when trying to reuse dropped registry keys.
|
||||
- Optimize userdata methods call when __index and fields_getters are nil
|
||||
|
||||
## v0.8.7
|
||||
|
||||
- Minimum Luau updated to 0.555 (`LUAI_MAXCSTACK` limit increased to 100000)
|
||||
- `_VERSION` in Luau now includes version number
|
||||
- Fixed lifetime of `DebugNames` in `Debug::names()` and `DebugSource` in `Debug::source()`
|
||||
- Fixed subtraction overflow when calculating index for `MultiValue::get()`
|
||||
|
||||
## v0.8.6
|
||||
|
||||
- Fixed bug when recycled Registry slot can be set to Nil
|
||||
|
||||
## v0.8.5
|
||||
|
||||
- Fixed potential unsoundness when using `Layout::from_size_align_unchecked` and Rust 1.65+
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mlua"
|
||||
version = "0.8.5" # remember to update html_root_url and mlua_derive
|
||||
version = "0.8.9" # remember to update mlua_derive
|
||||
authors = ["Aleksandr Orlenko <zxteam@pm.me>", "kyren <catherine@chucklefish.org>"]
|
||||
edition = "2021"
|
||||
repository = "https://github.com/khvzak/mlua"
|
||||
@@ -56,9 +56,9 @@ parking_lot = { version = "0.12", optional = true }
|
||||
[build-dependencies]
|
||||
cc = { version = "1.0" }
|
||||
pkg-config = { version = "0.3.17" }
|
||||
lua-src = { version = ">= 544.0.0, < 550.0.0", optional = true }
|
||||
lua-src = { version = ">= 546.0.0, < 550.0.0", optional = true }
|
||||
luajit-src = { version = ">= 210.4.0, < 220.0.0", optional = true }
|
||||
luau0-src = { version = "0.4.0", optional = true }
|
||||
luau0-src = { version = "0.5.0", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
rustyline = "10.0"
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
[crates.io]: https://crates.io/crates/mlua
|
||||
[API Documentation]: https://docs.rs/mlua/badge.svg
|
||||
[docs.rs]: https://docs.rs/mlua
|
||||
[Coverage Status]: https://codecov.io/gh/khvzak/mlua/branch/master/graph/badge.svg?token=99339FS1CG
|
||||
[Coverage Status]: https://codecov.io/gh/khvzak/mlua/branch/v0.8/graph/badge.svg?token=99339FS1CG
|
||||
[codecov.io]: https://codecov.io/gh/khvzak/mlua
|
||||
[MSRV]: https://img.shields.io/badge/rust-1.56+-brightgreen.svg?&logo=rust
|
||||
|
||||
|
||||
@@ -112,7 +112,10 @@ extern "C" {
|
||||
pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
|
||||
pub fn lua_close(L: *mut lua_State);
|
||||
pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
|
||||
// Deprecated in Lua 5.4.6
|
||||
pub fn lua_resetthread(L: *mut lua_State) -> c_int;
|
||||
#[cfg(feature = "vendored")]
|
||||
pub fn lua_closethread(L: *mut lua_State, from: *mut lua_State) -> c_int;
|
||||
|
||||
pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
|
||||
|
||||
|
||||
+6
-3
@@ -7,12 +7,15 @@ use std::ptr;
|
||||
// Option for multiple returns in 'lua_pcall' and 'lua_call'
|
||||
pub const LUA_MULTRET: c_int = -1;
|
||||
|
||||
// Max number of Lua stack slots
|
||||
const LUAI_MAXCSTACK: c_int = 100000;
|
||||
|
||||
//
|
||||
// Pseudo-indices
|
||||
//
|
||||
pub const LUA_REGISTRYINDEX: c_int = -10000;
|
||||
pub const LUA_ENVIRONINDEX: c_int = -10001;
|
||||
pub const LUA_GLOBALSINDEX: c_int = -10002;
|
||||
pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXCSTACK - 2000;
|
||||
pub const LUA_ENVIRONINDEX: c_int = -LUAI_MAXCSTACK - 2001;
|
||||
pub const LUA_GLOBALSINDEX: c_int = -LUAI_MAXCSTACK - 2002;
|
||||
|
||||
pub const fn lua_upvalueindex(i: c_int) -> c_int {
|
||||
LUA_GLOBALSINDEX - i
|
||||
|
||||
+2
-2
@@ -63,7 +63,7 @@ impl<'lua> Debug<'lua> {
|
||||
}
|
||||
|
||||
/// Corresponds to the `n` what mask.
|
||||
pub fn names(&self) -> DebugNames<'lua> {
|
||||
pub fn names(&self) -> DebugNames {
|
||||
unsafe {
|
||||
#[cfg(not(feature = "luau"))]
|
||||
mlua_assert!(
|
||||
@@ -87,7 +87,7 @@ impl<'lua> Debug<'lua> {
|
||||
}
|
||||
|
||||
/// Corresponds to the `S` what mask.
|
||||
pub fn source(&self) -> DebugSource<'lua> {
|
||||
pub fn source(&self) -> DebugSource {
|
||||
unsafe {
|
||||
#[cfg(not(feature = "luau"))]
|
||||
mlua_assert!(
|
||||
|
||||
@@ -71,8 +71,6 @@
|
||||
//! [`serde::Serialize`]: https://docs.serde.rs/serde/ser/trait.Serialize.html
|
||||
//! [`serde::Deserialize`]: https://docs.serde.rs/serde/de/trait.Deserialize.html
|
||||
|
||||
// mlua types in rustdoc of other crates get linked to here.
|
||||
#![doc(html_root_url = "https://docs.rs/mlua/0.8.5")]
|
||||
// Deny warnings inside doc tests / examples. When this isn't present, rustdoc doesn't show *any*
|
||||
// warnings at all.
|
||||
#![doc(test(attr(deny(warnings))))]
|
||||
|
||||
+52
-26
@@ -88,6 +88,8 @@ pub(crate) struct ExtraData {
|
||||
|
||||
registered_userdata: FxHashMap<TypeId, c_int>,
|
||||
registered_userdata_mt: FxHashMap<*const c_void, Option<TypeId>>,
|
||||
|
||||
// When Lua instance dropped, setting `None` would prevent collecting `RegistryKey`s
|
||||
registry_unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
|
||||
|
||||
#[cfg(not(feature = "send"))]
|
||||
@@ -1736,8 +1738,10 @@ impl Lua {
|
||||
let extra = &mut *self.extra.get();
|
||||
if extra.recycled_thread_cache.len() < extra.recycled_thread_cache.capacity() {
|
||||
let thread_state = ffi::lua_tothread(extra.ref_thread, thread.0.index);
|
||||
#[cfg(feature = "lua54")]
|
||||
#[cfg(all(feature = "lua54", not(feature = "vendored")))]
|
||||
let status = ffi::lua_resetthread(thread_state);
|
||||
#[cfg(all(feature = "lua54", feature = "vendored"))]
|
||||
let status = ffi::lua_closethread(thread_state, self.state);
|
||||
#[cfg(feature = "lua54")]
|
||||
if status != ffi::LUA_OK {
|
||||
// Error object is on top, drop it
|
||||
@@ -2060,34 +2064,38 @@ impl Lua {
|
||||
/// [`RegistryKey`]: crate::RegistryKey
|
||||
pub fn create_registry_value<'lua, T: ToLua<'lua>>(&'lua self, t: T) -> Result<RegistryKey> {
|
||||
let t = t.to_lua(self)?;
|
||||
if t == Value::Nil {
|
||||
// Special case to skip calling `luaL_ref` and use `LUA_REFNIL` instead
|
||||
let unref_list = unsafe { (*self.extra.get()).registry_unref_list.clone() };
|
||||
return Ok(RegistryKey::new(ffi::LUA_REFNIL, unref_list));
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let _sg = StackGuard::new(self.state);
|
||||
check_stack(self.state, 4)?;
|
||||
|
||||
let unref_list = (*self.extra.get()).registry_unref_list.clone();
|
||||
self.push_value(t)?;
|
||||
|
||||
// Try to reuse previously allocated RegistryKey
|
||||
let unref_list2 = unref_list.clone();
|
||||
let mut unref_list2 = mlua_expect!(unref_list2.lock(), "unref list poisoned");
|
||||
if let Some(registry_id) = unref_list2.as_mut().and_then(|x| x.pop()) {
|
||||
// Try to reuse previously allocated slot
|
||||
let unref_list = (*self.extra.get()).registry_unref_list.clone();
|
||||
let free_registry_id = mlua_expect!(unref_list.lock(), "unref list poisoned")
|
||||
.as_mut()
|
||||
.and_then(|x| x.pop());
|
||||
if let Some(registry_id) = free_registry_id {
|
||||
// It must be safe to replace the value without triggering memory error
|
||||
ffi::lua_rawseti(self.state, ffi::LUA_REGISTRYINDEX, registry_id as Integer);
|
||||
return Ok(RegistryKey {
|
||||
registry_id,
|
||||
unref_list,
|
||||
});
|
||||
return Ok(RegistryKey::new(registry_id, unref_list));
|
||||
}
|
||||
|
||||
// Allocate a new RegistryKey
|
||||
let registry_id = protect_lua!(self.state, 1, 0, |state| {
|
||||
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
|
||||
})?;
|
||||
|
||||
Ok(RegistryKey {
|
||||
registry_id,
|
||||
unref_list,
|
||||
})
|
||||
let registry_id = if self.unlikely_memory_error() {
|
||||
ffi::luaL_ref(self.state, ffi::LUA_REGISTRYINDEX)
|
||||
} else {
|
||||
protect_lua!(self.state, 1, 0, |state| {
|
||||
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
|
||||
})?
|
||||
};
|
||||
Ok(RegistryKey::new(registry_id, unref_list))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2102,13 +2110,16 @@ impl Lua {
|
||||
return Err(Error::MismatchedRegistryKey);
|
||||
}
|
||||
|
||||
let value = unsafe {
|
||||
let _sg = StackGuard::new(self.state);
|
||||
check_stack(self.state, 1)?;
|
||||
let value = match key.is_nil() {
|
||||
true => Value::Nil,
|
||||
false => unsafe {
|
||||
let _sg = StackGuard::new(self.state);
|
||||
check_stack(self.state, 1)?;
|
||||
|
||||
let id = key.registry_id as Integer;
|
||||
ffi::lua_rawgeti(self.state, ffi::LUA_REGISTRYINDEX, id);
|
||||
self.pop_value()
|
||||
let id = key.registry_id as Integer;
|
||||
ffi::lua_rawgeti(self.state, ffi::LUA_REGISTRYINDEX, id);
|
||||
self.pop_value()
|
||||
},
|
||||
};
|
||||
T::from_lua(value, self)
|
||||
}
|
||||
@@ -2147,13 +2158,28 @@ impl Lua {
|
||||
}
|
||||
|
||||
let t = t.to_lua(self)?;
|
||||
if t == Value::Nil && key.is_nil() {
|
||||
// Nothing to replace
|
||||
return Ok(());
|
||||
} else if t != Value::Nil && key.registry_id == ffi::LUA_REFNIL {
|
||||
// We cannot update `LUA_REFNIL` slot
|
||||
let err = "cannot replace nil value with non-nil".to_string();
|
||||
return Err(Error::RuntimeError(err));
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let _sg = StackGuard::new(self.state);
|
||||
check_stack(self.state, 2)?;
|
||||
|
||||
self.push_value(t)?;
|
||||
// It must be safe to replace the value without triggering memory error
|
||||
let id = key.registry_id as Integer;
|
||||
if t == Value::Nil {
|
||||
self.push_value(Value::Integer(id))?;
|
||||
key.set_nil(true);
|
||||
} else {
|
||||
self.push_value(t)?;
|
||||
key.set_nil(false);
|
||||
}
|
||||
// It must be safe to replace the value without triggering memory error
|
||||
ffi::lua_rawseti(self.state, ffi::LUA_REGISTRYINDEX, id);
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -22,6 +22,12 @@ impl Lua {
|
||||
globals.raw_set("require", self.create_function(lua_require)?)?;
|
||||
globals.raw_set("vector", self.create_c_function(lua_vector)?)?;
|
||||
|
||||
// Set `_VERSION` global to include version number
|
||||
// The environment variable `LUAU_VERSION` set by the build script
|
||||
if let Some(version) = option_env!("LUAU_VERSION") {
|
||||
globals.raw_set("_VERSION", format!("Luau {version}"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+23
-1
@@ -327,9 +327,31 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::LightUserData(ud) if ud.0.is_null() => visitor.visit_unit(),
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::LightUserData(ud) if ud.0.is_null() => visitor.visit_unit(),
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
serde::forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes
|
||||
byte_buf unit unit_struct identifier ignored_any
|
||||
byte_buf identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -201,8 +201,10 @@ impl<'lua> Thread<'lua> {
|
||||
lua.push_ref(&self.0);
|
||||
let thread_state = ffi::lua_tothread(lua.state, -1);
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
#[cfg(all(feature = "lua54", not(feature = "vendored")))]
|
||||
let status = ffi::lua_resetthread(thread_state);
|
||||
#[cfg(all(feature = "lua54", feature = "vendored"))]
|
||||
let status = ffi::lua_closethread(thread_state, lua.state);
|
||||
#[cfg(feature = "lua54")]
|
||||
if status != ffi::LUA_OK {
|
||||
return Err(pop_error(thread_state, status));
|
||||
|
||||
+33
-4
@@ -1,6 +1,7 @@
|
||||
use std::cell::UnsafeCell;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{fmt, mem, ptr};
|
||||
|
||||
@@ -104,6 +105,7 @@ pub(crate) struct DestructedUserdata;
|
||||
/// [`AnyUserData::get_user_value`]: crate::AnyUserData::get_user_value
|
||||
pub struct RegistryKey {
|
||||
pub(crate) registry_id: c_int,
|
||||
pub(crate) is_nil: AtomicBool,
|
||||
pub(crate) unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
|
||||
}
|
||||
|
||||
@@ -129,15 +131,27 @@ impl Eq for RegistryKey {}
|
||||
|
||||
impl Drop for RegistryKey {
|
||||
fn drop(&mut self) {
|
||||
let mut unref_list = mlua_expect!(self.unref_list.lock(), "unref list poisoned");
|
||||
if let Some(list) = unref_list.as_mut() {
|
||||
list.push(self.registry_id);
|
||||
// We don't need to collect nil slot
|
||||
if self.registry_id > ffi::LUA_REFNIL {
|
||||
let mut unref_list = mlua_expect!(self.unref_list.lock(), "unref list poisoned");
|
||||
if let Some(list) = unref_list.as_mut() {
|
||||
list.push(self.registry_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RegistryKey {
|
||||
// Destroys the RegistryKey without adding to the drop list
|
||||
// Creates a new instance of `RegistryKey`
|
||||
pub(crate) const fn new(id: c_int, unref_list: Arc<Mutex<Option<Vec<c_int>>>>) -> Self {
|
||||
RegistryKey {
|
||||
registry_id: id,
|
||||
is_nil: AtomicBool::new(id == ffi::LUA_REFNIL),
|
||||
unref_list,
|
||||
}
|
||||
}
|
||||
|
||||
// Destroys the `RegistryKey` without adding to the unref list
|
||||
pub(crate) fn take(self) -> c_int {
|
||||
let registry_id = self.registry_id;
|
||||
unsafe {
|
||||
@@ -146,6 +160,21 @@ impl RegistryKey {
|
||||
}
|
||||
registry_id
|
||||
}
|
||||
|
||||
// Returns true if this `RegistryKey` holds a nil value
|
||||
#[inline(always)]
|
||||
pub(crate) fn is_nil(&self) -> bool {
|
||||
self.is_nil.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
// Marks value of this `RegistryKey` as `Nil`
|
||||
#[inline(always)]
|
||||
pub(crate) fn set_nil(&self, enabled: bool) {
|
||||
// We cannot replace previous value with nil in as this will break
|
||||
// Lua mechanism to find free keys.
|
||||
// Instead, we set a special flag to mark value as nil.
|
||||
self.is_nil.store(enabled, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct LuaRef<'lua> {
|
||||
|
||||
+10
-3
@@ -245,7 +245,8 @@ pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error {
|
||||
// Uses 3 (or 1 if unprotected) stack spaces, does not call checkstack.
|
||||
#[inline(always)]
|
||||
pub unsafe fn push_string(state: *mut ffi::lua_State, s: &[u8], protect: bool) -> Result<()> {
|
||||
if protect {
|
||||
// Always use protected mode if the string is too long
|
||||
if protect || s.len() > (1 << 30) {
|
||||
protect_lua!(state, 0, 1, |state| {
|
||||
ffi::lua_pushlstring(state, s.as_ptr() as *const c_char, s.len());
|
||||
})
|
||||
@@ -427,11 +428,17 @@ unsafe fn init_userdata_metatable_index(state: *mut ffi::lua_State) -> Result<()
|
||||
}
|
||||
ffi::lua_pop(state, 1);
|
||||
|
||||
// Create and cache `__index` helper
|
||||
// Create and cache `__index` generator
|
||||
let code = cstr!(
|
||||
r#"
|
||||
local error, isfunction = ...
|
||||
return function (__index, field_getters, methods)
|
||||
-- Fastpath to return methods table for index access
|
||||
if __index == nil and field_getters == nil then
|
||||
return methods
|
||||
end
|
||||
|
||||
-- Alternatively return a function for index access
|
||||
return function (self, key)
|
||||
if field_getters ~= nil then
|
||||
local field_getter = field_getters[key]
|
||||
@@ -481,7 +488,7 @@ pub unsafe fn init_userdata_metatable_newindex(state: *mut ffi::lua_State) -> Re
|
||||
}
|
||||
ffi::lua_pop(state, 1);
|
||||
|
||||
// Create and cache `__newindex` helper
|
||||
// Create and cache `__newindex` generator
|
||||
let code = cstr!(
|
||||
r#"
|
||||
local error, isfunction = ...
|
||||
|
||||
+4
-2
@@ -197,7 +197,6 @@ pub struct MultiValue<'lua>(Vec<Value<'lua>>);
|
||||
|
||||
impl<'lua> MultiValue<'lua> {
|
||||
/// Creates an empty `MultiValue` containing no values.
|
||||
#[inline]
|
||||
pub const fn new() -> MultiValue<'lua> {
|
||||
MultiValue(Vec::new())
|
||||
}
|
||||
@@ -276,7 +275,10 @@ impl<'lua> MultiValue<'lua> {
|
||||
|
||||
#[inline]
|
||||
pub fn get(&self, index: usize) -> Option<&Value<'lua>> {
|
||||
self.0.get(self.0.len() - index - 1)
|
||||
if index < self.0.len() {
|
||||
return self.0.get(self.0.len() - index - 1);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
[lua54_coverage]
|
||||
features = "lua54,vendored,async,serialize,macros"
|
||||
|
||||
[lua51_coverage]
|
||||
features = "lua51,vendored,async,serialize,macros"
|
||||
|
||||
[luau_coverage]
|
||||
features = "luau,async,serialize,macros"
|
||||
+18
-5
@@ -391,31 +391,44 @@ fn test_from_value_newtype_struct() -> Result<(), Box<dyn std::error::Error>> {
|
||||
#[test]
|
||||
fn test_from_value_enum() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let lua = Lua::new();
|
||||
lua.globals().set("null", lua.null())?;
|
||||
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
enum E {
|
||||
struct UnitStruct;
|
||||
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
enum E<T = ()> {
|
||||
Unit,
|
||||
Integer(u32),
|
||||
Tuple(u32, u32),
|
||||
Struct { a: u32 },
|
||||
Wrap(T),
|
||||
}
|
||||
|
||||
let value = lua.load(r#""Unit""#).eval()?;
|
||||
let got = lua.from_value(value)?;
|
||||
let got: E = lua.from_value(value)?;
|
||||
assert_eq!(E::Unit, got);
|
||||
|
||||
let value = lua.load(r#"{Integer = 1}"#).eval()?;
|
||||
let got = lua.from_value(value)?;
|
||||
let got: E = lua.from_value(value)?;
|
||||
assert_eq!(E::Integer(1), got);
|
||||
|
||||
let value = lua.load(r#"{Tuple = {1, 2}}"#).eval()?;
|
||||
let got = lua.from_value(value)?;
|
||||
let got: E = lua.from_value(value)?;
|
||||
assert_eq!(E::Tuple(1, 2), got);
|
||||
|
||||
let value = lua.load(r#"{Struct = {a = 3}}"#).eval()?;
|
||||
let got = lua.from_value(value)?;
|
||||
let got: E = lua.from_value(value)?;
|
||||
assert_eq!(E::Struct { a: 3 }, got);
|
||||
|
||||
let value = lua.load(r#"{Wrap = null}"#).eval()?;
|
||||
let got = lua.from_value(value)?;
|
||||
assert_eq!(E::Wrap(UnitStruct), got);
|
||||
|
||||
let value = lua.load(r#"{Wrap = null}"#).eval()?;
|
||||
let got = lua.from_value(value)?;
|
||||
assert_eq!(E::Wrap(()), got);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
+38
-2
@@ -276,7 +276,10 @@ fn test_error() -> Result<()> {
|
||||
end, 3)
|
||||
|
||||
local function handler(err)
|
||||
if string.match(_VERSION, ' 5%.1$') or string.match(_VERSION, ' 5%.2$') or _VERSION == "Luau" then
|
||||
if string.match(_VERSION, " 5%.1$")
|
||||
or string.match(_VERSION, " 5%.2$")
|
||||
or string.match(_VERSION, "Luau")
|
||||
then
|
||||
-- Special case for Lua 5.1/5.2 and Luau
|
||||
local caps = string.match(err, ': (%d+)$')
|
||||
if caps then
|
||||
@@ -793,6 +796,17 @@ fn test_replace_registry_value() -> Result<()> {
|
||||
let key = lua.create_registry_value::<i32>(42)?;
|
||||
lua.replace_registry_value(&key, "new value")?;
|
||||
assert_eq!(lua.registry_value::<String>(&key)?, "new value");
|
||||
lua.replace_registry_value(&key, Value::Nil)?;
|
||||
assert_eq!(lua.registry_value::<Value>(&key)?, Value::Nil);
|
||||
lua.replace_registry_value(&key, 123)?;
|
||||
assert_eq!(lua.registry_value::<i32>(&key)?, 123);
|
||||
|
||||
// It should be impossible to replace (initial) nil value with non-nil
|
||||
let key2 = lua.create_registry_value(Value::Nil)?;
|
||||
match lua.replace_registry_value(&key2, "abc") {
|
||||
Err(Error::RuntimeError(_)) => {}
|
||||
r => panic!("expected RuntimeError, got {r:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -844,6 +858,28 @@ fn test_mismatched_registry_key() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_registry_value_reuse() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let r1 = lua.create_registry_value("value1")?;
|
||||
let r1_slot = format!("{r1:?}");
|
||||
drop(r1);
|
||||
|
||||
// Previous slot must not be reused by nil value
|
||||
let r2 = lua.create_registry_value(Value::Nil)?;
|
||||
let r2_slot = format!("{r2:?}");
|
||||
assert_ne!(r1_slot, r2_slot);
|
||||
drop(r2);
|
||||
|
||||
// But should be reused by non-nil value
|
||||
let r3 = lua.create_registry_value("value3")?;
|
||||
let r3_slot = format!("{r3:?}");
|
||||
assert_eq!(r1_slot, r3_slot);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_application_data() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -955,7 +991,7 @@ fn test_ref_stack_exhaustion() {
|
||||
match catch_unwind(AssertUnwindSafe(|| -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
let mut vals = Vec::new();
|
||||
for _ in 0..1000000 {
|
||||
for _ in 0..10000000 {
|
||||
vals.push(lua.create_table()?);
|
||||
}
|
||||
Ok(())
|
||||
|
||||
+19
-1
@@ -1,6 +1,6 @@
|
||||
use std::ptr;
|
||||
|
||||
use mlua::{Lua, Result, Value};
|
||||
use mlua::{Lua, MultiValue, Result, Value};
|
||||
|
||||
#[test]
|
||||
fn test_value_eq() -> Result<()> {
|
||||
@@ -63,3 +63,21 @@ fn test_value_eq() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_value() {
|
||||
let mut multi_value = MultiValue::new();
|
||||
assert_eq!(multi_value.len(), 0);
|
||||
assert_eq!(multi_value.get(0), None);
|
||||
|
||||
multi_value.push_front(Value::Number(2.));
|
||||
multi_value.push_front(Value::Number(1.));
|
||||
assert_eq!(multi_value.get(0), Some(&Value::Number(1.)));
|
||||
assert_eq!(multi_value.get(1), Some(&Value::Number(2.)));
|
||||
|
||||
assert_eq!(multi_value.pop_front(), Some(Value::Number(1.)));
|
||||
assert_eq!(multi_value[0], Value::Number(2.));
|
||||
|
||||
multi_value.clear();
|
||||
assert!(multi_value.is_empty());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user