/src/hermes/include/hermes/BCGen/HBC/SerializedLiteralParserBase.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #ifndef HERMES_BCGEN_HBC_SERIALIZEDLITERALPARSERBASE_H |
9 | | #define HERMES_BCGEN_HBC_SERIALIZEDLITERALPARSERBASE_H |
10 | | |
11 | | #include "hermes/Support/Conversions.h" |
12 | | #include "llvh/ADT/ArrayRef.h" |
13 | | |
14 | | namespace hermes { |
15 | | namespace hbc { |
16 | | |
17 | | /// SerializedLiteralParserBase is the base class for SerializedLiteralParser |
18 | | /// containing VM independent parsing logic. |
19 | | class SerializedLiteralParserBase { |
20 | | protected: |
21 | | using CharArray = llvh::ArrayRef<unsigned char>; |
22 | | |
23 | | /// Stores the unsigned char buffer in which the literals are serialized. |
24 | | CharArray buffer_; |
25 | | |
26 | | /// Stores the elements left in the generator. |
27 | | unsigned int elemsLeft_; |
28 | | |
29 | | /// Stores the last sequence type. |
30 | | unsigned char lastTag_; |
31 | | |
32 | | /// Stores the amount of consecutive elements of the same type that are ahead |
33 | | /// of the current index. |
34 | | int leftInSeq_{0}; |
35 | | |
36 | | /// Stores the current index into the buffer. |
37 | | int currIdx_{0}; |
38 | | |
39 | | /// Called whenever the parser needs to read in the tag from the buffer |
40 | | /// i.e. during the first call to get, and whenever leftInSeq_ == 0 |
41 | | /// Checks the first byte (or two) of the buffer and treats it as a tag |
42 | | /// of the same format as outlined above. |
43 | | /// It sets `lastTag_` and `leftInSeq_`, and increments `currIdx_` |
44 | | /// to make it point to the next byte after the tag. |
45 | | void parseTagAndSeqLength(); |
46 | | |
47 | | protected: |
48 | | /// Creates a parser which generates HermesValues from a char buffer. |
49 | | /// buff represents the char buffer that will be parsed. |
50 | | /// totalLen represents the amount of elements to be parsed. |
51 | | /// runtimeModule represents the runtimeModule from which to get |
52 | | /// string primitives. |
53 | | /// If the nullptr is passed instead of a runtimeModule, the parser |
54 | | /// knows to return the StringID directly (for object keys) |
55 | | explicit SerializedLiteralParserBase(CharArray buff, unsigned int totalLen) |
56 | 4 | : buffer_(buff), elemsLeft_(totalLen) {} |
57 | | |
58 | | public: |
59 | 66.7k | bool hasNext() const { |
60 | 66.7k | return elemsLeft_ != 0; |
61 | 66.7k | } |
62 | | }; |
63 | | |
64 | | } // namespace hbc |
65 | | } // namespace hermes |
66 | | |
67 | | #endif // HERMES_BCGEN_HBC_SERIALIZEDLITERALPARSERBASE_H |