* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* 实现步骤:
* 1.使用AKI的JSBIND_ADDON注册OpenHarmony Native插件。
* 2.使用AKI的JSBIND_GLOBAL注册FFI特性。在JSBIND_GLOBAL作用域下使用AKI的JSBIND_FUNCTION绑定C++全局函数AkiThreadsCallJs。
* 3.在AkiThreadsCallJs中创建子线程,子线程中使用aki::JSBind::GetJSFunction获取指定JavaScript函数akiAccumulate的句柄后,使用Invoke触发调用JS函数。
*/
#include <aki/jsbind.h>
void AkiThreadsCallJs(int value)
{
std::thread subThread([=]() {
if (auto func = aki::JSBind::GetJSFunction("akiAccumulate")) {
std::function<void(int)> callback = [](int value) {};
func->Invoke<void>(value, callback);
}
});
subThread.detach();
return;
}
JSBIND_ADDON(akiusepractice)
JSBIND_GLOBAL() {
JSBIND_FUNCTION(AkiThreadsCallJs);
}