Fixed template literal from producing byte-strings.
Previously, as a side effect of creating a key for the values hash a
byte-string was created. This byte-string was reused internally and
might appear in template literal. As a result a byte-string was
produced as a value for a template literal. Byte-strings are obsolete
and are scheduled for removal because they can cause issues with
internal routines not prepared for them.
diff --git a/src/njs_generator.c b/src/njs_generator.c
index db10892..6ba3c06 100644
--- a/src/njs_generator.c
+++ b/src/njs_generator.c
@@ -4756,6 +4756,7 @@
njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_bool_t exception)
{
+ ssize_t length;
njs_int_t ret;
njs_index_t index;
njs_value_t property;
@@ -4783,8 +4784,13 @@
return NJS_ERROR;
}
- ret = njs_string_set(vm, &property, lex_entry->name.start,
- lex_entry->name.length);
+ length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
+ if (njs_slow_path(length < 0)) {
+ return NJS_ERROR;
+ }
+
+ ret = njs_string_new(vm, &property, lex_entry->name.start,
+ lex_entry->name.length, length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c
index e700676..9c19745 100644
--- a/src/test/njs_unit_test.c
+++ b/src/test/njs_unit_test.c
@@ -7174,6 +7174,10 @@
{ njs_str("`\\${a}bc"),
njs_str("SyntaxError: Unterminated template literal in 1") },
+ { njs_str("var v = undefined; var u8 = 'α';"
+ "[`undefined${u8}`.length, `undefineQ${u8}`.length]"),
+ njs_str("10,10") },
+
{ njs_str("`text1\ntext2`;"),
njs_str("text1\ntext2") },