Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: mojo/public/js/bindings/buffer.js

Issue 411553003: Validate incoming JS Message Headers: test message parser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Factored common Buffer class, moved JS binding tests Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 define([], function () {
abarth-chromium 2014/07/23 02:13:51 I think you can just omit [] when it's empty.
hansmuller 2014/07/23 18:06:35 Done.
6
7 function Buffer(sizeOrArrayBuffer) {
8 if (sizeOrArrayBuffer instanceof ArrayBuffer)
9 this.arrayBuffer = sizeOrArrayBuffer;
10 else
11 this.arrayBuffer = new ArrayBuffer(sizeOrArrayBuffer);
12
13 this.dataView = new DataView(this.arrayBuffer);
14 this.byteLength = this.arrayBuffer.byteLength;
yzshen1 2014/07/23 07:26:55 Maybe we could make a getter which simply reads th
hansmuller 2014/07/23 18:06:35 Good point.
15 this.next = 0;
16 }
17
18 var kHostIsLittleEndian = (function () {
yzshen1 2014/07/23 07:26:55 (Not familiar with JS style.) Maybe we should ord
hansmuller 2014/07/23 18:06:35 OK.
19 var endianArrayBuffer = new ArrayBuffer(2);
20 var endianUint8Array = new Uint8Array(endianArrayBuffer);
21 var endianUint16Array = new Uint16Array(endianArrayBuffer);
22 endianUint16Array[0] = 1;
23 return endianUint8Array[0] == 1;
24 })();
25
26 var kHighWordMultiplier = 0x100000000;
27
28 function getInt64(dataView, byteOffset, value) {
29 var lo, hi;
30 if (kHostIsLittleEndian) {
31 lo = dataView.getUint32(byteOffset, kHostIsLittleEndian);
32 hi = dataView.getInt32(byteOffset + 4, kHostIsLittleEndian);
33 } else {
34 hi = dataView.getInt32(byteOffset, kHostIsLittleEndian);
35 lo = dataView.getUint32(byteOffset + 4, kHostIsLittleEndian);
36 }
37 return lo + hi * kHighWordMultiplier;
38 }
39
40 function getUint64(dataView, byteOffset, value) {
41 var lo, hi;
42 if (kHostIsLittleEndian) {
43 lo = dataView.getUint32(byteOffset, kHostIsLittleEndian);
44 hi = dataView.getUint32(byteOffset + 4, kHostIsLittleEndian);
45 } else {
46 hi = dataView.getUint32(byteOffset, kHostIsLittleEndian);
47 lo = dataView.getUint32(byteOffset + 4, kHostIsLittleEndian);
48 }
49 return lo + hi * kHighWordMultiplier;
50 }
51
52 function setInt64(dataView, byteOffset, value) {
53 var hi = Math.floor(value / kHighWordMultiplier);
54 if (kHostIsLittleEndian) {
55 dataView.setInt32(byteOffset, value, kHostIsLittleEndian);
56 dataView.setInt32(byteOffset + 4, hi, kHostIsLittleEndian);
57 } else {
58 dataView.setInt32(byteOffset, hi, kHostIsLittleEndian);
59 dataView.setInt32(byteOffset + 4, value, kHostIsLittleEndian);
60 }
61 }
62
63 function setUint64(dataView, byteOffset, value) {
64 var hi = (value / kHighWordMultiplier) | 0;
65 if (kHostIsLittleEndian) {
66 dataView.setInt32(byteOffset, value, kHostIsLittleEndian);
67 dataView.setInt32(byteOffset + 4, hi, kHostIsLittleEndian);
68 } else {
69 dataView.setInt32(byteOffset, hi, kHostIsLittleEndian);
70 dataView.setInt32(byteOffset + 4, value, kHostIsLittleEndian);
71 }
72 }
73
74 Buffer.prototype.alloc = function(size) {
75 var pointer = this.next;
76 this.next += size;
77 if (this.next > this.byteLength) {
78 var newSize = (1.5 * (this.byteLength + size)) | 0;
79 this.grow(newSize);
80 }
81 return pointer;
82 };
83
84 function copyArrayBuffer(dstArrayBuffer, srcArrayBuffer) {
85 (new Uint8Array(dstArrayBuffer)).set(new Uint8Array(srcArrayBuffer));
86 }
87
88 Buffer.prototype.grow = function(size) {
89 var newArrayBuffer = new ArrayBuffer(size);
90 copyArrayBuffer(newArrayBuffer, this.arrayBuffer);
91 this.arrayBuffer = newArrayBuffer;
92 this.dataView = new DataView(this.arrayBuffer);
93 this.byteLength = size;
94 };
95
96 Buffer.prototype.trim = function() {
97 this.arrayBuffer = this.arrayBuffer.slice(0, this.next);
98 this.dataView = new DataView(this.arrayBuffer);
99 this.byteLength = this.next;
100 };
101
102 Buffer.prototype.getUint8 = function(offset) {
103 return this.dataView.getUint8(offset);
104 }
105 Buffer.prototype.getUint16 = function(offset) {
106 return this.dataView.getUint16(offset, kHostIsLittleEndian);
107 }
108 Buffer.prototype.getUint32 = function(offset) {
109 return this.dataView.getUint32(offset, kHostIsLittleEndian);
110 }
111 Buffer.prototype.getUint64 = function(offset) {
112 return getUint64(this.dataView, offset, kHostIsLittleEndian);
yzshen1 2014/07/23 07:26:55 Does it make sense to merge the impl of getUint64(
hansmuller 2014/07/23 18:06:35 Yes, that makes sense.
113 }
114
115 Buffer.prototype.getInt8 = function(offset) {
116 return this.dataView.getInt8(offset);
117 }
118 Buffer.prototype.getInt16 = function(offset) {
119 return this.dataView.getInt16(offset, kHostIsLittleEndian);
120 }
121 Buffer.prototype.getInt32 = function(offset) {
122 return this.dataView.getInt32(offset, kHostIsLittleEndian);
123 }
124 Buffer.prototype.getInt64 = function(offset) {
125 return getInt64(this.dataView, offset, kHostIsLittleEndian);
126 }
127
128 Buffer.prototype.getFloat32 = function(offset) {
129 return this.dataView.getFloat32(offset, kHostIsLittleEndian);
130 }
131 Buffer.prototype.getFloat64 = function(offset) {
132 return this.dataView.getFloat64(offset, kHostIsLittleEndian);
133 }
134
135 Buffer.prototype.setUint8 = function(offset, value) {
136 this.dataView.setUint8(offset, value);
137 }
138 Buffer.prototype.setUint16 = function(offset, value) {
139 this.dataView.setUint16(offset, value, kHostIsLittleEndian);
140 }
141 Buffer.prototype.setUint32 = function(offset, value) {
142 this.dataView.setUint32(offset, value, kHostIsLittleEndian);
143 }
144 Buffer.prototype.setUint64 = function(offset, value) {
145 setUint64(this.dataView, offset, value, kHostIsLittleEndian);
146 }
147
148 Buffer.prototype.setInt8 = function(offset, value) {
149 this.dataView.setInt8(offset, value);
150 }
151 Buffer.prototype.setInt16 = function(offset, value) {
152 this.dataView.setInt16(offset, value, kHostIsLittleEndian);
153 }
154 Buffer.prototype.setInt32 = function(offset, value) {
155 this.dataView.setInt32(offset, value, kHostIsLittleEndian);
156 }
157 Buffer.prototype.setInt64 = function(offset, value) {
158 setInt64(this.dataView, offset, value, kHostIsLittleEndian);
159 }
160
161 Buffer.prototype.setFloat32 = function(offset, value) {
162 this.dataView.setFloat32(offset, value, kHostIsLittleEndian);
163 }
164 Buffer.prototype.setFloat64 = function(offset, value) {
165 this.dataView.setFloat64(offset, value, kHostIsLittleEndian);
166 }
167
168 return Buffer;
Matt Perry 2014/07/23 00:39:38 I think you should return a dictionary object that
abarth-chromium 2014/07/23 02:13:51 +1
hansmuller 2014/07/23 18:06:35 I didn't want user s of this module to have to wri
169 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698