Skip to content

Commit 065be7a

Browse files
committed
clean some unsafe
1 parent 8eebf3d commit 065be7a

17 files changed

Lines changed: 275 additions & 174 deletions

File tree

crates/luars/src/compiler/func_state.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use crate::LuaProto;
12
use crate::compiler::{ExpDesc, ExpKind, ExpUnion, statement};
3+
use crate::lua_vm::GlobalState;
24
use crate::lua_vm::lua_limits::{MAX_SRC_LEN, MAXCCALLS, MAXUPVAL, MAXVARS};
3-
use crate::{LuaProto, LuaVM};
45
use crate::{LuaValue, compiler::parser::LuaLexer};
56

67
// Upvalue descriptor
@@ -22,7 +23,7 @@ pub struct FuncState<'a> {
2223
/// encapsulate the single unsafe dereference.
2324
prev: Option<*mut FuncState<'a>>,
2425
pub lexer: &'a mut LuaLexer<'a>,
25-
pub vm: &'a mut LuaVM,
26+
pub vm: &'a mut GlobalState,
2627
pub compiler_state: &'a mut CompilerState,
2728
pub block_cnt_id: Option<BlockCntId>,
2829
pub pc: usize, // next position to code (equivalent to pc)
@@ -188,7 +189,7 @@ pub struct VarDesc {
188189
impl<'a> FuncState<'a> {
189190
pub fn new(
190191
lexer: &'a mut LuaLexer<'a>,
191-
vm: &'a mut LuaVM,
192+
vm: &'a mut GlobalState,
192193
compiler_state: &'a mut CompilerState,
193194
is_vararg: bool,
194195
source_name: String,

crates/luars/src/compiler/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ mod parser; // Lexer/token provider
1111
mod statement; // Statement parsing (lparser.c)
1212

1313
// Re-exports
14-
use crate::LuaVM;
1514
pub use crate::compiler::parser::LuaLanguageLevel;
1615
use crate::compiler::parser::{LuaLexer, LuaTokenKind, LuaTokenize, Reader, TokensizeConfig};
1716
use crate::lua_value::{LuaProto, UpvalueDesc};
17+
use crate::lua_vm::GlobalState;
1818
use crate::lua_vm::OpCode;
1919
pub use code::*;
2020
pub use expression::*;
@@ -23,13 +23,13 @@ pub use func_state::*;
2323
// Structures are now in separate files (func_state.rs, expression.rs)
2424

2525
// Port of luaY_parser from lparser.c
26-
pub fn compile_code(source: &str, vm: &mut LuaVM) -> Result<LuaProto, String> {
26+
pub fn compile_code(source: &str, vm: &mut GlobalState) -> Result<LuaProto, String> {
2727
compile_code_with_name(source, vm, "@chunk")
2828
}
2929

3030
pub fn compile_code_with_name(
3131
source: &str,
32-
vm: &mut LuaVM,
32+
vm: &mut GlobalState,
3333
chunk_name: &str,
3434
) -> Result<LuaProto, String> {
3535
let level = vm.version;

crates/luars/src/gc/object_allocator.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ impl ObjectAllocator {
276276
let mut gc_thread =
277277
GcObjectOwner::Thread(Box::new(GcThread::new(thread, current_white, size as u32)));
278278
let ptr = gc_thread.as_thread_ptr().unwrap();
279-
unsafe {
280-
gc_thread.as_thread_mut().unwrap().set_thread_ptr(ptr);
281-
}
279+
gc_thread.as_thread_mut().unwrap().set_thread_ptr(ptr);
282280

283281
gc.trace_object(gc_thread)?;
284282
Ok(LuaValue::thread(ptr))

crates/luars/src/lib_registry.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::lua_api;
55
use crate::lua_value::LuaValue;
66
use crate::lua_vm::LuaState;
7-
use crate::lua_vm::{CFunction, LuaResult, LuaVM};
7+
use crate::lua_vm::{CFunction, GlobalState, LuaResult};
88
use crate::stdlib::{self, Stdlib};
99
// use crate::stdlib;
1010

@@ -19,7 +19,7 @@ pub trait LuaLibrary {
1919
}
2020

2121
/// Type for value initializers - functions that create values when the module loads
22-
pub type ValueInitializer = fn(&mut LuaVM) -> LuaResult<LuaValue>;
22+
pub type ValueInitializer = fn(&mut GlobalState) -> LuaResult<LuaValue>;
2323

2424
/// Type for module initializers - functions that set up additional module fields
2525
pub type ModuleInitializer = fn(&mut LuaState) -> LuaResult<()>;
@@ -168,15 +168,15 @@ impl LibraryRegistry {
168168
}
169169

170170
/// Load all registered libraries into a VM
171-
pub fn load_all(&self, vm: &mut LuaVM) -> LuaResult<()> {
171+
pub fn load_all(&self, vm: &mut GlobalState) -> LuaResult<()> {
172172
for module in &self.modules {
173173
self.load_module(vm, module)?;
174174
}
175175
Ok(())
176176
}
177177

178178
/// Load a specific module into the VM
179-
pub fn load_module(&self, vm: &mut LuaVM, module: &LibraryModule) -> LuaResult<()> {
179+
pub fn load_module(&self, vm: &mut GlobalState, module: &LibraryModule) -> LuaResult<()> {
180180
load_library_module(vm, module)
181181
}
182182

@@ -186,7 +186,7 @@ impl LibraryRegistry {
186186
}
187187
}
188188

189-
fn load_library_module(vm: &mut LuaVM, module: &LibraryModule) -> LuaResult<()> {
189+
fn load_library_module(vm: &mut GlobalState, module: &LibraryModule) -> LuaResult<()> {
190190
// Create a table for the library
191191
let lib_table = vm.create_table(0, 0)?;
192192

crates/luars/src/lua_api/lua.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{LuaError, LuaFullError};
1616
/// This type sits on top of `luars::LuaVM` and exposes a narrower API that
1717
/// avoids raw `LuaValue` plumbing in the common host-facing surface.
1818
pub struct Lua {
19-
vm: Box<LuaVM>,
19+
vm: LuaVM,
2020
}
2121

2222
impl Lua {

crates/luars/src/lua_value/chunk_serializer.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::{LocVar, LuaProto, LuaValue, UpvalueDesc};
55
use crate::Instruction;
66
use crate::gc::{GcProto, ObjectAllocator, ProtoPtr};
7-
use crate::lua_vm::LuaVM;
7+
use crate::lua_vm::GlobalState;
88
use crate::lua_vm::lua_limits::LUAI_MAXSHORTLEN;
99
use std::collections::HashMap;
1010
use std::io::{Cursor, Read};
@@ -95,7 +95,10 @@ pub fn deserialize_chunk(data: &[u8]) -> Result<LuaProto, String> {
9595
}
9696

9797
/// Deserialize binary data to a Chunk, directly creating strings with VM
98-
pub fn deserialize_chunk_with_strings_vm(data: &[u8], vm: &mut LuaVM) -> Result<LuaProto, String> {
98+
pub fn deserialize_chunk_with_strings_vm(
99+
data: &[u8],
100+
vm: &mut GlobalState,
101+
) -> Result<LuaProto, String> {
99102
let mut cursor = Cursor::new(data);
100103

101104
// Verify magic number
@@ -736,7 +739,10 @@ fn read_constant(cursor: &mut Cursor<&[u8]>) -> Result<LuaValue, String> {
736739
}
737740

738741
#[allow(dead_code)]
739-
fn read_chunk_with_vm(cursor: &mut Cursor<&[u8]>, vm: &mut LuaVM) -> Result<LuaProto, String> {
742+
fn read_chunk_with_vm(
743+
cursor: &mut Cursor<&[u8]>,
744+
vm: &mut GlobalState,
745+
) -> Result<LuaProto, String> {
740746
// Read code
741747
let code_len = read_u32(cursor)? as usize;
742748
let mut code = Vec::with_capacity(code_len);
@@ -827,7 +833,10 @@ fn read_chunk_with_vm(cursor: &mut Cursor<&[u8]>, vm: &mut LuaVM) -> Result<LuaP
827833
}
828834

829835
#[allow(dead_code)]
830-
fn read_constant_with_vm(cursor: &mut Cursor<&[u8]>, vm: &mut LuaVM) -> Result<LuaValue, String> {
836+
fn read_constant_with_vm(
837+
cursor: &mut Cursor<&[u8]>,
838+
vm: &mut GlobalState,
839+
) -> Result<LuaValue, String> {
831840
let tag = read_u8(cursor)?;
832841
match tag {
833842
TAG_NIL => Ok(LuaValue::nil()),
@@ -846,7 +855,7 @@ fn read_constant_with_vm(cursor: &mut Cursor<&[u8]>, vm: &mut LuaVM) -> Result<L
846855

847856
fn read_chunk_with_vm_dedup(
848857
cursor: &mut Cursor<&[u8]>,
849-
vm: &mut LuaVM,
858+
vm: &mut GlobalState,
850859
string_table: &mut Vec<String>,
851860
) -> Result<LuaProto, String> {
852861
// Read code
@@ -940,7 +949,7 @@ fn read_chunk_with_vm_dedup(
940949

941950
fn read_constant_with_vm_dedup(
942951
cursor: &mut Cursor<&[u8]>,
943-
vm: &mut LuaVM,
952+
vm: &mut GlobalState,
944953
string_table: &mut Vec<String>,
945954
) -> Result<LuaValue, String> {
946955
let tag = read_u8(cursor)?;

0 commit comments

Comments
 (0)