@@ -608,7 +608,7 @@ impl<'a> FunctionCallHandler for Interpreter<'a> {
608608
609609// 函数指针调用的辅助方法
610610impl < ' a > Interpreter < ' a > {
611- fn call_function_pointer_impl ( & mut self , func_ptr : & super :: value:: FunctionPointerInstance , args : Vec < Value > ) -> Value {
611+ pub fn call_function_pointer_impl ( & mut self , func_ptr : & super :: value:: FunctionPointerInstance , args : Vec < Value > ) -> Value {
612612 debug_println ( & format ! ( "调用函数指针: {}" , func_ptr. function_name) ) ;
613613
614614 if func_ptr. is_null {
@@ -768,7 +768,7 @@ impl<'a> Interpreter<'a> {
768768 }
769769 }
770770
771- fn call_lambda_function_pointer_impl ( & mut self , lambda_ptr : & super :: value:: LambdaFunctionPointerInstance , args : Vec < Value > ) -> Value {
771+ pub fn call_lambda_function_pointer_impl ( & mut self , lambda_ptr : & super :: value:: LambdaFunctionPointerInstance , args : Vec < Value > ) -> Value {
772772 debug_println ( & format ! ( "调用Lambda函数指针: {}" , lambda_ptr. function_name) ) ;
773773
774774 if lambda_ptr. is_null {
@@ -785,10 +785,16 @@ impl<'a> Interpreter<'a> {
785785 // 保存当前局部环境
786786 let saved_local_env = self . local_env . clone ( ) ;
787787
788- // 创建Lambda执行环境
788+ // 创建Lambda执行环境,包含闭包环境
789789 let mut lambda_env = HashMap :: new ( ) ;
790790
791- // 绑定参数(使用实际的参数名)
791+ // 首先添加闭包环境中的变量
792+ for ( var_name, var_value) in & lambda_ptr. closure_env {
793+ lambda_env. insert ( var_name. clone ( ) , var_value. clone ( ) ) ;
794+ debug_println ( & format ! ( "闭包变量: {} = {:?}" , var_name, var_value) ) ;
795+ }
796+
797+ // 然后绑定参数(参数会覆盖同名的闭包变量)
792798 for ( param, arg) in lambda_ptr. lambda_params . iter ( ) . zip ( args. iter ( ) ) {
793799 lambda_env. insert ( param. name . clone ( ) , arg. clone ( ) ) ;
794800 debug_println ( & format ! ( "绑定参数: {} = {:?}" , param. name, arg) ) ;
0 commit comments