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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/typedObjects/utils.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/typedObjects/utils.js
@@ -0,0 +1,131 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+"use strict";
+
+/**
+ * Calculates the binary logarithm (position of highest bit) of an integer.
+ * Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
+ */
+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.
+{
+ num = num | 0;
+ let result = 0;
+ while (num >>= 1)
+ result++;
+ return result;
+};
+
+/**
+ * Round up a 32-bit integer to the next power of two.
+ * Source: See http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
+ */
+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
+{
+ num = num | 0;
+ num--;
+ num |= num >> 1;
+ num |= num >> 2;
+ num |= num >> 4;
+ num |= num >> 8;
+ num |= num >> 16;
+ return num + 1;
+};
+
+/**
+ * Returns a property descriptor for an immutable property with given value.
+ */
+exports.fixedPropertyDescriptor = function fixedPropertyDescriptor(value) /**Object*/
+{
+ return {
+ value: value,
+ writable: false,
+ configurable: false,
+ enumerable: true
+ };
+};
+
+/**
+ * Generates the array of views that need to be passed as parameters for getters
+ * and setters of a given type.
+ *
+ * @param type
+ * @param viewTypes array of existing view types (will be extended if necessary)
+ * @param views array containing a views arrays for each view type (will be
+ * extended if necessary)
+ * @return array of views required for the given type
+ */
+let getViewsForType = exports.getViewsForType = function getViewsForType(type, viewTypes, views)
+{
+ let requiredViews = type.viewTypes;
+ let result = [];
+ for (let i = 0, l = requiredViews.length | 0; i < l; i++)
+ {
+ let viewType = requiredViews[i];
+ let index = viewTypes.indexOf(viewType) | 0;
+ if (index < 0)
+ {
+ index = (viewTypes.push(viewType) | 0) - 1;
+ views.push([]);
+ }
+ result.push(views[index]);
+ }
+ return result;
+};
+
+/**
+ * Defines properties with given name and type on an object.
+ *
+ * @param obj object to define properties on
+ * @param propList array of properties where each property is represented by
+ * [name, type]
+ * @param viewTypes see getViewsForType()
+ * @param views see getViewsForType()
+ * @param [offset] byte array offset at which the properties should start
+ * @return new start offset for additional properties
+ */
+exports.defineProperties = function defineProperties(obj, propList, viewTypes, views, offset)
+{
+ offset = offset | 0;
+
+ // Put larger properties first to make sure alignment requirements are met.
+ propList.sort(function(a, b)
+ {
+ return b[1].referenceLength - a[1].referenceLength;
+ });
+
+ // Generates getters and setters for each property.
+ let descriptors = {};
+ for (let i = 0, l = propList.length | 0; i < l; i++)
+ {
+ let [name, type] = propList[i];
+
+ let viewParams = getViewsForType(type, viewTypes, views);
+ descriptors[name] = {
+ get: type.createGetter.apply(type, [offset].concat(viewParams)),
+ set: type.createSetter.apply(type, [offset].concat(viewParams)),
+ configurable: false,
+ enumerable: true
+ };
+ offset += type.referenceLength;
+ }
+
+ // Define properties
+ Object.defineProperties(obj, descriptors);
+
+ return offset;
+};

Powered by Google App Engine
This is Rietveld