Skip to content

Commit 5c146ff

Browse files
committed
update
1 parent a5e5087 commit 5c146ff

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use upvalue_instructions::*;
1919
use super::{LuaError, LuaResult, LuaVM, OpCode};
2020
use crate::lua_value::{TAG_FALSE, TAG_FLOAT, TAG_INTEGER, TAG_NIL, TAG_TRUE, TYPE_MASK};
2121
use crate::lua_vm::LuaCallFrame;
22-
use crate::{LuaValue, get_a, get_ax, get_b, get_bx, get_c, get_k, get_op, get_sbx, get_sc, get_sj};
22+
use crate::{LuaValue, get_a, get_ax, get_b, get_bx, get_k, get_op, get_sbx, get_sj};
2323

2424
/// Save current pc to frame (like Lua C's savepc macro)
2525
/// Called before operations that may call Lua functions (CALL, metamethods, etc.)

crates/luars/src/lua_vm/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ impl LuaVM {
655655
let is_first_resume = unsafe { (*thread_ptr).frame_count == 0 };
656656

657657
// Handle first resume upvalue closing (only when needed)
658+
// This is required to properly capture upvalues from the parent scope
658659
if is_first_resume {
659660
let func = unsafe { (&(*thread_ptr).register_stack).get(0).cloned() };
660661
if let Some(func) = func {
@@ -781,8 +782,24 @@ impl LuaVM {
781782
}
782783

783784
/// Helper: close upvalues for a function being resumed in a coroutine
784-
#[inline(never)]
785+
/// OPTIMIZED: Skip if function has no upvalues, avoid clone
786+
#[inline(always)]
785787
fn close_function_upvalues_for_thread(&mut self, func_id: FunctionId) {
788+
// First check: does this function have any upvalues?
789+
let upvalue_count = {
790+
if let Some(func_ref) = self.object_pool.get_function(func_id) {
791+
func_ref.upvalues.len()
792+
} else {
793+
return;
794+
}
795+
};
796+
797+
// Fast path: no upvalues, nothing to close
798+
if upvalue_count == 0 {
799+
return;
800+
}
801+
802+
// Clone only if we have upvalues to process
786803
let upvalue_ids: Vec<UpvalueId> = {
787804
if let Some(func_ref) = self.object_pool.get_function(func_id) {
788805
func_ref.upvalues.clone()

0 commit comments

Comments
 (0)