diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-09 18:18:56 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-09 20:04:11 +0200 |
commit | b78f1c12617e7a79ad47df6c5df4a3e43700c537 (patch) | |
tree | 7185ea329304558453618da76a8dc100f21e0329 /Userland/Libraries | |
parent | e9bafd768d66be41b4e50d64478e683c04a9fc09 (diff) | |
download | serenity-b78f1c12617e7a79ad47df6c5df4a3e43700c537.zip |
LibJS: Generate bytecode for throw statements
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/AST.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Instruction.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Op.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Op.h | 14 |
5 files changed, 35 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index 76176fb771..18c8a0b130 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -1245,6 +1245,7 @@ public: virtual void dump(int indent) const override; virtual Value execute(Interpreter&, GlobalObject&) const override; + virtual void generate_bytecode(Bytecode::Generator&) const override; private: NonnullRefPtr<Expression> m_argument; diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 866a143136..1db9b3f5e8 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -668,4 +669,10 @@ void UpdateExpression::generate_bytecode(Bytecode::Generator& generator) const TODO(); } +void ThrowStatement::generate_bytecode(Bytecode::Generator& generator) const +{ + m_argument->generate_bytecode(generator); + generator.emit<Bytecode::Op::Throw>(); +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/Instruction.h b/Userland/Libraries/LibJS/Bytecode/Instruction.h index 9b67fa096c..b52a712123 100644 --- a/Userland/Libraries/LibJS/Bytecode/Instruction.h +++ b/Userland/Libraries/LibJS/Bytecode/Instruction.h @@ -56,7 +56,8 @@ O(InstanceOf) \ O(ConcatString) \ O(Increment) \ - O(Decrement) + O(Decrement) \ + O(Throw) namespace JS::Bytecode { diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index cea771567f..cdd26dd7ba 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -255,6 +256,11 @@ void Decrement::execute(Bytecode::Interpreter& interpreter) const interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })); } +void Throw::execute(Bytecode::Interpreter& interpreter) const +{ + interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator()); +} + String Load::to_string(Bytecode::Executable const&) const { return String::formatted("Load {}", m_src); @@ -383,4 +389,9 @@ String Decrement::to_string(Bytecode::Executable const&) const return "Decrement"; } +String Throw::to_string(Bytecode::Executable const&) const +{ + return "Throw"; +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index 7398724afc..8893bf925e 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -395,6 +396,19 @@ public: String to_string(Bytecode::Executable const&) const; }; +class Throw final : public Instruction { +public: + constexpr static bool IsTerminator = true; + + Throw() + : Instruction(Type::Throw) + { + } + + void execute(Bytecode::Interpreter&) const; + String to_string(Bytecode::Executable const&) const; +}; + } namespace JS::Bytecode { |