Pārlūkot izejas kodu

Added Globals in DebugInformation

Massimiliano Gentile 10 gadi atpakaļ
vecāks
revīzija
1db58da6bb

+ 4 - 3
Jint.Tests/Runtime/EngineTests.cs

@@ -1320,10 +1320,11 @@ namespace Jint.Tests.Runtime
 
             Assert.Equal(1, debugInfo.CallStack.Count);
             Assert.Equal("func1()", debugInfo.CallStack.Peek());
-            Assert.Contains(debugInfo.Locals, kvp => kvp.Key.Equals("global", StringComparison.Ordinal) && kvp.Value.AsBoolean() == true);
+            Assert.Contains(debugInfo.Globals, kvp => kvp.Key.Equals("global", StringComparison.Ordinal) && kvp.Value.AsBoolean() == true);
+            Assert.Contains(debugInfo.Globals, kvp => kvp.Key.Equals("local", StringComparison.Ordinal) && kvp.Value.AsBoolean() == false);
             Assert.Contains(debugInfo.Locals, kvp => kvp.Key.Equals("local", StringComparison.Ordinal) && kvp.Value.AsBoolean() == false);
-
-
+            Assert.DoesNotContain(debugInfo.Locals, kvp => kvp.Key.Equals("global", StringComparison.Ordinal));
+            
             countBreak++;
             return stepMode;
         }

+ 29 - 12
Jint/Runtime/Debugger/DebugHandler.cs

@@ -135,34 +135,51 @@ namespace Jint.Runtime.Debugger
 
             if (_engine.ExecutionContext != null && _engine.ExecutionContext.LexicalEnvironment != null)
             {
-                info.Locals = GetIdentifierReference(_engine.ExecutionContext.LexicalEnvironment);
+                var lexicalEnvironment = _engine.ExecutionContext.LexicalEnvironment;
+                info.Locals = GetLocalVariables(lexicalEnvironment);
+                info.Globals = GetGlobalVariables(lexicalEnvironment);
             }
 
             return info;
         }
 
-        public static Dictionary<string, JsValue> GetIdentifierReference(LexicalEnvironment lex)
+        private static Dictionary<string, JsValue> GetLocalVariables(LexicalEnvironment lex)
         {
             Dictionary<string, JsValue> locals = new Dictionary<string, JsValue>();
+            if (lex != null && lex.Record != null)
+            {
+                AddRecordsFromEnvironment(lex, locals);
+            }
+            return locals;
+        }
+
+        private static Dictionary<string, JsValue> GetGlobalVariables(LexicalEnvironment lex)
+        {
+            Dictionary<string, JsValue> globals = new Dictionary<string, JsValue>();
             LexicalEnvironment tempLex = lex;
 
             while (tempLex != null && tempLex.Record != null)
             {
-                var bindings = tempLex.Record.GetAllBindingNames();
-                foreach (var binding in bindings)
+                AddRecordsFromEnvironment(tempLex, globals);
+                tempLex = tempLex.Outer;
+            }
+            return globals;
+        }
+
+        private static void AddRecordsFromEnvironment(LexicalEnvironment lex, Dictionary<string, JsValue> locals)
+        {
+            var bindings = lex.Record.GetAllBindingNames();
+            foreach (var binding in bindings)
+            {
+                if (locals.ContainsKey(binding) == false)
                 {
-                    if (locals.ContainsKey(binding) == false)
+                    var jsValue = lex.Record.GetBindingValue(binding, false);
+                    if (jsValue.TryCast<ICallable>() == null)
                     {
-                        var jsValue = tempLex.Record.GetBindingValue(binding, false);
-                        if (jsValue.TryCast<ICallable>() == null)
-                        {
-                            locals.Add(binding, jsValue);
-                        }
+                        locals.Add(binding, jsValue);
                     }
                 }
-                tempLex = tempLex.Outer;
             }
-            return locals;
         }
     }
 }

+ 1 - 0
Jint/Runtime/Debugger/DebugInformation.cs

@@ -11,5 +11,6 @@ namespace Jint.Runtime.Debugger
         public Stack<String> CallStack { get; set; }
         public Statement CurrentStatement { get; set; }
         public Dictionary<string, JsValue> Locals { get; set; }
+        public Dictionary<string, JsValue> Globals { get; set; }
     }
 }