Bladeren bron

Return error location with JavaScriptException.

Paul Vowles 10 jaren geleden
bovenliggende
commit
f06f1b73b1

+ 5 - 3
Jint/Native/Function/ScriptFunctionInstance.cs

@@ -62,7 +62,7 @@ namespace Jint.Native.Function
                 JsValue thisBinding;
                 if (StrictModeScope.IsStrictModeCode)
                 {
-                    thisBinding = thisArg;
+                    thisBinding = (thisArg == Undefined.Instance || thisArg == Null.Instance) ? Engine.Global : thisArg;
                 }
                 else if (thisArg == Undefined.Instance || thisArg == Null.Instance)
                 {
@@ -94,7 +94,9 @@ namespace Jint.Native.Function
 
                     if (result.Type == Completion.Throw)
                     {
-                        throw new JavaScriptException(result.GetValueOrDefault());
+                        JavaScriptException ex = new JavaScriptException(result.GetValueOrDefault());
+                        ex.Location = result.Location;
+                        throw ex;
                     }
 
                     if (result.Type == Completion.Return)
@@ -134,4 +136,4 @@ namespace Jint.Native.Function
 
         public ObjectInstance PrototypeObject { get; private set; }
     }
-}
+}

+ 2 - 0
Jint/Runtime/Completion.cs

@@ -28,5 +28,7 @@ namespace Jint.Runtime
         {
             return Value.HasValue ? Value.Value : Undefined.Instance;
         }
+
+        public Jint.Parser.Location Location { get; set; }
     }
 }

+ 6 - 0
Jint/Runtime/JavaScriptException.cs

@@ -43,5 +43,11 @@ namespace Jint.Runtime
         {
             return _errorObject.ToString();
         }
+
+        public Jint.Parser.Location Location { get; set; }
+
+        public int LineNumber { get { return null == Location ? 0 : Location.Start.Line; } }
+
+        public int Column { get { return null == Location ? 0 : Location.Start.Column; } }
     }
 }

+ 9 - 2
Jint/Runtime/StatementInterpreter.cs

@@ -332,6 +332,7 @@ namespace Jint.Runtime
             catch (JavaScriptException e)
             {
                 c = new Completion(Completion.Throw, e.Error, null);
+                c.Location = withStatement.Location;
             }
             finally
             {
@@ -409,11 +410,13 @@ namespace Jint.Runtime
         {
             var c = new Completion(Completion.Normal, null, null);
             Completion sl = c;
+            Statement s = null;
 
             try
             {
                 foreach (var statement in statementList)
                 {
+                    s = statement;
                     c = ExecuteStatement(statement);
                     if (c.Type != Completion.Normal)
                     {
@@ -425,7 +428,9 @@ namespace Jint.Runtime
             }
             catch(JavaScriptException v)
             {
-                return new Completion(Completion.Throw, v.Error, null);
+                c = new Completion(Completion.Throw, v.Error, null);
+                c.Location = s.Location;
+                return c;
             }
 
             return new Completion(c.Type, c.GetValueOrDefault(), c.Identifier);
@@ -439,7 +444,9 @@ namespace Jint.Runtime
         public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
         {
             var exprRef = _engine.EvaluateExpression(throwStatement.Argument);
-            return new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
+            Completion c = new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
+            c.Location = throwStatement.Location;
+            return c;
         }
 
         /// <summary>