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