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

Side by Side Diff: lib/typedObjects/references.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 let {ceilLog2, defineProperties} = require("typedObjects/utils");
21 let {uint16, int16, uint32} = require("typedObjects/primitiveTypes");
22 let {fixedPropertyDescriptor} = require("typedObjects/utils");
23
24 function calculateSize(propList)
25 {
26 let result = 0;
27 for (let i = 0, l = propList.length | 0; i < l; i++)
28 result += propList[i][1].referenceLength;
29 return ceilLog2(result);
30 }
31
32 function getViewTypes(propList)
33 {
34 let result = [];
35 for (let i = 0, l = propList.length | 0; i < l; i++)
36 {
37 let requiredViews = propList[i][1].viewTypes;
38 for (let j = 0, ll = requiredViews.length | 0; j < ll; j++)
39 if (result.indexOf(requiredViews[j]) < 0)
40 result.push(requiredViews[j]);
41 }
42 return result;
43 }
44
45 let TypedReference_properties = [
46 ["targetBufferIndex", int16],
47 ["targetByteOffset", uint32]
48 ];
49
50 let Reference_properties = [
51 ["typeId", int16]
52 ].concat(TypedReference_properties);
53
54 /**
55 * Helper class to read/write properties referencing other objects. bufferIndex
56 * and byteOffset properties of the reference need to be set in order to use it.
57 *
58 * @param types list of registered object types used to resolve typeId
59 * @param views list of views corresponding to Reference.viewTypes
60 */
61 function Reference(types, views)
62 {
63 let result = Object.create(Reference.prototype, {
64 types: fixedPropertyDescriptor(types)
65 });
66 defineProperties(result, Reference_properties, Reference.viewTypes, views, 0);
67 return result;
68 }
69 Reference.prototype = {
70 get typeInfo()
71 {
72 let typeId = this.typeId | 0;
73 if (this.typeId >= 0)
74 return this.types[this.typeId];
75 else
76 return null;
77 },
78 bufferIndex: -1,
79 byteOffset: 0
80 }
81 Reference.byteLength = calculateSize(Reference_properties);
82 Reference.viewTypes = Object.freeze(getViewTypes(Reference_properties));
83 exports.Reference = Reference;
84
85 /**
86 * Helper class to read/write references to a fixed type, this is useful for
87 * references to free buffer elements. bufferIndex and byteOffset properites
88 * of the reference need to be set in order to use it.
89 *
90 * @param typeInfo metadata of the type that this reference should be used for
91 * @param views list of views corresponding to TypedReference.viewTypes
92 */
93 function TypedReference(typeInfo, views)
94 {
95 let result = Object.create(Reference.prototype, {
96 typeInfo: fixedPropertyDescriptor(typeInfo),
97 });
98 defineProperties(result, TypedReference_properties, TypedReference.viewTypes, views, 0);
99 return result;
100 }
101 TypedReference.byteLength = calculateSize(TypedReference_properties);
102 TypedReference.viewTypes = Object.freeze(getViewTypes(TypedReference_properties) );
103 exports.TypedReference = TypedReference;
OLDNEW

Powered by Google App Engine
This is Rietveld