base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / mojo / public / js / buffer.js
blobe35f69513fb85b0c5be70c7e78fc4fb18c9e88a0
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 define("mojo/public/js/buffer", function() {
7 var kHostIsLittleEndian = (function () {
8 var endianArrayBuffer = new ArrayBuffer(2);
9 var endianUint8Array = new Uint8Array(endianArrayBuffer);
10 var endianUint16Array = new Uint16Array(endianArrayBuffer);
11 endianUint16Array[0] = 1;
12 return endianUint8Array[0] == 1;
13 })();
15 var kHighWordMultiplier = 0x100000000;
17 function Buffer(sizeOrArrayBuffer) {
18 if (sizeOrArrayBuffer instanceof ArrayBuffer)
19 this.arrayBuffer = sizeOrArrayBuffer;
20 else
21 this.arrayBuffer = new ArrayBuffer(sizeOrArrayBuffer);
23 this.dataView = new DataView(this.arrayBuffer);
24 this.next = 0;
27 Object.defineProperty(Buffer.prototype, "byteLength", {
28 get: function() { return this.arrayBuffer.byteLength; }
29 });
31 Buffer.prototype.alloc = function(size) {
32 var pointer = this.next;
33 this.next += size;
34 if (this.next > this.byteLength) {
35 var newSize = (1.5 * (this.byteLength + size)) | 0;
36 this.grow(newSize);
38 return pointer;
41 function copyArrayBuffer(dstArrayBuffer, srcArrayBuffer) {
42 (new Uint8Array(dstArrayBuffer)).set(new Uint8Array(srcArrayBuffer));
45 Buffer.prototype.grow = function(size) {
46 var newArrayBuffer = new ArrayBuffer(size);
47 copyArrayBuffer(newArrayBuffer, this.arrayBuffer);
48 this.arrayBuffer = newArrayBuffer;
49 this.dataView = new DataView(this.arrayBuffer);
52 Buffer.prototype.trim = function() {
53 this.arrayBuffer = this.arrayBuffer.slice(0, this.next);
54 this.dataView = new DataView(this.arrayBuffer);
57 Buffer.prototype.getUint8 = function(offset) {
58 return this.dataView.getUint8(offset);
60 Buffer.prototype.getUint16 = function(offset) {
61 return this.dataView.getUint16(offset, kHostIsLittleEndian);
63 Buffer.prototype.getUint32 = function(offset) {
64 return this.dataView.getUint32(offset, kHostIsLittleEndian);
66 Buffer.prototype.getUint64 = function(offset) {
67 var lo, hi;
68 if (kHostIsLittleEndian) {
69 lo = this.dataView.getUint32(offset, kHostIsLittleEndian);
70 hi = this.dataView.getUint32(offset + 4, kHostIsLittleEndian);
71 } else {
72 hi = this.dataView.getUint32(offset, kHostIsLittleEndian);
73 lo = this.dataView.getUint32(offset + 4, kHostIsLittleEndian);
75 return lo + hi * kHighWordMultiplier;
78 Buffer.prototype.getInt8 = function(offset) {
79 return this.dataView.getInt8(offset);
81 Buffer.prototype.getInt16 = function(offset) {
82 return this.dataView.getInt16(offset, kHostIsLittleEndian);
84 Buffer.prototype.getInt32 = function(offset) {
85 return this.dataView.getInt32(offset, kHostIsLittleEndian);
87 Buffer.prototype.getInt64 = function(offset) {
88 var lo, hi;
89 if (kHostIsLittleEndian) {
90 lo = this.dataView.getUint32(offset, kHostIsLittleEndian);
91 hi = this.dataView.getInt32(offset + 4, kHostIsLittleEndian);
92 } else {
93 hi = this.dataView.getInt32(offset, kHostIsLittleEndian);
94 lo = this.dataView.getUint32(offset + 4, kHostIsLittleEndian);
96 return lo + hi * kHighWordMultiplier;
99 Buffer.prototype.getFloat32 = function(offset) {
100 return this.dataView.getFloat32(offset, kHostIsLittleEndian);
102 Buffer.prototype.getFloat64 = function(offset) {
103 return this.dataView.getFloat64(offset, kHostIsLittleEndian);
106 Buffer.prototype.setUint8 = function(offset, value) {
107 this.dataView.setUint8(offset, value);
109 Buffer.prototype.setUint16 = function(offset, value) {
110 this.dataView.setUint16(offset, value, kHostIsLittleEndian);
112 Buffer.prototype.setUint32 = function(offset, value) {
113 this.dataView.setUint32(offset, value, kHostIsLittleEndian);
115 Buffer.prototype.setUint64 = function(offset, value) {
116 var hi = (value / kHighWordMultiplier) | 0;
117 if (kHostIsLittleEndian) {
118 this.dataView.setInt32(offset, value, kHostIsLittleEndian);
119 this.dataView.setInt32(offset + 4, hi, kHostIsLittleEndian);
120 } else {
121 this.dataView.setInt32(offset, hi, kHostIsLittleEndian);
122 this.dataView.setInt32(offset + 4, value, kHostIsLittleEndian);
126 Buffer.prototype.setInt8 = function(offset, value) {
127 this.dataView.setInt8(offset, value);
129 Buffer.prototype.setInt16 = function(offset, value) {
130 this.dataView.setInt16(offset, value, kHostIsLittleEndian);
132 Buffer.prototype.setInt32 = function(offset, value) {
133 this.dataView.setInt32(offset, value, kHostIsLittleEndian);
135 Buffer.prototype.setInt64 = function(offset, value) {
136 var hi = Math.floor(value / kHighWordMultiplier);
137 if (kHostIsLittleEndian) {
138 this.dataView.setInt32(offset, value, kHostIsLittleEndian);
139 this.dataView.setInt32(offset + 4, hi, kHostIsLittleEndian);
140 } else {
141 this.dataView.setInt32(offset, hi, kHostIsLittleEndian);
142 this.dataView.setInt32(offset + 4, value, kHostIsLittleEndian);
146 Buffer.prototype.setFloat32 = function(offset, value) {
147 this.dataView.setFloat32(offset, value, kHostIsLittleEndian);
149 Buffer.prototype.setFloat64 = function(offset, value) {
150 this.dataView.setFloat64(offset, value, kHostIsLittleEndian);
153 var exports = {};
154 exports.Buffer = Buffer;
155 return exports;