• 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 #include "array.h"
17 
18 namespace NapiApi {
19 
Array(napi_env env,size_t count)20 Array::Array(napi_env env, size_t count) : env_(env)
21 {
22     napi_create_array_with_length(env, count, &array_);
23 }
24 
Array(napi_env env,napi_value v)25 Array::Array(napi_env env, napi_value v)
26 {
27     napi_valuetype jstype;
28     napi_typeof(env, v, &jstype);
29     if (jstype != napi_object) {
30         return;
31     }
32     bool isArray = false;
33     napi_is_array(env, v, &isArray);
34     if (!isArray) {
35         return;
36     }
37     env_ = env;
38     array_ = v;
39 }
40 
operator napi_value() const41 Array::operator napi_value() const
42 {
43     return array_;
44 }
45 
GetEnv() const46 napi_env Array::GetEnv() const
47 {
48     return env_;
49 }
50 
Count() const51 size_t Array::Count() const
52 {
53     uint32_t size = 0;
54     napi_get_array_length(env_, array_, &size);
55     return size;
56 }
57 
Type(size_t index) const58 napi_valuetype Array::Type(size_t index) const
59 {
60     napi_value element;
61     napi_get_element(env_, array_, index, &element);
62     napi_valuetype jstype;
63     napi_status status = napi_invalid_arg;
64     status = napi_typeof(env_, element, &jstype);
65     return jstype;
66 }
67 
Get_value(size_t index) const68 napi_value Array::Get_value(size_t index) const
69 {
70     napi_value result;
71     napi_get_element(env_, array_, index, &result);
72     return result;
73 }
74 
Set_value(size_t index,napi_value v) const75 void Array::Set_value(size_t index, napi_value v) const
76 {
77     napi_set_element(env_, array_, index, v);
78 }
79 
80 } // namespace NapiApi
81