1 /*
2 * Copyright (c) 2021 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 #include "js_napi_common.h"
16 #include "js_native_api.h"
17 #include "napi/native_api.h"
18 #include "napi/native_common.h"
19 #include "napi/native_node_api.h"
20 #include "utils/log.h"
21 #define NAPI_VERSION 8
22 namespace ACE {
23 namespace NAPI {
24 namespace SYSTEM_TEST_NAPI {
25 static constexpr int ARG_TWO = 2;
26 static const napi_type_tag typeTags[2] = {
27 { 0xdaf987b3daf987b3, 0xb745b049b745b049 },
28 { 0xbb7936c3bb7936c6, 0xa9548d07a9548d07 }
29 };
30
ObjectTypeTaggedInstance(napi_env env,napi_callback_info info)31 static napi_value ObjectTypeTaggedInstance(napi_env env, napi_callback_info info)
32 {
33 HILOG_INFO("%{public}s,called", __func__);
34 size_t argc = 1;
35 uint32_t typeIndex = 0;
36 napi_value instance = nullptr;
37 napi_value whichType = nullptr;
38
39 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &whichType, NULL, NULL));
40 NAPI_CALL(env, napi_get_value_uint32(env, whichType, &typeIndex));
41 NAPI_CALL(env, napi_create_object(env, &instance));
42 NAPI_CALL(env, napi_type_tag_object(env, instance, &typeTags[typeIndex]));
43
44 HILOG_INFO("%{public}s,called typeIndex=%{public}d", __func__, typeIndex);
45 return instance;
46 }
47
ObjectCheckTypeTag(napi_env env,napi_callback_info info)48 static napi_value ObjectCheckTypeTag(napi_env env, napi_callback_info info)
49 {
50 HILOG_INFO("%{public}s,called", __func__);
51 size_t argc = ARG_TWO;
52 bool result = false;
53 napi_value argv[ARG_TWO] = { nullptr };
54 napi_value jsResult = nullptr;
55 uint32_t typeIndex = 0;
56
57 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
58 NAPI_CALL(env, napi_get_value_uint32(env, argv[0], &typeIndex));
59 NAPI_CALL(env, napi_check_object_type_tag(env, argv[1], &typeTags[typeIndex], &result));
60 NAPI_CALL(env, napi_get_boolean(env, result, &jsResult));
61
62 HILOG_INFO("%{public}s,called typeIndex=%{public}d, result=%{public}s", __func__, typeIndex,
63 result ? "true" : "false");
64
65 return jsResult;
66 }
67
ObjectFreeze(napi_env env,napi_callback_info info)68 static napi_value ObjectFreeze(napi_env env, napi_callback_info info)
69 {
70 HILOG_INFO("%{public}s,called", __func__);
71 size_t argc = 1;
72 napi_value args[1] = { nullptr };
73 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
74
75 napi_value object = args[0];
76 NAPI_CALL(env, napi_object_freeze(env, object));
77
78 return object;
79 }
80
ObjectSeal(napi_env env,napi_callback_info info)81 static napi_value ObjectSeal(napi_env env, napi_callback_info info)
82 {
83 HILOG_INFO("%{public}s,called", __func__);
84 size_t argc = 1;
85 napi_value args[1] = { nullptr };
86 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
87
88 napi_value object = args[0];
89 NAPI_CALL(env, napi_object_seal(env, object));
90
91 return object;
92 }
93
AddReturnedStatus(napi_env env,const char * key,napi_value object,char * expected_message,napi_status expected_status,napi_status actual_status)94 void AddReturnedStatus(napi_env env, const char* key, napi_value object,
95 char* expected_message, napi_status expected_status, napi_status actual_status)
96 {
97 HILOG_INFO("%{public}s,called", __func__);
98 napi_value propValue = nullptr;
99
100 if (actual_status != expected_status) {
101 HILOG_INFO("%{public}s,called actual_status != expected_status", __func__);
102 }
103
104 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(
105 env, expected_message, NAPI_AUTO_LENGTH, &propValue));
106 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, key, propValue));
107 }
108
AddLastStatus(napi_env env,const char * key,napi_value returnValue)109 void AddLastStatus(napi_env env, const char* key, napi_value returnValue)
110 {
111 HILOG_INFO("%{public}s,called", __func__);
112 napi_value prop_value = nullptr;
113 const napi_extended_error_info* pLastError = nullptr;
114
115 NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &pLastError));
116
117 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, (pLastError->error_message == nullptr ?
118 "Invalid argument" : pLastError->error_message), NAPI_AUTO_LENGTH, &prop_value));
119
120 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, returnValue, key, prop_value));
121 }
122
ObjectGetAllPropertyNames(napi_env env,napi_callback_info info)123 static napi_value ObjectGetAllPropertyNames(napi_env env, napi_callback_info info)
124 {
125 HILOG_INFO("%{public}s,called", __func__);
126
127 napi_value returnValue = nullptr;
128 napi_value props = nullptr;
129 NAPI_CALL(env, napi_create_object(env, &returnValue));
130 napi_status actual_status = napi_get_all_property_names(
131 NULL, returnValue, napi_key_own_only, napi_key_writable, napi_key_keep_numbers, &props);
132 AddReturnedStatus(env, "envIsNull", returnValue, (char*)("Invalid argument"), napi_invalid_arg, actual_status);
133 napi_get_all_property_names(env, NULL, napi_key_own_only, napi_key_writable, napi_key_keep_numbers, &props);
134
135 AddLastStatus(env, "objectIsNull", returnValue);
136 napi_get_all_property_names(env, returnValue, napi_key_own_only, napi_key_writable, napi_key_keep_numbers, NULL);
137 AddLastStatus(env, "valueIsNull", returnValue);
138 return returnValue;
139 }
140
ObjectInit(napi_env env,napi_value exports)141 napi_value ObjectInit(napi_env env, napi_value exports)
142 {
143 HILOG_INFO("%{public}s,called", __func__);
144
145 napi_property_descriptor methods[] = {
146 DECLARE_NAPI_FUNCTION("testObjectTypeTaggedInstance", ObjectTypeTaggedInstance),
147 DECLARE_NAPI_FUNCTION("testObjectCheckTypeTag", ObjectCheckTypeTag),
148 DECLARE_NAPI_FUNCTION("testObjectFreeze", ObjectFreeze),
149 DECLARE_NAPI_FUNCTION("testObjectSeal", ObjectSeal),
150 DECLARE_NAPI_FUNCTION("testObjectGetAllPropertyNames", ObjectGetAllPropertyNames),
151 };
152
153 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(methods) / sizeof(methods[0]), methods));
154
155 return exports;
156 }
157 } // namespace SYSTEM_TEST_NAPI
158 } // namespace NAPI
159 } // namespace ACE