Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/common/to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {zoneSymbol} from './utils';

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

const ORIGINAL_DELEGATE_SYMBOL = zoneSymbol('OriginalDelegate');
const PROMISE_SYMBOL = zoneSymbol('Promise');
const ERROR_SYMBOL = zoneSymbol('Error');
Function.prototype.toString = function() {
const newFunctionToString = function toString() {
if (typeof this === 'function') {
const originalDelegate = this[ORIGINAL_DELEGATE_SYMBOL];
if (originalDelegate) {
Expand All @@ -42,6 +41,8 @@ Zone.__load_patch('toString', (global: any, Zone: ZoneType) => {
}
return originalFunctionToString.apply(this, arguments);
};
(newFunctionToString as any)[ORIGINAL_DELEGATE_SYMBOL] = originalFunctionToString;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current version, we attached originalFunctionToString to Zone, and if we attached it to Function.prototype, we don't need to attached it to Zone any more.

so please just change this line https://github.com/gdh1995/zone.js/blob/b98f284030a4296a0e476728829298515edc3b0d/lib/common/to-string.ts#L14

const originalFunctionToString = Function.prototype[ORIGINAL_DELEGATE_SYMBOL] =
      Function.prototype.toString;

Function.prototype.toString = newFunctionToString;


// patch Object.prototype.toString to let them look like native
Expand Down
4 changes: 4 additions & 0 deletions test/common/toString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('global function patch', () => {
expect(Function.prototype.toString.call(Error)).toContain('[native code]');
});

it('Function toString should look like native', () => {
expect(Function.prototype.toString.call(Function.prototype.toString)).toContain('[native code]');
});

it('EventTarget addEventListener should look like native', ifEnvSupports('HTMLElement', () => {
expect(Function.prototype.toString.call(HTMLElement.prototype.addEventListener))
.toContain('[native code]');
Expand Down