OLD | NEW |
1 // Copyright (C) 2011 Google Inc. | 1 // Copyright (C) 2011 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } | 96 } |
97 | 97 |
98 if (typeof WeakMap === 'function') { | 98 if (typeof WeakMap === 'function') { |
99 // assumed fine, so we're done. | 99 // assumed fine, so we're done. |
100 return; | 100 return; |
101 } | 101 } |
102 | 102 |
103 var hop = Object.prototype.hasOwnProperty; | 103 var hop = Object.prototype.hasOwnProperty; |
104 var gopn = Object.getOwnPropertyNames; | 104 var gopn = Object.getOwnPropertyNames; |
105 var defProp = Object.defineProperty; | 105 var defProp = Object.defineProperty; |
106 | 106 var isExtensible = Object.isExtensible; |
107 /** | |
108 * Holds the orginal static properties of the Object constructor, | |
109 * after repairES5 fixes these if necessary to be a more complete | |
110 * secureable ES5 environment, but before installing the following | |
111 * WeakMap emulation overrides and before any untrusted code runs. | |
112 */ | |
113 var originalProps = {}; | |
114 gopn(Object).forEach(function(name) { | |
115 originalProps[name] = Object[name]; | |
116 }); | |
117 | 107 |
118 /** | 108 /** |
119 * Security depends on HIDDEN_NAME being both <i>unguessable</i> and | 109 * Security depends on HIDDEN_NAME being both <i>unguessable</i> and |
120 * <i>undiscoverable</i> by untrusted code. | 110 * <i>undiscoverable</i> by untrusted code. |
121 * | 111 * |
122 * <p>Given the known weaknesses of Math.random() on existing | 112 * <p>Given the known weaknesses of Math.random() on existing |
123 * browsers, it does not generate unguessability we can be confident | 113 * browsers, it does not generate unguessability we can be confident |
124 * of. | 114 * of. |
125 * | 115 * |
126 * <p>It is the monkey patching logic in this file that is intended | 116 * <p>It is the monkey patching logic in this file that is intended |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 name.substr(name.length - 3) === '___'); | 177 name.substr(name.length - 3) === '___'); |
188 }); | 178 }); |
189 } | 179 } |
190 }); | 180 }); |
191 | 181 |
192 /** | 182 /** |
193 * getPropertyNames is not in ES5 but it is proposed for ES6 and | 183 * getPropertyNames is not in ES5 but it is proposed for ES6 and |
194 * does appear in our whitelist, so we need to clean it too. | 184 * does appear in our whitelist, so we need to clean it too. |
195 */ | 185 */ |
196 if ('getPropertyNames' in Object) { | 186 if ('getPropertyNames' in Object) { |
| 187 var originalGetPropertyNames = Object.getPropertyNames; |
197 defProp(Object, 'getPropertyNames', { | 188 defProp(Object, 'getPropertyNames', { |
198 value: function fakeGetPropertyNames(obj) { | 189 value: function fakeGetPropertyNames(obj) { |
199 return originalProps.getPropertyNames(obj).filter(function(name) { | 190 return originalGetPropertyNames(obj).filter(function(name) { |
200 return name !== HIDDEN_NAME; | 191 return name !== HIDDEN_NAME; |
201 }); | 192 }); |
202 } | 193 } |
203 }); | 194 }); |
204 } | 195 } |
205 | 196 |
206 /** | 197 /** |
207 * <p>To treat objects as identity-keys with reasonable efficiency | 198 * <p>To treat objects as identity-keys with reasonable efficiency |
208 * on ES5 by itself (i.e., without any object-keyed collections), we | 199 * on ES5 by itself (i.e., without any object-keyed collections), we |
209 * need to add a hidden property to such key objects when we | 200 * need to add a hidden property to such key objects when we |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 * use the key object itself to represent its identity in a brute | 232 * use the key object itself to represent its identity in a brute |
242 * force leaky map stored in the weak map, losing all the advantages | 233 * force leaky map stored in the weak map, losing all the advantages |
243 * of weakness for these. | 234 * of weakness for these. |
244 */ | 235 */ |
245 function getHiddenRecord(key) { | 236 function getHiddenRecord(key) { |
246 if (key !== Object(key)) { | 237 if (key !== Object(key)) { |
247 throw new TypeError('Not an object: ' + key); | 238 throw new TypeError('Not an object: ' + key); |
248 } | 239 } |
249 var hiddenRecord = key[HIDDEN_NAME]; | 240 var hiddenRecord = key[HIDDEN_NAME]; |
250 if (hiddenRecord && hiddenRecord.key === key) { return hiddenRecord; } | 241 if (hiddenRecord && hiddenRecord.key === key) { return hiddenRecord; } |
251 if (!originalProps.isExtensible(key)) { | 242 if (!isExtensible(key)) { |
252 // Weak map must brute force, as explained in doc-comment above. | 243 // Weak map must brute force, as explained in doc-comment above. |
253 return void 0; | 244 return void 0; |
254 } | 245 } |
255 var gets = []; | 246 var gets = []; |
256 var vals = []; | 247 var vals = []; |
257 hiddenRecord = { | 248 hiddenRecord = { |
258 key: key, // self pointer for quick own check above. | 249 key: key, // self pointer for quick own check above. |
259 gets: gets, // get___ methods identifying weak maps | 250 gets: gets, // get___ methods identifying weak maps |
260 vals: vals // values associated with this key in each | 251 vals: vals // values associated with this key in each |
261 // corresponding weak map. | 252 // corresponding weak map. |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 */ | 436 */ |
446 value: function remove(key) { | 437 value: function remove(key) { |
447 return this.delete___(key); | 438 return this.delete___(key); |
448 }, | 439 }, |
449 writable: true, | 440 writable: true, |
450 configurable: true | 441 configurable: true |
451 } | 442 } |
452 }); | 443 }); |
453 | 444 |
454 })(); | 445 })(); |
OLD | NEW |