Reviewers: Vyacheslav Egorov,

Message:
PTAL. Not part of the spec, but I guess it's still a nice thing to have since
both Firefox and Opera support it.

Description:
Add properties fileName, lineNumber and columnNumber to the Error object.

BUG=v8:1914
TEST=mjsunit/error-properties.js


Please review this at http://codereview.chromium.org/9323075/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/messages.js
  A test/mjsunit/error-properties.js


Index: src/messages.js
diff --git a/src/messages.js b/src/messages.js
index 5310938a6612f75bdd6044b3715d48f879ad2273..2350eab8cb74da74d2e8de8b9f473ee52f4b6efa 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -1071,6 +1071,18 @@ function FormatRawStackTrace(error, raw_stack) {
   }
 }

+function GetTopFrame(raw_stack) {
+  for (var i = 0; i < raw_stack.length; i += 4) {
+    var recv = raw_stack[i];
+    var fun = raw_stack[i + 1];
+    var code = raw_stack[i + 2];
+    var pc = raw_stack[i + 3];
+    var pos = %FunctionGetPositionForOffset(code, pc);
+    var top_frame = new CallSite(recv, fun, pos);
+    if (!top_frame.isNative()) return top_frame;
+  }
+  return undefined;
+}

 function captureStackTrace(obj, cons_opt) {
   var stackTraceLimit = $Error.stackTraceLimit;
@@ -1084,6 +1096,21 @@ function captureStackTrace(obj, cons_opt) {
   DefineOneShotAccessor(obj, 'stack', function (obj) {
     return FormatRawStackTrace(obj, raw_stack);
   });
+
+  DefineOneShotAccessor(obj, 'lineNumber', function (obj) {
+    var frame = GetTopFrame(raw_stack);
+    return frame ? frame.getLineNumber() : undefined;
+  });
+  DefineOneShotAccessor(obj, 'columnNumber', function (obj) {
+    var frame = GetTopFrame(raw_stack);
+    return frame ? frame.getColumnNumber() : undefined;
+  });
+  DefineOneShotAccessor(obj, 'fileName', function (obj) {
+    var frame = GetTopFrame(raw_stack);
+    return frame ? (frame.isEval() ? frame.getEvalOrigin()
+                                   : frame.getFileName())
+                 : undefined;
+  });
 }


Index: test/mjsunit/error-properties.js
diff --git a/test/mjsunit/error-properties.js b/test/mjsunit/error-properties.js
new file mode 100644
index 0000000000000000000000000000000000000000..0156308cedaa47633923c10757d32333d5938b3a
--- /dev/null
+++ b/test/mjsunit/error-properties.js
@@ -0,0 +1,49 @@
+// Copyright 2012 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+// Test properties of the Error object.
+
+try {
+  error;
+} catch (e) {
+  assertEquals(32, e.lineNumber);
+  assertEquals(3, e.columnNumber);
+  assertTrue(/error-properties\.js$/.test(e.fileName));
+}
+
+function f() {
+  eval("error;");
+}
+
+try {
+  f();
+} catch (e) {
+  assertEquals(1, e.lineNumber);
+  assertEquals(1, e.columnNumber);
+  assertTrue(/eval at f/.test(e.fileName));
+}


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to