Browse Source

Adding Options.AllowDebuggerStatement

Sebastien Ros 11 years ago
parent
commit
0e332ddad8
3 changed files with 35 additions and 4 deletions
  1. 6 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 22 0
      Jint/Options.cs
  3. 7 4
      Jint/Runtime/StatementInterpreter.cs

+ 6 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -557,5 +557,11 @@ namespace Jint.Tests.Runtime
 
             Assert.Equal(expected, result);
         }
+
+        [Fact]
+        public void ShouldNotExecuteDebuggerStatement()
+        {
+            new Engine().Execute("debugger"); 
+        }
     }
 }

+ 22 - 0
Jint/Options.cs

@@ -4,6 +4,7 @@
     {
         private bool _discardGlobal;
         private bool _strict;
+        private bool _allowDebuggerStatement;
 
         /// <summary>
         /// When called, doesn't initialize the global scope.
@@ -15,12 +16,28 @@
             return this;
         }
 
+        /// <summary>
+        /// Run the script in strict mode.
+        /// </summary>
         public Options Strict(bool strict = true)
         {
             _strict = strict;
             return this;
         }
 
+        /// <summary>
+        /// Allow the <code>debugger</code> statement to be called in a script.
+        /// </summary>
+        /// <remarks>
+        /// Because the <code>debugger</code> statement can start the 
+        /// Visual Studio debugger, is it disabled by default
+        /// </remarks>
+        public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
+        {
+            _allowDebuggerStatement = allowDebuggerStatement;
+            return this;
+        }
+
         internal bool GetDiscardGlobal()
         {
             return _discardGlobal;
@@ -30,5 +47,10 @@
         {
             return _strict;
         }
+        
+        internal bool IsDebuggerStatementAllowed()
+        {
+            return _allowDebuggerStatement;
+        }
     }
 }

+ 7 - 4
Jint/Runtime/StatementInterpreter.cs

@@ -523,12 +523,15 @@ namespace Jint.Runtime
 
         public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
         {
-            if (!System.Diagnostics.Debugger.IsAttached)
+            if (_engine.Options.IsDebuggerStatementAllowed())
             {
-                System.Diagnostics.Debugger.Launch();
+                if (!System.Diagnostics.Debugger.IsAttached)
+                {
+                    System.Diagnostics.Debugger.Launch();
+                }
+
+                System.Diagnostics.Debugger.Break();
             }
-            
-            System.Diagnostics.Debugger.Break();
 
             return new Completion(Completion.Normal, null, null);
         }