Skip to content

Commit 00d12c0

Browse files
authored
Implement BigInt asIntN and asUintN methods (#5165)
The following methods were implemented: - BigInt.asIntN - BigInt.asUintN Custom dispatcher also added to builtin_bigint. The implementation is based on PR #4736, only applied the requested changes. Co-authored-by: Daniel Batiz [email protected] JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi [email protected]
1 parent a7e24fe commit 00d12c0

8 files changed

+557
-26
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-bigint.c

+328
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include "ecma-big-uint.h"
1617
#include "ecma-bigint.h"
1718
#include "ecma-builtins.h"
1819
#include "ecma-exceptions.h"
@@ -22,6 +23,21 @@
2223
#define ECMA_BUILTINS_INTERNAL
2324
#include "ecma-builtins-internal.h"
2425

26+
/**
27+
* This object has a custom dispatch function.
28+
*/
29+
#define BUILTIN_CUSTOM_DISPATCH
30+
31+
/**
32+
* List of built-in routine identifiers.
33+
*/
34+
enum
35+
{
36+
ECMA_BUILTIN_BIGINT_START = 0, /**< Special value, should be ignored */
37+
ECMA_BUILTIN_BIGINT_AS_INT_N, /**< The 'asIntN' routine of the BigInt object */
38+
ECMA_BUILTIN_BIGINT_AS_U_INT_N, /**< The 'asUintN' routine of the BigInt object */
39+
};
40+
2541
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-bigint.inc.h"
2642
#define BUILTIN_UNDERSCORED_ID bigint
2743
#include "ecma-builtin-internal-routines-template.inc.h"
@@ -36,6 +52,286 @@
3652
* @{
3753
*/
3854

55+
/**
56+
* The BigInt object's 'asIntN' and 'asUintN' routines
57+
*
58+
* See also:
59+
* ECMA-262 v15, 21.2.2
60+
*
61+
* @return ecma value
62+
* Truncated BigInt value for the given number of LSBs
63+
* as an integer or unsigned integer.
64+
* Returned value must be freed with ecma_free_value.
65+
*/
66+
static ecma_value_t
67+
ecma_builtin_bigint_object_as_int_n (ecma_value_t bits, /**< number of bits */
68+
ecma_value_t bigint, /**< bigint number */
69+
bool is_signed) /**< The operation is signed */
70+
{
71+
ecma_number_t input_bits;
72+
ecma_value_t bit_value = ecma_op_to_index (bits, &input_bits);
73+
74+
if (ECMA_IS_VALUE_ERROR (bit_value))
75+
{
76+
return bit_value;
77+
}
78+
79+
ecma_value_t bigint_value = ecma_bigint_to_bigint (bigint, false);
80+
81+
if (ECMA_IS_VALUE_ERROR (bigint_value))
82+
{
83+
return bigint_value;
84+
}
85+
86+
if (input_bits == 0 || bigint_value == ECMA_BIGINT_ZERO)
87+
{
88+
ecma_free_value (bigint_value);
89+
return ECMA_BIGINT_ZERO;
90+
}
91+
92+
ecma_extended_primitive_t *input_bigint_p = ecma_get_extended_primitive_from_value (bigint_value);
93+
uint32_t bigint_size = ECMA_BIGINT_GET_SIZE (input_bigint_p);
94+
95+
if (input_bits >= UINT32_MAX)
96+
{
97+
return bigint_value;
98+
}
99+
100+
uint8_t input_bigint_sign = input_bigint_p->u.bigint_sign_and_size & ECMA_BIGINT_SIGN;
101+
const uint32_t size_of_divisor_in_bits = sizeof (uint32_t) * JERRY_BITSINBYTE;
102+
uint32_t whole_part = (uint32_t) input_bits / size_of_divisor_in_bits;
103+
uint32_t remainder = (uint32_t) input_bits % size_of_divisor_in_bits;
104+
uint32_t input_bit_length =
105+
(whole_part == 0 || remainder == 0) ? (uint32_t) input_bits : (whole_part + 1) * size_of_divisor_in_bits;
106+
107+
uint32_t input_byte_size = (uint32_t) input_bits / JERRY_BITSINBYTE;
108+
109+
if ((uint32_t) input_bits % JERRY_BITSINBYTE != 0)
110+
{
111+
input_byte_size += 1;
112+
}
113+
114+
const uint32_t input_bits_in_byte = (uint32_t) input_bit_length / JERRY_BITSINBYTE;
115+
uint32_t min_size = (input_bits_in_byte < bigint_size) ? input_bits_in_byte : bigint_size;
116+
117+
if (input_bigint_sign && (input_byte_size > bigint_size))
118+
{
119+
min_size = (input_bits_in_byte > bigint_size) ? input_bits_in_byte : bigint_size;
120+
}
121+
122+
if (min_size < sizeof (uint32_t))
123+
{
124+
min_size = sizeof (uint32_t);
125+
}
126+
127+
ecma_extended_primitive_t *result_p = ecma_bigint_create ((uint32_t) min_size);
128+
129+
if (JERRY_UNLIKELY (result_p == NULL))
130+
{
131+
ecma_deref_bigint (input_bigint_p);
132+
return ECMA_VALUE_ERROR;
133+
}
134+
135+
ecma_bigint_digit_t *last_digit_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, bigint_size);
136+
137+
/* Calculate the leading zeros of the input_bigint */
138+
139+
ecma_bigint_digit_t zeros = ecma_big_uint_count_leading_zero (last_digit_p[-1]);
140+
uint32_t bits_of_bigint = (uint32_t) (bigint_size * JERRY_BITSINBYTE) - zeros;
141+
uint32_t exact_size =
142+
(input_bigint_sign || ((bits_of_bigint > (size_of_divisor_in_bits - 1)) && (input_byte_size < bigint_size)))
143+
? (uint32_t) input_byte_size
144+
: bigint_size;
145+
146+
if (input_bigint_sign)
147+
{
148+
bits_of_bigint += 1;
149+
}
150+
151+
if (bits_of_bigint > (input_bits - 1) || input_bigint_sign)
152+
{
153+
ecma_bigint_digit_t *digits_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, 0);
154+
ecma_bigint_digit_t *digits_end_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, exact_size);
155+
ecma_bigint_digit_t *result_number_p = ECMA_BIGINT_GET_DIGITS (result_p, 0);
156+
int32_t first_cell = 0;
157+
uint32_t surplus_bits =
158+
(whole_part > 0) ? (uint32_t) (whole_part * size_of_divisor_in_bits) : (uint32_t) size_of_divisor_in_bits;
159+
uint32_t mask_bit = (whole_part == 0) ? (uint32_t) input_bits : (uint32_t) input_bits - surplus_bits;
160+
161+
if (mask_bit == 0)
162+
{
163+
mask_bit = size_of_divisor_in_bits - 1;
164+
}
165+
166+
uint32_t check_sign_mask = (uint32_t) 1 << (mask_bit - 1);
167+
uint32_t mask = ((uint32_t) 1 << mask_bit) - 1;
168+
uint32_t last_cell = (exact_size >= sizeof (uint32_t)) ? (uint32_t) (min_size / sizeof (uint32_t)) - 1 : 0;
169+
bool is_positive = false;
170+
bool is_representation_positive = false;
171+
172+
if (is_signed)
173+
{
174+
if (input_bigint_sign && ((~digits_p[last_cell] + 1) & check_sign_mask) == 0)
175+
{
176+
is_positive = true;
177+
}
178+
179+
if ((digits_p[last_cell] & check_sign_mask) == 0)
180+
{
181+
is_representation_positive = true;
182+
}
183+
}
184+
185+
do
186+
{
187+
*result_number_p++ =
188+
(is_representation_positive || (!is_signed && !input_bigint_sign)) ? *digits_p++ : ~(*digits_p++);
189+
first_cell--;
190+
} while (digits_p < digits_end_p);
191+
192+
int16_t equal_bits = 0;
193+
194+
if (remainder != 0)
195+
{
196+
equal_bits = -1;
197+
}
198+
199+
int32_t last_cell_negative = (last_cell != 0) ? ((int32_t) last_cell * (-1)) : -1;
200+
bool is_zero_values = false;
201+
202+
if (!is_signed)
203+
{
204+
if (input_bigint_sign)
205+
{
206+
is_zero_values = true;
207+
}
208+
}
209+
else
210+
{
211+
if (((digits_p[-1] & check_sign_mask) > 0) || (result_number_p[-1] & check_sign_mask) > 0)
212+
{
213+
is_zero_values = true;
214+
}
215+
}
216+
217+
if (is_zero_values)
218+
{
219+
result_number_p[first_cell] += 1;
220+
221+
if (result_number_p[first_cell] == 0)
222+
{
223+
do
224+
{
225+
result_number_p[++first_cell] += 1;
226+
} while (first_cell != equal_bits);
227+
228+
first_cell = last_cell_negative;
229+
}
230+
}
231+
232+
result_number_p[-1] &= mask;
233+
uint32_t surplus = (uint32_t) (min_size - exact_size) / sizeof (ecma_char_t);
234+
uint32_t new_size = result_p->u.bigint_sign_and_size;
235+
236+
if ((min_size - exact_size) % (sizeof (ecma_char_t)) > 0 && surplus == 0)
237+
{
238+
surplus += (uint32_t) sizeof (ecma_char_t);
239+
}
240+
else
241+
{
242+
surplus = (uint32_t) (surplus * sizeof (ecma_char_t));
243+
}
244+
245+
if (min_size / JERRY_BITSINBYTE < 1)
246+
{
247+
surplus = 0;
248+
}
249+
250+
if (is_signed)
251+
{
252+
if (result_p->u.bigint_sign_and_size > exact_size && min_size > sizeof (uint32_t)
253+
&& result_number_p[last_cell_negative] < 1)
254+
{
255+
new_size -= surplus;
256+
}
257+
258+
new_size += 1;
259+
260+
if (is_positive || ((digits_p[-1] & check_sign_mask) == 0 && !input_bigint_sign))
261+
{
262+
new_size -= 1;
263+
}
264+
}
265+
266+
while (first_cell != 0 && result_number_p[first_cell] == 0)
267+
{
268+
first_cell++;
269+
}
270+
271+
if (first_cell == 0)
272+
{
273+
ecma_deref_bigint (result_p);
274+
ecma_deref_bigint (input_bigint_p);
275+
276+
return ECMA_BIGINT_ZERO;
277+
}
278+
279+
last_cell_negative = first_cell + (int32_t) last_cell;
280+
int16_t zero_section_cnt = 0;
281+
282+
while (last_cell_negative > first_cell)
283+
{
284+
if (result_number_p[last_cell_negative] == 0)
285+
{
286+
zero_section_cnt++;
287+
}
288+
289+
last_cell_negative--;
290+
}
291+
292+
uint32_t size_limit = sizeof (uint32_t);
293+
294+
if (zero_section_cnt >= 1)
295+
{
296+
size_limit = new_size - (uint32_t) zero_section_cnt * size_limit;
297+
new_size = (size_limit < sizeof (uint32_t)) ? (uint32_t) (JERRY_BITSINBYTE - size_limit) : size_limit;
298+
}
299+
300+
if (new_size < result_p->u.bigint_sign_and_size)
301+
{
302+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
303+
uint32_t new_size_remainder = new_size % sizeof (uint32_t);
304+
ecma_extended_primitive_t *new_result_p = ecma_bigint_create (new_size - new_size_remainder);
305+
306+
new_result_p->u.bigint_sign_and_size += new_size_remainder;
307+
memcpy (new_result_p + 1, result_p + 1, new_size - new_size_remainder);
308+
309+
ecma_deref_bigint (result_p);
310+
ecma_deref_bigint (input_bigint_p);
311+
312+
return ecma_make_extended_primitive_value (new_result_p, ECMA_TYPE_BIGINT);
313+
}
314+
315+
result_p->u.bigint_sign_and_size = new_size;
316+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
317+
318+
ecma_deref_bigint (input_bigint_p);
319+
return ecma_make_extended_primitive_value (result_p, ECMA_TYPE_BIGINT);
320+
}
321+
322+
memcpy (result_p + 1, input_bigint_p + 1, exact_size);
323+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
324+
325+
if (input_bigint_p->u.bigint_sign_and_size & ECMA_BIGINT_SIGN)
326+
{
327+
ecma_deref_bigint (input_bigint_p);
328+
return ecma_bigint_negate (result_p);
329+
}
330+
331+
ecma_deref_bigint (input_bigint_p);
332+
return ecma_make_extended_primitive_value (result_p, ECMA_TYPE_BIGINT);
333+
} /* ecma_builtin_bigint_object_as_int_n */
334+
39335
/**
40336
* Handle calling [[Call]] of built-in BigInt object
41337
*
@@ -71,6 +367,38 @@ ecma_builtin_bigint_dispatch_construct (const ecma_value_t *arguments_list_p, /*
71367
return ecma_raise_type_error (ECMA_ERR_BIGINT_FUNCTION_NOT_CONSTRUCTOR);
72368
} /* ecma_builtin_bigint_dispatch_construct */
73369

