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

Side by Side Diff: lib/typedObjects/utils.js

Issue 4827963358969856: Issue 147 - [Typed objects] Implement object types (Closed)
Patch Set: Created April 4, 2014, 10:28 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 /**
21 * Calculates the binary logarithm (position of highest bit) of an integer.
22 * Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
23 */
24 exports.log2 = function log2(/**Integer*/ num) /**Integer*/
René Jeschke 2014/04/21 17:24:12 I'm not really sure is this is the right name for
Wladimir Palant 2014/04/22 15:25:05 Changed name into ilog2 but I didn't really feel l
René Jeschke 2014/04/22 15:40:34 Alright then.
25 {
26 num = num | 0;
27 let result = 0;
28 while (num >>= 1)
29 result++;
30 return result;
31 };
32
33 /**
34 * Round up a 32-bit integer to the next power of two.
35 * Source: See http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerO f2
36 */
37 exports.ceilLog2 = function ceilLog2(/**Integer*/ num) /**Integer*/
René Jeschke 2014/04/21 17:24:12 See above for naming issues, maybe just call it 'n
Wladimir Palant 2014/04/22 15:25:05 Renamed into nextPow2
38 {
39 num = num | 0;
40 num--;
41 num |= num >> 1;
42 num |= num >> 2;
43 num |= num >> 4;
44 num |= num >> 8;
45 num |= num >> 16;
46 return num + 1;
47 };
48
49 /**
50 * Returns a property descriptor for an immutable property with given value.
51 */
52 exports.fixedPropertyDescriptor = function fixedPropertyDescriptor(value) /**Obj ect*/
53 {
54 return {
55 value: value,
56 writable: false,
57 configurable: false,
58 enumerable: true
59 };
60 };
61
62 /**
63 * Generates the array of views that need to be passed as parameters for getters
64 * and setters of a given type.
65 *
66 * @param type
67 * @param viewTypes array of existing view types (will be extended if necessary)
68 * @param views array containing a views arrays for each view type (will be
69 * extended if necessary)
70 * @return array of views required for the given type
71 */
72 let getViewsForType = exports.getViewsForType = function getViewsForType(type, v iewTypes, views)
73 {
74 let requiredViews = type.viewTypes;
75 let result = [];
76 for (let i = 0, l = requiredViews.length | 0; i < l; i++)
77 {
78 let viewType = requiredViews[i];
79 let index = viewTypes.indexOf(viewType) | 0;
80 if (index < 0)
81 {
82 index = (viewTypes.push(viewType) | 0) - 1;
83 views.push([]);
84 }
85 result.push(views[index]);
86 }
87 return result;
88 };
89
90 /**
91 * Defines properties with given name and type on an object.
92 *
93 * @param obj object to define properties on
94 * @param propList array of properties where each property is represented by
95 * [name, type]
96 * @param viewTypes see getViewsForType()
97 * @param views see getViewsForType()
98 * @param [offset] byte array offset at which the properties should start
99 * @return new start offset for additional properties
100 */
101 exports.defineProperties = function defineProperties(obj, propList, viewTypes, v iews, offset)
102 {
103 offset = offset | 0;
104
105 // Put larger properties first to make sure alignment requirements are met.
106 propList.sort(function(a, b)
107 {
108 return b[1].referenceLength - a[1].referenceLength;
109 });
110
111 // Generates getters and setters for each property.
112 let descriptors = {};
113 for (let i = 0, l = propList.length | 0; i < l; i++)
114 {
115 let [name, type] = propList[i];
116
117 let viewParams = getViewsForType(type, viewTypes, views);
118 descriptors[name] = {
119 get: type.createGetter.apply(type, [offset].concat(viewParams)),
120 set: type.createSetter.apply(type, [offset].concat(viewParams)),
121 configurable: false,
122 enumerable: true
123 };
124 offset += type.referenceLength;
125 }
126
127 // Define properties
128 Object.defineProperties(obj, descriptors);
129
130 return offset;
131 };
OLDNEW

Powered by Google App Engine
This is Rietveld