Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 1 | "use strict"; |
| 2 | Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | exports.Reference = exports.toReference = void 0; |
| 4 | var bit_width_util_1 = require("./bit-width-util"); |
| 5 | var value_type_1 = require("./value-type"); |
| 6 | var value_type_util_1 = require("./value-type-util"); |
| 7 | var reference_util_1 = require("./reference-util"); |
| 8 | var flexbuffers_util_1 = require("./flexbuffers-util"); |
| 9 | var bit_width_1 = require("./bit-width"); |
| 10 | function toReference(buffer) { |
| 11 | var len = buffer.byteLength; |
| 12 | if (len < 3) { |
| 13 | throw "Buffer needs to be bigger than 3"; |
| 14 | } |
| 15 | var dataView = new DataView(buffer); |
| 16 | var byteWidth = dataView.getUint8(len - 1); |
| 17 | var packedType = dataView.getUint8(len - 2); |
| 18 | var parentWidth = bit_width_util_1.fromByteWidth(byteWidth); |
| 19 | var offset = len - byteWidth - 2; |
| 20 | return new Reference(dataView, offset, parentWidth, packedType, "/"); |
| 21 | } |
| 22 | exports.toReference = toReference; |
| 23 | var Reference = /** @class */ (function () { |
| 24 | function Reference(dataView, offset, parentWidth, packedType, path) { |
| 25 | this.dataView = dataView; |
| 26 | this.offset = offset; |
| 27 | this.parentWidth = parentWidth; |
| 28 | this.packedType = packedType; |
| 29 | this.path = path; |
| 30 | this._length = -1; |
| 31 | this.byteWidth = 1 << (packedType & 3); |
| 32 | this.valueType = packedType >> 2; |
| 33 | } |
| 34 | Reference.prototype.isNull = function () { return this.valueType === value_type_1.ValueType.NULL; }; |
| 35 | Reference.prototype.isNumber = function () { return value_type_util_1.isNumber(this.valueType) || value_type_util_1.isIndirectNumber(this.valueType); }; |
| 36 | Reference.prototype.isFloat = function () { return value_type_1.ValueType.FLOAT === this.valueType || value_type_1.ValueType.INDIRECT_FLOAT === this.valueType; }; |
| 37 | Reference.prototype.isInt = function () { return this.isNumber() && !this.isFloat(); }; |
| 38 | Reference.prototype.isString = function () { return value_type_1.ValueType.STRING === this.valueType || value_type_1.ValueType.KEY === this.valueType; }; |
| 39 | Reference.prototype.isBool = function () { return value_type_1.ValueType.BOOL === this.valueType; }; |
| 40 | Reference.prototype.isBlob = function () { return value_type_1.ValueType.BLOB === this.valueType; }; |
| 41 | Reference.prototype.isVector = function () { return value_type_util_1.isAVector(this.valueType); }; |
| 42 | Reference.prototype.isMap = function () { return value_type_1.ValueType.MAP === this.valueType; }; |
| 43 | Reference.prototype.boolValue = function () { |
| 44 | if (this.isBool()) { |
| 45 | return reference_util_1.readInt(this.dataView, this.offset, this.parentWidth) > 0; |
| 46 | } |
| 47 | return null; |
| 48 | }; |
| 49 | Reference.prototype.intValue = function () { |
| 50 | if (this.valueType === value_type_1.ValueType.INT) { |
| 51 | return reference_util_1.readInt(this.dataView, this.offset, this.parentWidth); |
| 52 | } |
| 53 | if (this.valueType === value_type_1.ValueType.UINT) { |
| 54 | return reference_util_1.readUInt(this.dataView, this.offset, this.parentWidth); |
| 55 | } |
| 56 | if (this.valueType === value_type_1.ValueType.INDIRECT_INT) { |
| 57 | return reference_util_1.readInt(this.dataView, reference_util_1.indirect(this.dataView, this.offset, this.parentWidth), bit_width_util_1.fromByteWidth(this.byteWidth)); |
| 58 | } |
| 59 | if (this.valueType === value_type_1.ValueType.INDIRECT_UINT) { |
| 60 | return reference_util_1.readUInt(this.dataView, reference_util_1.indirect(this.dataView, this.offset, this.parentWidth), bit_width_util_1.fromByteWidth(this.byteWidth)); |
| 61 | } |
| 62 | return null; |
| 63 | }; |
| 64 | Reference.prototype.floatValue = function () { |
| 65 | if (this.valueType === value_type_1.ValueType.FLOAT) { |
| 66 | return reference_util_1.readFloat(this.dataView, this.offset, this.parentWidth); |
| 67 | } |
| 68 | if (this.valueType === value_type_1.ValueType.INDIRECT_FLOAT) { |
| 69 | return reference_util_1.readFloat(this.dataView, reference_util_1.indirect(this.dataView, this.offset, this.parentWidth), bit_width_util_1.fromByteWidth(this.byteWidth)); |
| 70 | } |
| 71 | return null; |
| 72 | }; |
| 73 | Reference.prototype.numericValue = function () { return this.floatValue() || this.intValue(); }; |
| 74 | Reference.prototype.stringValue = function () { |
| 75 | if (this.valueType === value_type_1.ValueType.STRING || this.valueType === value_type_1.ValueType.KEY) { |
| 76 | var begin = reference_util_1.indirect(this.dataView, this.offset, this.parentWidth); |
| 77 | return flexbuffers_util_1.fromUTF8Array(new Uint8Array(this.dataView.buffer, begin, this.length())); |
| 78 | } |
| 79 | return null; |
| 80 | }; |
| 81 | Reference.prototype.blobValue = function () { |
| 82 | if (this.isBlob()) { |
| 83 | var begin = reference_util_1.indirect(this.dataView, this.offset, this.parentWidth); |
| 84 | return new Uint8Array(this.dataView.buffer, begin, this.length()); |
| 85 | } |
| 86 | return null; |
| 87 | }; |
| 88 | Reference.prototype.get = function (key) { |
| 89 | var length = this.length(); |
| 90 | if (Number.isInteger(key) && value_type_util_1.isAVector(this.valueType)) { |
| 91 | if (key >= length || key < 0) { |
| 92 | throw "Key: [" + key + "] is not applicable on " + this.path + " of " + this.valueType + " length: " + length; |
| 93 | } |
| 94 | var _indirect = reference_util_1.indirect(this.dataView, this.offset, this.parentWidth); |
| 95 | var elementOffset = _indirect + key * this.byteWidth; |
| 96 | var _packedType = this.dataView.getUint8(_indirect + length * this.byteWidth + key); |
| 97 | if (value_type_util_1.isTypedVector(this.valueType)) { |
| 98 | var _valueType = value_type_util_1.typedVectorElementType(this.valueType); |
| 99 | _packedType = value_type_util_1.packedType(_valueType, bit_width_1.BitWidth.WIDTH8); |
| 100 | } |
| 101 | else if (value_type_util_1.isFixedTypedVector(this.valueType)) { |
| 102 | var _valueType = value_type_util_1.fixedTypedVectorElementType(this.valueType); |
| 103 | _packedType = value_type_util_1.packedType(_valueType, bit_width_1.BitWidth.WIDTH8); |
| 104 | } |
| 105 | return new Reference(this.dataView, elementOffset, bit_width_util_1.fromByteWidth(this.byteWidth), _packedType, this.path + "[" + key + "]"); |
| 106 | } |
| 107 | if (typeof key === 'string') { |
| 108 | var index = reference_util_1.keyIndex(key, this.dataView, this.offset, this.parentWidth, this.byteWidth, length); |
| 109 | if (index !== null) { |
| 110 | return reference_util_1.valueForIndexWithKey(index, key, this.dataView, this.offset, this.parentWidth, this.byteWidth, length, this.path); |
| 111 | } |
| 112 | } |
| 113 | throw "Key [" + key + "] is not applicable on " + this.path + " of " + this.valueType; |
| 114 | }; |
| 115 | Reference.prototype.length = function () { |
| 116 | var size; |
| 117 | if (this._length > -1) { |
| 118 | return this._length; |
| 119 | } |
| 120 | if (value_type_util_1.isFixedTypedVector(this.valueType)) { |
| 121 | this._length = value_type_util_1.fixedTypedVectorElementSize(this.valueType); |
| 122 | } |
| 123 | else if (this.valueType === value_type_1.ValueType.BLOB |
| 124 | || this.valueType === value_type_1.ValueType.MAP |
| 125 | || value_type_util_1.isAVector(this.valueType)) { |
| 126 | this._length = reference_util_1.readUInt(this.dataView, reference_util_1.indirect(this.dataView, this.offset, this.parentWidth) - this.byteWidth, bit_width_util_1.fromByteWidth(this.byteWidth)); |
| 127 | } |
| 128 | else if (this.valueType === value_type_1.ValueType.NULL) { |
| 129 | this._length = 0; |
| 130 | } |
| 131 | else if (this.valueType === value_type_1.ValueType.STRING) { |
| 132 | var _indirect = reference_util_1.indirect(this.dataView, this.offset, this.parentWidth); |
| 133 | var sizeByteWidth = this.byteWidth; |
| 134 | size = reference_util_1.readUInt(this.dataView, _indirect - sizeByteWidth, bit_width_util_1.fromByteWidth(this.byteWidth)); |
| 135 | while (this.dataView.getInt8(_indirect + size) !== 0) { |
| 136 | sizeByteWidth <<= 1; |
| 137 | size = reference_util_1.readUInt(this.dataView, _indirect - sizeByteWidth, bit_width_util_1.fromByteWidth(this.byteWidth)); |
| 138 | } |
| 139 | this._length = size; |
| 140 | } |
| 141 | else if (this.valueType === value_type_1.ValueType.KEY) { |
| 142 | var _indirect = reference_util_1.indirect(this.dataView, this.offset, this.parentWidth); |
| 143 | size = 1; |
| 144 | while (this.dataView.getInt8(_indirect + size) !== 0) { |
| 145 | size++; |
| 146 | } |
| 147 | this._length = size; |
| 148 | } |
| 149 | else { |
| 150 | this._length = 1; |
| 151 | } |
| 152 | return this._length; |
| 153 | }; |
| 154 | Reference.prototype.toObject = function () { |
| 155 | var length = this.length(); |
| 156 | if (this.isVector()) { |
| 157 | var result = []; |
| 158 | for (var i = 0; i < length; i++) { |
| 159 | result.push(this.get(i).toObject()); |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | if (this.isMap()) { |
| 164 | var result = {}; |
| 165 | for (var i = 0; i < length; i++) { |
| 166 | var key = reference_util_1.keyForIndex(i, this.dataView, this.offset, this.parentWidth, this.byteWidth); |
| 167 | result[key] = reference_util_1.valueForIndexWithKey(i, key, this.dataView, this.offset, this.parentWidth, this.byteWidth, length, this.path).toObject(); |
| 168 | } |
| 169 | return result; |
| 170 | } |
| 171 | if (this.isNull()) { |
| 172 | return null; |
| 173 | } |
| 174 | if (this.isBool()) { |
| 175 | return this.boolValue(); |
| 176 | } |
| 177 | if (this.isNumber()) { |
| 178 | return this.numericValue(); |
| 179 | } |
| 180 | return this.blobValue() || this.stringValue(); |
| 181 | }; |
| 182 | return Reference; |
| 183 | }()); |
| 184 | exports.Reference = Reference; |