fix(zone.js): zone.js toString patch should check typeof Promise is function (#38350)
Close #38361 zone.js monkey patch toString, and check the instance is `Promise` or not by using `instanceof Promise`, sometimes when Promise is not available, the `instanceof` operation fails and throw `TypeError: Right-hand side of 'instanceof' is not an object` this PR check `typeof Promise` equals to function or not to prevent the error. PR Close #38350
This commit is contained in:
parent
cb3db0d31b
commit
18e474f522
|
@ -49,9 +49,10 @@ Zone.__load_patch('toString', (global: any) => {
|
||||||
const originalObjectToString = Object.prototype.toString;
|
const originalObjectToString = Object.prototype.toString;
|
||||||
const PROMISE_OBJECT_TO_STRING = '[object Promise]';
|
const PROMISE_OBJECT_TO_STRING = '[object Promise]';
|
||||||
Object.prototype.toString = function() {
|
Object.prototype.toString = function() {
|
||||||
if (this instanceof Promise) {
|
if (typeof Promise === 'function' && this instanceof Promise) {
|
||||||
return PROMISE_OBJECT_TO_STRING;
|
return PROMISE_OBJECT_TO_STRING;
|
||||||
}
|
}
|
||||||
|
|
||||||
return originalObjectToString.call(this);
|
return originalObjectToString.call(this);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,6 +18,18 @@ describe('global function patch', () => {
|
||||||
.toEqual(Function.prototype.toString.call(g[zoneSymbol('setTimeout')]));
|
.toEqual(Function.prototype.toString.call(g[zoneSymbol('setTimeout')]));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not throw error if Promise is not a function', () => {
|
||||||
|
const P = g.Promise;
|
||||||
|
try {
|
||||||
|
g.Promise = undefined;
|
||||||
|
expect(() => {
|
||||||
|
const a = {}.toString();
|
||||||
|
}).not.toThrow();
|
||||||
|
} finally {
|
||||||
|
g.Promise = P;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('MutationObserver toString should be the same with native version',
|
it('MutationObserver toString should be the same with native version',
|
||||||
ifEnvSupports('MutationObserver', () => {
|
ifEnvSupports('MutationObserver', () => {
|
||||||
const nativeMutationObserver = g[zoneSymbol('MutationObserver')];
|
const nativeMutationObserver = g[zoneSymbol('MutationObserver')];
|
||||||
|
|
Loading…
Reference in New Issue