blob: fc035b9ffc03bdce06db49033703e46c18c8cb7e [file] [log] [blame]
Austin Schuh7c75e582020-11-14 16:41:18 -08001"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.keyForIndex = exports.valueForIndexWithKey = exports.diffKeys = exports.keyIndex = exports.indirect = exports.readFloat = exports.readUInt = exports.readInt = exports.validateOffset = void 0;
4var bit_width_1 = require("./bit-width");
5var bit_width_util_1 = require("./bit-width-util");
6var flexbuffers_util_1 = require("./flexbuffers-util");
7var reference_1 = require("./reference");
8var long_1 = require("../long");
9function validateOffset(dataView, offset, width) {
10 if (dataView.byteLength <= offset + width || (offset & (bit_width_util_1.toByteWidth(width) - 1)) !== 0) {
11 throw "Bad offset: " + offset + ", width: " + width;
12 }
13}
14exports.validateOffset = validateOffset;
15function readInt(dataView, offset, width) {
16 if (width < 2) {
17 if (width < 1) {
18 return dataView.getInt8(offset);
19 }
20 else {
21 return dataView.getInt16(offset, true);
22 }
23 }
24 else {
25 if (width < 3) {
26 return dataView.getInt32(offset, true);
27 }
28 else {
29 if (dataView.setBigInt64 === undefined) {
30 return new long_1.Long(dataView.getUint32(offset, true), dataView.getUint32(offset + 4, true));
31 }
32 return dataView.getBigInt64(offset, true);
33 }
34 }
35}
36exports.readInt = readInt;
37function readUInt(dataView, offset, width) {
38 if (width < 2) {
39 if (width < 1) {
40 return dataView.getUint8(offset);
41 }
42 else {
43 return dataView.getUint16(offset, true);
44 }
45 }
46 else {
47 if (width < 3) {
48 return dataView.getUint32(offset, true);
49 }
50 else {
51 if (dataView.getBigUint64 === undefined) {
52 return new long_1.Long(dataView.getUint32(offset, true), dataView.getUint32(offset + 4, true));
53 }
54 return dataView.getBigUint64(offset, true);
55 }
56 }
57}
58exports.readUInt = readUInt;
59function readFloat(dataView, offset, width) {
60 if (width < bit_width_1.BitWidth.WIDTH32) {
61 throw "Bad width: " + width;
62 }
63 if (width === bit_width_1.BitWidth.WIDTH32) {
64 return dataView.getFloat32(offset, true);
65 }
66 return dataView.getFloat64(offset, true);
67}
68exports.readFloat = readFloat;
69function indirect(dataView, offset, width) {
70 var step = readUInt(dataView, offset, width);
71 return offset - step;
72}
73exports.indirect = indirect;
74function keyIndex(key, dataView, offset, parentWidth, byteWidth, length) {
75 var input = flexbuffers_util_1.toUTF8Array(key);
76 var keysVectorOffset = indirect(dataView, offset, parentWidth) - byteWidth * 3;
77 var bitWidth = bit_width_util_1.fromByteWidth(byteWidth);
78 var indirectOffset = keysVectorOffset - readUInt(dataView, keysVectorOffset, bitWidth);
79 var _byteWidth = readUInt(dataView, keysVectorOffset + byteWidth, bitWidth);
80 var low = 0;
81 var high = length - 1;
82 while (low <= high) {
83 var mid = (high + low) >> 1;
84 var dif = diffKeys(input, mid, dataView, indirectOffset, _byteWidth);
85 if (dif === 0)
86 return mid;
87 if (dif < 0) {
88 high = mid - 1;
89 }
90 else {
91 low = mid + 1;
92 }
93 }
94 return null;
95}
96exports.keyIndex = keyIndex;
97function diffKeys(input, index, dataView, offset, width) {
98 var keyOffset = offset + index * width;
99 var keyIndirectOffset = keyOffset - readUInt(dataView, keyOffset, bit_width_util_1.fromByteWidth(width));
100 for (var i = 0; i < input.length; i++) {
101 var dif = input[i] - dataView.getUint8(keyIndirectOffset + i);
102 if (dif !== 0) {
103 return dif;
104 }
105 }
106 return dataView.getUint8(keyIndirectOffset + input.length) === 0 ? 0 : -1;
107}
108exports.diffKeys = diffKeys;
109function valueForIndexWithKey(index, key, dataView, offset, parentWidth, byteWidth, length, path) {
110 var _indirect = indirect(dataView, offset, parentWidth);
111 var elementOffset = _indirect + index * byteWidth;
112 var packedType = dataView.getUint8(_indirect + length * byteWidth + index);
113 return new reference_1.Reference(dataView, elementOffset, bit_width_util_1.fromByteWidth(byteWidth), packedType, path + "/" + key);
114}
115exports.valueForIndexWithKey = valueForIndexWithKey;
116function keyForIndex(index, dataView, offset, parentWidth, byteWidth) {
117 var keysVectorOffset = indirect(dataView, offset, parentWidth) - byteWidth * 3;
118 var bitWidth = bit_width_util_1.fromByteWidth(byteWidth);
119 var indirectOffset = keysVectorOffset - readUInt(dataView, keysVectorOffset, bitWidth);
120 var _byteWidth = readUInt(dataView, keysVectorOffset + byteWidth, bitWidth);
121 var keyOffset = indirectOffset + index * _byteWidth;
122 var keyIndirectOffset = keyOffset - readUInt(dataView, keyOffset, bit_width_util_1.fromByteWidth(_byteWidth));
123 var length = 0;
124 while (dataView.getUint8(keyIndirectOffset + length) !== 0) {
125 length++;
126 }
127 return flexbuffers_util_1.fromUTF8Array(new Uint8Array(dataView.buffer, keyIndirectOffset, length));
128}
129exports.keyForIndex = keyForIndex;