From bea6e31ddcc85936eb920fc2ec0326b5e06ff65b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 4 Jun 2021 20:30:23 +0200 Subject: LibJS: Add a NewObject bytecode instruction for ObjectExpression :^) --- Userland/Libraries/LibJS/AST.h | 1 + Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 12 ++++++++++++ Userland/Libraries/LibJS/Bytecode/Op.cpp | 10 ++++++++++ Userland/Libraries/LibJS/Bytecode/Op.h | 15 +++++++++++++++ 4 files changed, 38 insertions(+) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index 8a7c2cf1a9..b7a2d72e1e 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -1040,6 +1040,7 @@ public: virtual Value execute(Interpreter&, GlobalObject&) const override; virtual void dump(int indent) const override; + virtual Optional generate_bytecode(Bytecode::Generator&) const override; private: NonnullRefPtrVector m_properties; diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index b515870349..d009fdce40 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -115,4 +115,16 @@ Optional DoWhileStatement::generate_bytecode(Bytecode::Gener return body_result_reg; } +Optional ObjectExpression::generate_bytecode(Bytecode::Generator& generator) const +{ + auto reg = generator.allocate_register(); + generator.emit(reg); + + if (!m_properties.is_empty()) { + TODO(); + } + + return reg; +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 51eba805ce..bff5b53347 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -41,6 +41,11 @@ void NewString::execute(Bytecode::Interpreter& interpreter) const interpreter.reg(m_dst) = js_string(interpreter.vm(), m_string); } +void NewObject::execute(Bytecode::Interpreter& interpreter) const +{ + interpreter.reg(m_dst) = Object::create_empty(interpreter.global_object()); +} + void GetVariable::execute(Bytecode::Interpreter& interpreter) const { interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object()); @@ -102,6 +107,11 @@ String NewString::to_string() const return String::formatted("NewString dst:{}, string:\"{}\"", m_dst, m_string); } +String NewObject::to_string() const +{ + return String::formatted("NewObject dst:{}", m_dst); +} + String GetVariable::to_string() const { return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier); diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index 7b3c38bc17..a7fe80d306 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -125,6 +125,21 @@ private: String m_string; }; +class NewObject final : public Instruction { +public: + explicit NewObject(Register dst) + : m_dst(dst) + { + } + + virtual ~NewObject() override { } + virtual void execute(Bytecode::Interpreter&) const override; + virtual String to_string() const override; + +private: + Register m_dst; +}; + class SetVariable final : public Instruction { public: SetVariable(FlyString identifier, Register src) -- cgit v1.2.3