From 41c17df0dd4acdee69422955c0a577d6c6b3f2a6 Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Thu, 8 Feb 2018 13:46:27 +0900 Subject: [PATCH 1/2] doc(electron): add document for electron patch (#985) --- NON-STANDARD-APIS.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/NON-STANDARD-APIS.md b/NON-STANDARD-APIS.md index 61fe7da1a..b87f2022e 100644 --- a/NON-STANDARD-APIS.md +++ b/NON-STANDARD-APIS.md @@ -162,13 +162,22 @@ import 'zone.js/dist/zone-patch-rxjs'; * electron +In electron, we patched the following APIs with `zone.js` + +1. Browser API +2. NodeJS +3. Electorn Native API + ## Usage. -add following line into `polyfill.ts` after load zone-mix. +add/update following line into `polyfill.ts`. ``` -import 'zone.js/dist/zone-patch-electron'; +//import 'zone.js/dist/zone'; // originally added by angular-cli, comment it out +import 'zone.js/dist/zone-mix'; // add zone-mix to patch both Browser and Nodejs +import 'zone.js/dist/zone-patch-electron'; // add zone-patch-electron to patch Electron native API ``` -there is a sampel repo [zone-electron](https://github.com/JiaLiPassion/zone-electron) here +there is a sampel repo [zone-electron](https://github.com/JiaLiPassion/zone-electron). + From 2dc7e5c72ac2cc7ad21ea5a90456301609a1fd21 Mon Sep 17 00:00:00 2001 From: Dahan Gong Date: Thu, 8 Feb 2018 12:48:03 +0800 Subject: [PATCH 2/2] fix: add OriginalDelegate prop to Function::toString (#993) 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 --- lib/common/to-string.ts | 9 +++++---- test/common/toString.spec.ts | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/common/to-string.ts b/lib/common/to-string.ts index a486ff423..ae884a7ac 100644 --- a/lib/common/to-string.ts +++ b/lib/common/to-string.ts @@ -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) { @@ -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 diff --git a/test/common/toString.spec.ts b/test/common/toString.spec.ts index 48e7474f0..1eb33c839 100644 --- a/test/common/toString.spec.ts +++ b/test/common/toString.spec.ts @@ -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]');