ohos-rs / example

A set of exmaples with ohos-rs for HarmonyOS
MIT License
7 stars 1 forks source link

调用arkts函数有问题 #24

Closed liujingxuan closed 2 weeks ago

liujingxuan commented 2 weeks ago

当参数为多个参数的时候,会出问题,比如, args是"string11","string22", 但是最后是:string11string22, undefine。所有参数会集中在第一个。 match module.call::<, , ()>(&method, args) { Ok(_) => qlog!(" successful"), Err(e) => qlog!("failed: {:?}", e), }

github-actions[bot] commented 2 weeks ago

@richerfu Please check this issue, thanks.

richerfu commented 2 weeks ago

请给一个复现的 case @liujingxuan

liujingxuan commented 2 weeks ago

好的好的,这个是我的arkts代码: function updateOldHybrids(newData: string, oldKey: string) { console.log("arkts updateOldHybrids1 :" + newData); console.log("arkts updateOldHybrids2 :" + oldKey); }

这个是我大rust代码: `pub fn call_arkts_func_no_return(method:String, args:Vec){ let global_runtime_clone = Arc::clone(&GLOBAL_RUNTIME); let mut runtime_guard = global_runtime_clone.lock().unwrap(); if runtime_guard.is_none() { *runtime_guard = Some(ArkRuntime::new().unwrap()); } let runtime = runtime_guard.as_ref().unwrap(); let module = runtime.load_with_info("Manager","com.harm.hos/entry").unwrap(); let js_strings: Vec = args.iter() .map(|arg| runtime.env.create_string(arg).unwrap()) .collect(); // let ret:JsString = module.call(&method, js_strings).unwrap();

match module.call::<, , ()>(&method, args) { Ok(_) => log!("call_arkts_func_no_return successful"), Err(e) => log!("call_arkts_func_no_return failed: {:?}", e), } ` pub fn updata_old_hybrids(json: &String, old_key: &str) { let args = vec![String::from(json), old_key.to_string()]; call_arkts_func_no_return("updateOldHybrids".to_string(), args); }

两个string传进去后,得到的现象就是arkts的数据,第一个有两个字符串拼接的,比如str1,str2,但是第二个没有。不知道哪里写的有问题。我也深入到源码分析了,没看出来。多谢啊 @richerfu

richerfu commented 2 weeks ago

多参数使用元组传递 Vec 会被转成数组

liujingxuan commented 2 weeks ago

有没有什么好的方法呢,传入多个参数呢

richerfu commented 2 weeks ago

https://github.com/ohos-rs/ohos-rs/blob/ohos/examples/hello/src/ohos/mod.rs#L57