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

Unified Diff: lib/typedObjects/primitiveTypes.js

Issue 4827963358969856: Issue 147 - [Typed objects] Implement object types (Closed)
Patch Set: Fixed comments and one more issue Created April 22, 2014, 3:10 p.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
« no previous file with comments | « lib/typedObjects/objectTypes.js ('k') | lib/typedObjects/references.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/typedObjects/primitiveTypes.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/typedObjects/primitiveTypes.js
@@ -0,0 +1,65 @@
+/*
+ * 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";
+
+let {ilog2} = require("typedObjects/utils");
+
+function createGetter(shift, offset, view)
+{
+ shift = shift | 0;
+ offset = offset | 0;
+ offset >>= shift;
+ return function()
+ {
+ return view[this.bufferIndex][((this.byteOffset | 0) >> shift) + offset];
+ };
+}
+
+function createSetter(shift, offset, view)
+{
+ shift = shift | 0;
+ offset = offset | 0;
+ offset >>= shift;
+ return function(value)
+ {
+ view[this.bufferIndex][((this.byteOffset | 0) >> shift) + offset] = value;
+ };
+}
+
+function PrimitiveType(viewType)
+{
+ let result = Object.create(PrimitiveType.prototype);
+ result.viewTypes = [viewType];
+ result.byteLength = result.referenceLength = viewType.BYTES_PER_ELEMENT | 0;
+
+ let offsetShift = ilog2(result.byteLength) | 0;
+ result.createGetter = createGetter.bind(null, offsetShift);
+ result.createSetter = createSetter.bind(null, offsetShift);
+ Object.freeze(result);
+ return result;
+}
+
+exports.uint8 = exports.boolean = new PrimitiveType(Uint8Array);
+exports.uint8clamped = new PrimitiveType(Uint8ClampedArray);
+exports.int8 = new PrimitiveType(Int8Array);
+exports.uint16 = new PrimitiveType(Uint16Array);
+exports.int16 = new PrimitiveType(Int16Array);
+exports.uint32 = new PrimitiveType(Uint32Array);
+exports.int32 = new PrimitiveType(Int32Array);
+exports.float32 = new PrimitiveType(Float32Array);
+exports.float64 = new PrimitiveType(Float64Array);
« no previous file with comments | « lib/typedObjects/objectTypes.js ('k') | lib/typedObjects/references.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld