1 /* 2 * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_COMPONENTEXT_EXT_COMMON_EXT_NAPI_UTILS_H 17 #define FOUNDATION_ACE_COMPONENTEXT_EXT_COMMON_EXT_NAPI_UTILS_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 #include "base/utils/macros.h" 25 26 namespace OHOS::Ace { 27 class ACE_FORCE_EXPORT NapiAsyncEvent { 28 public: 29 NapiAsyncEvent(napi_env env, napi_value callback); 30 ~NapiAsyncEvent(); 31 napi_value Call(int32_t argc, napi_value* argv); 32 napi_env GetEnv(); 33 34 private: 35 napi_env env_; 36 napi_ref ref_; 37 }; 38 39 class ACE_FORCE_EXPORT ExtNapiUtils { 40 public: 41 static napi_value CreateInt32(napi_env env, int32_t code); 42 static int32_t GetCInt32(napi_env env, napi_value value); 43 static int64_t GetCInt64(napi_env env, napi_value value); 44 static napi_value CreateNull(napi_env env); 45 static bool GetBool(napi_env env, napi_value value); 46 static napi_valuetype GetValueType(napi_env env, napi_value value); 47 static std::string GetStringFromValueUtf8(napi_env env, napi_value value); 48 static bool CheckTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType); 49 }; 50 } 51 #endif // FOUNDATION_ACE_COMPONENTEXT_EXT_COMMON_EXT_NAPI_UTILS_H