• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "global.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 constexpr size_t MAX_ARGV_COUNT = 10;
Execute(const std::shared_ptr<JSCallbackObject> & object,const ArgContainer & argContainer,napi_value & output)23 void JsCallbackHandler::Execute(const std::shared_ptr<JSCallbackObject> &object, const ArgContainer &argContainer,
24     napi_value &output)
25 {
26     if (object->threadId_ != std::this_thread::get_id()) {
27         IMSA_HILOGW("threadId not same!");
28         return;
29     }
30     napi_value argv[MAX_ARGV_COUNT] = { nullptr };
31     if (argContainer.argvProvider != nullptr && !argContainer.argvProvider(object->env_, argv, MAX_ARGV_COUNT)) {
32         return;
33     }
34     napi_value callback = nullptr;
35     napi_value global = nullptr;
36     napi_get_reference_value(object->env_, object->callback_, &callback);
37     if (callback == nullptr) {
38         IMSA_HILOGE("callback is nullptr!");
39         return;
40     }
41     napi_get_global(object->env_, &global);
42     InputMethodSyncTrace tracer("Execute napi_call_function");
43     auto status = napi_call_function(object->env_, global, callback, argContainer.argc, argv, &output);
44     if (status != napi_ok) {
45         output = nullptr;
46     }
47 }
48 } // namespace MiscServices
49 } // namespace OHOS