370+
/**
371+
* Dispatcher of the built-in's routines
372+
*
373+
* @return ecma value
374+
* Returned value must be freed with ecma_free_value.
375+
*/
376+
ecma_value_t
377+
ecma_builtin_bigint_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
378+
ecma_value_t this_arg, /**< 'this' argument value */
379+
const ecma_value_t arguments_list_p[], /**< list of arguments
380+
* passed to routine */
381+
uint32_t arguments_number) /**< length of arguments' list */
382+
{
383+
JERRY_UNUSED_2 (this_arg, arguments_number);
384+
385+
switch (builtin_routine_id)
386+
{
387+
case ECMA_BUILTIN_BIGINT_AS_INT_N:
388+
{
389+
return ecma_builtin_bigint_object_as_int_n (arguments_list_p[0], arguments_list_p[1], true);
390+
}
391+
case ECMA_BUILTIN_BIGINT_AS_U_INT_N:
392+
{
393+
return ecma_builtin_bigint_object_as_int_n (arguments_list_p[0], arguments_list_p[1], false);
394+
}
395+
default:
396+
{
397+
JERRY_UNREACHABLE ();
398+
}
399+
}
400+
} /* ecma_builtin_bigint_dispatch_routine */
401+
74402
/**
75403
* @}
76404
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-bigint.inc.h

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ STRING_VALUE (LIT_MAGIC_STRING_NAME, LIT_MAGIC_STRING_BIGINT_UL, ECMA_PROPERTY_F
3636
/* ECMA-262 v11, 20.2.2.3 */
3737
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE, ECMA_BUILTIN_ID_BIGINT_PROTOTYPE, ECMA_PROPERTY_FIXED)
3838

