Make State Send and Sync
ci/woodpecker/push/debug Pipeline was successful
Details
ci/woodpecker/push/debug Pipeline was successful
Details
This commit is contained in:
parent
54c9926a04
commit
a761f4094b
|
@ -20,7 +20,7 @@ impl<'a> Drop for LuaRef<'a> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
if Arc::strong_count(&self.lref) == 1 {
|
if Arc::strong_count(&self.lref) == 1 {
|
||||||
let s = self.state.lua.as_ptr();
|
let s = *self.state.lua.as_ptr();
|
||||||
lua::luaL_unref(s, lua::LUA_REGISTRYINDEX, *self.lref);
|
lua::luaL_unref(s, lua::LUA_REGISTRYINDEX, *self.lref);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use core::ffi;
|
use core::ffi;
|
||||||
use std::{alloc::{self, Layout}, any::TypeId, cell::RefCell, collections::HashMap, ffi::{CStr, CString}, mem, ptr::{self, NonNull}, str::Utf8Error, sync::Arc};
|
use std::{alloc::{self, Layout}, any::TypeId, cell::RefCell, collections::HashMap, ffi::{CStr, CString}, mem, ptr::{self, NonNull}, str::Utf8Error, sync::{atomic::AtomicPtr, Arc}};
|
||||||
|
|
||||||
use lua::lua_gc;
|
use lua::lua_gc;
|
||||||
use mlua_sys as lua;
|
use mlua_sys as lua;
|
||||||
|
@ -117,7 +117,8 @@ impl ExtraSpace {
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct StatePtr {
|
pub struct StatePtr {
|
||||||
pub lua: Arc<NonNull<lua::lua_State>>,
|
// arc is used here to make self Cloneable
|
||||||
|
pub lua: Arc<AtomicPtr<lua::lua_State>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
|
@ -167,7 +168,7 @@ impl State {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let s = Self {
|
let s = Self {
|
||||||
ptr: StatePtr {
|
ptr: StatePtr {
|
||||||
lua: Arc::new(unsafe { NonNull::new_unchecked(lua::luaL_newstate()) })
|
lua: Arc::new(AtomicPtr::new(unsafe { lua::luaL_newstate() }))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -196,7 +197,7 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn state_ptr(&self) -> *mut lua::lua_State {
|
pub(crate) fn state_ptr(&self) -> *mut lua::lua_State {
|
||||||
self.ptr.lua.as_ptr()
|
unsafe { *self.ptr.lua.as_ptr() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alloc_extra_space(&self) {
|
fn alloc_extra_space(&self) {
|
||||||
|
|
40
src/table.rs
40
src/table.rs
|
@ -235,6 +235,46 @@ impl<'a> Table<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn raw_seti<V>(&self, key: i64, val: V) -> Result<()>
|
||||||
|
where
|
||||||
|
V: AsLua<'a>
|
||||||
|
{
|
||||||
|
debug_assert!(key > 0, "Indexes in Lua start at 1");
|
||||||
|
|
||||||
|
let s = self.state.state_ptr();
|
||||||
|
unsafe {
|
||||||
|
self.state.ensure_stack(2)?;
|
||||||
|
let _g = StackGuard::new(self.state);
|
||||||
|
|
||||||
|
self.lref.push_to_lua_stack(self.state)?;
|
||||||
|
val.as_lua(self.state)?
|
||||||
|
.push_to_lua_stack(self.state)?;
|
||||||
|
|
||||||
|
lua::lua_rawseti(s, -2, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn raw_geti<K, V>(&self, key: i64) -> Result<V>
|
||||||
|
where
|
||||||
|
V: FromLua<'a>,
|
||||||
|
{
|
||||||
|
debug_assert!(key > 0, "Indexes in Lua start at 1");
|
||||||
|
|
||||||
|
let s = self.state.state_ptr();
|
||||||
|
unsafe {
|
||||||
|
self.state.ensure_stack(1)?;
|
||||||
|
let _g = StackGuard::new(self.state);
|
||||||
|
|
||||||
|
self.lref.push_to_lua_stack(self.state)?;
|
||||||
|
lua::lua_rawgeti(s, -1, key);
|
||||||
|
|
||||||
|
let val = Value::from_lua_stack(self.state)?;
|
||||||
|
V::from_lua(self.state, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a boolean indicating if this table has a key without calling any meta methods.
|
/// Returns a boolean indicating if this table has a key without calling any meta methods.
|
||||||
pub fn raw_has_key<K>(&self, key: K) -> Result<bool>
|
pub fn raw_has_key<K>(&self, key: K) -> Result<bool>
|
||||||
where
|
where
|
||||||
|
|
Loading…
Reference in New Issue