HarmonyOS 鸿蒙Next OH_JSVM_CreateReference 函数对非对象引用报错问题
HarmonyOS 鸿蒙Next OH_JSVM_CreateReference 函数对非对象引用报错问题
升级HarmonyOS后,感觉手机的整体性能都有了很大的提升。
关于HarmonyOS 鸿蒙Next OH_JSVM_CreateReference 函数对非对象引用报错问题的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
static void TestOH_JSVM_CreateReference()
{
OH_LOG_INFO(LOG_APP, "enter TestOH_JSVM_CreateReference");
JSVM_VM vm;
OH_JSVM_CreateVM(nullptr, &vm);
JSVM_VMScope vmScope;
OH_JSVM_OpenVMScope(vm, &vmScope);
JSVM_Env env;
// 将native函数注册成js可调用的方法,hello_cb中记录该native方法的指针和参数等信息
JSVM_PropertyDescriptor descriptors[] = {{"hellott", NULL, &hello_cb, NULL, NULL, NULL, JSVM_DEFAULT}};
OH_JSVM_CreateEnv(vm, 1, descriptors, &env);
JSVM_EnvScope envScope;
OH_JSVM_OpenEnvScope(env, &envScope);
JSVM_Status status = JSVM_OK;
{
JSVM_HandleScope handleScope;
OH_JSVM_OpenHandleScope(env, &handleScope);
JSVM_Value jsSrc;
std::string src = "abc";
status = OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc);
if (status != JSVM_OK) {
OH_LOG_INFO(LOG_APP, "TestOH_JSVM_CreateReference, loc1 status:%{public}d, OH_JSVM_CreateStringUtf8 failed", status);
return;
}
JSVM_Value object;
OH_JSVM_CreateObject(env, &object);
OH_JSVM_SetElement(env, object, 1, jsSrc);
JSVM_Ref srcRef = nullptr;
status = OH_JSVM_CreateReference(env, object, 1, &srcRef);
if (status != JSVM_OK) {
OH_LOG_INFO(LOG_APP, "TestOH_JSVM_CreateReference, loc2 status:%{public}d, OH_JSVM_CreateReference failed", status);
return;
}
status = OH_JSVM_DeleteReference(env, srcRef);
if (status != JSVM_OK) {
OH_LOG_INFO(LOG_APP, "TestOH_JSVM_CreateReference, loc3 status:%{public}d, OH_JSVM_DeleteReference failed", status);
return;
}
srcRef = nullptr;
int32_t ivalue= 3;
JSVM_Value jsIValue;
status = OH_JSVM_CreateInt32(env, ivalue, &jsIValue);
if (status != JSVM_OK) {
OH_LOG_INFO(LOG_APP, "TestOH_JSVM_CreateReference, loc4 status:%{public}d, OH_JSVM_CreateInt32 failed", status);
return;
}
基本数据类型,可以直接解析成C++的基本类型(如整型,字符串型等)存储,没有必要创建引用,后续使用时,在将基本类型转换成JSVM_Value。
感谢。
希望HarmonyOS能继续加强在AI领域的研发,让系统更加智能化。