浏览代码

LibJS: Add some helpers and use them to re-implement Console functions

Also add const overloads for some getters.

Also const-qualify Interpreter::join_arguments().
Emanuele Torre 5 年之前
父节点
当前提交
bc7ed4524e
共有 4 个文件被更改,包括 46 次插入20 次删除
  1. 28 18
      Libraries/LibJS/Console.cpp
  2. 5 0
      Libraries/LibJS/Console.h
  3. 10 1
      Libraries/LibJS/Interpreter.cpp
  4. 3 1
      Libraries/LibJS/Interpreter.h

+ 28 - 18
Libraries/LibJS/Console.cpp

@@ -78,10 +78,8 @@ Value Console::trace()
     StringBuilder message_text;
     StringBuilder message_text;
     message_text.append(m_interpreter.join_arguments());
     message_text.append(m_interpreter.join_arguments());
 
 
-    auto call_stack = m_interpreter.call_stack();
-    // -2 to skip the console.trace() call frame
-    for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
-        auto function_name = call_stack[i].function_name;
+    auto trace = m_interpreter.get_trace();
+    for (auto function_name : trace) {
         message_text.append("\n      -> ");
         message_text.append("\n      -> ");
         if (String(function_name).is_empty())
         if (String(function_name).is_empty())
             function_name = "<anonymous>";
             function_name = "<anonymous>";
@@ -96,16 +94,8 @@ Value Console::count()
 {
 {
     auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
     auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
 
 
-    auto counter_value = m_counters.get(label);
-    if (!counter_value.has_value()) {
-        dbg() << "log: " << label << ": 1";
-        m_counters.set(label, 1);
-        return js_undefined();
-    }
-
-    auto new_counter_value = counter_value.value() + 1;
-    dbg() << "log: " << label << ": " << new_counter_value;
-    m_counters.set(label, new_counter_value);
+    auto counter_value = counter_increment(label);
+    dbg() << "log: " << label << ": " << counter_value;
 
 
     return js_undefined();
     return js_undefined();
 }
 }
@@ -114,14 +104,34 @@ Value Console::count_reset()
 {
 {
     auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
     auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
 
 
-    if (m_counters.contains(label)) {
+    if (counter_reset(label))
+        dbg() << "log: " << label << ": 0";
+    else
         dbg() << "warn: \"" << label << "\" doesn't have a count";
         dbg() << "warn: \"" << label << "\" doesn't have a count";
-        return js_undefined();
+
+    return js_undefined();
+}
+
+unsigned Console::counter_increment(String label)
+{
+    auto value = m_counters.get(label);
+    if (!value.has_value()) {
+        m_counters.set(label, 1);
+        return 1;
     }
     }
 
 
+    auto new_value = value.value() + 1;
+    m_counters.set(label, new_value);
+    return new_value;
+}
+
+bool Console::counter_reset(String label)
+{
+    if (!m_counters.contains(label))
+        return false;
+
     m_counters.remove(label);
     m_counters.remove(label);
-    dbg() << "log: " << label << ": 0";
-    return js_undefined();
+    return true;
 }
 }
 
 
 }
 }

+ 5 - 0
Libraries/LibJS/Console.h

@@ -41,8 +41,10 @@ public:
     Console(Interpreter&);
     Console(Interpreter&);
 
 
     Interpreter& interpreter() { return m_interpreter; }
     Interpreter& interpreter() { return m_interpreter; }
+    const Interpreter& interpreter() const { return m_interpreter; }
 
 
     HashMap<String, unsigned>& counters() { return m_counters; }
     HashMap<String, unsigned>& counters() { return m_counters; }
+    const HashMap<String, unsigned>& counters() const { return m_counters; }
 
 
     Value debug();
     Value debug();
     Value error();
     Value error();
@@ -57,6 +59,9 @@ public:
     Value count();
     Value count();
     Value count_reset();
     Value count_reset();
 
 
+    unsigned counter_increment(String label);
+    bool counter_reset(String label);
+
 private:
 private:
     Interpreter& m_interpreter;
     Interpreter& m_interpreter;
 
 

+ 10 - 1
Libraries/LibJS/Interpreter.cpp

@@ -262,7 +262,7 @@ const GlobalObject& Interpreter::global_object() const
     return static_cast<const GlobalObject&>(*m_global_object);
     return static_cast<const GlobalObject&>(*m_global_object);
 }
 }
 
 
-String Interpreter::join_arguments()
+String Interpreter::join_arguments() const
 {
 {
     StringBuilder joined_arguments;
     StringBuilder joined_arguments;
     for (size_t i = 0; i < argument_count(); ++i) {
     for (size_t i = 0; i < argument_count(); ++i) {
@@ -273,4 +273,13 @@ String Interpreter::join_arguments()
     return joined_arguments.build();
     return joined_arguments.build();
 }
 }
 
 
+Vector<String> Interpreter::get_trace() const
+{
+    Vector<String> trace;
+    // -2 to skip the console.trace() call frame
+    for (ssize_t i = m_call_stack.size() - 2; i >= 0; --i)
+        trace.append(m_call_stack[i].function_name);
+    return trace;
+}
+
 }
 }

+ 3 - 1
Libraries/LibJS/Interpreter.h

@@ -165,8 +165,10 @@ public:
     Value last_value() const { return m_last_value; }
     Value last_value() const { return m_last_value; }
 
 
     Console& console() { return m_console; }
     Console& console() { return m_console; }
+    const Console& console() const { return m_console; }
 
 
-    String join_arguments();
+    String join_arguments() const;
+    Vector<String> get_trace() const;
 
 
 private:
 private:
     Interpreter();
     Interpreter();