Browse Source

LibJS: Add VM::dump_scope_chain()

This is a handy helper that dumps the current scope chain, starting at
the innermost scope.
Andreas Kling 4 years ago
parent
commit
0d2aba07aa
2 changed files with 14 additions and 0 deletions
  1. 13 0
      Userland/Libraries/LibJS/Runtime/VM.cpp
  2. 1 0
      Userland/Libraries/LibJS/Runtime/VM.h

+ 13 - 0
Userland/Libraries/LibJS/Runtime/VM.cpp

@@ -622,4 +622,17 @@ void VM::dump_backtrace() const
         dbgln("-> {}", m_call_stack[i]->function_name);
         dbgln("-> {}", m_call_stack[i]->function_name);
 }
 }
 
 
+void VM::dump_scope_chain() const
+{
+    for (auto* scope = current_scope(); scope; scope = scope->parent()) {
+        dbgln("+> {} ({:p})", scope->class_name(), scope);
+        if (is<LexicalEnvironment>(*scope)) {
+            auto& lexical_environment = static_cast<LexicalEnvironment const&>(*scope);
+            for (auto& variable : lexical_environment.variables()) {
+                dbgln("    {}", variable.key);
+            }
+        }
+    }
+}
+
 }
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/VM.h

@@ -72,6 +72,7 @@ public:
     void clear_exception() { m_exception = nullptr; }
     void clear_exception() { m_exception = nullptr; }
 
 
     void dump_backtrace() const;
     void dump_backtrace() const;
+    void dump_scope_chain() const;
 
 
     class InterpreterExecutionScope {
     class InterpreterExecutionScope {
     public:
     public: