Skip to content

Commit a4659a8

Browse files
authored
Add support of escaping backslash and backtick to String.raw (#3736)
Template literals already supports escaping backslash and backtick, this commit enables escaping this in String.raw. It also fixes invalid behaviour of String.raw when using new line character. JerryScript-DCO-1.0-Signed-off-by: Rafal Walczyna [email protected]
1 parent bbb5c91 commit a4659a8

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,9 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
998998
else if (*source_p == LEXER_NEWLINE_LS_PS_BYTE_1 && LEXER_NEWLINE_LS_PS_BYTE_23 (source_p))
999999
{
10001000
source_p += 3;
1001+
#if ENABLED (JERRY_ES2015)
1002+
length += 3 * raw_length_inc;
1003+
#endif /* ENABLED (JERRY_ES2015) */
10011004
line++;
10021005
column = 1;
10031006
continue;
@@ -1006,6 +1009,12 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
10061009
#if ENABLED (JERRY_ES2015)
10071010
if (opts & LEXER_STRING_RAW)
10081011
{
1012+
if ((*source_p == LIT_CHAR_GRAVE_ACCENT) || (*source_p == LIT_CHAR_BACKSLASH))
1013+
{
1014+
source_p++;
1015+
column++;
1016+
length++;
1017+
}
10091018
continue;
10101019
}
10111020
#endif /* ENABLED (JERRY_ES2015) */
@@ -2281,6 +2290,16 @@ lexer_convert_literal_to_chars (parser_context_t *context_p, /**< context */
22812290
}
22822291
continue;
22832292
}
2293+
if ((*source_p == LIT_CHAR_BACKSLASH) && is_raw)
2294+
{
2295+
JERRY_ASSERT (source_p + 1 < context_p->source_end_p);
2296+
if ((*(source_p + 1) == LIT_CHAR_GRAVE_ACCENT) || (*(source_p + 1) == LIT_CHAR_BACKSLASH))
2297+
{
2298+
*destination_p++ = *source_p++;
2299+
*destination_p++ = *source_p++;
2300+
continue;
2301+
}
2302+
}
22842303
}
22852304
#endif /* ENABLED (JERRY_ES2015) */
22862305

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ parser_tagged_template_literal_append_strings (parser_context_t *context_p, /**<
4343
{
4444
lexer_lit_location_t *lit_loc_p = &context_p->token.lit_location;
4545

46-
if (lit_loc_p->length == 0)
46+
if (lit_loc_p->length == 0 && !lit_loc_p->has_escape)
4747
{
4848
ecma_builtin_helper_def_prop_by_index (template_obj_p,
4949
prop_idx,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/* This test must be left unchanged - testing JerryScript crash */
17+
18+
try
19+
{
20+
var s = 'print(String.raw`\\`)\n// `';
21+
eval (s)
22+
asserts(false)
23+
}
24+
catch (error)
25+
{
26+
}

tests/jerry/es2015/string-raw.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ var raw = new Proxy({length: 2, 0: '', 1: ''}, { get: function(o, k) { get.push(
6262
var p = new Proxy({raw: raw}, { get: function(o, k) { get.push(k); return o[k]; }});
6363
String.raw(p);
6464
assert(get + '' === "raw,length,0,1");
65+
66+
assert(String.raw`\\` == "\\\\")
67+
assert(String.raw`\`` == "\\`")
68+
assert(String.raw`\
69+
\
70+
` == "\\\n\\\n")
71+
assert(String.raw`\
` == "\\\u2029")

0 commit comments

Comments
 (0)