Skip to content

Commit a506053

Browse files
committed
clean 100 unsafe
1 parent 787ad53 commit a506053

15 files changed

Lines changed: 423 additions & 331 deletions

File tree

crates/luars/src/compiler/code.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ pub fn posfix(
21262126
BinaryOperator::OpBAnd => TmKind::Band, // TM_BAND
21272127
BinaryOperator::OpBOr => TmKind::Bor, // TM_BOR
21282128
BinaryOperator::OpBXor => TmKind::Bxor, // TM_BXOR
2129-
_ => TmKind::N, // Invalid for other ops
2129+
_ => TmKind::None, // Invalid for other ops
21302130
};
21312131
// Use code_abck to include flip bit (k-flag) like in MMBINI
21322132
code_abck(
@@ -2193,7 +2193,7 @@ pub fn posfix(
21932193
BinaryOperator::OpBXor => TmKind::Bxor,
21942194
BinaryOperator::OpShl => TmKind::Shl,
21952195
BinaryOperator::OpShr => TmKind::Shr,
2196-
_ => TmKind::N,
2196+
_ => TmKind::None,
21972197
};
21982198
code_abc(fs, OpCode::MmBin, o1 as u32, o2 as u32, tm_event as u32);
21992199
fixline(fs, line);

crates/luars/src/gc/gc_kind.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@ pub enum GcObjectKind {
1111
Userdata = 7,
1212
Proto = 8,
1313
}
14+
15+
impl GcObjectKind {
16+
pub fn from_u8(value: u8) -> Option<Self> {
17+
match value {
18+
0 => Some(Self::String),
19+
1 => Some(Self::Table),
20+
2 => Some(Self::Function),
21+
3 => Some(Self::CClosure),
22+
4 => Some(Self::RClosure),
23+
5 => Some(Self::Upvalue),
24+
6 => Some(Self::Thread),
25+
7 => Some(Self::Userdata),
26+
8 => Some(Self::Proto),
27+
_ => None,
28+
}
29+
}
30+
}

crates/luars/src/gc/gc_object.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,10 @@ impl GcObjectPtr {
539539
#[inline(always)]
540540
pub fn kind(&self) -> GcObjectKind {
541541
// Safety: tag values 0-8 match repr(u8) of GcObjectKind
542-
unsafe { std::mem::transmute(self.tag()) }
542+
match GcObjectKind::from_u8(self.tag()) {
543+
Some(k) => k,
544+
None => unreachable!(),
545+
}
543546
}
544547

545548
#[inline(always)]

crates/luars/src/gc/object_allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl ObjectAllocator {
7171
start: usize,
7272
end: usize,
7373
) -> CreateResult {
74+
// TODO: performance optimizations:
7475
let source_is_ascii = s_value.as_str().is_some_and(str::is_ascii);
7576
let Some(bytes) = s_value.as_bytes() else {
7677
return self.create_string(gc, "");

crates/luars/src/lib_registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl PreloadModule {
4848

4949
impl LuaLibrary for PreloadModule {
5050
fn install(&self, lua: &mut lua_api::Lua) -> LuaResult<()> {
51-
let vm = unsafe { lua.vm_mut() };
51+
let vm = lua.vm_mut();
5252
vm.register_preload(&self.name, self.loader)
5353
}
5454
}
@@ -91,7 +91,7 @@ impl LibraryModule {
9191

9292
impl LuaLibrary for LibraryModule {
9393
fn install(&self, lua: &mut lua_api::Lua) -> LuaResult<()> {
94-
let vm = unsafe { lua.vm_mut() };
94+
let vm = lua.vm_mut();
9595
load_library_module(vm, self)
9696
}
9797
}

crates/luars/src/lua_api/lua.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ impl Lua {
459459
&mut self,
460460
reference: &mut T,
461461
) -> LuaResult<UserDataRef<T>> {
462-
let value = unsafe { self.vm.main_state().create_userdata_ref(reference) }?;
462+
let ud = unsafe { LuaUserdata::from_ref(reference) };
463+
let value = self.vm.create_userdata(ud)?;
463464
self.value_to_userdata(value)
464465
}
465466

@@ -584,7 +585,7 @@ impl Lua {
584585
///
585586
/// The caller must preserve Lua VM invariants while holding the returned
586587
/// mutable reference and must not keep invalidated references across VM operations.
587-
pub unsafe fn vm_mut(&mut self) -> &mut LuaVM {
588+
pub fn vm_mut(&mut self) -> &mut LuaVM {
588589
&mut self.vm
589590
}
590591
}

crates/luars/src/lua_api/scope.rs

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ impl<T: UserDataTrait> ScopedBorrowedUserData<T> {
220220
fn alive(&self) -> bool {
221221
self.active.get()
222222
}
223+
224+
#[allow(clippy::mut_from_ref)]
225+
fn mutate(&self) -> Option<&mut T> {
226+
if self.alive() {
227+
Some(unsafe { &mut *self.ptr })
228+
} else {
229+
None
230+
}
231+
}
223232
}
224233

225234
impl<T: UserDataTrait> UserDataTrait for ScopedBorrowedUserData<T> {
@@ -232,129 +241,83 @@ impl<T: UserDataTrait> UserDataTrait for ScopedBorrowedUserData<T> {
232241
}
233242

234243
fn get_field(&self, key: &str) -> Option<UdValue> {
235-
if !self.alive() {
236-
return None;
237-
}
238-
unsafe { &*self.ptr }.get_field(key)
244+
self.mutate()?.get_field(key)
239245
}
240246

241247
fn set_field(&mut self, key: &str, value: UdValue) -> Option<Result<(), String>> {
242248
if !self.alive() {
243249
return Some(Err(scoped_expired_error().to_owned()));
244250
}
245-
unsafe { &mut *self.ptr }.set_field(key, value)
251+
self.mutate()?.set_field(key, value)
246252
}
247253

248254
fn lua_tostring(&self) -> Option<String> {
249-
if !self.alive() {
250-
return Some(scoped_expired_error().to_owned());
251-
}
252-
unsafe { &*self.ptr }.lua_tostring()
255+
self.mutate()?.lua_tostring()
253256
}
254257

255258
fn lua_eq(&self, other: &dyn UserDataTrait) -> Option<bool> {
256-
if !self.alive() {
257-
return Some(false);
258-
}
259-
unsafe { &*self.ptr }.lua_eq(other)
259+
self.mutate()?.lua_eq(other)
260260
}
261261

262262
fn lua_lt(&self, other: &dyn UserDataTrait) -> Option<bool> {
263-
if !self.alive() {
264-
return None;
265-
}
266-
unsafe { &*self.ptr }.lua_lt(other)
263+
self.mutate()?.lua_lt(other)
267264
}
268265

269266
fn lua_le(&self, other: &dyn UserDataTrait) -> Option<bool> {
270-
if !self.alive() {
271-
return None;
272-
}
273-
unsafe { &*self.ptr }.lua_le(other)
267+
self.mutate()?.lua_le(other)
274268
}
275269

276270
fn lua_len(&self) -> Option<UdValue> {
277-
if !self.alive() {
278-
return None;
279-
}
280-
unsafe { &*self.ptr }.lua_len()
271+
self.mutate()?.lua_len()
281272
}
282273

283274
fn lua_unm(&self) -> Option<UdValue> {
284-
if !self.alive() {
285-
return None;
286-
}
287-
unsafe { &*self.ptr }.lua_unm()
275+
self.mutate()?.lua_unm()
288276
}
289277

290278
fn lua_add(&self, other: &UdValue) -> Option<UdValue> {
291-
if !self.alive() {
292-
return None;
293-
}
294-
unsafe { &*self.ptr }.lua_add(other)
279+
self.mutate()?.lua_add(other)
295280
}
296281

297282
fn lua_sub(&self, other: &UdValue) -> Option<UdValue> {
298-
if !self.alive() {
299-
return None;
300-
}
301-
unsafe { &*self.ptr }.lua_sub(other)
283+
self.mutate()?.lua_sub(other)
302284
}
303285

304286
fn lua_mul(&self, other: &UdValue) -> Option<UdValue> {
305-
if !self.alive() {
306-
return None;
307-
}
308-
unsafe { &*self.ptr }.lua_mul(other)
287+
self.mutate()?.lua_mul(other)
309288
}
310289

311290
fn lua_div(&self, other: &UdValue) -> Option<UdValue> {
312-
if !self.alive() {
313-
return None;
314-
}
315-
unsafe { &*self.ptr }.lua_div(other)
291+
self.mutate()?.lua_div(other)
316292
}
317293

318294
fn lua_mod(&self, other: &UdValue) -> Option<UdValue> {
319-
if !self.alive() {
320-
return None;
321-
}
322-
unsafe { &*self.ptr }.lua_mod(other)
295+
self.mutate()?.lua_mod(other)
323296
}
324297

325298
fn lua_concat(&self, other: &UdValue) -> Option<UdValue> {
326-
if !self.alive() {
327-
return None;
328-
}
329-
unsafe { &*self.ptr }.lua_concat(other)
299+
self.mutate()?.lua_concat(other)
330300
}
331301

332302
fn lua_call(&self) -> Option<crate::CFunction> {
333-
if !self.alive() {
334-
return None;
335-
}
336-
unsafe { &*self.ptr }.lua_call()
303+
self.mutate()?.lua_call()
337304
}
338305

339306
fn lua_next(&self, control: &UdValue) -> Option<(UdValue, UdValue)> {
340-
if !self.alive() {
341-
return None;
342-
}
343-
unsafe { &*self.ptr }.lua_next(control)
307+
self.mutate()?.lua_next(control)
344308
}
345309

346310
fn field_names(&self) -> &'static [&'static str] {
347-
if !self.alive() {
348-
return &[];
311+
match self.mutate() {
312+
Some(m) => m.field_names(),
313+
None => &[],
349314
}
350-
unsafe { &*self.ptr }.field_names()
351315
}
352316

353317
fn as_any(&self) -> &dyn Any {
354-
if self.alive() {
355-
unsafe { &*self.ptr }.as_any()
356-
} else {
357-
self
318+
match self.mutate() {
319+
Some(m) => m.as_any(),
320+
None => self,
358321
}
359322
}
360323

0 commit comments

Comments
 (0)