Implement BigInt literal parsing in the parser. (#4089)

Support octal literals for BigInts.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-08-04 14:16:38 +02:00
committed by GitHub
parent be9dbeffdb
commit b5a96c9eca
16 changed files with 481 additions and 215 deletions
+83 -28
View File
@@ -15,6 +15,9 @@
#include "common.h"
#include "ecma-helpers.h"
#include "ecma-big-uint.h"
#include "ecma-bigint.h"
#include "lit-char-helpers.h"
#if ENABLED (JERRY_PARSER)
@@ -77,41 +80,93 @@ util_print_number (ecma_number_t num_p) /**< number to print */
JERRY_DEBUG_MSG ("%s", str_buf);
} /* util_print_number */
#if ENABLED (JERRY_BUILTIN_BIGINT)
/**
* Debug utility to print a bigint.
*/
static void
util_print_bigint (ecma_value_t bigint) /**< bigint to print */
{
ecma_extended_primitive_t *bigint_p = ecma_get_extended_primitive_from_value (bigint);
if (ECMA_BIGINT_GET_SIZE (bigint_p) == 0)
{
JERRY_DEBUG_MSG ("0");
return;
}
uint32_t char_start_p, char_size_p;
lit_utf8_byte_t *string_buffer_p = ecma_big_uint_to_string (bigint_p, 10, &char_start_p, &char_size_p);
if (JERRY_UNLIKELY (string_buffer_p == NULL))
{
JERRY_DEBUG_MSG ("<out-of-memory>");
return;
}
JERRY_ASSERT (char_start_p > 0);
if (bigint_p->u.bigint_sign_and_size & ECMA_BIGINT_SIGN)
{
string_buffer_p[--char_start_p] = LIT_CHAR_MINUS;
}
util_print_chars (string_buffer_p + char_start_p, char_size_p - char_start_p);
jmem_heap_free_block (string_buffer_p, char_size_p);
} /* util_print_bigint */
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
/**
* Print literal.
*/
void
util_print_literal (lexer_literal_t *literal_p) /**< literal */
{
if (literal_p->type == LEXER_IDENT_LITERAL)
switch (literal_p->type)
{
JERRY_DEBUG_MSG ("ident(");
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
}
else if (literal_p->type == LEXER_FUNCTION_LITERAL)
{
JERRY_DEBUG_MSG ("function");
return;
}
else if (literal_p->type == LEXER_STRING_LITERAL)
{
JERRY_DEBUG_MSG ("string(");
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
}
else if (literal_p->type == LEXER_NUMBER_LITERAL)
{
JERRY_DEBUG_MSG ("number(");
util_print_number (ecma_get_number_from_value (literal_p->u.value));
}
else if (literal_p->type == LEXER_REGEXP_LITERAL)
{
JERRY_DEBUG_MSG ("regexp");
return;
}
else
{
JERRY_DEBUG_MSG ("unknown");
return;
case LEXER_IDENT_LITERAL:
{
JERRY_DEBUG_MSG ("ident(");
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
break;
}
case LEXER_FUNCTION_LITERAL:
{
JERRY_DEBUG_MSG ("function");
return;
}
case LEXER_STRING_LITERAL:
{
JERRY_DEBUG_MSG ("string(");
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
break;
}
case LEXER_NUMBER_LITERAL:
{
#if ENABLED (JERRY_BUILTIN_BIGINT)
if (ecma_is_value_bigint (literal_p->u.value))
{
JERRY_DEBUG_MSG ("bigint(");
util_print_bigint (literal_p->u.value);
break;
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
JERRY_DEBUG_MSG ("number(");
util_print_number (ecma_get_number_from_value (literal_p->u.value));
break;
}
case LEXER_REGEXP_LITERAL:
{
JERRY_DEBUG_MSG ("regexp");
return;
}
default:
{
JERRY_DEBUG_MSG ("unknown");
return;
}
}
JERRY_DEBUG_MSG (")");
+98 -38
View File
@@ -14,6 +14,7 @@
*/
#include "ecma-alloc.h"
#include "ecma-bigint.h"
#include "ecma-helpers.h"
#include "ecma-function-object.h"
#include "ecma-literal-storage.h"
@@ -1303,6 +1304,9 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
const uint8_t *source_p = context_p->source_p;
const uint8_t *source_end_p = context_p->source_end_p;
bool can_be_float = false;
#if ENABLED (JERRY_BUILTIN_BIGINT)
bool can_be_bigint = true;
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
size_t length;
context_p->token.type = LEXER_LITERAL;
@@ -1337,8 +1341,6 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
else if (LEXER_TO_ASCII_LOWERCASE (source_p[1]) == LIT_CHAR_LOWERCASE_O)
{
context_p->token.extra_value = LEXER_NUMBER_OCTAL;
context_p->token.lit_location.char_p++;
context_p->source_p++;
source_p += 2;
if (source_p >= source_end_p
@@ -1354,6 +1356,9 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
&& source_p[1] <= LIT_CHAR_7)
{
context_p->token.extra_value = LEXER_NUMBER_OCTAL;
#if ENABLED (JERRY_BUILTIN_BIGINT)
can_be_bigint = false;
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
if (context_p->status_flags & PARSER_IS_STRICT)
{
@@ -1371,8 +1376,6 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
else if (LEXER_TO_ASCII_LOWERCASE (source_p[1]) == LIT_CHAR_LOWERCASE_B)
{
context_p->token.extra_value = LEXER_NUMBER_BINARY;
context_p->token.lit_location.char_p++;
context_p->source_p++;
source_p += 2;
if (source_p >= source_end_p
@@ -1413,6 +1416,10 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
&& source_p[0] == LIT_CHAR_DOT)
{
source_p++;
#if ENABLED (JERRY_BUILTIN_BIGINT)
can_be_bigint = false;
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
while (source_p < source_end_p
&& source_p[0] >= LIT_CHAR_0
&& source_p[0] <= LIT_CHAR_9)
@@ -1425,6 +1432,9 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
&& LEXER_TO_ASCII_LOWERCASE (source_p[0]) == LIT_CHAR_LOWERCASE_E)
{
source_p++;
#if ENABLED (JERRY_BUILTIN_BIGINT)
can_be_bigint = false;
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
if (source_p < source_end_p
&& (source_p[0] == LIT_CHAR_PLUS || source_p[0] == LIT_CHAR_MINUS))
@@ -1449,6 +1459,18 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
}
}
#if ENABLED (JERRY_BUILTIN_BIGINT)
if (source_p < source_end_p && source_p[0] == LIT_CHAR_LOWERCASE_N)
{
if (!can_be_bigint)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_BIGINT);
}
context_p->token.extra_value = LEXER_NUMBER_BIGINT;
source_p++;
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
length = (size_t) (source_p - context_p->source_p);
if (length > PARSER_MAXIMUM_IDENT_LENGTH)
{
@@ -2515,56 +2537,94 @@ lexer_construct_number_object (parser_context_t *context_p, /**< context */
{
parser_list_iterator_t literal_iterator;
lexer_literal_t *literal_p;
ecma_value_t lit_value;
ecma_number_t num;
uint32_t literal_index = 0;
prop_length_t length = context_p->token.lit_location.length;
if (context_p->token.extra_value < LEXER_NUMBER_OCTAL)
#if ENABLED (JERRY_BUILTIN_BIGINT)
if (JERRY_LIKELY (context_p->token.extra_value != LEXER_NUMBER_BIGINT))
{
num = ecma_utf8_string_to_number (context_p->token.lit_location.char_p,
length);
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
if (context_p->token.extra_value < LEXER_NUMBER_OCTAL)
{
num = ecma_utf8_string_to_number (context_p->token.lit_location.char_p,
length);
}
else
{
const uint8_t *src_p = context_p->token.lit_location.char_p;
const uint8_t *src_end_p = src_p + length - 1;
ecma_number_t multiplier = 8.0;
JERRY_ASSERT (src_p[0] == LIT_CHAR_0);
#if ENABLED (JERRY_ESNEXT)
if (context_p->token.extra_value == LEXER_NUMBER_BINARY)
{
src_p++;
multiplier = 2.0;
}
else if (LEXER_TO_ASCII_LOWERCASE (src_p[1]) == LIT_CHAR_LOWERCASE_O)
{
src_p++;
}
#endif /* ENABLED (JERRY_ESNEXT) */
num = 0;
do
{
num = num * multiplier + (ecma_number_t) (*(++src_p) - LIT_CHAR_0);
}
while (src_p < src_end_p);
}
if (is_expr)
{
int32_t int_num = (int32_t) num;
if (int_num == num
&& int_num <= CBC_PUSH_NUMBER_BYTE_RANGE_END
&& (int_num != 0 || !is_negative_number))
{
context_p->lit_object.index = (uint16_t) int_num;
return true;
}
}
if (is_negative_number)
{
num = -num;
}
lit_value = ecma_find_or_create_literal_number (num);
#if ENABLED (JERRY_BUILTIN_BIGINT)
}
else
{
const uint8_t *src_p = context_p->token.lit_location.char_p;
const uint8_t *src_end_p = src_p + length - 1;
ecma_number_t multiplier = 8.0;
uint32_t options = ECMA_BIGINT_PARSE_DISALLOW_SYNTAX_ERROR | ECMA_BIGINT_PARSE_DISALLOW_MEMORY_ERROR;
#if ENABLED (JERRY_ESNEXT)
if (context_p->token.extra_value == LEXER_NUMBER_BINARY)
if (is_negative_number)
{
multiplier = 2.0;
options |= ECMA_BIGINT_PARSE_SET_NEGATIVE;
}
#endif /* ENABLED (JERRY_ESNEXT) */
num = 0;
do
JERRY_ASSERT (length >= 2);
lit_value = ecma_bigint_parse_string (context_p->token.lit_location.char_p,
(lit_utf8_size_t) (length - 1),
options);
JERRY_ASSERT (lit_value != ECMA_VALUE_FALSE && !ECMA_IS_VALUE_ERROR (lit_value));
if (lit_value == ECMA_VALUE_NULL)
{
src_p++;
num = num * multiplier + (ecma_number_t) (*src_p - LIT_CHAR_0);
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
}
while (src_p < src_end_p);
lit_value = ecma_find_or_create_literal_bigint (lit_value);
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
if (is_expr)
{
int32_t int_num = (int32_t) num;
if (int_num == num
&& int_num <= CBC_PUSH_NUMBER_BYTE_RANGE_END
&& (int_num != 0 || !is_negative_number))
{
context_p->lit_object.index = (uint16_t) int_num;
return true;
}
}
if (is_negative_number)
{
num = -num;
}
ecma_value_t lit_value = ecma_find_or_create_literal_number (num);
parser_list_iterator_init (&context_p->literal_pool, &literal_iterator);
while ((literal_p = (lexer_literal_t *) parser_list_iterator_next (&literal_iterator)) != NULL)
+3
View File
@@ -279,6 +279,9 @@ typedef enum
LEXER_NUMBER_HEXADECIMAL, /**< hexadecimal number */
LEXER_NUMBER_OCTAL, /**< octal number */
LEXER_NUMBER_BINARY, /**< binary number */
#if ENABLED (JERRY_BUILTIN_BIGINT)
LEXER_NUMBER_BIGINT, /**< bigint number */
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
} lexer_number_type_t;
/**
+6
View File
@@ -869,6 +869,12 @@ parser_error_to_string (parser_error_t error) /**< error code */
{
return "Identifier cannot start after a number.";
}
#if ENABLED (JERRY_BUILTIN_BIGINT)
case PARSER_ERR_INVALID_BIGINT:
{
return "Number is not a valid BigInt.";
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
case PARSER_ERR_INVALID_REGEXP:
{
return "Invalid regular expression.";
+3
View File
@@ -56,6 +56,9 @@ typedef enum
PARSER_ERR_INVALID_NUMBER, /**< invalid number literal */
PARSER_ERR_MISSING_EXPONENT, /**< missing exponent */
PARSER_ERR_IDENTIFIER_AFTER_NUMBER, /**< identifier start after number */
#if ENABLED (JERRY_BUILTIN_BIGINT)
PARSER_ERR_INVALID_BIGINT, /**< number is not a valid BigInt */
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
PARSER_ERR_INVALID_REGEXP, /**< invalid regular expression */
PARSER_ERR_UNKNOWN_REGEXP_FLAG, /**< unknown regexp flag */