Skip to content

Commit 9deb744

Browse files
committed
Implement BigInt asIntN and asUintN methods
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 d2e0d71 commit 9deb744

File tree

8 files changed

+558
-26
lines changed

8 files changed

+558
-26
lines changed

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

Lines changed: 329 additions & 0 deletions
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,287 @@
3652
* @{
3753
*/
3854

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

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

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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)