• 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         {"testNapiIsArray", nullptr, testNapiIsArray, nullptr, nullptr, nullptr, napi_default, nullptr},
28         {"testNapiStrictEquals", nullptr, testNapiStrictEquals, nullptr, nullptr, nullptr, napi_default, nullptr}
29     };
30 
31     // Allocate memory
32     napi_property_descriptor *newDesc =
33         (napi_property_descriptor *)malloc(sizeof(napi_property_descriptor) * *len + sizeof(descToBeAppended));
34     if (newDesc == nullptr) {
35         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, "jsAbstractOpsInit", "Failed to allocated memory");
36         return;
37     }
38 
39     // Copy old properties & free memory
40     for (size_t i = 0; i < *len; ++i) {
41         newDesc[i] = (*origDescPtr)[i];
42     }
43     free(*origDescPtr);
44     *origDescPtr = newDesc;
45 
46     // Copy new properties
47     for (size_t i = 0; i < sizeof(descToBeAppended) / sizeof(napi_property_descriptor); ++i) {
48         newDesc[*len + i] = descToBeAppended[i];
49     }
50 
51     *len += sizeof(descToBeAppended) / sizeof(napi_property_descriptor);
52 }