• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "common.h"
17 #include "javascriptapi.h"
18 
jsAbstractOpsInit(napi_property_descriptor ** origDescPtr,size_t * len)19 void jsAbstractOpsInit(napi_property_descriptor **origDescPtr, size_t *len)
20 {
21     napi_property_descriptor descToBeAppended[] = {
22         {"testNapiCoerceToBool", nullptr, testNapiCoerceToBool, nullptr, nullptr, nullptr, napi_default, nullptr},
23         {"testNapiCoerceToNumber", nullptr, testNapiCoerceToNumber, nullptr, nullptr, nullptr, napi_default, nullptr},
24         {"testNapiCoerceToObject", nullptr, testNapiCoerceToObject, nullptr, nullptr, nullptr, napi_default, nullptr},
25         {"testNapiCoerceToString", nullptr, testNapiCoerceToString, nullptr, nullptr, nullptr, napi_default, nullptr},
26         {"testNapiTypeof", nullptr, testNapiTypeof, nullptr, nullptr, nullptr, napi_default, nullptr}
27     };
28 
29     // Allocate memory
30     napi_property_descriptor *newDesc =
31         (napi_property_descriptor *)malloc(sizeof(napi_property_descriptor) * *len + sizeof(descToBeAppended));
32     if (newDesc == nullptr) {
33         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, "jsAbstractOpsInit", "Failed to allocated memory");
34         return;
35     }
36 
37     // Copy old properties & free memory
38     for (size_t i = 0; i < *len; ++i) {
39         newDesc[i] = (*origDescPtr)[i];
40     }
41     free(*origDescPtr);
42     *origDescPtr = newDesc;
43 
44     // Copy new properties
45     for (size_t i = 0; i < sizeof(descToBeAppended) / sizeof(napi_property_descriptor); ++i) {
46         newDesc[*len + i] = descToBeAppended[i];
47     }
48 
49     *len += sizeof(descToBeAppended) / sizeof(napi_property_descriptor);
50 }