代码拉取完成,页面将自动刷新
同步操作将从 OpenHarmony Development Docs/Starts 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Copyright (c) 2021-2022 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.
*/
#include <cstdint>
#include <memory>
#include <string>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
namespace OHOS {
namespace PreferencesJsKit {
static napi_value JSTestSyncFunc(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2] = { nullptr };
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valueType0;
napi_typeof(env, args[0], &valueType0);
napi_valuetype valueType1;
napi_typeof(env, args[1], &valueType1);
NAPI_ASSERT(env, valueType0 == napi_number && valueType1 == napi_number, "Wrong argument type. Numbers expected");
double value0;
double value1;
napi_get_value_double(env, args[0], &value0);
napi_get_value_double(env, args[1], &value1);
napi_value sum;
napi_create_double(env, value0 + value1, &sum);
return sum;
}
static napi_value JSTestSyncFuncOnly(napi_env env, napi_callback_info info)
{
napi_value ret = nullptr;
napi_create_double(env, 3, &ret);
return ret;
}
struct AsyncCallbackInfo {
napi_env env = nullptr;
napi_async_work work = nullptr;
napi_deferred deferred = nullptr;
napi_ref callbackRef = nullptr;
double value0;
double value1;
double sum;
};
static napi_value JSTestAsyncFunc(napi_env env, napi_callback_info info)
{
size_t argc = 3;
napi_value args[3] = { 0 };
napi_value thisArg = nullptr;
void *data = nullptr;
napi_get_cb_info(env, info, &argc, args, &thisArg, &data);
auto asyncContext = new AsyncCallbackInfo();
asyncContext->env = env;
napi_valuetype valueType0;
napi_typeof(env, args[0], &valueType0);
napi_valuetype valueType1;
napi_typeof(env, args[1], &valueType1);
NAPI_ASSERT(env, valueType0 == napi_number && valueType1 == napi_number, "Wrong argument type. Numbers expected");
napi_get_value_double(env, args[0], &asyncContext->value0);
napi_get_value_double(env, args[1], &asyncContext->value1);
napi_value resourceName = nullptr;
napi_create_string_utf8(env, "JSTestAsyncFunc", NAPI_AUTO_LENGTH, &resourceName);
napi_value result = nullptr;
if (argc == 3) {
napi_valuetype valueType2;
napi_typeof(env, args[2], &valueType2);
NAPI_ASSERT(env, valueType2 == napi_function, "Wrong argument type. Numbers expected");
napi_create_reference(env, args[2], 1, &asyncContext->callbackRef);
} else {
napi_create_promise(env, &asyncContext->deferred, &result);
}
napi_create_async_work(
env, nullptr, resourceName,
[](napi_env env, void *data) {
AsyncCallbackInfo *asyncContext = (AsyncCallbackInfo *)data;
double value0 = asyncContext->value0;
double value1 = asyncContext->value1;
asyncContext->sum = value0 + value1;
},
[](napi_env env, napi_status status, void *data) {
AsyncCallbackInfo *asyncContext = (AsyncCallbackInfo *)data;
napi_value argv = nullptr;
napi_create_double(env, asyncContext->sum, &argv);
if (asyncContext->callbackRef) {
napi_value callback = nullptr;
napi_value callbackResult = nullptr;
napi_get_reference_value(env, asyncContext->callbackRef, &callback);
napi_call_function(env, nullptr, callback, 1, &argv, &callbackResult);
napi_delete_reference(env, asyncContext->callbackRef);
} else if (asyncContext->deferred) {
napi_resolve_deferred(env, asyncContext->deferred, argv);
}
napi_delete_async_work(env, asyncContext->work);
delete asyncContext;
},
(void *)asyncContext, &asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
return result;
}
static napi_value JSTestAsyncFuncOnly(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = { 0 };
napi_value thisArg = nullptr;
void *data = nullptr;
napi_get_cb_info(env, info, &argc, args, &thisArg, &data);
auto asyncContext = new AsyncCallbackInfo();
asyncContext->env = env;
napi_value resourceName = nullptr;
napi_create_string_utf8(env, "JSTestAsyncFunc", NAPI_AUTO_LENGTH, &resourceName);
napi_value result = nullptr;
if (argc == 1) {
napi_valuetype valueType2;
napi_typeof(env, args[0], &valueType2);
NAPI_ASSERT(env, valueType2 == napi_function, "Wrong argument type. Numbers expected");
napi_create_reference(env, args[0], 1, &asyncContext->callbackRef);
} else {
napi_create_promise(env, &asyncContext->deferred, &result);
}
napi_create_async_work(
env, nullptr, resourceName,
[](napi_env env, void *data) {
AsyncCallbackInfo *asyncContext = (AsyncCallbackInfo *)data;
asyncContext->sum = 3;
},
[](napi_env env, napi_status status, void *data) {
AsyncCallbackInfo *asyncContext = (AsyncCallbackInfo *)data;
napi_value argv = nullptr;
napi_create_double(env, asyncContext->sum, &argv);
if (asyncContext->callbackRef) {
napi_value callback = nullptr;
napi_value callbackResult = nullptr;
napi_get_reference_value(env, asyncContext->callbackRef, &callback);
napi_call_function(env, nullptr, callback, 1, &argv, &callbackResult);
napi_delete_reference(env, asyncContext->callbackRef);
} else if (asyncContext->deferred) {
napi_resolve_deferred(env, asyncContext->deferred, argv);
}
napi_delete_async_work(env, asyncContext->work);
delete asyncContext;
asyncContext = nullptr;
},
(void *)asyncContext, &asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("testSyncFunc", JSTestSyncFunc),
DECLARE_NAPI_FUNCTION("testAsyncFunc", JSTestAsyncFunc),
DECLARE_NAPI_FUNCTION("testSyncFuncOnly", JSTestSyncFuncOnly),
DECLARE_NAPI_FUNCTION("testAsyncFuncOnly", JSTestAsyncFuncOnly),
};
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
return exports;
}
EXTERN_C_END
static napi_module performanceTestModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "performanceTest",
.nm_priv = ((void *)0),
.reserved = { 0 },
};
static __attribute__((constructor)) void RegisterModule(void)
{
napi_module_register(&performanceTestModule);
}
} // namespace PreferencesJsKit
} // namespace OHOS
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。