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 <uv.h>
16 #include "js_napi_common.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 namespace ACE {
22 namespace NAPI {
23 namespace SYSTEM_TEST_NAPI {
BigIntIsLossless(napi_env env,napi_callback_info info)24 static napi_value BigIntIsLossless(napi_env env, napi_callback_info info)
25 {
26 HILOG_INFO("%{public}s,called", __func__);
27 size_t argc = 2;
28 napi_value args[2] = { nullptr };
29 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
30
31 bool is_signed = false;
32 NAPI_CALL(env, napi_get_value_bool(env, args[1], &is_signed));
33
34 bool lossless = false;
35 if (is_signed) {
36 int64_t input = 0;
37 NAPI_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless));
38 } else {
39 uint64_t input = 0;
40 NAPI_CALL(env, napi_get_value_bigint_uint64(env, args[0], &input, &lossless));
41 }
42
43 napi_value output = nullptr;
44 NAPI_CALL(env, napi_get_boolean(env, lossless, &output));
45
46 return output;
47 }
48
BigInt64(napi_env env,napi_callback_info info)49 static napi_value BigInt64(napi_env env, napi_callback_info info)
50 {
51 HILOG_INFO("%{public}s,called", __func__);
52 size_t argc = 1;
53 napi_value args[1] = { nullptr };
54 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
55 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
56 napi_valuetype valuetype;
57 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
58 NAPI_ASSERT(env, valuetype == napi_bigint, "Wrong type of arguments. Expects a bigint as first argument.");
59 int64_t input = 0;
60 bool lossless = false;
61 NAPI_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless));
62 napi_value output = nullptr;
63 NAPI_CALL(env, napi_create_bigint_int64(env, input, &output));
64 return output;
65 }
66
BigUint64(napi_env env,napi_callback_info info)67 static napi_value BigUint64(napi_env env, napi_callback_info info)
68 {
69 HILOG_INFO("%{public}s,called", __func__);
70 size_t argc = 1;
71 napi_value args[1] = { nullptr };
72 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
73
74 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
75
76 napi_valuetype valuetype0;
77 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
78
79 NAPI_ASSERT(env, valuetype0 == napi_bigint, "Wrong type of arguments. Expects a bigint as first argument.");
80
81 uint64_t input = 0;
82 bool lossless = false;
83 NAPI_CALL(env, napi_get_value_bigint_uint64(env, args[0], &input, &lossless));
84
85 napi_value output = nullptr;
86 NAPI_CALL(env, napi_create_bigint_uint64(env, input, &output));
87
88 return output;
89 }
90
BigIntWords(napi_env env,napi_callback_info info)91 static napi_value BigIntWords(napi_env env, napi_callback_info info)
92 {
93 HILOG_INFO("%{public}s,called", __func__);
94 size_t argc = 1;
95 napi_value args[1] = { nullptr };
96 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
97 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
98
99 napi_valuetype valuetype;
100 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
101 NAPI_ASSERT(env, valuetype == napi_bigint, "Wrong type of arguments. Expects a bigint as first argument.");
102
103 size_t expected_word_count;
104 NAPI_CALL(env, napi_get_value_bigint_words(env, args[0], nullptr, &expected_word_count, nullptr));
105
106 int sign_bit = 0;
107 size_t word_count = 10;
108 uint64_t words[10] = { 0 };
109
110 NAPI_CALL(env, napi_get_value_bigint_words(env, args[0], &sign_bit, &word_count, words));
111 NAPI_ASSERT(env, word_count == expected_word_count, "word counts do not match");
112
113 napi_value output = nullptr;
114 NAPI_CALL(env, napi_create_bigint_words(env, sign_bit, word_count, words, &output));
115
116 return output;
117 }
118
119 // throws RangeError
CreateTooBigBigInt(napi_env env,napi_callback_info info)120 static napi_value CreateTooBigBigInt(napi_env env, napi_callback_info info)
121 {
122 HILOG_INFO("%{public}s,called", __func__);
123 int sign_bit = 0;
124 size_t word_count = 10;
125 uint64_t words[10] = { 0 };
126 napi_value output = nullptr;
127 NAPI_CALL(env, napi_create_bigint_words(env, sign_bit, word_count, words, &output));
128
129 return output;
130 }
131
BigIntInit(napi_env env,napi_value exports)132 napi_value BigIntInit(napi_env env, napi_value exports)
133 {
134 napi_property_descriptor descriptors[] = {
135 DECLARE_NAPI_FUNCTION("testBigIntIsLossless", BigIntIsLossless),
136 DECLARE_NAPI_FUNCTION("testBigInt64", BigInt64),
137 DECLARE_NAPI_FUNCTION("testBigUint64", BigUint64),
138 DECLARE_NAPI_FUNCTION("testBigIntWords", BigIntWords),
139 DECLARE_NAPI_FUNCTION("testCreateTooBigBigInt", CreateTooBigBigInt),
140 };
141
142 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(descriptors) / sizeof(descriptors[0]), descriptors));
143
144 return exports;
145 }
146 } // namespace SYSTEM_TEST_NAPI
147 } // namespace NAPI
148 } // namespace ACE
149