Skip to content

Commit c667f4a

Browse files
committed
refactor global and register
1 parent e2733e4 commit c667f4a

File tree

12 files changed

+183
-200
lines changed

12 files changed

+183
-200
lines changed

crates/luars/src/gc/gc_object.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ pub struct GcTable {
166166
}
167167

168168
/// C Function type - Rust function callable from Lua
169-
pub type CFunction = fn(&mut crate::lua_vm::LuaVM) -> crate::lua_vm::LuaResult<crate::lua_value::MultiValue>;
169+
pub type CFunction =
170+
fn(&mut crate::lua_vm::LuaVM) -> crate::lua_vm::LuaResult<crate::lua_value::MultiValue>;
170171

171172
/// Function body - either Lua bytecode or C function
172173
pub enum FunctionBody {
@@ -191,15 +192,18 @@ impl GcFunction {
191192
/// Check if this is a C function (any C variant)
192193
#[inline(always)]
193194
pub fn is_c_function(&self) -> bool {
194-
matches!(self.body, FunctionBody::C(_) | FunctionBody::CClosureInline1(_, _))
195+
matches!(
196+
self.body,
197+
FunctionBody::C(_) | FunctionBody::CClosureInline1(_, _)
198+
)
195199
}
196-
200+
197201
/// Check if this is a Lua function
198202
#[inline(always)]
199203
pub fn is_lua_function(&self) -> bool {
200204
matches!(self.body, FunctionBody::Lua(_))
201205
}
202-
206+
203207
/// Get the chunk if this is a Lua function
204208
#[inline(always)]
205209
pub fn chunk(&self) -> Option<&Rc<Chunk>> {
@@ -208,7 +212,7 @@ impl GcFunction {
208212
_ => None,
209213
}
210214
}
211-
215+
212216
/// Get the chunk reference for Lua functions (panics if C function)
213217
/// Use this in contexts where we know it's a Lua function
214218
#[inline(always)]
@@ -218,7 +222,7 @@ impl GcFunction {
218222
_ => panic!("Called lua_chunk() on a C function"),
219223
}
220224
}
221-
225+
222226
/// Get the C function pointer if this is any C function variant
223227
#[inline(always)]
224228
pub fn c_function(&self) -> Option<CFunction> {
@@ -228,7 +232,7 @@ impl GcFunction {
228232
FunctionBody::Lua(_) => None,
229233
}
230234
}
231-
235+
232236
/// Get inline upvalue 1 for CClosureInline1
233237
#[inline(always)]
234238
pub fn inline_upvalue1(&self) -> Option<LuaValue> {

crates/luars/src/gc/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ impl GC {
533533
if let Some(func) = pool.functions.get(gc_id.index()) {
534534
let upvalue_ids = func.upvalues.clone();
535535
// C closures don't have constants
536-
let constants = func.chunk().map(|c| c.constants.clone()).unwrap_or_default();
536+
let constants = func
537+
.chunk()
538+
.map(|c| c.constants.clone())
539+
.unwrap_or_default();
537540

538541
if let Some(f) = pool.functions.get_mut(gc_id.index()) {
539542
if f.header.is_gray() {
@@ -1148,7 +1151,10 @@ impl GC {
11481151
if let Some(func) = pool.functions.get(id.0) {
11491152
if func.header.is_white() {
11501153
// C closures don't have constants
1151-
let consts = func.chunk().map(|c| c.constants.clone()).unwrap_or_default();
1154+
let consts = func
1155+
.chunk()
1156+
.map(|c| c.constants.clone())
1157+
.unwrap_or_default();
11521158
(true, func.upvalues.clone(), consts)
11531159
} else {
11541160
(false, vec![], vec![])

crates/luars/src/gc/object_pool.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// 5. Free list for slot reuse
99
// 6. GC headers embedded in objects for mark-sweep
1010

11+
use crate::gc::gc_object::{CFunction, FunctionBody};
1112
use crate::lua_value::{Chunk, LuaThread, LuaUserdata};
1213
use crate::{
1314
FunctionId, GcFunction, GcHeader, GcString, GcTable, GcThread, GcUpvalue, LuaString, LuaTable,
1415
LuaValue, StringId, TableId, ThreadId, UpvalueId, UpvalueState, UserdataId,
1516
};
16-
use crate::gc::gc_object::{FunctionBody, CFunction};
1717
use std::rc::Rc;
1818

1919
// ============ Pool Storage ============
@@ -397,12 +397,12 @@ pub struct ObjectPool {
397397
pub tm_shr: StringId, // "__shr"
398398
pub tm_concat: StringId, // "__concat"
399399
pub tm_metatable: StringId, // "__metatable"
400-
400+
401401
// Pre-cached coroutine status strings for fast coroutine.status
402-
pub str_suspended: StringId, // "suspended"
403-
pub str_running: StringId, // "running"
404-
pub str_normal: StringId, // "normal"
405-
pub str_dead: StringId, // "dead"
402+
pub str_suspended: StringId, // "suspended"
403+
pub str_running: StringId, // "running"
404+
pub str_normal: StringId, // "normal"
405+
pub str_dead: StringId, // "dead"
406406
}
407407

408408
// ============ Lua-style String Interning Table ============
@@ -668,7 +668,7 @@ impl ObjectPool {
668668
pool.tm_shr = pool.create_string("__shr");
669669
pool.tm_concat = pool.create_string("__concat");
670670
pool.tm_metatable = pool.create_string("__metatable");
671-
671+
672672
// Pre-create coroutine status strings
673673
pool.str_suspended = pool.create_string("suspended");
674674
pool.str_running = pool.create_string("running");
@@ -1036,7 +1036,7 @@ impl ObjectPool {
10361036
};
10371037
FunctionId(self.functions.alloc(gc_func))
10381038
}
1039-
1039+
10401040
/// Create a C closure (native function with upvalues)
10411041
#[inline]
10421042
pub fn create_c_closure(&mut self, func: CFunction, upvalue_ids: Vec<UpvalueId>) -> FunctionId {
@@ -1047,7 +1047,7 @@ impl ObjectPool {
10471047
};
10481048
FunctionId(self.functions.alloc(gc_func))
10491049
}
1050-
1050+
10511051
/// Create a C closure with single inline upvalue (fast path)
10521052
/// This avoids UpvalueId allocation for common single-upvalue C closures
10531053
#[inline]

crates/luars/src/lua_value/lua_table.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ impl LuaTable {
252252

253253
/// Get the metatable of this table
254254
pub fn get_metatable(&self) -> Option<LuaValue> {
255-
self.metatable
256-
.map(|mt_id| LuaValue::table(mt_id))
255+
self.metatable.map(|mt_id| LuaValue::table(mt_id))
257256
}
258257

259258
/// Set the metatable of this table

crates/luars/src/lua_vm/execute/control_instructions.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ pub fn exec_call(
797797

798798
// Get function info - check if Lua or C closure
799799
let func_ref = unsafe { vm.object_pool.get_function_unchecked(func_id) };
800-
800+
801801
// FAST PATH: Lua function (most common case) - check first!
802802
if let crate::gc::FunctionBody::Lua(chunk) = &func_ref.body {
803803
// Lua function path - inline for speed
@@ -898,7 +898,7 @@ pub fn exec_call(
898898
*frame_ptr_ptr = vm.push_frame(new_frame);
899899
return Ok(());
900900
}
901-
901+
902902
// C closure paths (less common)
903903
match &func_ref.body {
904904
crate::gc::FunctionBody::C(c_func) => {
@@ -956,21 +956,17 @@ pub fn exec_call(
956956

957957
// Slow path: Check for __call metamethod (for Table and Userdata)
958958
let metatable_opt = match func.kind() {
959-
LuaValueKind::Table => {
960-
func.as_table_id().and_then(|table_id| {
961-
vm.object_pool
962-
.get_table(table_id)
963-
.and_then(|t| t.get_metatable())
964-
})
965-
}
966-
LuaValueKind::Userdata => {
967-
func.as_userdata_id().and_then(|ud_id| {
968-
vm.object_pool
969-
.get_userdata(ud_id)
970-
.map(|ud| ud.get_metatable())
971-
.filter(|mt| !mt.is_nil())
972-
})
973-
}
959+
LuaValueKind::Table => func.as_table_id().and_then(|table_id| {
960+
vm.object_pool
961+
.get_table(table_id)
962+
.and_then(|t| t.get_metatable())
963+
}),
964+
LuaValueKind::Userdata => func.as_userdata_id().and_then(|ud_id| {
965+
vm.object_pool
966+
.get_userdata(ud_id)
967+
.map(|ud| ud.get_metatable())
968+
.filter(|mt| !mt.is_nil())
969+
}),
974970
_ => None,
975971
};
976972

@@ -980,17 +976,7 @@ pub fn exec_call(
980976
if let Some(call_func) = vm.table_get_with_meta(&metatable, &call_key) {
981977
if call_func.is_callable() {
982978
if call_func.is_cfunction() {
983-
exec_call_cfunction(
984-
vm,
985-
call_func,
986-
a,
987-
b,
988-
c,
989-
base,
990-
true,
991-
func,
992-
frame_ptr_ptr,
993-
)?;
979+
exec_call_cfunction(vm, call_func, a, b, c, base, true, func, frame_ptr_ptr)?;
994980
return Ok(());
995981
} else {
996982
exec_call_lua_function(
@@ -1446,7 +1432,7 @@ fn exec_call_c_closure_inline1(
14461432
// Store inline upvalue in VM state for access during call
14471433
let old_inline_upvalue = vm.c_closure_inline_upvalue;
14481434
vm.c_closure_inline_upvalue = inline_upvalue;
1449-
1435+
14501436
// Clear pointer-based upvalues (not used for inline1)
14511437
let old_upvalues_ptr = vm.c_closure_upvalues_ptr;
14521438
let old_upvalues_len = vm.c_closure_upvalues_len;

0 commit comments

Comments
 (0)