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
This commit is contained in:
Rafal Walczyna 2020-05-22 10:11:47 +02:00 committed by GitHub
parent 87b1d1eeb0
commit 5d6069176b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 18 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);