Repository navigation
Template literals convert substitutions too late (and look up String.prototype.concat) - #1811
Merged
Merged
Conversation
An untagged template literal was compiled as a call to String.prototype.concat on its first chunk. Replacing concat changed every template, and each substitution was converted only after all of them had been evaluated, so a later substitution could change what an earlier one printed. ECMA-262 (13.2.8.6) applies ToString to each substitution right after evaluating it. Emit OP_to_propkey and OP_add after each substitution and OP_add for each later chunk, instead of looking up concat. OP_to_propkey is ToString but keeps a symbol, which OP_add then rejects with a TypeError, as ToString does. Tagged templates are unchanged.
bnoordhuis
approved these changes
Oct 8, 2026
Contributor
There was a problem hiding this comment.
Seems fine to me. You can remove the concat atom from quickjs-atom.h, there are no other users anymore after your change.
edit: although that affects bytecode generation so you probably also have to bump BC_VERSION and run make codegen
edit2: this change affects codegen anyway, running make codegen is unavoidable.
The template literal change compiles substitutions with OP_to_propkey and OP_add instead of a call to String.prototype.concat, so the precompiled bytecode in gen/ and the builtin-iterator-zip headers changes with it.
Template literals no longer look up String.prototype.concat, so JS_ATOM_concat has no users left. Removing it renumbers the atoms that follow, so BC_VERSION goes to 29 and the bytecode in gen/ and the builtin headers is regenerated with make codegen.
Contributor
Author
|
Thanks for the review! Done: I removed the now-unused |
saghul
approved these changes
Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! First, thank you for QuickJS-ng. I run the same JavaScript on QuickJS-ng
and on V8 and compare the results, and I found a small difference in
template literals.
QuickJS-ng compiles
`a${x}b${y}`as"a".concat(x, "b", y). Two thingsfollow from that:
them were evaluated. The spec converts each one with ToString right after
evaluating it (ECMA-262, 13.2.8.6, step 4 before step 5).
String.prototype.concatis looked up at run time, so code that replacesit changes every template.
Repro (plain JS):
Mon Jan 15 2024toString,nexta1bMon Jan 15 2024toString,nexta1bSat Jan 15 2000next,toStringconcat was calledThe fix is small and only touches untagged templates in
js_parse_template:after each substitution it emits
OP_to_propkey+OP_add(ToString rightaway, with a TypeError for a symbol as before), and
OP_addfor each laterchunk, instead of
get_field2 concat+call_method. It also removes theold
XXX: should convert to string at this stage?comment, since thisanswers it. Tagged templates are unchanged.
I added
test_template_tostring_ordertotests/test_language.js. It failson master and passes with the change.
make testgives0/120 errors, andtest262's
template-literalandtagged-templatefolders still pass(
0/160 errors). As far as I can see, no test262 test covers this order; Ican propose one there too if that helps.
Happy to change anything you'd prefer done differently. Thanks again!