浏览代码

LibJS: Add a convenience helper for visiting a JS::Value

We only really care to visit values if they refer to a Cell, but it's
nice to be able to say visit(some_value).
Andreas Kling 5 年之前
父节点
当前提交
386867da9f
共有 3 个文件被更改,包括 12 次插入4 次删除
  1. 8 0
      Libraries/LibJS/Cell.cpp
  2. 2 0
      Libraries/LibJS/Cell.h
  3. 2 4
      Libraries/LibJS/Object.cpp

+ 8 - 0
Libraries/LibJS/Cell.cpp

@@ -26,9 +26,17 @@
 
 #include <AK/LogStream.h>
 #include <LibJS/Cell.h>
+#include <LibJS/Object.h>
+#include <LibJS/Value.h>
 
 namespace JS {
 
+void Cell::Visitor::visit(Value value)
+{
+    if (value.is_object())
+        visit(value.as_object());
+}
+
 const LogStream& operator<<(const LogStream& stream, const Cell* cell)
 {
     if (!cell)

+ 2 - 0
Libraries/LibJS/Cell.h

@@ -27,6 +27,7 @@
 #pragma once
 
 #include <AK/Forward.h>
+#include <LibJS/Forward.h>
 
 namespace JS {
 
@@ -45,6 +46,7 @@ public:
     class Visitor {
     public:
         virtual void visit(Cell*) = 0;
+        void visit(Value);
     };
 
     virtual void visit_children(Visitor&) {}

+ 2 - 4
Libraries/LibJS/Object.cpp

@@ -51,10 +51,8 @@ void Object::put(String property_name, Value value)
 void Object::visit_children(Cell::Visitor& visitor)
 {
     Cell::visit_children(visitor);
-    for (auto& it : m_properties) {
-        if (it.value.is_object())
-            visitor.visit(it.value.as_object());
-    }
+    for (auto& it : m_properties)
+        visitor.visit(it.value);
 }
 
 }