Skip to content

Commit f04aa7f

Browse files
committed
refactor
1 parent 06e226a commit f04aa7f

File tree

14 files changed

+497
-509
lines changed

14 files changed

+497
-509
lines changed

crates/luars/src/compiler/stmt.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -718,15 +718,15 @@ fn compile_return_stat(c: &mut Compiler, stat: &LuaReturnStat) -> Result<(), Str
718718
// First, compile all expressions except the last directly to target registers
719719
for (i, expr) in exprs.iter().take(num_exprs - 1).enumerate() {
720720
let target_reg = base_reg + i as u32;
721-
721+
722722
// Try to compile expression directly to target register
723723
let src_reg = compile_expr_to(c, expr, Some(target_reg))?;
724-
724+
725725
// If expression couldn't be placed in target, emit a MOVE
726726
if src_reg != target_reg {
727727
emit_move(c, target_reg, src_reg);
728728
}
729-
729+
730730
if target_reg >= c.freereg {
731731
c.freereg = target_reg + 1;
732732
}
@@ -767,16 +767,16 @@ fn compile_return_stat(c: &mut Compiler, stat: &LuaReturnStat) -> Result<(), Str
767767
// Compile expressions directly to target registers when possible
768768
for i in 0..num_exprs {
769769
let target_reg = base_reg + i as u32;
770-
770+
771771
// Try to compile expression directly to target register
772772
// compile_expr_to will use get_result_reg which ensures max_stack_size is updated
773773
let src_reg = compile_expr_to(c, &exprs[i], Some(target_reg))?;
774-
774+
775775
// If expression couldn't be placed in target, emit a MOVE
776776
if src_reg != target_reg {
777777
emit_move(c, target_reg, src_reg);
778778
}
779-
779+
780780
// Update freereg to account for this register
781781
if target_reg >= c.freereg {
782782
c.freereg = target_reg + 1;
@@ -1138,7 +1138,7 @@ fn compile_for_stat(c: &mut Compiler, stat: &LuaForStat) -> Result<(), String> {
11381138
}
11391139

11401140
/// Compile generic for loop using TFORPREP/TFORCALL/TFORLOOP instructions
1141-
///
1141+
///
11421142
/// Lua 5.4 for-in register layout:
11431143
/// R[A] = iter_func (f)
11441144
/// R[A+1] = state (s)
@@ -1176,7 +1176,7 @@ fn compile_for_range_stat(c: &mut Compiler, stat: &LuaForRangeStat) -> Result<()
11761176
// FIRST: Compile iterator expressions BEFORE allocating the for-in block
11771177
// This prevents the call results from overlapping with loop variables
11781178
let base = c.freereg;
1179-
1179+
11801180
// Compile iterator expressions to get (iter_func, state, control_var, to-be-closed) at base
11811181
// Lua 5.4 for-in needs 4 control slots: iterator, state, control, closing value
11821182
if iter_exprs.len() == 1 {
@@ -1285,7 +1285,8 @@ fn compile_for_range_stat(c: &mut Compiler, stat: &LuaForRangeStat) -> Result<()
12851285
// Patch TFORPREP to jump to TFORCALL
12861286
// TFORPREP jumps forward by Bx
12871287
let tforprep_jump = tforcall_pc - tforprep_pc - 1;
1288-
c.chunk.code[tforprep_pc] = Instruction::encode_abx(OpCode::TForPrep, base, tforprep_jump as u32);
1288+
c.chunk.code[tforprep_pc] =
1289+
Instruction::encode_abx(OpCode::TForPrep, base, tforprep_jump as u32);
12891290

12901291
end_loop(c);
12911292
end_scope(c);

crates/luars/src/gc/gc_id.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// ============ Object IDs ============
2+
// All IDs are simple u32 indices - compact and efficient
3+
4+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
5+
#[repr(transparent)]
6+
pub struct StringId(pub u32);
7+
8+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
9+
#[repr(transparent)]
10+
pub struct TableId(pub u32);
11+
12+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
13+
#[repr(transparent)]
14+
pub struct FunctionId(pub u32);
15+
16+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
17+
#[repr(transparent)]
18+
pub struct UpvalueId(pub u32);
19+
20+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
21+
#[repr(transparent)]
22+
pub struct UserdataId(pub u32);
23+
24+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
25+
#[repr(transparent)]
26+
pub struct ThreadId(pub u32);
27+
28+
/// Object type tags (3 bits, supports up to 8 types)
29+
#[repr(u8)]
30+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31+
pub enum GcType {
32+
String = 0,
33+
Table = 1,
34+
Function = 2,
35+
Upvalue = 3,
36+
Thread = 4,
37+
Userdata = 5,
38+
}
39+
40+
/// Unified GC object identifier
41+
/// Layout: [type: 3 bits][index: 29 bits]
42+
/// Supports up to 536 million objects per type
43+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
44+
pub enum GcId {
45+
StringId(StringId),
46+
TableId(TableId),
47+
FunctionId(FunctionId),
48+
UpvalueId(UpvalueId),
49+
ThreadId(ThreadId),
50+
UserdataId(UserdataId),
51+
}
52+
53+
impl GcId {
54+
#[inline(always)]
55+
pub fn gc_type(self) -> GcType {
56+
match self {
57+
GcId::StringId(_) => GcType::String,
58+
GcId::TableId(_) => GcType::Table,
59+
GcId::FunctionId(_) => GcType::Function,
60+
GcId::UpvalueId(_) => GcType::Upvalue,
61+
GcId::ThreadId(_) => GcType::Thread,
62+
GcId::UserdataId(_) => GcType::Userdata,
63+
}
64+
}
65+
66+
#[inline(always)]
67+
pub fn index(self) -> u32 {
68+
match self {
69+
GcId::StringId(StringId(id)) => id,
70+
GcId::TableId(TableId(id)) => id,
71+
GcId::FunctionId(FunctionId(id)) => id,
72+
GcId::UpvalueId(UpvalueId(id)) => id,
73+
GcId::ThreadId(ThreadId(id)) => id,
74+
GcId::UserdataId(UserdataId(id)) => id,
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)