1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_callback_handler.h"
17
18 namespace OHOS {
19 namespace MiscServices {
20 constexpr size_t MAX_ARGV_COUNT = 10;
Execute(const std::shared_ptr<JSCallbackObject> & object,const ArgContainer & argContainer,napi_value & output)21 void JsCallbackHandler::Execute(
22 const std::shared_ptr<JSCallbackObject> &object, const ArgContainer &argContainer, napi_value &output)
23 {
24 if (object->threadId_ != std::this_thread::get_id()) {
25 return;
26 }
27 napi_value argv[MAX_ARGV_COUNT] = { nullptr };
28 if (argContainer.argvProvider != nullptr && !argContainer.argvProvider(object->env_, argv, MAX_ARGV_COUNT)) {
29 return;
30 }
31 napi_value callback = nullptr;
32 napi_value global = nullptr;
33 napi_get_reference_value(object->env_, object->callback_, &callback);
34 if (callback != nullptr) {
35 napi_get_global(object->env_, &global);
36 InputMethodSyncTrace tracer("Execute napi_call_function");
37 auto status = napi_call_function(object->env_, global, callback, argContainer.argc, argv, &output);
38 if (status != napi_ok) {
39 output = nullptr;
40 }
41 }
42 }
43 } // namespace MiscServices
44 } // namespace OHOS
45