Skip to content

Commit c812ef9

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. JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi [email protected] Co-authored-by: Daniel Batiz [email protected]
1 parent d2e0d71 commit c812ef9

File tree

8 files changed

+579
-26
lines changed

8 files changed

+579
-26
lines changed

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

Lines changed: 350 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,308 @@
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 = 1;
103+
104+
if (whole_part == 0)
105+
{
106+
input_bit_length = (uint32_t) input_bits;
107+
}
108+
else
109+
{
110+
input_bit_length = (remainder > 0) ? (whole_part + 1) * size_of_divisor_in_bits : (uint32_t) input_bits;
111+
}
112+
113+
uint32_t input_byte_size = (uint32_t) input_bits / JERRY_BITSINBYTE;
114+
115+
if ((uint32_t) input_bits % JERRY_BITSINBYTE != 0)
116+
{
117+
input_byte_size += 1;
118+
}
119+
120+
const uint32_t input_bits_in_byte = (uint32_t) input_bit_length / JERRY_BITSINBYTE;
121+
uint32_t min_size = (input_bits_in_byte < bigint_size) ? input_bits_in_byte : bigint_size;
122+
123+
if (input_bigint_sign && (input_byte_size > bigint_size))
124+
{
125+
min_size = (input_bits_in_byte > bigint_size) ? input_bits_in_byte : bigint_size;
126+
}
127+
128+
if (min_size < sizeof (uint32_t))
129+
{
130+
min_size = sizeof (uint32_t);
131+
}
132+
133+
ecma_extended_primitive_t *result_p = ecma_bigint_create ((uint32_t) min_size);
134+
135+
if (JERRY_UNLIKELY (result_p == NULL))
136+
{
137+
ecma_deref_bigint (input_bigint_p);
138+
return ECMA_VALUE_ERROR;
139+
}
140+
141+
ecma_bigint_digit_t *last_digit_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, bigint_size);
142+
143+
/* Calculate the leading zeros of the input_bigint */
144+
145+
ecma_bigint_digit_t zeros = ecma_big_uint_count_leading_zero (last_digit_p[-1]);
146+
uint32_t bits_of_bigint = (uint32_t) (bigint_size * JERRY_BITSINBYTE) - zeros;
147+
uint32_t exact_size = 1;
148+
149+
if ((bits_of_bigint > (size_of_divisor_in_bits - 1)) || input_bigint_sign)
150+
{
151+
exact_size = (input_byte_size < bigint_size) ? (uint32_t) input_byte_size : bigint_size;
152+
153+
if (input_bigint_sign && (input_byte_size > bigint_size))
154+
{
155+
exact_size = (input_byte_size > bigint_size) ? (uint32_t) input_byte_size : bigint_size;
156+
}
157+
}
158+
else
159+
{
160+
exact_size = bigint_size;
161+
}
162+
163+
if (input_bigint_sign)
164+
{
165+
bits_of_bigint += 1;
166+
}
167+
168+
if (bits_of_bigint > (input_bits - 1) || input_bigint_sign)
169+
{
170+
ecma_bigint_digit_t *digits_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, 0);
171+
ecma_bigint_digit_t *digits_end_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, exact_size);
172+
ecma_bigint_digit_t *result_number_p = ECMA_BIGINT_GET_DIGITS (result_p, 0);
173+
int32_t first_cell = 0;
174+
uint32_t surplus_bits =
175+
(whole_part > 0) ? (uint32_t) (whole_part * size_of_divisor_in_bits) : (uint32_t) size_of_divisor_in_bits;
176+
uint32_t mask_bit = (whole_part == 0) ? (uint32_t) input_bits : (uint32_t) input_bits - surplus_bits;
177+
178+
if (mask_bit == 0)
179+
{
180+
mask_bit = size_of_divisor_in_bits - 1;
181+
}
182+
183+
uint32_t check_sign_mask = (uint32_t) 1 << (mask_bit - 1);
184+
uint32_t mask = ((uint32_t) 1 << mask_bit) - 1;
185+
uint32_t last_cell = (exact_size >= sizeof (uint32_t)) ? (uint32_t) (min_size / sizeof (uint32_t)) - 1 : 0;
186+
bool is_positive = false;
187+
bool is_representation_positive = false;
188+
189+
if (is_signed)
190+
{
191+
if (input_bigint_sign && ((~digits_p[last_cell] + 1) & check_sign_mask) == 0)
192+
{
193+
is_positive = true;
194+
}
195+
196+
if ((digits_p[last_cell] & check_sign_mask) == 0)
197+
{
198+
is_representation_positive = true;
199+
}
200+
}
201+
202+
do
203+
{
204+
*result_number_p++ =
205+
(is_representation_positive || (!is_signed && !input_bigint_sign)) ? *digits_p++ : ~(*digits_p++);
206+
first_cell--;
207+
} while (digits_p < digits_end_p);
208+
209+
int16_t equal_bits = 0;
210+
211+
if (remainder != 0)
212+
{
213+
equal_bits = -1;
214+
}
215+
216+
int32_t last_cell_negative = (last_cell != 0) ? ((int32_t) last_cell * (-1)) : -1;
217+
bool is_zero_values = false;
218+
219+
if (!is_signed)
220+
{
221+
if (input_bigint_sign)
222+
{
223+
is_zero_values = true;
224+
}
225+
}
226+
else
227+
{
228+
if (((digits_p[-1] & check_sign_mask) > 0) || (result_number_p[-1] & check_sign_mask) > 0)
229+
{
230+
is_zero_values = true;
231+
}
232+
}
233+
234+
if (is_zero_values)
235+
{
236+
result_number_p[first_cell] += 1;
237+
238+
if (result_number_p[first_cell] == 0)
239+
{
240+
do
241+
{
242+
result_number_p[++first_cell] += 1;
243+
} while (first_cell != equal_bits);
244+
245+
first_cell = last_cell_negative;
246+
}
247+
}
248+
249+
result_number_p[-1] &= mask;
250+
uint32_t surplus = (uint32_t) (min_size - exact_size) / sizeof (ecma_char_t);
251+
uint32_t new_size = result_p->u.bigint_sign_and_size;
252+
253+
if ((min_size - exact_size) % (sizeof (ecma_char_t)) > 0 && surplus == 0)
254+
{
255+
surplus += (uint32_t) sizeof (ecma_char_t);
256+
}
257+
else
258+
{
259+
surplus = (uint32_t) (surplus * sizeof (ecma_char_t));
260+
}
261+
262+
if (min_size / JERRY_BITSINBYTE < 1)
263+
{
264+
surplus = 0;
265+
}
266+
267+
if (is_signed)
268+
{
269+
if (result_p->u.bigint_sign_and_size > exact_size && min_size > sizeof (uint32_t)
270+
&& result_number_p[last_cell_negative] < 1)
271+
{
272+
new_size -= surplus;
273+
}
274+
275+
new_size += 1;
276+
277+
if (is_positive || ((digits_p[-1] & check_sign_mask) == 0 && !input_bigint_sign))
278+
{
279+
new_size -= 1;
280+
}
281+
}
282+
283+
while (first_cell != 0)
284+
{
285+
if (result_number_p[first_cell] != 0)
286+
{
287+
break;
288+
}
289+
290+
first_cell++;
291+
}
292+
293+
if (first_cell == 0)
294+
{
295+
ecma_deref_bigint (result_p);
296+
ecma_deref_bigint (input_bigint_p);
297+
298+
return ECMA_BIGINT_ZERO;
299+
}
300+
301+
last_cell_negative = first_cell + (int32_t) last_cell;
302+
int16_t zero_section_cnt = 0;
303+
304+
while (last_cell_negative > first_cell)
305+
{
306+
if (result_number_p[last_cell_negative] == 0)
307+
{
308+
zero_section_cnt++;
309+
}
310+
311+
last_cell_negative--;
312+
}
313+
314+
uint32_t size_limit = sizeof (uint32_t);
315+
316+
if (zero_section_cnt >= 1)
317+
{
318+
size_limit = new_size - (uint32_t) zero_section_cnt * size_limit;
319+
new_size = (size_limit < sizeof (uint32_t)) ? (uint32_t) (JERRY_BITSINBYTE - size_limit) : size_limit;
320+
}
321+
322+
if (new_size < result_p->u.bigint_sign_and_size)
323+
{
324+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
325+
uint32_t new_size_remainder = new_size % sizeof (uint32_t);
326+
ecma_extended_primitive_t *new_result_p = ecma_bigint_create (new_size - new_size_remainder);
327+
328+
new_result_p->u.bigint_sign_and_size += new_size_remainder;
329+
memcpy (new_result_p + 1, result_p + 1, new_size - new_size_remainder);
330+
331+
ecma_deref_bigint (result_p);
332+
ecma_deref_bigint (input_bigint_p);
333+
334+
return ecma_make_extended_primitive_value (new_result_p, ECMA_TYPE_BIGINT);
335+
}
336+
337+
result_p->u.bigint_sign_and_size = new_size;
338+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
339+
340+
ecma_deref_bigint (input_bigint_p);
341+
return ecma_make_extended_primitive_value (result_p, ECMA_TYPE_BIGINT);
342+
}
343+
344+
memcpy (result_p + 1, input_bigint_p + 1, exact_size);
345+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
346+
347+
if (input_bigint_p->u.bigint_sign_and_size & ECMA_BIGINT_SIGN)
348+
{
349+
ecma_deref_bigint (input_bigint_p);
350+
return ecma_bigint_negate (result_p);
351+
}
352+
353+
ecma_deref_bigint (input_bigint_p);
354+
return ecma_make_extended_primitive_value (result_p, ECMA_TYPE_BIGINT);
355+
} /* ecma_builtin_bigint_object_as_int_n */
356+
39357
/**
40358
* Handle calling [[Call]] of built-in BigInt object
41359
*
@@ -71,6 +389,38 @@ ecma_builtin_bigint_dispatch_construct (const ecma_value_t *arguments_list_p, /*
71389
return ecma_raise_type_error (ECMA_ERR_BIGINT_FUNCTION_NOT_CONSTRUCTOR);
72390
} /* ecma_builtin_bigint_dispatch_construct */
73391