39+
/* Routine properties:
40+
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
41+
42+
ROUTINE (LIT_MAGIC_STRING_AS_INT_N, ECMA_BUILTIN_BIGINT_AS_INT_N, 2, 2)
43+
ROUTINE (LIT_MAGIC_STRING_AS_U_INT_N, ECMA_BUILTIN_BIGINT_AS_U_INT_N, 2, 2)
3944
#endif /* JERRY_BUILTIN_BIGINT */
4045

4146
#include "ecma-builtin-helpers-macro-undefs.inc.h"

jerry-core/lit/lit-magic-strings.inc.h

+6
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SCRIPT_UL, "Script")
294294
#endif /* JERRY_PARSER */
295295
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_STRING_UL, "String")
296296
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SYMBOL_UL, "Symbol")
297+
#if JERRY_BUILTIN_BIGINT
298+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_AS_INT_N, "asIntN")
299+
#endif /* JERRY_BUILTIN_BIGINT */
297300
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ASSIGN, "assign")
298301
#if JERRY_BUILTIN_BIGINT
299302
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BIGINT, "bigint")
@@ -411,6 +414,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_WEAKREF_UL, "WeakRef")
411414
#if JERRY_BUILTIN_CONTAINER
412415
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_WEAKSET_UL, "WeakSet")
413416
#endif /* JERRY_BUILTIN_CONTAINER */
417+
#if JERRY_BUILTIN_BIGINT
418+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_AS_U_INT_N, "asUintN")
419+
#endif /* JERRY_BUILTIN_BIGINT */
414420
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BOOLEAN, "boolean")
415421
#if JERRY_BUILTIN_ANNEXB && JERRY_BUILTIN_REGEXP
416422
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COMPILE, "compile")

jerry-core/lit/lit-magic-strings.ini

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ LIT_MAGIC_STRING_THROW = "throw"
144144
LIT_MAGIC_STRING_TRUNC = "trunc"
145145
LIT_MAGIC_STRING_VALUE = "value"
146146
LIT_MAGIC_STRING_SOURCE_NAME_EVAL = "<eval>"
147+
LIT_MAGIC_STRING_AS_INT_N = "asIntN"
148+
LIT_MAGIC_STRING_AS_U_INT_N = "asUintN"
147149
LIT_MAGIC_STRING_BIGINT_UL = "BigInt"
148150
LIT_MAGIC_STRING_ERRORS_UL = "errors"
149151
LIT_MAGIC_STRING_HAS_OWN_UL = "hasOwn"

0 commit comments

Comments
 (0)