blob: 98ca4824f0673afeec5957fd257332ac4e10ed2a [file] [log] [blame]
Austin Schuh7c75e582020-11-14 16:41:18 -08001"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.StackValue = void 0;
4var bit_width_1 = require("./bit-width");
5var bit_width_util_1 = require("./bit-width-util");
6var value_type_1 = require("./value-type");
7var value_type_util_1 = require("./value-type-util");
8var StackValue = /** @class */ (function () {
9 function StackValue(builder, type, width, value, offset) {
10 if (value === void 0) { value = null; }
11 if (offset === void 0) { offset = 0; }
12 this.builder = builder;
13 this.type = type;
14 this.width = width;
15 this.value = value;
16 this.offset = offset;
17 }
18 StackValue.prototype.elementWidth = function (size, index) {
19 if (value_type_util_1.isInline(this.type))
20 return this.width;
21 for (var i = 0; i < 4; i++) {
22 var width = 1 << i;
23 var offsetLoc = size + bit_width_util_1.paddingSize(size, width) + index * width;
24 var offset = offsetLoc - this.offset;
25 var bitWidth = bit_width_util_1.uwidth(offset);
26 if (1 << bitWidth === width) {
27 return bitWidth;
28 }
29 }
30 throw "Element is unknown. Size: " + size + " at index: " + index + ". This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new";
31 };
32 StackValue.prototype.writeToBuffer = function (byteWidth) {
33 var newOffset = this.builder.computeOffset(byteWidth);
34 if (this.type === value_type_1.ValueType.FLOAT) {
35 if (this.width === bit_width_1.BitWidth.WIDTH32) {
36 this.builder.view.setFloat32(this.builder.offset, this.value, true);
37 }
38 else {
39 this.builder.view.setFloat64(this.builder.offset, this.value, true);
40 }
41 }
42 else if (this.type === value_type_1.ValueType.INT) {
43 var bitWidth = bit_width_util_1.fromByteWidth(byteWidth);
44 this.builder.pushInt(this.value, bitWidth);
45 }
46 else if (this.type === value_type_1.ValueType.UINT) {
47 var bitWidth = bit_width_util_1.fromByteWidth(byteWidth);
48 this.builder.pushUInt(this.value, bitWidth);
49 }
50 else if (this.type === value_type_1.ValueType.NULL) {
51 this.builder.pushInt(0, this.width);
52 }
53 else if (this.type === value_type_1.ValueType.BOOL) {
54 this.builder.pushInt(this.value ? 1 : 0, this.width);
55 }
56 else {
57 throw "Unexpected type: " + this.type + ". This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new";
58 }
59 this.offset = newOffset;
60 };
61 StackValue.prototype.storedWidth = function (width) {
62 if (width === void 0) { width = bit_width_1.BitWidth.WIDTH8; }
63 return value_type_util_1.isInline(this.type) ? Math.max(width, this.width) : this.width;
64 };
65 StackValue.prototype.storedPackedType = function (width) {
66 if (width === void 0) { width = bit_width_1.BitWidth.WIDTH8; }
67 return value_type_util_1.packedType(this.type, this.storedWidth(width));
68 };
69 StackValue.prototype.isOffset = function () {
70 return !value_type_util_1.isInline(this.type);
71 };
72 return StackValue;
73}());
74exports.StackValue = StackValue;