111 lines
3.8 KiB
JavaScript
111 lines
3.8 KiB
JavaScript
(function(root, factory) {
|
|
'use strict';
|
|
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, Rhino, and browsers.
|
|
|
|
/* istanbul ignore next */
|
|
if (typeof define === 'function' && define.amd) {
|
|
define('stackframe', [], factory);
|
|
} else if (typeof exports === 'object') {
|
|
module.exports = factory();
|
|
} else {
|
|
root.StackFrame = factory();
|
|
}
|
|
}(this, function() {
|
|
'use strict';
|
|
function _isNumber(n) {
|
|
return !isNaN(parseFloat(n)) && isFinite(n);
|
|
}
|
|
|
|
function _capitalize(str) {
|
|
return str.charAt(0).toUpperCase() + str.substring(1);
|
|
}
|
|
|
|
function _getter(p) {
|
|
return function() {
|
|
return this[p];
|
|
};
|
|
}
|
|
|
|
var booleanProps = ['isConstructor', 'isEval', 'isNative', 'isToplevel'];
|
|
var numericProps = ['columnNumber', 'lineNumber'];
|
|
var stringProps = ['fileName', 'functionName', 'source'];
|
|
var arrayProps = ['args'];
|
|
|
|
var props = booleanProps.concat(numericProps, stringProps, arrayProps);
|
|
|
|
function StackFrame(obj) {
|
|
if (obj instanceof Object) {
|
|
for (var i = 0; i < props.length; i++) {
|
|
if (obj.hasOwnProperty(props[i]) && obj[props[i]] !== undefined) {
|
|
this['set' + _capitalize(props[i])](obj[props[i]]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
StackFrame.prototype = {
|
|
getArgs: function() {
|
|
return this.args;
|
|
},
|
|
setArgs: function(v) {
|
|
if (Object.prototype.toString.call(v) !== '[object Array]') {
|
|
throw new TypeError('Args must be an Array');
|
|
}
|
|
this.args = v;
|
|
},
|
|
|
|
getEvalOrigin: function() {
|
|
return this.evalOrigin;
|
|
},
|
|
setEvalOrigin: function(v) {
|
|
if (v instanceof StackFrame) {
|
|
this.evalOrigin = v;
|
|
} else if (v instanceof Object) {
|
|
this.evalOrigin = new StackFrame(v);
|
|
} else {
|
|
throw new TypeError('Eval Origin must be an Object or StackFrame');
|
|
}
|
|
},
|
|
|
|
toString: function() {
|
|
var functionName = this.getFunctionName() || '{anonymous}';
|
|
var args = '(' + (this.getArgs() || []).join(',') + ')';
|
|
var fileName = this.getFileName() ? ('@' + this.getFileName()) : '';
|
|
var lineNumber = _isNumber(this.getLineNumber()) ? (':' + this.getLineNumber()) : '';
|
|
var columnNumber = _isNumber(this.getColumnNumber()) ? (':' + this.getColumnNumber()) : '';
|
|
return functionName + args + fileName + lineNumber + columnNumber;
|
|
}
|
|
};
|
|
|
|
for (var i = 0; i < booleanProps.length; i++) {
|
|
StackFrame.prototype['get' + _capitalize(booleanProps[i])] = _getter(booleanProps[i]);
|
|
StackFrame.prototype['set' + _capitalize(booleanProps[i])] = (function(p) {
|
|
return function(v) {
|
|
this[p] = Boolean(v);
|
|
};
|
|
})(booleanProps[i]);
|
|
}
|
|
|
|
for (var j = 0; j < numericProps.length; j++) {
|
|
StackFrame.prototype['get' + _capitalize(numericProps[j])] = _getter(numericProps[j]);
|
|
StackFrame.prototype['set' + _capitalize(numericProps[j])] = (function(p) {
|
|
return function(v) {
|
|
if (!_isNumber(v)) {
|
|
throw new TypeError(p + ' must be a Number');
|
|
}
|
|
this[p] = Number(v);
|
|
};
|
|
})(numericProps[j]);
|
|
}
|
|
|
|
for (var k = 0; k < stringProps.length; k++) {
|
|
StackFrame.prototype['get' + _capitalize(stringProps[k])] = _getter(stringProps[k]);
|
|
StackFrame.prototype['set' + _capitalize(stringProps[k])] = (function(p) {
|
|
return function(v) {
|
|
this[p] = String(v);
|
|
};
|
|
})(stringProps[k]);
|
|
}
|
|
|
|
return StackFrame;
|
|
}));
|