From b78f1c12617e7a79ad47df6c5df4a3e43700c537 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 9 Jun 2021 18:18:56 +0200 Subject: LibJS: Generate bytecode for throw statements --- Userland/Libraries/LibJS/AST.h | 1 + Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 7 +++++++ Userland/Libraries/LibJS/Bytecode/Instruction.h | 3 ++- Userland/Libraries/LibJS/Bytecode/Op.cpp | 11 +++++++++++ Userland/Libraries/LibJS/Bytecode/Op.h | 14 ++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) (limited to 'Userland/Libraries') 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 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 * Copyright (c) 2021, Linus Groh + * Copyright (c) 2021, Gunnar Beutner * * 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(); +} + } 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 * Copyright (c) 2021, Linus Groh + * Copyright (c) 2021, Gunnar Beutner * * 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 * Copyright (c) 2021, Linus Groh + * Copyright (c) 2021, Gunnar Beutner * * 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 { -- cgit v1.2.3