• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5(function() {
6  var internal = mojo.internal;
7
8  var kHostIsLittleEndian = (function () {
9    var endianArrayBuffer = new ArrayBuffer(2);
10    var endianUint8Array = new Uint8Array(endianArrayBuffer);
11    var endianUint16Array = new Uint16Array(endianArrayBuffer);
12    endianUint16Array[0] = 1;
13    return endianUint8Array[0] == 1;
14  })();
15
16  var kHighWordMultiplier = 0x100000000;
17
18  function Buffer(sizeOrArrayBuffer) {
19    if (sizeOrArrayBuffer instanceof ArrayBuffer)
20      this.arrayBuffer = sizeOrArrayBuffer;
21    else
22      this.arrayBuffer = new ArrayBuffer(sizeOrArrayBuffer);
23
24    this.dataView = new DataView(this.arrayBuffer);
25    this.next = 0;
26  }
27
28  Object.defineProperty(Buffer.prototype, "byteLength", {
29    get: function() { return this.arrayBuffer.byteLength; }
30  });
31
32  Buffer.prototype.alloc = function(size) {
33    var pointer = this.next;
34    this.next += size;
35    if (this.next > this.byteLength) {
36      var newSize = (1.5 * (this.byteLength + size)) | 0;
37      this.grow(newSize);
38    }
39    return pointer;
40  };
41
42  function copyArrayBuffer(dstArrayBuffer, srcArrayBuffer) {
43    (new Uint8Array(dstArrayBuffer)).set(new Uint8Array(srcArrayBuffer));
44  }
45
46  Buffer.prototype.grow = function(size) {
47    var newArrayBuffer = new ArrayBuffer(size);
48    copyArrayBuffer(newArrayBuffer, this.arrayBuffer);
49    this.arrayBuffer = newArrayBuffer;
50    this.dataView = new DataView(this.arrayBuffer);
51  };
52
53  Buffer.prototype.trim = function() {
54    this.arrayBuffer = this.arrayBuffer.slice(0, this.next);
55    this.dataView = new DataView(this.arrayBuffer);
56  };
57
58  Buffer.prototype.getUint8 = function(offset) {
59    return this.dataView.getUint8(offset);
60  }
61  Buffer.prototype.getUint16 = function(offset) {
62    return this.dataView.getUint16(offset, kHostIsLittleEndian);
63  }
64  Buffer.prototype.getUint32 = function(offset) {
65    return this.dataView.getUint32(offset, kHostIsLittleEndian);
66  }
67  Buffer.prototype.getUint64 = function(offset) {
68    var lo, hi;
69    if (kHostIsLittleEndian) {
70      lo = this.dataView.getUint32(offset, kHostIsLittleEndian);
71      hi = this.dataView.getUint32(offset + 4, kHostIsLittleEndian);
72    } else {
73      hi = this.dataView.getUint32(offset, kHostIsLittleEndian);
74      lo = this.dataView.getUint32(offset + 4, kHostIsLittleEndian);
75    }
76    return lo + hi * kHighWordMultiplier;
77  }
78
79  Buffer.prototype.getInt8 = function(offset) {
80    return this.dataView.getInt8(offset);
81  }
82  Buffer.prototype.getInt16 = function(offset) {
83    return this.dataView.getInt16(offset, kHostIsLittleEndian);
84  }
85  Buffer.prototype.getInt32 = function(offset) {
86    return this.dataView.getInt32(offset, kHostIsLittleEndian);
87  }
88  Buffer.prototype.getInt64 = function(offset) {
89    var lo, hi;
90    if (kHostIsLittleEndian) {
91      lo = this.dataView.getUint32(offset, kHostIsLittleEndian);
92      hi = this.dataView.getInt32(offset + 4, kHostIsLittleEndian);
93    } else {
94      hi = this.dataView.getInt32(offset, kHostIsLittleEndian);
95      lo = this.dataView.getUint32(offset + 4, kHostIsLittleEndian);
96    }
97    return lo + hi * kHighWordMultiplier;
98  }
99
100  Buffer.prototype.getFloat32 = function(offset) {
101    return this.dataView.getFloat32(offset, kHostIsLittleEndian);
102  }
103  Buffer.prototype.getFloat64 = function(offset) {
104    return this.dataView.getFloat64(offset, kHostIsLittleEndian);
105  }
106
107  Buffer.prototype.setUint8 = function(offset, value) {
108    this.dataView.setUint8(offset, value);
109  }
110  Buffer.prototype.setUint16 = function(offset, value) {
111    this.dataView.setUint16(offset, value, kHostIsLittleEndian);
112  }
113  Buffer.prototype.setUint32 = function(offset, value) {
114    this.dataView.setUint32(offset, value, kHostIsLittleEndian);
115  }
116  Buffer.prototype.setUint64 = function(offset, value) {
117    var hi = (value / kHighWordMultiplier) | 0;
118    if (kHostIsLittleEndian) {
119      this.dataView.setInt32(offset, value, kHostIsLittleEndian);
120      this.dataView.setInt32(offset + 4, hi, kHostIsLittleEndian);
121    } else {
122      this.dataView.setInt32(offset, hi, kHostIsLittleEndian);
123      this.dataView.setInt32(offset + 4, value, kHostIsLittleEndian);
124    }
125  }
126
127  Buffer.prototype.setInt8 = function(offset, value) {
128    this.dataView.setInt8(offset, value);
129  }
130  Buffer.prototype.setInt16 = function(offset, value) {
131    this.dataView.setInt16(offset, value, kHostIsLittleEndian);
132  }
133  Buffer.prototype.setInt32 = function(offset, value) {
134    this.dataView.setInt32(offset, value, kHostIsLittleEndian);
135  }
136  Buffer.prototype.setInt64 = function(offset, value) {
137    var hi = Math.floor(value / kHighWordMultiplier);
138    if (kHostIsLittleEndian) {
139      this.dataView.setInt32(offset, value, kHostIsLittleEndian);
140      this.dataView.setInt32(offset + 4, hi, kHostIsLittleEndian);
141    } else {
142      this.dataView.setInt32(offset, hi, kHostIsLittleEndian);
143      this.dataView.setInt32(offset + 4, value, kHostIsLittleEndian);
144    }
145  }
146
147  Buffer.prototype.setFloat32 = function(offset, value) {
148    this.dataView.setFloat32(offset, value, kHostIsLittleEndian);
149  }
150  Buffer.prototype.setFloat64 = function(offset, value) {
151    this.dataView.setFloat64(offset, value, kHostIsLittleEndian);
152  }
153
154  internal.Buffer = Buffer;
155})();
156