浏览代码

Add current memory usage to debug information (#705)

Gabriel Francisco 5 年之前
父节点
当前提交
f2a923000d
共有 3 个文件被更改,包括 13 次插入5 次删除
  1. 5 3
      Jint/Engine.cs
  2. 6 1
      Jint/Runtime/Debugger/DebugHandler.cs
  3. 2 1
      Jint/Runtime/Debugger/DebugInformation.cs

+ 5 - 3
Jint/Engine.cs

@@ -279,6 +279,8 @@ namespace Jint
 
         public GlobalSymbolRegistry GlobalSymbolRegistry { get; }
 
+        internal long CurrentMemoryUsage { get; private set; }
+
         internal Options Options { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
 
         #region Debugger
@@ -464,10 +466,10 @@ namespace Jint
             {
                 if (GetAllocatedBytesForCurrentThread != null)
                 {
-                    var memoryUsage = GetAllocatedBytesForCurrentThread() - _initialMemoryUsage;
-                    if (memoryUsage > _memoryLimit)
+                    CurrentMemoryUsage = GetAllocatedBytesForCurrentThread() - _initialMemoryUsage;
+                    if (CurrentMemoryUsage > _memoryLimit)
                     {
-                        ExceptionHelper.ThrowMemoryLimitExceededException($"Script has allocated {memoryUsage} but is limited to {_memoryLimit}");
+                        ExceptionHelper.ThrowMemoryLimitExceededException($"Script has allocated {CurrentMemoryUsage} but is limited to {_memoryLimit}");
                     }
                 }
                 else

+ 6 - 1
Jint/Runtime/Debugger/DebugHandler.cs

@@ -145,7 +145,12 @@ namespace Jint.Runtime.Debugger
 
         private DebugInformation CreateDebugInformation(Statement statement)
         {
-            var info = new DebugInformation { CurrentStatement = statement, CallStack = _debugCallStack };
+            var info = new DebugInformation
+            {
+                CurrentStatement = statement,
+                CallStack = _debugCallStack,
+                CurrentMemoryUsage = _engine.CurrentMemoryUsage
+            };
 
             if (_engine.ExecutionContext.LexicalEnvironment != null)
             {

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

@@ -7,8 +7,9 @@ namespace Jint.Runtime.Debugger
 {
     public class DebugInformation : EventArgs
     {
-        public Stack<String> CallStack { get; set; }
+        public Stack<string> CallStack { get; set; }
         public Statement CurrentStatement { get; set; }
+        public long CurrentMemoryUsage { get; set; }
         public Dictionary<string, JsValue> Locals { get; set; }
         public Dictionary<string, JsValue> Globals { get; set; }
     }