Skip to content

Commit cf89f86

Browse files
authored
fix: Update how wrapped functions are detected as native functions (#1106)
1 parent 9cb17c1 commit cf89f86

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@
7777
},
7878
"lint-staged": {
7979
"src/**/*.js": [
80+
"eslint",
8081
"prettier --write",
8182
"git add"
8283
],
8384
"test/**/*.js": [
85+
"eslint",
8486
"prettier --write",
8587
"git add"
8688
],
8789
"plugins/**/*.js": [
90+
"eslint",
8891
"prettier --write",
8992
"git add"
9093
]

src/raven.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ Raven.prototype = {
221221
TraceKit.report.subscribe(function() {
222222
self._handleOnErrorStackInfo.apply(self, arguments);
223223
});
224+
225+
self._patchFunctionToString();
226+
224227
if (self._globalOptions.instrument && self._globalOptions.instrument.tryCatch) {
225228
self._instrumentTryCatch();
226229
}
@@ -377,6 +380,7 @@ Raven.prototype = {
377380
uninstall: function() {
378381
TraceKit.report.uninstall();
379382

383+
this._unpatchFunctionToString();
380384
this._restoreBuiltIns();
381385

382386
Error.stackTraceLimit = this._originalErrorStackTraceLimit;
@@ -939,6 +943,25 @@ Raven.prototype = {
939943
});
940944
},
941945

946+
_patchFunctionToString: function() {
947+
var self = this;
948+
self._originalFunctionToString = Function.prototype.toString;
949+
// eslint-disable-next-line no-extend-native
950+
Function.prototype.toString = function() {
951+
if (typeof this === 'function' && this.__raven__) {
952+
return self._originalFunctionToString.apply(this.__orig_method__, arguments);
953+
}
954+
return self._originalFunctionToString.apply(this, arguments);
955+
};
956+
},
957+
958+
_unpatchFunctionToString: function() {
959+
if (this._originalFunctionToString) {
960+
// eslint-disable-next-line no-extend-native
961+
Function.prototype.toString = this._originalFunctionToString;
962+
}
963+
},
964+
942965
/**
943966
* Wrap timer functions and event targets to catch errors and provide
944967
* better metadata.

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ function isSameStacktrace(stack1, stack2) {
357357
function fill(obj, name, replacement, track) {
358358
var orig = obj[name];
359359
obj[name] = replacement(orig);
360+
obj[name].__raven__ = true;
361+
obj[name].__orig_method__ = orig;
360362
if (track) {
361363
track.push([obj, name, orig]);
362364
}

test/integration/test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,46 @@ describe('integration', function() {
12541254
}
12551255
);
12561256
});
1257+
1258+
it('should preserve native code detection compatibility', function(done) {
1259+
var iframe = this.iframe;
1260+
1261+
iframeExecute(
1262+
iframe,
1263+
done,
1264+
function() {
1265+
done();
1266+
},
1267+
function() {
1268+
assert.include(
1269+
Function.prototype.toString.call(window.setTimeout),
1270+
'[native code]'
1271+
);
1272+
assert.include(
1273+
Function.prototype.toString.call(window.setInterval),
1274+
'[native code]'
1275+
);
1276+
assert.include(
1277+
Function.prototype.toString.call(window.addEventListener),
1278+
'[native code]'
1279+
);
1280+
assert.include(
1281+
Function.prototype.toString.call(window.removeEventListener),
1282+
'[native code]'
1283+
);
1284+
assert.include(
1285+
Function.prototype.toString.call(window.requestAnimationFrame),
1286+
'[native code]'
1287+
);
1288+
if ('fetch' in window) {
1289+
assert.include(
1290+
Function.prototype.toString.call(window.fetch),
1291+
'[native code]'
1292+
);
1293+
}
1294+
}
1295+
);
1296+
});
12571297
});
12581298

12591299
describe('uninstall', function() {

0 commit comments

Comments
 (0)