Permalink
Browse files

Support parsing "native" call sites

  • Loading branch information...
1 parent c76b11f commit 5a31bf82efb567b782b41f1bfbd7dc652bf91089 @felixge committed Jul 13, 2011
Showing with 64 additions and 34 deletions.
  1. +11 −5 lib/stack-trace.js
  2. +53 −29 test/integration/test-parse.js
View
@@ -18,14 +18,15 @@ exports.parse = function(err) {
var lines = err.stack.split('\n').slice(1);
return lines.map(function(line) {
- var lineMatch = line.match(/at ([^\s]+)\s+\((.+?):(\d+):(\d+)\)/);
+ var lineMatch = line.match(/at ([^\s]+)\s+\((?:(.+?):(\d+):(\d+)|([^)]+))\)/);
var methodMatch = lineMatch[1].match(/([^\.]+)(?:\.(.+))?/);
var object = methodMatch[1];
var method = methodMatch[2];
var functionName = lineMatch[1];
var methodName = null;
var typeName = 'Object';
+ var isNative = (lineMatch[5] === 'native');
if (method) {
typeName = object;
@@ -38,12 +39,13 @@ exports.parse = function(err) {
}
var properties = {
- fileName: lineMatch[2],
- lineNumber: parseInt(lineMatch[3], 10),
+ fileName: lineMatch[2] || null,
+ lineNumber: parseInt(lineMatch[3], 10) || null,
functionName: functionName,
typeName: typeName,
methodName: methodName,
- columnNumber: parseInt(lineMatch[4], 10),
+ columnNumber: parseInt(lineMatch[4], 10) || null,
+ 'native': isNative,
};
return self._createParsedCallSite(properties);
@@ -53,7 +55,11 @@ exports.parse = function(err) {
exports._createParsedCallSite = function(properties) {
var methods = {};
for (var property in properties) {
- var method = 'get' + property.substr(0, 1).toUpperCase() + property.substr(1);
+ var prefix = 'get';
+ if (property === 'native') {
+ prefix = 'is';
+ }
+ var method = prefix + property.substr(0, 1).toUpperCase() + property.substr(1);
(function(property) {
methods[method] = function() {
@@ -20,41 +20,65 @@ var stackTrace = require(common.dir.lib + '/stack-trace');
})();
(function testCompareRealWithParsedStackTrace() {
- var realTrace = stackTrace.get(); var err = new Error('something went wrong');
- var parsedTrace = stackTrace.parse(err);
+ var realTrace = stackTrace.get(); var err = new Error('something went wrong');
+ var parsedTrace = stackTrace.parse(err);
- realTrace.forEach(function(real, i) {
- var parsed = parsedTrace[i];
+ realTrace.forEach(function(real, i) {
+ var parsed = parsedTrace[i];
- function compare(method, exceptions) {
- var realValue = real[method]();
- var parsedValue = parsed[method]();
+ function compare(method, exceptions) {
+ var realValue = real[method]();
+ var parsedValue = parsed[method]();
- if (exceptions && exceptions[i]) {
- realValue = exceptions[i];
- }
+ if (exceptions && exceptions[i]) {
+ realValue = exceptions[i];
+ }
- var realJson = JSON.stringify(realValue);
- var parsedJson = JSON.stringify(parsedValue);
+ var realJson = JSON.stringify(realValue);
+ var parsedJson = JSON.stringify(parsedValue);
- var message =
- method + ': ' + realJson + ' != ' + parsedJson + ' (#' + i + ')';
+ var message =
+ method + ': ' + realJson + ' != ' + parsedJson + ' (#' + i + ')';
- assert.strictEqual(realValue, parsedValue, message);
- }
+ assert.strictEqual(realValue, parsedValue, message);
+ }
- compare('getFileName');
- compare('getFunctionName', {
- 3: 'Object..js',
- 5: 'Function._load',
- 6: 'Array.0',
- 7: 'EventEmitter._tickCallback',
- });
- compare('getTypeName');
- compare('getMethodName');
- compare('getLineNumber');
- compare('getColumnNumber', {
- 0: 49
- });
+ compare('getFileName');
+ compare('getFunctionName', {
+ 3: 'Object..js',
+ 5: 'Function._load',
+ 6: 'Array.0',
+ 7: 'EventEmitter._tickCallback',
});
+ compare('getTypeName');
+ compare('getMethodName');
+ compare('getLineNumber');
+ compare('getColumnNumber', {
+ 0: 47
+ });
+ compare('isNative');
+ });
+})();
+
+(function testStackWithNativeCall() {
+ var err = {};
+ err.stack =
+'AssertionError: true == false\n' +
+' at Test.fn (/Users/felix/code/node-fast-or-slow/test/fast/example/test-example.js:6:10)\n' +
+' at Test.run (/Users/felix/code/node-fast-or-slow/lib/test.js:45:10)\n' +
+' at TestCase.runNext (/Users/felix/code/node-fast-or-slow/lib/test_case.js:73:8)\n' +
+' at TestCase.run (/Users/felix/code/node-fast-or-slow/lib/test_case.js:61:8)\n' +
+' at Array.0 (native)\n' +
+' at EventEmitter._tickCallback (node.js:126:26)';
+
+ var trace = stackTrace.parse(err);
+ var nativeCallSite = trace[4];
+
+ assert.strictEqual(nativeCallSite.getFileName(), null);
+ assert.strictEqual(nativeCallSite.getFunctionName(), 'Array.0');
+ assert.strictEqual(nativeCallSite.getTypeName(), 'Array');
+ assert.strictEqual(nativeCallSite.getMethodName(), '0');
+ assert.strictEqual(nativeCallSite.getLineNumber(), null);
+ assert.strictEqual(nativeCallSite.getColumnNumber(), null);
+ assert.strictEqual(nativeCallSite.isNative(), true);
})();

0 comments on commit 5a31bf8

Please sign in to comment.