flatbuffers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. var flatbuffers = {};
  2. var json = json || require('./json');
  3. flatbuffers.get = (name) => {
  4. flatbuffers._map = flatbuffers._map || new Map();
  5. if (!flatbuffers._map.has(name)) {
  6. flatbuffers._map.set(name, {});
  7. }
  8. return flatbuffers._map.get(name);
  9. };
  10. flatbuffers.BinaryReader = class {
  11. static open(data) {
  12. return data ? new flatbuffers.BinaryReader(data) : null;
  13. }
  14. constructor(data) {
  15. const buffer = data instanceof Uint8Array ? data : data.peek();
  16. this._buffer = buffer;
  17. this._position = 0;
  18. this._dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  19. }
  20. get root() {
  21. return this.int32(this._position) + this._position;
  22. }
  23. get identifier() {
  24. if (this._buffer.length >= 8) {
  25. const buffer = this._buffer.slice(4, 8);
  26. if (buffer.every((c) => c >= 32 && c <= 128)) {
  27. return String.fromCharCode(...buffer);
  28. }
  29. }
  30. return '';
  31. }
  32. bool(offset) {
  33. return !!this.int8(offset);
  34. }
  35. bool_(position, offset, defaultValue) {
  36. offset = this._offset(position, offset);
  37. return offset ? this.bool(position + offset) : defaultValue;
  38. }
  39. int8(offset) {
  40. return this.uint8(offset) << 24 >> 24;
  41. }
  42. int8_(position, offset, defaultValue) {
  43. offset = this._offset(position, offset);
  44. return offset ? this.int8(position + offset) : defaultValue;
  45. }
  46. uint8(offset) {
  47. return this._buffer[offset];
  48. }
  49. uint8_(position, offset, defaultValue) {
  50. offset = this._offset(position, offset);
  51. return offset ? this.uint8(position + offset) : defaultValue;
  52. }
  53. int16(offset) {
  54. return this._dataView.getInt16(offset, true);
  55. }
  56. int16_(position, offset, defaultValue) {
  57. offset = this._offset(position, offset);
  58. return offset ? this.int16(position + offset) : defaultValue;
  59. }
  60. uint16(offset) {
  61. return this._dataView.getUint16(offset, true);
  62. }
  63. uint16_(position, offset, defaultValue) {
  64. offset = this._offset(position, offset);
  65. return offset ? this.uint16(position + offset) : defaultValue;
  66. }
  67. int32(offset) {
  68. return this._dataView.getInt32(offset, true);
  69. }
  70. int32_(position, offset, defaultValue) {
  71. offset = this._offset(position, offset);
  72. return offset ? this.int32(position + offset) : defaultValue;
  73. }
  74. uint32(offset) {
  75. return this._dataView.getUint32(offset, true);
  76. }
  77. uint32_(position, offset, defaultValue) {
  78. offset = this._offset(position, offset);
  79. return offset ? this.int32(position + offset) : defaultValue;
  80. }
  81. int64(offset) {
  82. return this._dataView.getInt64(offset, true);
  83. }
  84. int64_(position, offset, defaultValue) {
  85. offset = this._offset(position, offset);
  86. return offset ? this.int64(position + offset) : defaultValue;
  87. }
  88. uint64(offset) {
  89. return this._dataView.getUint64(offset, true);
  90. }
  91. uint64_(position, offset, defaultValue) {
  92. offset = this._offset(position, offset);
  93. return offset ? this.uint64(position + offset) : defaultValue;
  94. }
  95. float32(offset) {
  96. return this._dataView.getFloat32(offset, true);
  97. }
  98. float32_(position, offset, defaultValue) {
  99. offset = this._offset(position, offset);
  100. return offset ? this.float32(position + offset) : defaultValue;
  101. }
  102. float64(offset) {
  103. return this._dataView.getFloat64(offset, true);
  104. }
  105. float64_(position, offset, defaultValue) {
  106. offset = this._offset(position, offset);
  107. return offset ? this.float64(position + offset) : defaultValue;
  108. }
  109. string(offset, encoding) {
  110. offset += this.int32(offset);
  111. const length = this.int32(offset);
  112. var result = '';
  113. var i = 0;
  114. offset += 4;
  115. if (encoding === 1) {
  116. return this._buffer.subarray(offset, offset + length);
  117. }
  118. while (i < length) {
  119. var codePoint;
  120. // Decode UTF-8
  121. const a = this.uint8(offset + i++);
  122. if (a < 0xC0) {
  123. codePoint = a;
  124. }
  125. else {
  126. const b = this.uint8(offset + i++);
  127. if (a < 0xE0) {
  128. codePoint = ((a & 0x1F) << 6) | (b & 0x3F);
  129. }
  130. else {
  131. const c = this.uint8(offset + i++);
  132. if (a < 0xF0) {
  133. codePoint = ((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F);
  134. }
  135. else {
  136. const d = this.uint8(offset + i++);
  137. codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F);
  138. }
  139. }
  140. }
  141. // Encode UTF-16
  142. if (codePoint < 0x10000) {
  143. result += String.fromCharCode(codePoint);
  144. }
  145. else {
  146. codePoint -= 0x10000;
  147. result += String.fromCharCode((codePoint >> 10) + 0xD800, (codePoint & ((1 << 10) - 1)) + 0xDC00);
  148. }
  149. }
  150. return result;
  151. }
  152. string_(position, offset, defaultValue) {
  153. offset = this._offset(position, offset);
  154. return offset ? this.string(position + offset) : defaultValue;
  155. }
  156. bools_(position, offset) {
  157. offset = this._offset(position, offset);
  158. if (offset) {
  159. const length = this._length(position + offset);
  160. offset = this._vector(position + offset);
  161. const array = new Array(length);
  162. for (let i = 0; i < length; i++) {
  163. array[i] = this.uint8(offset + i + 4) ? true : false;
  164. }
  165. return array;
  166. }
  167. return [];
  168. }
  169. int64s_(position, offset) {
  170. offset = this._offset(position, offset);
  171. if (offset) {
  172. const length = this._length(position + offset);
  173. offset = this._vector(position + offset);
  174. const array = new Array(length);
  175. for (let i = 0; i < length; i++) {
  176. array[i] = this.int64(offset + (i << 3));
  177. }
  178. return array;
  179. }
  180. return [];
  181. }
  182. uint64s_(position, offset) {
  183. offset = this._offset(position, offset);
  184. if (offset) {
  185. const length = this._length(position + offset);
  186. offset = this._vector(position + offset);
  187. const array = new Array(length);
  188. for (let i = 0; i < length; i++) {
  189. array[i] = this.uint64(offset + (i << 3));
  190. }
  191. return array;
  192. }
  193. return [];
  194. }
  195. strings_(position, offset) {
  196. offset = this._offset(position, offset);
  197. if (offset) {
  198. const length = this._length(position + offset);
  199. offset = this._vector(position + offset);
  200. const array = new Array(length);
  201. for (let i = 0; i < length; i++) {
  202. array[i] = this.string(offset + i * 4);
  203. }
  204. return array;
  205. }
  206. return [];
  207. }
  208. struct(position, offset, decode) {
  209. offset = this._offset(position, offset);
  210. return offset ? decode(this, position + offset) : null;
  211. }
  212. table(position, offset, decode) {
  213. offset = this._offset(position, offset);
  214. return offset ? decode(this, this._indirect(position + offset)) : null;
  215. }
  216. union(position, offset, decode) {
  217. const type_offset = this._offset(position, offset);
  218. const type = type_offset ? this.uint8(position + type_offset) : 0;
  219. offset = this._offset(position, offset + 2);
  220. return offset ? decode(this, this._union(position + offset), type) : null;
  221. }
  222. typedArray(position, offset, type) {
  223. offset = this._offset(position, offset);
  224. return offset ? new type(this._buffer.buffer, this._buffer.byteOffset + this._vector(position + offset), this._length(position + offset)) : new type(0);
  225. }
  226. unionArray(/* position, offset, decode */) {
  227. throw new flatbuffers.Error('Not implemented.');
  228. }
  229. structArray(position, offset, size, decode) {
  230. offset = this._offset(position, offset);
  231. const length = offset ? this._length(position + offset) : 0;
  232. const list = new Array(length);
  233. for (let i = 0; i < length; i++) {
  234. list[i] = decode(this, this._indirect(this._vector(position + offset) + i * 4));
  235. }
  236. return list;
  237. }
  238. tableArray(position, offset, decode) {
  239. offset = this._offset(position, offset);
  240. const length = offset ? this._length(position + offset) : 0;
  241. const list = new Array(length);
  242. for (let i = 0; i < length; i++) {
  243. list[i] = decode(this, this._indirect(this._vector(position + offset) + i * 4));
  244. }
  245. return list;
  246. }
  247. _offset(bb_pos, vtableOffset) {
  248. var vtable = bb_pos - this.int32(bb_pos);
  249. return vtableOffset < this.int16(vtable) ? this.int16(vtable + vtableOffset) : 0;
  250. }
  251. _indirect(offset) {
  252. return offset + this.int32(offset);
  253. }
  254. _vector(offset) {
  255. return offset + this.int32(offset) + 4;
  256. }
  257. _length(offset) {
  258. return this.int32(offset + this.int32(offset));
  259. }
  260. _union(offset) {
  261. return offset + this.int32(offset);
  262. }
  263. };
  264. flatbuffers.TextReader = class {
  265. static open(obj) {
  266. return new flatbuffers.TextReader(obj);
  267. }
  268. constructor(obj) {
  269. this._root = obj;
  270. }
  271. get root() {
  272. return this._root;
  273. }
  274. value(obj, defaultValue) {
  275. return obj !== undefined ? obj : defaultValue;
  276. }
  277. object(obj, decode) {
  278. return obj !== undefined ? decode(this, obj) : obj;
  279. }
  280. array(obj) {
  281. if (Array.isArray(obj)) {
  282. const target = new Array(obj.length);
  283. for (let i = 0; i < obj.length; i++) {
  284. target[i] = obj[i];
  285. }
  286. return target;
  287. }
  288. if (!obj) {
  289. return [];
  290. }
  291. throw new flatbuffers.Error('Inalid value array.');
  292. }
  293. typedArray(obj, type) {
  294. if (Array.isArray(obj)) {
  295. const target = new type(obj.length);
  296. for (let i = 0; i < obj.length; i++) {
  297. target[i] = obj[i];
  298. }
  299. return target;
  300. }
  301. if (!obj) {
  302. return new type(0);
  303. }
  304. throw new flatbuffers.Error('Inalid typed array.');
  305. }
  306. objectArray(obj, decode) {
  307. if (Array.isArray(obj)) {
  308. const target = new Array(obj.length);
  309. for (let i = 0; i < obj.length; i++) {
  310. target[i] = decode(this, obj[i]);
  311. }
  312. return target;
  313. }
  314. if (!obj) {
  315. return [];
  316. }
  317. throw new flatbuffers.Error('Inalid object array.');
  318. }
  319. };
  320. flatbuffers.Error = class extends Error {
  321. constructor(message) {
  322. super(message);
  323. this.name = 'FlatBuffers Error';
  324. this.message = message;
  325. }
  326. };
  327. if (typeof module !== "undefined" && typeof module.exports === "object") {
  328. module.exports.BinaryReader = flatbuffers.BinaryReader;
  329. module.exports.TextReader = flatbuffers.TextReader;
  330. module.exports.get = flatbuffers.get;
  331. }