Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: polyfill.js

Issue 29697596: Issue 6388 - Wrap inherited function properties as well (Closed)
Patch Set: Simplify our approach Created Feb. 16, 2018, 1:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 object = object[node]; 71 object = object[node];
72 72
73 if (!object) 73 if (!object)
74 return; 74 return;
75 } 75 }
76 76
77 let func = object[name]; 77 let func = object[name];
78 if (!func) 78 if (!func)
79 return; 79 return;
80 80
81 let descriptor = Object.getOwnPropertyDescriptor(object, name); 81 // If the property is not writable assigning will fail, so we use
82 // Object.defineProperty here instead. Assuming the property isn't
83 // inherited its other attributes (e.g. enumerable) are presereved,
Manish Jethani 2018/02/16 14:03:53 Nit: "preserved" (spelling)
kzar 2018/02/16 16:41:08 Done.
84 // except accessor properties since we're specifying a value.
Manish Jethani 2018/02/16 14:03:53 Nit: "except accessor attributes" (?)
kzar 2018/02/16 16:41:08 How's this?
Manish Jethani 2018/02/16 16:47:38 Since "enumerable" is referred to as an attribute
85 Object.defineProperty(object, name, {
86 value: function(...args)
Manish Jethani 2018/02/16 14:03:53 Since we're using an object literal here now we ca
kzar 2018/02/16 16:41:08 Good point, eslint picked that up I just forgot to
87 {
kzar 2018/02/16 13:36:46 Sorry for changing the indentation here, it makes
88 let callStack = new Error().stack;
82 89
83 delete descriptor["get"]; 90 if (typeof args[args.length - 1] == "function")
84 delete descriptor["set"]; 91 return func.apply(object, args);
85 92
86 descriptor.value = function(...args) 93 // If the last argument is undefined, we drop it from the list assuming
87 { 94 // it stands for the optional callback. We must do this, because we have
88 let callStack = new Error().stack; 95 // to replace it with our own callback. If we simply append our own
96 // callback to the list, it won't match the signature of the function
97 // and will cause an exception.
98 if (typeof args[args.length - 1] == "undefined")
99 args.pop();
89 100
90 if (typeof args[args.length - 1] == "function") 101 let resolvePromise = null;
91 return func.apply(object, args); 102 let rejectPromise = null;
92 103
93 // If the last argument is undefined, we drop it from the list assuming 104 func.call(object, ...args, result =>
94 // it stands for the optional callback. We must do this, because we have 105 {
95 // to replace it with our own callback. If we simply append our own 106 let error = browser.runtime.lastError;
96 // callback to the list, it won't match the signature of the function and 107 if (error && !portClosedBeforeResponseError.test(error.message))
97 // will cause an exception. 108 {
98 if (typeof args[args.length - 1] == "undefined") 109 // runtime.lastError is already an Error instance on Edge, while on
99 args.pop(); 110 // Chrome it is a plain object with only a message property.
111 if (!(error instanceof Error))
112 {
113 error = new Error(error.message);
100 114
101 let resolvePromise = null; 115 // Add a more helpful stack trace.
102 let rejectPromise = null; 116 error.stack = callStack;
117 }
103 118
104 func.call(object, ...args, result => 119 rejectPromise(error);
105 { 120 }
106 let error = browser.runtime.lastError; 121 else
107 if (error && !portClosedBeforeResponseError.test(error.message)) 122 {
123 resolvePromise(result);
124 }
125 });
126
127 return new Promise((resolve, reject) =>
108 { 128 {
109 // runtime.lastError is already an Error instance on Edge, while on 129 resolvePromise = resolve;
110 // Chrome it is a plain object with only a message property. 130 rejectPromise = reject;
111 if (!(error instanceof Error)) 131 });
112 { 132 }
113 error = new Error(error.message); 133 });
114
115 // Add a more helpful stack trace.
116 error.stack = callStack;
117 }
118
119 rejectPromise(error);
120 }
121 else
122 {
123 resolvePromise(result);
124 }
125 });
126
127 return new Promise((resolve, reject) =>
128 {
129 resolvePromise = resolve;
130 rejectPromise = reject;
131 });
132 };
133
134 Object.defineProperty(object, name, descriptor);
135 } 134 }
136 135
137 function wrapRuntimeOnMessage() 136 function wrapRuntimeOnMessage()
138 { 137 {
139 let {onMessage} = browser.runtime; 138 let {onMessage} = browser.runtime;
140 let {addListener, removeListener} = onMessage; 139 let {addListener, removeListener} = onMessage;
141 140
142 onMessage.addListener = function(listener) 141 onMessage.addListener = function(listener)
143 { 142 {
144 if (typeof listener != "function") 143 if (typeof listener != "function")
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 227
229 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList 228 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
230 // didn't have iterator support before Chrome 51. 229 // didn't have iterator support before Chrome 51.
231 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 230 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
232 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) 231 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
233 { 232 {
234 if (!(Symbol.iterator in object.prototype)) 233 if (!(Symbol.iterator in object.prototype))
235 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; 234 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
236 } 235 }
237 } 236 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld