From 5d6069176b972d438d0c644cc5e4dc0b139120af Mon Sep 17 00:00:00 2001 From: Rafal Walczyna Date: Fri, 22 May 2020 10:11:47 +0200 Subject: [PATCH] Change raw string length calculation method (#3772) New method uses length of source to calculate raw string length. Also bug with template literal was fixed. Template object should have indexed properties enumerable. JerryScript-DCO-1.0-Signed-off-by: Rafal Walczyna r.walczyna@samsung.com --- jerry-core/parser/js/js-lexer.c | 27 +++++++++---------- .../js/js-parser-tagged-template-literal.c | 8 +++--- tests/jerry/es2015/tagged-template-literal.js | 11 ++++++++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index f7e9503df..d327fb321 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -915,7 +915,7 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ lexer_string_options_t opts) /**< options */ { #if ENABLED (JERRY_ES2015) - const size_t raw_length_inc = (opts & LEXER_STRING_RAW) ? 1 : 0; + size_t raw_length_dec = 0; #else /* ENABLED (JERRY_ES2015) */ JERRY_UNUSED (opts); #endif /* ENABLED (JERRY_ES2015) */ @@ -962,10 +962,6 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ continue; } -#if ENABLED (JERRY_ES2015) - length += raw_length_inc; -#endif /* ENABLED (JERRY_ES2015) */ - has_escape = true; /* Newline is ignored. */ @@ -975,13 +971,13 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ if (source_p < source_end_p && *source_p == LIT_CHAR_LF) { +#if ENABLED (JERRY_ES2015) + raw_length_dec++; +#endif /* ENABLED (JERRY_ES2015) */ source_p++; } line++; -#if ENABLED (JERRY_ES2015) - length += raw_length_inc; -#endif /* ENABLED (JERRY_ES2015) */ column = 1; continue; } @@ -989,18 +985,12 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ { source_p++; line++; -#if ENABLED (JERRY_ES2015) - length += raw_length_inc; -#endif /* ENABLED (JERRY_ES2015) */ column = 1; continue; } else if (*source_p == LEXER_NEWLINE_LS_PS_BYTE_1 && LEXER_NEWLINE_LS_PS_BYTE_23 (source_p)) { source_p += 3; -#if ENABLED (JERRY_ES2015) - length += 3 * raw_length_inc; -#endif /* ENABLED (JERRY_ES2015) */ line++; column = 1; continue; @@ -1131,6 +1121,7 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ source_p + 1 < source_end_p && source_p[1] == LIT_CHAR_LEFT_BRACE) { + raw_length_dec++; source_p++; break; } @@ -1167,6 +1158,7 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ && *source_p == LIT_CHAR_LF) { source_p++; + raw_length_dec++; } line++; column = 1; @@ -1211,6 +1203,13 @@ lexer_parse_string (parser_context_t *context_p, /**< context */ } } +#if ENABLED (JERRY_ES2015) + if (opts & LEXER_STRING_RAW) + { + length = (size_t) (source_p - string_start_p) - raw_length_dec; + } +#endif /* ENABLED (JERRY_ES2015) */ + if (length > PARSER_MAXIMUM_STRING_LENGTH) { parser_raise_error (context_p, PARSER_ERR_STRING_TOO_LONG); diff --git a/jerry-core/parser/js/js-parser-tagged-template-literal.c b/jerry-core/parser/js/js-parser-tagged-template-literal.c index b8251ef87..3ed96751e 100644 --- a/jerry-core/parser/js/js-parser-tagged-template-literal.c +++ b/jerry-core/parser/js/js-parser-tagged-template-literal.c @@ -48,12 +48,12 @@ parser_tagged_template_literal_append_strings (parser_context_t *context_p, /**< ecma_builtin_helper_def_prop_by_index (template_obj_p, prop_idx, ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY), - ECMA_PROPERTY_FIXED); + ECMA_PROPERTY_FLAG_ENUMERABLE); ecma_builtin_helper_def_prop_by_index (raw_strings_p, prop_idx, ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY), - ECMA_PROPERTY_FIXED); + ECMA_PROPERTY_FLAG_ENUMERABLE); return; } @@ -88,12 +88,12 @@ parser_tagged_template_literal_append_strings (parser_context_t *context_p, /**< ecma_builtin_helper_def_prop_by_index (template_obj_p, prop_idx, ecma_make_string_value (cooked_str_p), - ECMA_PROPERTY_FIXED); + ECMA_PROPERTY_FLAG_ENUMERABLE); ecma_builtin_helper_def_prop_by_index (raw_strings_p, prop_idx, ecma_make_string_value (raw_str_p), - ECMA_PROPERTY_FIXED); + ECMA_PROPERTY_FLAG_ENUMERABLE); ecma_deref_ecma_string (cooked_str_p); ecma_deref_ecma_string (raw_str_p); diff --git a/tests/jerry/es2015/tagged-template-literal.js b/tests/jerry/es2015/tagged-template-literal.js index 07d8b5867..a54154e83 100644 --- a/tests/jerry/es2015/tagged-template-literal.js +++ b/tests/jerry/es2015/tagged-template-literal.js @@ -126,3 +126,14 @@ assert (String.raw`Hi\n${2+3}!` === "Hi\\n5!"); var localNew = new getStr(); assert(chainedCall === getStr() && chainedCall === localNew); })(); + +var templateObject; + +(function(p) { + templateObject = p; +})`str`; + +var desc = Object.getOwnPropertyDescriptor(templateObject, '0'); +assert(desc.writable === false); +assert(desc.enumerable === true); +assert(desc.configurable === false);