• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 NAPI_API_FUNCTION_H
17 #define NAPI_API_FUNCTION_H
18 
19 #ifdef __OHOS_PLATFORM__
20 #include "napi/native_api.h"
21 #else
22 #include <node_api.h>
23 #endif
24 
25 #include "env.h"
26 
27 namespace NapiApi {
28 
29 class Object;
30 
31 // The args that will be passed to JavaScript functions (including constructors).
32 struct JsFuncArgs {
JsFuncArgsJsFuncArgs33     JsFuncArgs() : argc(0), argv(nullptr) {}
34     template<size_t N>
JsFuncArgsJsFuncArgs35     JsFuncArgs(napi_value (&argv)[N]) : argc(N), argv(argv)
36     {}
37 
38     uint32_t argc;
39     napi_value* argv;
40 };
41 
42 class Function {
43 public:
44     Function() = default;
45     Function(napi_env env, napi_value v);
46 
47     operator napi_value() const;
48     NapiApi::Env Env() const;
49     napi_env GetEnv() const;
50 
51     napi_value Invoke(const NapiApi::Object& thisJS, size_t argc = 0, napi_value* argv = nullptr) const;
52 
53     bool IsDefined();
54     bool IsNull();
55 
56 private:
57     NapiApi::Env env_ { nullptr };
58     napi_value func_ { nullptr };
59     napi_valuetype jstype { napi_undefined };
60 };
61 
62 } // namespace NapiApi
63 
64 #endif
65