[d565449] | 1 | (function(root, factory) {
|
---|
| 2 | 'use strict';
|
---|
| 3 | // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, Rhino, and browsers.
|
---|
| 4 |
|
---|
| 5 | /* istanbul ignore next */
|
---|
| 6 | if (typeof define === 'function' && define.amd) {
|
---|
| 7 | define('stackframe', [], factory);
|
---|
| 8 | } else if (typeof exports === 'object') {
|
---|
| 9 | module.exports = factory();
|
---|
| 10 | } else {
|
---|
| 11 | root.StackFrame = factory();
|
---|
| 12 | }
|
---|
| 13 | }(this, function() {
|
---|
| 14 | 'use strict';
|
---|
| 15 | function _isNumber(n) {
|
---|
| 16 | return !isNaN(parseFloat(n)) && isFinite(n);
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | function _capitalize(str) {
|
---|
| 20 | return str.charAt(0).toUpperCase() + str.substring(1);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | function _getter(p) {
|
---|
| 24 | return function() {
|
---|
| 25 | return this[p];
|
---|
| 26 | };
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | var booleanProps = ['isConstructor', 'isEval', 'isNative', 'isToplevel'];
|
---|
| 30 | var numericProps = ['columnNumber', 'lineNumber'];
|
---|
| 31 | var stringProps = ['fileName', 'functionName', 'source'];
|
---|
| 32 | var arrayProps = ['args'];
|
---|
| 33 | var objectProps = ['evalOrigin'];
|
---|
| 34 |
|
---|
| 35 | var props = booleanProps.concat(numericProps, stringProps, arrayProps, objectProps);
|
---|
| 36 |
|
---|
| 37 | function StackFrame(obj) {
|
---|
| 38 | if (!obj) return;
|
---|
| 39 | for (var i = 0; i < props.length; i++) {
|
---|
| 40 | if (obj[props[i]] !== undefined) {
|
---|
| 41 | this['set' + _capitalize(props[i])](obj[props[i]]);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | StackFrame.prototype = {
|
---|
| 47 | getArgs: function() {
|
---|
| 48 | return this.args;
|
---|
| 49 | },
|
---|
| 50 | setArgs: function(v) {
|
---|
| 51 | if (Object.prototype.toString.call(v) !== '[object Array]') {
|
---|
| 52 | throw new TypeError('Args must be an Array');
|
---|
| 53 | }
|
---|
| 54 | this.args = v;
|
---|
| 55 | },
|
---|
| 56 |
|
---|
| 57 | getEvalOrigin: function() {
|
---|
| 58 | return this.evalOrigin;
|
---|
| 59 | },
|
---|
| 60 | setEvalOrigin: function(v) {
|
---|
| 61 | if (v instanceof StackFrame) {
|
---|
| 62 | this.evalOrigin = v;
|
---|
| 63 | } else if (v instanceof Object) {
|
---|
| 64 | this.evalOrigin = new StackFrame(v);
|
---|
| 65 | } else {
|
---|
| 66 | throw new TypeError('Eval Origin must be an Object or StackFrame');
|
---|
| 67 | }
|
---|
| 68 | },
|
---|
| 69 |
|
---|
| 70 | toString: function() {
|
---|
| 71 | var fileName = this.getFileName() || '';
|
---|
| 72 | var lineNumber = this.getLineNumber() || '';
|
---|
| 73 | var columnNumber = this.getColumnNumber() || '';
|
---|
| 74 | var functionName = this.getFunctionName() || '';
|
---|
| 75 | if (this.getIsEval()) {
|
---|
| 76 | if (fileName) {
|
---|
| 77 | return '[eval] (' + fileName + ':' + lineNumber + ':' + columnNumber + ')';
|
---|
| 78 | }
|
---|
| 79 | return '[eval]:' + lineNumber + ':' + columnNumber;
|
---|
| 80 | }
|
---|
| 81 | if (functionName) {
|
---|
| 82 | return functionName + ' (' + fileName + ':' + lineNumber + ':' + columnNumber + ')';
|
---|
| 83 | }
|
---|
| 84 | return fileName + ':' + lineNumber + ':' + columnNumber;
|
---|
| 85 | }
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | StackFrame.fromString = function StackFrame$$fromString(str) {
|
---|
| 89 | var argsStartIndex = str.indexOf('(');
|
---|
| 90 | var argsEndIndex = str.lastIndexOf(')');
|
---|
| 91 |
|
---|
| 92 | var functionName = str.substring(0, argsStartIndex);
|
---|
| 93 | var args = str.substring(argsStartIndex + 1, argsEndIndex).split(',');
|
---|
| 94 | var locationString = str.substring(argsEndIndex + 1);
|
---|
| 95 |
|
---|
| 96 | if (locationString.indexOf('@') === 0) {
|
---|
| 97 | var parts = /@(.+?)(?::(\d+))?(?::(\d+))?$/.exec(locationString, '');
|
---|
| 98 | var fileName = parts[1];
|
---|
| 99 | var lineNumber = parts[2];
|
---|
| 100 | var columnNumber = parts[3];
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return new StackFrame({
|
---|
| 104 | functionName: functionName,
|
---|
| 105 | args: args || undefined,
|
---|
| 106 | fileName: fileName,
|
---|
| 107 | lineNumber: lineNumber || undefined,
|
---|
| 108 | columnNumber: columnNumber || undefined
|
---|
| 109 | });
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | for (var i = 0; i < booleanProps.length; i++) {
|
---|
| 113 | StackFrame.prototype['get' + _capitalize(booleanProps[i])] = _getter(booleanProps[i]);
|
---|
| 114 | StackFrame.prototype['set' + _capitalize(booleanProps[i])] = (function(p) {
|
---|
| 115 | return function(v) {
|
---|
| 116 | this[p] = Boolean(v);
|
---|
| 117 | };
|
---|
| 118 | })(booleanProps[i]);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | for (var j = 0; j < numericProps.length; j++) {
|
---|
| 122 | StackFrame.prototype['get' + _capitalize(numericProps[j])] = _getter(numericProps[j]);
|
---|
| 123 | StackFrame.prototype['set' + _capitalize(numericProps[j])] = (function(p) {
|
---|
| 124 | return function(v) {
|
---|
| 125 | if (!_isNumber(v)) {
|
---|
| 126 | throw new TypeError(p + ' must be a Number');
|
---|
| 127 | }
|
---|
| 128 | this[p] = Number(v);
|
---|
| 129 | };
|
---|
| 130 | })(numericProps[j]);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | for (var k = 0; k < stringProps.length; k++) {
|
---|
| 134 | StackFrame.prototype['get' + _capitalize(stringProps[k])] = _getter(stringProps[k]);
|
---|
| 135 | StackFrame.prototype['set' + _capitalize(stringProps[k])] = (function(p) {
|
---|
| 136 | return function(v) {
|
---|
| 137 | this[p] = String(v);
|
---|
| 138 | };
|
---|
| 139 | })(stringProps[k]);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | return StackFrame;
|
---|
| 143 | }));
|
---|