Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(778)

Unified Diff: src/messages.js

Issue 159403: Added Error.captureStackTrace function. (Closed)
Patch Set: Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/messages.js
diff --git a/src/messages.js b/src/messages.js
index 870c969f98fa69ae200ff7b921e17e0e2eb8d244..fd505ff67f669e4ec1646048ea21526138dfab70 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -561,20 +561,24 @@ function GetStackTraceLine(recv, fun, pos, isGlobal) {
var kAddMessageAccessorsMarker = { };
// Defines accessors for a property that is calculated the first time
-// the property is read and then replaces the accessor with the value.
-// Also, setting the property causes the accessors to be deleted.
+// the property is read.
function DefineOneShotAccessor(obj, name, fun) {
// Note that the accessors consistently operate on 'obj', not 'this'.
// Since the object may occur in someone else's prototype chain we
// can't rely on 'this' being the same as 'obj'.
+ var hasBeenSet = false;
+ var value;
obj.__defineGetter__(name, function () {
- var value = fun(obj);
- obj[name] = value;
+ if (hasBeenSet) {
+ return value;
+ }
+ hasBeenSet = true;
+ value = fun(obj);
return value;
});
obj.__defineSetter__(name, function (v) {
- delete obj[name];
- obj[name] = v;
+ hasBeenSet = true;
+ value = v;
});
}
@@ -833,22 +837,25 @@ function DefineError(f) {
} else if (!IS_UNDEFINED(m)) {
this.message = ToString(m);
}
- var stackTraceLimit = $Error.stackTraceLimit;
- if (stackTraceLimit) {
- // Cap the limit to avoid extremely big traces
- if (stackTraceLimit < 0 || stackTraceLimit > 10000)
- stackTraceLimit = 10000;
- var raw_stack = %CollectStackTrace(f, stackTraceLimit);
- DefineOneShotAccessor(this, 'stack', function (obj) {
- return FormatRawStackTrace(obj, raw_stack);
- });
- }
+ captureStackTrace(this, f);
} else {
return new f(m);
}
});
}
+function captureStackTrace(obj, cons_opt) {
+ var stackTraceLimit = $Error.stackTraceLimit;
+ if (!stackTraceLimit) return;
+ if (stackTraceLimit < 0 || stackTraceLimit > 10000)
+ stackTraceLimit = 10000;
+ var raw_stack = %CollectStackTrace(cons_opt ? cons_opt : captureStackTrace,
+ stackTraceLimit);
+ DefineOneShotAccessor(obj, 'stack', function (obj) {
+ return FormatRawStackTrace(obj, raw_stack);
+ });
+};
+
$Math.__proto__ = global.Object.prototype;
DefineError(function Error() { });
@@ -859,6 +866,8 @@ DefineError(function ReferenceError() { });
DefineError(function EvalError() { });
DefineError(function URIError() { });
+$Error.captureStackTrace = captureStackTrace;
+
// Setup extra properties of the Error.prototype object.
$Error.prototype.message = '';
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698