OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 }; | 88 }; |
89 | 89 |
90 /** | 90 /** |
91 * Defines properties with given name and type on an object. | 91 * Defines properties with given name and type on an object. |
92 * | 92 * |
93 * @param obj object to define properties on | 93 * @param obj object to define properties on |
94 * @param properties object mapping property names to their respective types | 94 * @param properties object mapping property names to their respective types |
95 * @param viewTypes see getViewsForType() | 95 * @param viewTypes see getViewsForType() |
96 * @param views see getViewsForType() | 96 * @param views see getViewsForType() |
97 * @param [offset] byte array offset at which the properties should start | 97 * @param [offset] byte array offset at which the properties should start |
| 98 * @param [cleanupValues] array of property/value combinations to be set when th
e object is created or destroyed |
98 * @return new start offset for additional properties | 99 * @return new start offset for additional properties |
99 */ | 100 */ |
100 exports.defineProperties = function defineProperties(obj, properties, viewTypes,
views, offset) | 101 exports.defineProperties = function defineProperties(obj, properties, viewTypes,
views, offset, cleanupValues) |
101 { | 102 { |
102 offset = offset | 0; | 103 offset = offset | 0; |
103 | 104 |
104 let propList = []; | 105 let propList = []; |
105 for (let name in properties) | 106 for (let name in properties) |
106 propList.push([name, properties[name]]); | 107 propList.push([name, properties[name]]); |
107 | 108 |
108 // Put larger properties first to make sure alignment requirements are met. | 109 // Put larger properties first to make sure alignment requirements are met. |
109 propList.sort(function(a, b) | 110 propList.sort(function(a, b) |
110 { | 111 { |
111 return b[1].referenceLength - a[1].referenceLength; | 112 return b[1].referenceLength - a[1].referenceLength; |
112 }); | 113 }); |
113 | 114 |
114 // Generates getters and setters for each property. | 115 // Generates getters and setters for each property. |
115 let descriptors = {}; | 116 let descriptors = {}; |
116 for (let i = 0, l = propList.length | 0; i < l; i++) | 117 for (let i = 0, l = propList.length | 0; i < l; i++) |
117 { | 118 { |
118 let [name, type] = propList[i]; | 119 let [name, type] = propList[i]; |
119 | 120 |
120 let viewParams = getViewsForType(type, viewTypes, views); | 121 let viewParams = getViewsForType(type, viewTypes, views); |
121 descriptors[name] = { | 122 descriptors[name] = { |
122 get: type.createGetter.apply(type, [offset].concat(viewParams)), | 123 get: type.createGetter.apply(type, [offset].concat(viewParams)), |
123 set: type.createSetter.apply(type, [offset].concat(viewParams)), | 124 set: type.createSetter.apply(type, [offset].concat(viewParams)), |
124 configurable: false, | 125 configurable: false, |
125 enumerable: true | 126 enumerable: true |
126 }; | 127 }; |
127 offset += type.referenceLength; | 128 offset += type.referenceLength; |
| 129 if (cleanupValues && typeof type.cleanupValue != "undefined") |
| 130 cleanupValues.push([name, type.cleanupValue]); |
128 } | 131 } |
129 | 132 |
130 // Define properties | 133 // Define properties |
131 Object.defineProperties(obj, descriptors); | 134 Object.defineProperties(obj, descriptors); |
132 | 135 |
133 return offset; | 136 return offset; |
134 }; | 137 }; |
OLD | NEW |