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

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

Issue 5728072976302080: Issue 151 - [Typed objects] Implement dynamically-sized array types (Closed)
Patch Set: Use Uint8Array as constructor, not a function Created May 19, 2014, 7:57 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
« no previous file with comments | « lib/typedObjects/objectTypes.js ('k') | lib/typedObjects/utils.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 let {ilog2} = require("typedObjects/utils"); 20 let {ilog2} = require("typedObjects/utils");
21 let {createArrayType} = require("typedObjects/arrayTypes");
21 22
22 function createGetter(shift, offset, view) 23 function createGetter(shift, offset, view)
23 { 24 {
24 shift = shift | 0; 25 shift = shift | 0;
25 offset = offset | 0; 26 offset = offset | 0;
26 offset >>= shift; 27 offset >>= shift;
27 return function() 28 return function(bufferIndex, byteOffset)
28 { 29 {
29 return view[this.bufferIndex][((this.byteOffset | 0) >> shift) + offset]; 30 bufferIndex = bufferIndex | 0;
31 byteOffset = byteOffset | 0;
32 return view[bufferIndex][(byteOffset >> shift) + offset];
30 }; 33 };
31 } 34 }
32 35
33 function createSetter(shift, offset, view) 36 function createSetter(shift, offset, view)
34 { 37 {
35 shift = shift | 0; 38 shift = shift | 0;
36 offset = offset | 0; 39 offset = offset | 0;
37 offset >>= shift; 40 offset >>= shift;
38 return function(value) 41 return function(bufferIndex, byteOffset, value)
39 { 42 {
40 view[this.bufferIndex][((this.byteOffset | 0) >> shift) + offset] = value; 43 bufferIndex = bufferIndex | 0;
44 byteOffset = byteOffset | 0;
45 view[bufferIndex][(byteOffset >> shift) + offset] = value;
41 }; 46 };
42 } 47 }
43 48
44 function PrimitiveType(viewType) 49 function PrimitiveType(viewType)
45 { 50 {
46 let result = Object.create(PrimitiveType.prototype); 51 let result = Object.create(PrimitiveType.prototype);
47 result.viewTypes = [viewType]; 52 result.viewTypes = [viewType];
48 result.byteLength = result.referenceLength = viewType.BYTES_PER_ELEMENT | 0; 53 result.byteLength = result.referenceLength = viewType.BYTES_PER_ELEMENT | 0;
49 54
50 let offsetShift = ilog2(result.byteLength) | 0; 55 let offsetShift = ilog2(result.byteLength) | 0;
51 result.createGetter = createGetter.bind(null, offsetShift); 56 result.createGetter = createGetter.bind(null, offsetShift);
52 result.createSetter = createSetter.bind(null, offsetShift); 57 result.createSetter = createSetter.bind(null, offsetShift);
58 // Note: this is a pretty inefficient way to zero out initial values. We
59 // should consider using ArrayBuffer.fill(0) once it becomes available
60 // (https://bugzilla.mozilla.org/show_bug.cgi?id=730880).
61 result.cleanupValue = 0;
62 result.Array = createArrayType.bind(null, result);
53 Object.freeze(result); 63 Object.freeze(result);
54 return result; 64 return result;
55 } 65 }
56 66
57 exports.uint8 = exports.boolean = new PrimitiveType(Uint8Array); 67 exports.uint8 = exports.boolean = new PrimitiveType(Uint8Array);
58 exports.uint8clamped = new PrimitiveType(Uint8ClampedArray); 68 exports.uint8clamped = new PrimitiveType(Uint8ClampedArray);
59 exports.int8 = new PrimitiveType(Int8Array); 69 exports.int8 = new PrimitiveType(Int8Array);
60 exports.uint16 = new PrimitiveType(Uint16Array); 70 exports.uint16 = new PrimitiveType(Uint16Array);
61 exports.int16 = new PrimitiveType(Int16Array); 71 exports.int16 = new PrimitiveType(Int16Array);
62 exports.uint32 = new PrimitiveType(Uint32Array); 72 exports.uint32 = new PrimitiveType(Uint32Array);
63 exports.int32 = new PrimitiveType(Int32Array); 73 exports.int32 = new PrimitiveType(Int32Array);
64 exports.float32 = new PrimitiveType(Float32Array); 74 exports.float32 = new PrimitiveType(Float32Array);
65 exports.float64 = new PrimitiveType(Float64Array); 75 exports.float64 = new PrimitiveType(Float64Array);
OLDNEW
« no previous file with comments | « lib/typedObjects/objectTypes.js ('k') | lib/typedObjects/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld