blob: 961c53dbb52c739a6b9559c65cbed9e5fa1cd05c [file] [log] [blame]
Austin Schuh7c75e582020-11-14 16:41:18 -08001"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ByteBuffer = void 0;
4var constants_1 = require("./constants");
5var long_1 = require("./long");
6var utils_1 = require("./utils");
7var encoding_1 = require("./encoding");
8var ByteBuffer = /** @class */ (function () {
9 /**
10 * Create a new ByteBuffer with a given array of bytes (`Uint8Array`)
11 */
12 function ByteBuffer(bytes_) {
13 this.bytes_ = bytes_;
14 this.position_ = 0;
15 }
16 /**
17 * Create and allocate a new ByteBuffer with a given size.
18 */
19 ByteBuffer.allocate = function (byte_size) {
20 return new ByteBuffer(new Uint8Array(byte_size));
21 };
22 ByteBuffer.prototype.clear = function () {
23 this.position_ = 0;
24 };
25 /**
26 * Get the underlying `Uint8Array`.
27 */
28 ByteBuffer.prototype.bytes = function () {
29 return this.bytes_;
30 };
31 /**
32 * Get the buffer's position.
33 */
34 ByteBuffer.prototype.position = function () {
35 return this.position_;
36 };
37 /**
38 * Set the buffer's position.
39 */
40 ByteBuffer.prototype.setPosition = function (position) {
41 this.position_ = position;
42 };
43 /**
44 * Get the buffer's capacity.
45 */
46 ByteBuffer.prototype.capacity = function () {
47 return this.bytes_.length;
48 };
49 ByteBuffer.prototype.readInt8 = function (offset) {
50 return this.readUint8(offset) << 24 >> 24;
51 };
52 ByteBuffer.prototype.readUint8 = function (offset) {
53 return this.bytes_[offset];
54 };
55 ByteBuffer.prototype.readInt16 = function (offset) {
56 return this.readUint16(offset) << 16 >> 16;
57 };
58 ByteBuffer.prototype.readUint16 = function (offset) {
59 return this.bytes_[offset] | this.bytes_[offset + 1] << 8;
60 };
61 ByteBuffer.prototype.readInt32 = function (offset) {
62 return this.bytes_[offset] | this.bytes_[offset + 1] << 8 | this.bytes_[offset + 2] << 16 | this.bytes_[offset + 3] << 24;
63 };
64 ByteBuffer.prototype.readUint32 = function (offset) {
65 return this.readInt32(offset) >>> 0;
66 };
67 ByteBuffer.prototype.readInt64 = function (offset) {
68 return new long_1.Long(this.readInt32(offset), this.readInt32(offset + 4));
69 };
70 ByteBuffer.prototype.readUint64 = function (offset) {
71 return new long_1.Long(this.readUint32(offset), this.readUint32(offset + 4));
72 };
73 ByteBuffer.prototype.readFloat32 = function (offset) {
74 utils_1.int32[0] = this.readInt32(offset);
75 return utils_1.float32[0];
76 };
77 ByteBuffer.prototype.readFloat64 = function (offset) {
78 utils_1.int32[utils_1.isLittleEndian ? 0 : 1] = this.readInt32(offset);
79 utils_1.int32[utils_1.isLittleEndian ? 1 : 0] = this.readInt32(offset + 4);
80 return utils_1.float64[0];
81 };
82 ByteBuffer.prototype.writeInt8 = function (offset, value) {
83 this.bytes_[offset] = value;
84 };
85 ByteBuffer.prototype.writeUint8 = function (offset, value) {
86 this.bytes_[offset] = value;
87 };
88 ByteBuffer.prototype.writeInt16 = function (offset, value) {
89 this.bytes_[offset] = value;
90 this.bytes_[offset + 1] = value >> 8;
91 };
92 ByteBuffer.prototype.writeUint16 = function (offset, value) {
93 this.bytes_[offset] = value;
94 this.bytes_[offset + 1] = value >> 8;
95 };
96 ByteBuffer.prototype.writeInt32 = function (offset, value) {
97 this.bytes_[offset] = value;
98 this.bytes_[offset + 1] = value >> 8;
99 this.bytes_[offset + 2] = value >> 16;
100 this.bytes_[offset + 3] = value >> 24;
101 };
102 ByteBuffer.prototype.writeUint32 = function (offset, value) {
103 this.bytes_[offset] = value;
104 this.bytes_[offset + 1] = value >> 8;
105 this.bytes_[offset + 2] = value >> 16;
106 this.bytes_[offset + 3] = value >> 24;
107 };
108 ByteBuffer.prototype.writeInt64 = function (offset, value) {
109 this.writeInt32(offset, value.low);
110 this.writeInt32(offset + 4, value.high);
111 };
112 ByteBuffer.prototype.writeUint64 = function (offset, value) {
113 this.writeUint32(offset, value.low);
114 this.writeUint32(offset + 4, value.high);
115 };
116 ByteBuffer.prototype.writeFloat32 = function (offset, value) {
117 utils_1.float32[0] = value;
118 this.writeInt32(offset, utils_1.int32[0]);
119 };
120 ByteBuffer.prototype.writeFloat64 = function (offset, value) {
121 utils_1.float64[0] = value;
122 this.writeInt32(offset, utils_1.int32[utils_1.isLittleEndian ? 0 : 1]);
123 this.writeInt32(offset + 4, utils_1.int32[utils_1.isLittleEndian ? 1 : 0]);
124 };
125 /**
126 * Return the file identifier. Behavior is undefined for FlatBuffers whose
127 * schema does not include a file_identifier (likely points at padding or the
128 * start of a the root vtable).
129 */
130 ByteBuffer.prototype.getBufferIdentifier = function () {
131 if (this.bytes_.length < this.position_ + constants_1.SIZEOF_INT +
132 constants_1.FILE_IDENTIFIER_LENGTH) {
133 throw new Error('FlatBuffers: ByteBuffer is too short to contain an identifier.');
134 }
135 var result = "";
136 for (var i = 0; i < constants_1.FILE_IDENTIFIER_LENGTH; i++) {
137 result += String.fromCharCode(this.readInt8(this.position_ + constants_1.SIZEOF_INT + i));
138 }
139 return result;
140 };
141 /**
142 * Look up a field in the vtable, return an offset into the object, or 0 if the
143 * field is not present.
144 */
145 ByteBuffer.prototype.__offset = function (bb_pos, vtable_offset) {
146 var vtable = bb_pos - this.readInt32(bb_pos);
147 return vtable_offset < this.readInt16(vtable) ? this.readInt16(vtable + vtable_offset) : 0;
148 };
149 /**
150 * Initialize any Table-derived type to point to the union at the given offset.
151 */
152 ByteBuffer.prototype.__union = function (t, offset) {
153 t.bb_pos = offset + this.readInt32(offset);
154 t.bb = this;
155 return t;
156 };
157 /**
158 * Create a JavaScript string from UTF-8 data stored inside the FlatBuffer.
159 * This allocates a new string and converts to wide chars upon each access.
160 *
161 * To avoid the conversion to UTF-16, pass Encoding.UTF8_BYTES as
162 * the "optionalEncoding" argument. This is useful for avoiding conversion to
163 * and from UTF-16 when the data will just be packaged back up in another
164 * FlatBuffer later on.
165 *
166 * @param offset
167 * @param opt_encoding Defaults to UTF16_STRING
168 */
169 ByteBuffer.prototype.__string = function (offset, opt_encoding) {
170 offset += this.readInt32(offset);
171 var length = this.readInt32(offset);
172 var result = '';
173 var i = 0;
174 offset += constants_1.SIZEOF_INT;
175 if (opt_encoding === encoding_1.Encoding.UTF8_BYTES) {
176 return this.bytes_.subarray(offset, offset + length);
177 }
178 while (i < length) {
179 var codePoint = void 0;
180 // Decode UTF-8
181 var a = this.readUint8(offset + i++);
182 if (a < 0xC0) {
183 codePoint = a;
184 }
185 else {
186 var b = this.readUint8(offset + i++);
187 if (a < 0xE0) {
188 codePoint =
189 ((a & 0x1F) << 6) |
190 (b & 0x3F);
191 }
192 else {
193 var c = this.readUint8(offset + i++);
194 if (a < 0xF0) {
195 codePoint =
196 ((a & 0x0F) << 12) |
197 ((b & 0x3F) << 6) |
198 (c & 0x3F);
199 }
200 else {
201 var d = this.readUint8(offset + i++);
202 codePoint =
203 ((a & 0x07) << 18) |
204 ((b & 0x3F) << 12) |
205 ((c & 0x3F) << 6) |
206 (d & 0x3F);
207 }
208 }
209 }
210 // Encode UTF-16
211 if (codePoint < 0x10000) {
212 result += String.fromCharCode(codePoint);
213 }
214 else {
215 codePoint -= 0x10000;
216 result += String.fromCharCode((codePoint >> 10) + 0xD800, (codePoint & ((1 << 10) - 1)) + 0xDC00);
217 }
218 }
219 return result;
220 };
221 /**
222 * Handle unions that can contain string as its member, if a Table-derived type then initialize it,
223 * if a string then return a new one
224 *
225 * WARNING: strings are immutable in JS so we can't change the string that the user gave us, this
226 * makes the behaviour of __union_with_string different compared to __union
227 */
228 ByteBuffer.prototype.__union_with_string = function (o, offset) {
229 if (typeof o === 'string') {
230 return this.__string(offset);
231 }
232 return this.__union(o, offset);
233 };
234 /**
235 * Retrieve the relative offset stored at "offset"
236 */
237 ByteBuffer.prototype.__indirect = function (offset) {
238 return offset + this.readInt32(offset);
239 };
240 /**
241 * Get the start of data of a vector whose offset is stored at "offset" in this object.
242 */
243 ByteBuffer.prototype.__vector = function (offset) {
244 return offset + this.readInt32(offset) + constants_1.SIZEOF_INT; // data starts after the length
245 };
246 /**
247 * Get the length of a vector whose offset is stored at "offset" in this object.
248 */
249 ByteBuffer.prototype.__vector_len = function (offset) {
250 return this.readInt32(offset + this.readInt32(offset));
251 };
252 ByteBuffer.prototype.__has_identifier = function (ident) {
253 if (ident.length != constants_1.FILE_IDENTIFIER_LENGTH) {
254 throw new Error('FlatBuffers: file identifier must be length ' +
255 constants_1.FILE_IDENTIFIER_LENGTH);
256 }
257 for (var i = 0; i < constants_1.FILE_IDENTIFIER_LENGTH; i++) {
258 if (ident.charCodeAt(i) != this.readInt8(this.position() + constants_1.SIZEOF_INT + i)) {
259 return false;
260 }
261 }
262 return true;
263 };
264 /**
265 * A helper function to avoid generated code depending on this file directly.
266 */
267 ByteBuffer.prototype.createLong = function (low, high) {
268 return long_1.Long.create(low, high);
269 };
270 /**
271 * A helper function for generating list for obj api
272 */
273 ByteBuffer.prototype.createScalarList = function (listAccessor, listLength) {
274 var ret = [];
275 for (var i = 0; i < listLength; ++i) {
276 if (listAccessor(i) !== null) {
277 ret.push(listAccessor(i));
278 }
279 }
280 return ret;
281 };
282 /**
283 * This function is here only to get around typescript type system
284 */
285 ByteBuffer.prototype.createStringList = function (listAccessor, listLength) {
286 return this.createScalarList(listAccessor, listLength);
287 };
288 /**
289 * A helper function for generating list for obj api
290 * @param listAccessor function that accepts an index and return data at that index
291 * @param listLength listLength
292 * @param res result list
293 */
294 ByteBuffer.prototype.createObjList = function (listAccessor, listLength) {
295 var ret = [];
296 for (var i = 0; i < listLength; ++i) {
297 var val = listAccessor(i);
298 if (val !== null) {
299 ret.push(val.unpack());
300 }
301 }
302 return ret;
303 };
304 return ByteBuffer;
305}());
306exports.ByteBuffer = ByteBuffer;