@@ -5,6 +5,7 @@ use std::env;
55use libloading:: { Library , Symbol } ;
66use once_cell:: sync:: Lazy ;
77use crate :: interpreter:: debug_println;
8+ use crate :: interpreter:: value:: Value ;
89
910// 已加载库的缓存,使用Lazy静态变量确保线程安全的初始化
1011static LOADED_LIBRARIES : Lazy < Arc < Mutex < HashMap < String , Arc < Library > > > > > =
@@ -46,6 +47,18 @@ fn get_library_path(lib_name: &str) -> PathBuf {
4647 path
4748}
4849
50+ // 添加一个函数来打印库中的所有函数
51+ pub fn debug_library_functions ( lib_name : & str ) -> Result < ( ) , String > {
52+ let functions = load_library ( lib_name) ?;
53+
54+ debug_println ( & format ! ( "库 '{}' 中的所有函数:" , lib_name) ) ;
55+ for ( func_name, _) in functions. iter ( ) {
56+ debug_println ( & format ! ( " - {}" , func_name) ) ;
57+ }
58+
59+ Ok ( ( ) )
60+ }
61+
4962// 加载库并返回其函数映射
5063pub fn load_library ( lib_name : & str ) -> Result < Arc < HashMap < String , LibraryFunction > > , String > {
5164 debug_println ( & format ! ( "开始加载库: {}" , lib_name) ) ;
@@ -206,4 +219,33 @@ pub fn call_library_function(lib_name: &str, func_name: &str, args: Vec<String>)
206219 } ,
207220 None => Err ( format ! ( "库 '{}' 中未找到函数 '{}'" , lib_name, func_name) ) ,
208221 }
222+ }
223+
224+ // 新增函数,将Value类型转换为字符串参数
225+ pub fn convert_value_to_string_arg ( value : & Value ) -> String {
226+ match value {
227+ Value :: Int ( i) => i. to_string ( ) ,
228+ Value :: Float ( f) => f. to_string ( ) ,
229+ Value :: Bool ( b) => b. to_string ( ) ,
230+ Value :: String ( s) => s. clone ( ) ,
231+ Value :: Long ( l) => l. to_string ( ) ,
232+ Value :: Array ( arr) => {
233+ let elements: Vec < String > = arr. iter ( )
234+ . map ( |v| convert_value_to_string_arg ( v) )
235+ . collect ( ) ;
236+ format ! ( "[{}]" , elements. join( ", " ) )
237+ } ,
238+ Value :: Map ( map) => {
239+ let entries: Vec < String > = map. iter ( )
240+ . map ( |( k, v) | format ! ( "{}:{}" , k, convert_value_to_string_arg( v) ) )
241+ . collect ( ) ;
242+ format ! ( "{{{}}}" , entries. join( ", " ) )
243+ } ,
244+ Value :: None => "null" . to_string ( ) ,
245+ }
246+ }
247+
248+ // 从Vector<Value>转换为Vector<String>,用于库函数调用
249+ pub fn convert_values_to_string_args ( values : & [ Value ] ) -> Vec < String > {
250+ values. iter ( ) . map ( |v| convert_value_to_string_arg ( v) ) . collect ( )
209251}
0 commit comments