Skip to content

Commit 5d60691

Browse files
authored
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
1 parent 87b1d1e commit 5d60691

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

jerry-core/parser/js/js-lexer.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
915915
lexer_string_options_t opts) /**< options */
916916
{
917917
#if ENABLED (JERRY_ES2015)
918-
const size_t raw_length_inc = (opts & LEXER_STRING_RAW) ? 1 : 0;
918+
size_t raw_length_dec = 0;
919919
#else /* ENABLED (JERRY_ES2015) */
920920
JERRY_UNUSED (opts);
921921
#endif /* ENABLED (JERRY_ES2015) */
@@ -962,10 +962,6 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
962962
continue;
963963
}
964964

965-
#if ENABLED (JERRY_ES2015)
966-
length += raw_length_inc;
967-
#endif /* ENABLED (JERRY_ES2015) */
968-
969965
has_escape = true;
970966

971967
/* Newline is ignored. */
@@ -975,32 +971,26 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
975971
if (source_p < source_end_p
976972
&& *source_p == LIT_CHAR_LF)
977973
{
974+
#if ENABLED (JERRY_ES2015)
975+
raw_length_dec++;
976+
#endif /* ENABLED (JERRY_ES2015) */
978977
source_p++;
979978
}
980979

981980
line++;
982-
#if ENABLED (JERRY_ES2015)
983-
length += raw_length_inc;
984-
#endif /* ENABLED (JERRY_ES2015) */
985981
column = 1;
986982
continue;
987983
}
988984
else if (*source_p == LIT_CHAR_LF)
989985
{
990986
source_p++;
991987
line++;
992-
#if ENABLED (JERRY_ES2015)
993-
length += raw_length_inc;
994-
#endif /* ENABLED (JERRY_ES2015) */
995988
column = 1;
996989
continue;
997990
}
998991
else if (*source_p == LEXER_NEWLINE_LS_PS_BYTE_1 && LEXER_NEWLINE_LS_PS_BYTE_23 (source_p))
999992
{
1000993
source_p += 3;
1001-
#if ENABLED (JERRY_ES2015)
1002-
length += 3 * raw_length_inc;
1003-
#endif /* ENABLED (JERRY_ES2015) */
1004994
line++;
1005995
column = 1;
1006996
continue;
@@ -1131,6 +1121,7 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
11311121
source_p + 1 < source_end_p &&
11321122
source_p[1] == LIT_CHAR_LEFT_BRACE)
11331123
{
1124+
raw_length_dec++;
11341125
source_p++;
11351126
break;
11361127
}
@@ -1167,6 +1158,7 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
11671158
&& *source_p == LIT_CHAR_LF)
11681159
{
11691160
source_p++;
1161+
raw_length_dec++;
11701162
}
11711163
line++;
11721164
column = 1;
@@ -1211,6 +1203,13 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
12111203
}
12121204
}
12131205

1206+
#if ENABLED (JERRY_ES2015)
1207+
if (opts & LEXER_STRING_RAW)
1208+
{
1209+
length = (size_t) (source_p - string_start_p) - raw_length_dec;
1210+
}
1211+
#endif /* ENABLED (JERRY_ES2015) */
1212+
12141213
if (length > PARSER_MAXIMUM_STRING_LENGTH)
12151214
{
12161215
parser_raise_error (context_p, PARSER_ERR_STRING_TOO_LONG);

jerry-core/parser/js/js-parser-tagged-template-literal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ parser_tagged_template_literal_append_strings (parser_context_t *context_p, /**<
4848
ecma_builtin_helper_def_prop_by_index (template_obj_p,
4949
prop_idx,
5050
ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY),
51-
ECMA_PROPERTY_FIXED);
51+
ECMA_PROPERTY_FLAG_ENUMERABLE);
5252

5353
ecma_builtin_helper_def_prop_by_index (raw_strings_p,
5454
prop_idx,
5555
ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY),
56-
ECMA_PROPERTY_FIXED);
56+
ECMA_PROPERTY_FLAG_ENUMERABLE);
5757
return;
5858
}
5959

@@ -88,12 +88,12 @@ parser_tagged_template_literal_append_strings (parser_context_t *context_p, /**<
8888
ecma_builtin_helper_def_prop_by_index (template_obj_p,
8989
prop_idx,
9090
ecma_make_string_value (cooked_str_p),
91-
ECMA_PROPERTY_FIXED);
91+
ECMA_PROPERTY_FLAG_ENUMERABLE);
9292

9393
ecma_builtin_helper_def_prop_by_index (raw_strings_p,
9494
prop_idx,
9595
ecma_make_string_value (raw_str_p),
96-
ECMA_PROPERTY_FIXED);
96+
ECMA_PROPERTY_FLAG_ENUMERABLE);
9797

9898
ecma_deref_ecma_string (cooked_str_p);
9999
ecma_deref_ecma_string (raw_str_p);

tests/jerry/es2015/tagged-template-literal.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,14 @@ assert (String.raw`Hi\n${2+3}!` === "Hi\\n5!");
126126
var localNew = new getStr();
127127
assert(chainedCall === getStr() && chainedCall === localNew);
128128
})();
129+
130+
var templateObject;
131+
132+
(function(p) {
133+
templateObject = p;
134+
})`str`;
135+
136+
var desc = Object.getOwnPropertyDescriptor(templateObject, '0');
137+
assert(desc.writable === false);
138+
assert(desc.enumerable === true);
139+
assert(desc.configurable === false);

0 commit comments

Comments
 (0)