2019-05-31 11:56:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-05-31 11:56:07 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
import {zoneSymbol} from './utils';
|
|
|
|
|
|
|
|
// override Function.prototype.toString to make zone.js patched function
|
|
|
|
// look like native function
|
|
|
|
Zone.__load_patch('toString', (global: any) => {
|
|
|
|
// patch Func.prototype.toString to let them look like native
|
|
|
|
const originalFunctionToString = Function.prototype.toString;
|
|
|
|
|
|
|
|
const ORIGINAL_DELEGATE_SYMBOL = zoneSymbol('OriginalDelegate');
|
|
|
|
const PROMISE_SYMBOL = zoneSymbol('Promise');
|
|
|
|
const ERROR_SYMBOL = zoneSymbol('Error');
|
2019-06-26 04:33:02 -04:00
|
|
|
const newFunctionToString = function toString(this: unknown) {
|
2019-05-31 11:56:07 -04:00
|
|
|
if (typeof this === 'function') {
|
2019-06-26 04:33:02 -04:00
|
|
|
const originalDelegate = (this as any)[ORIGINAL_DELEGATE_SYMBOL];
|
2019-05-31 11:56:07 -04:00
|
|
|
if (originalDelegate) {
|
|
|
|
if (typeof originalDelegate === 'function') {
|
|
|
|
return originalFunctionToString.call(originalDelegate);
|
|
|
|
} else {
|
|
|
|
return Object.prototype.toString.call(originalDelegate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this === Promise) {
|
|
|
|
const nativePromise = global[PROMISE_SYMBOL];
|
|
|
|
if (nativePromise) {
|
|
|
|
return originalFunctionToString.call(nativePromise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this === Error) {
|
|
|
|
const nativeError = global[ERROR_SYMBOL];
|
|
|
|
if (nativeError) {
|
|
|
|
return originalFunctionToString.call(nativeError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return originalFunctionToString.call(this);
|
|
|
|
};
|
|
|
|
(newFunctionToString as any)[ORIGINAL_DELEGATE_SYMBOL] = originalFunctionToString;
|
|
|
|
Function.prototype.toString = newFunctionToString;
|
|
|
|
|
|
|
|
|
|
|
|
// patch Object.prototype.toString to let them look like native
|
|
|
|
const originalObjectToString = Object.prototype.toString;
|
|
|
|
const PROMISE_OBJECT_TO_STRING = '[object Promise]';
|
|
|
|
Object.prototype.toString = function() {
|
2020-08-05 15:42:50 -04:00
|
|
|
if (typeof Promise === 'function' && this instanceof Promise) {
|
2019-05-31 11:56:07 -04:00
|
|
|
return PROMISE_OBJECT_TO_STRING;
|
|
|
|
}
|
2020-08-05 15:42:50 -04:00
|
|
|
|
2019-05-31 11:56:07 -04:00
|
|
|
return originalObjectToString.call(this);
|
|
|
|
};
|
|
|
|
});
|