|
52 | 52 | * @return Array[StackFrame] |
53 | 53 | */ |
54 | 54 | get: function StackTrace$$get(opts) { |
55 | | - var err = new Error(); |
56 | | - if (err.stack || err['opera#sourceloc']) { |
57 | | - return this.fromError(err, opts); |
58 | | - } else { |
59 | | - return this.generateArtificially(opts); |
| 55 | + try { |
| 56 | + // Error must be thrown to get stack in IE |
| 57 | + throw new Error(); |
| 58 | + } catch (err) { |
| 59 | + if (err.stack || err['opera#sourceloc']) { |
| 60 | + return this.fromError(err, opts); |
| 61 | + } else { |
| 62 | + return this.generateArtificially(opts); |
| 63 | + } |
60 | 64 | } |
61 | 65 | }, |
62 | 66 |
|
|
73 | 77 | if (typeof opts.filter === 'function') { |
74 | 78 | stackframes = stackframes.filter(opts.filter); |
75 | 79 | } |
76 | | - resolve(Promise.all(stackframes.map(function(sf) { |
| 80 | + resolve(Promise.all(stackframes.map(function (sf) { |
77 | 81 | return new Promise(function (resolve) { |
78 | | - function resolveOriginal(_) { resolve(sf); } |
| 82 | + function resolveOriginal(_) { |
| 83 | + resolve(sf); |
| 84 | + } |
| 85 | + |
79 | 86 | new StackTraceGPS(opts).pinpoint(sf) |
80 | 87 | .then(resolve, resolveOriginal)['catch'](resolveOriginal); |
81 | 88 | }); |
|
95 | 102 | stackFrames = stackFrames.filter(opts.filter); |
96 | 103 | } |
97 | 104 | return Promise.resolve(stackFrames); |
| 105 | + }, |
| 106 | + |
| 107 | + /** |
| 108 | + * Given a function, wrap it such that invocations trigger a callback that |
| 109 | + * is called with a stack trace. |
| 110 | + * |
| 111 | + * @param {Function} fn to be instrumented |
| 112 | + * @param {Function} callback function to call with a stack trace on invocation |
| 113 | + * @param {Function} errback optional function to call with error if unable to get stack trace. |
| 114 | + * @param {Object} thisArg optional context object (e.g. window) |
| 115 | + */ |
| 116 | + instrument: function StackTrace$$instrument(fn, callback, errback, thisArg) { |
| 117 | + if (typeof fn !== 'function') { |
| 118 | + throw new Error('Cannot instrument non-function object'); |
| 119 | + } else if (typeof fn.__stacktraceOriginalFn === 'function') { |
| 120 | + // Already instrumented, return given Function |
| 121 | + return fn; |
| 122 | + } |
| 123 | + |
| 124 | + var instrumented = function StackTrace$$instrumented() { |
| 125 | + try { |
| 126 | + this.get().then(callback, errback)['catch'](errback); |
| 127 | + fn.apply(thisArg || this, arguments); |
| 128 | + } catch (e) { |
| 129 | + this.fromError(e).then(callback, errback)['catch'](errback); |
| 130 | + throw e; |
| 131 | + } |
| 132 | + }.bind(this); |
| 133 | + instrumented.__stacktraceOriginalFn = fn; |
| 134 | + |
| 135 | + return instrumented; |
| 136 | + }, |
| 137 | + |
| 138 | + /** |
| 139 | + * Given a function that has been instrumented, |
| 140 | + * revert the function to it's original (non-instrumented) state. |
| 141 | + * |
| 142 | + * @param fn {Function} |
| 143 | + */ |
| 144 | + deinstrument: function StackTrace$$deinstrument(fn) { |
| 145 | + if (typeof fn !== 'function') { |
| 146 | + throw new Error('Cannot de-instrument non-function object'); |
| 147 | + } else if (typeof fn.__stacktraceOriginalFn === 'function') { |
| 148 | + return fn.__stacktraceOriginalFn; |
| 149 | + } else { |
| 150 | + // Function not instrumented, return original |
| 151 | + return fn; |
| 152 | + } |
98 | 153 | } |
99 | 154 | }; |
100 | 155 | })); |
0 commit comments