MCPcopy Index your code
hub / github.com/angular/angular / patchToString

Function patchToString

packages/zone.js/lib/common/to-string.ts:12–61  ·  view source on GitHub ↗
(Zone: ZoneType)

Source from the content-addressed store, hash-verified

10import {zoneSymbol} from './utils';
11
12export function patchToString(Zone: ZoneType): void {
13 // override Function.prototype.toString to make zone.js patched function
14 // look like native function
15 Zone.__load_patch('toString', (global: any) => {
16 // patch Func.prototype.toString to let them look like native
17 const originalFunctionToString = Function.prototype.toString;
18
19 const ORIGINAL_DELEGATE_SYMBOL = zoneSymbol('OriginalDelegate');
20 const PROMISE_SYMBOL = zoneSymbol('Promise');
21 const ERROR_SYMBOL = zoneSymbol('Error');
22 const newFunctionToString = function toString(this: unknown) {
23 if (typeof this === 'function') {
24 const originalDelegate = (this as any)[ORIGINAL_DELEGATE_SYMBOL];
25 if (originalDelegate) {
26 if (typeof originalDelegate === 'function') {
27 return originalFunctionToString.call(originalDelegate);
28 } else {
29 return Object.prototype.toString.call(originalDelegate);
30 }
31 }
32 if (this === Promise) {
33 const nativePromise = global[PROMISE_SYMBOL];
34 if (nativePromise) {
35 return originalFunctionToString.call(nativePromise);
36 }
37 }
38 if (this === Error) {
39 const nativeError = global[ERROR_SYMBOL];
40 if (nativeError) {
41 return originalFunctionToString.call(nativeError);
42 }
43 }
44 }
45 return originalFunctionToString.call(this);
46 };
47 (newFunctionToString as any)[ORIGINAL_DELEGATE_SYMBOL] = originalFunctionToString;
48 Function.prototype.toString = newFunctionToString;
49
50 // patch Object.prototype.toString to let them look like native
51 const originalObjectToString = Object.prototype.toString;
52 const PROMISE_OBJECT_TO_STRING = '[object Promise]';
53 Object.prototype.toString = function () {
54 if (typeof Promise === 'function' && this instanceof Promise) {
55 return PROMISE_OBJECT_TO_STRING;
56 }
57
58 return originalObjectToString.call(this);
59 };
60 });
61}

Callers 4

patchCommonFunction · 0.90
rollupMainFunction · 0.90

Calls 1

__load_patchMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…