diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-08 19:14:01 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-08 19:47:32 +0200 |
commit | 75a12bc2b7c70840eee6980db40f1b5094b1a903 (patch) | |
tree | b2118af2a2d1e2c268704f688509d00f00439ad0 /Userland | |
parent | 0b2bf3d73a7e842957bba66894761fddf7ff2c42 (diff) | |
download | serenity-75a12bc2b7c70840eee6980db40f1b5094b1a903.zip |
LibJS: Generate bytecode for template literals
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/AST.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 20 |
2 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index 9b84e6d74f..a2efbcdea9 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -1092,6 +1092,7 @@ public: virtual Value execute(Interpreter&, GlobalObject&) const override; virtual void dump(int indent) const override; + virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override; const NonnullRefPtrVector<Expression>& expressions() const { return m_expressions; } const NonnullRefPtrVector<Expression>& raw_strings() const { return m_raw_strings; } diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index bd8363e4de..5a26fa6cdf 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -478,4 +478,24 @@ Optional<Bytecode::Register> SequenceExpression::generate_bytecode(Bytecode::Gen return last_reg; } +Optional<Bytecode::Register> TemplateLiteral::generate_bytecode(Bytecode::Generator& generator) const +{ + Optional<Bytecode::Register> result_reg; + + for (auto& expression : m_expressions) { + auto expr_reg = expression.generate_bytecode(generator); + if (!result_reg.has_value()) + result_reg = expr_reg; + else + generator.emit<Bytecode::Op::Add>(*result_reg, *result_reg, *expr_reg); + } + + if (!result_reg.has_value()) { + result_reg = generator.allocate_register(); + generator.emit<Bytecode::Op::NewString>(*result_reg, ""); + } + + return result_reg; +} + } |