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

Commit

Permalink
fix: add OriginalDelegate prop to Function::toString (#993)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gdh1995 authored and mhevery committed Feb 8, 2018
1 parent 41c17df commit 2dc7e5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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;
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

0 comments on commit 2dc7e5c

Please sign in to comment.