瀏覽代碼

LibJS: Add a NewObject bytecode instruction for ObjectExpression :^)

Andreas Kling 4 年之前
父節點
當前提交
bea6e31ddc

+ 1 - 0
Userland/Libraries/LibJS/AST.h

@@ -1040,6 +1040,7 @@ public:
 
 
     virtual Value execute(Interpreter&, GlobalObject&) const override;
     virtual Value execute(Interpreter&, GlobalObject&) const override;
     virtual void dump(int indent) const override;
     virtual void dump(int indent) const override;
+    virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
 
 
 private:
 private:
     NonnullRefPtrVector<ObjectProperty> m_properties;
     NonnullRefPtrVector<ObjectProperty> m_properties;

+ 12 - 0
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

@@ -115,4 +115,16 @@ Optional<Bytecode::Register> DoWhileStatement::generate_bytecode(Bytecode::Gener
     return body_result_reg;
     return body_result_reg;
 }
 }
 
 
+Optional<Bytecode::Register> ObjectExpression::generate_bytecode(Bytecode::Generator& generator) const
+{
+    auto reg = generator.allocate_register();
+    generator.emit<Bytecode::Op::NewObject>(reg);
+
+    if (!m_properties.is_empty()) {
+        TODO();
+    }
+
+    return reg;
+}
+
 }
 }

+ 10 - 0
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);
     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
 void GetVariable::execute(Bytecode::Interpreter& interpreter) const
 {
 {
     interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
     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);
     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
 String GetVariable::to_string() const
 {
 {
     return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier);
     return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier);

+ 15 - 0
Userland/Libraries/LibJS/Bytecode/Op.h

@@ -125,6 +125,21 @@ private:
     String m_string;
     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 {
 class SetVariable final : public Instruction {
 public:
 public:
     SetVariable(FlyString identifier, Register src)
     SetVariable(FlyString identifier, Register src)