Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 2dc7e5c

Browse files
gdh1995mhevery
authored andcommitted
fix: add OriginalDelegate prop to Function::toString (#993)
This makes `Function.prototype.toString.toString()` also returns `"function toString() { [native code] }"`, which should be a little safer. Squashed: store originalFunctionToString only in one place add a test to Function::toString
1 parent 41c17df commit 2dc7e5c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/common/to-string.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import {zoneSymbol} from './utils';
99

1010
// override Function.prototype.toString to make zone.js patched function
1111
// look like native function
12-
Zone.__load_patch('toString', (global: any, Zone: ZoneType) => {
12+
Zone.__load_patch('toString', (global: any) => {
1313
// patch Func.prototype.toString to let them look like native
14-
const originalFunctionToString = (Zone as any)['__zone_symbol__originalToString'] =
15-
Function.prototype.toString;
14+
const originalFunctionToString = Function.prototype.toString;
1615

1716
const ORIGINAL_DELEGATE_SYMBOL = zoneSymbol('OriginalDelegate');
1817
const PROMISE_SYMBOL = zoneSymbol('Promise');
1918
const ERROR_SYMBOL = zoneSymbol('Error');
20-
Function.prototype.toString = function() {
19+
const newFunctionToString = function toString() {
2120
if (typeof this === 'function') {
2221
const originalDelegate = this[ORIGINAL_DELEGATE_SYMBOL];
2322
if (originalDelegate) {
@@ -42,6 +41,8 @@ Zone.__load_patch('toString', (global: any, Zone: ZoneType) => {
4241
}
4342
return originalFunctionToString.apply(this, arguments);
4443
};
44+
(newFunctionToString as any)[ORIGINAL_DELEGATE_SYMBOL] = originalFunctionToString;
45+
Function.prototype.toString = newFunctionToString;
4546

4647

4748
// patch Object.prototype.toString to let them look like native

test/common/toString.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ describe('global function patch', () => {
3636
expect(Function.prototype.toString.call(Error)).toContain('[native code]');
3737
});
3838

39+
it('Function toString should look like native', () => {
40+
expect(Function.prototype.toString.call(Function.prototype.toString)).toContain('[native code]');
41+
});
42+
3943
it('EventTarget addEventListener should look like native', ifEnvSupports('HTMLElement', () => {
4044
expect(Function.prototype.toString.call(HTMLElement.prototype.addEventListener))
4145
.toContain('[native code]');

0 commit comments

Comments
 (0)