392+
/**
393+
* Dispatcher of the built-in's routines
394+
*
395+
* @return ecma value
396+
* Returned value must be freed with ecma_free_value.
397+
*/
398+
ecma_value_t
399+
ecma_builtin_bigint_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
400+
ecma_value_t this_arg, /**< 'this' argument value */
401+
const ecma_value_t arguments_list_p[], /**< list of arguments
402+
* passed to routine */
403+
uint32_t arguments_number) /**< length of arguments' list */
404+
{
405+
JERRY_UNUSED_2 (this_arg, arguments_number);
406+
407+
switch (builtin_routine_id)
408+
{
409+
case ECMA_BUILTIN_BIGINT_AS_INT_N:
410+
{
411+
return ecma_builtin_bigint_object_as_int_n (arguments_list_p[0], arguments_list_p[1], true);
412+
}
413+
case ECMA_BUILTIN_BIGINT_AS_U_INT_N:
414+
{
415+
return ecma_builtin_bigint_object_as_int_n (arguments_list_p[0], arguments_list_p[1], false);
416+
}
417+
default:
418+
{
419+
JERRY_UNREACHABLE ();
420+
}
421+
}
422+
} /* ecma_builtin_bigint_dispatch_routine */
423+
74424
/**
75425
* @}
76426
* @}

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"

0 commit comments

Comments
 (0)