forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializedLiteralParser.cpp
More file actions
83 lines (74 loc) · 2.91 KB
/
SerializedLiteralParser.cpp
File metadata and controls
83 lines (74 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/SerializedLiteralParser.h"
#include "hermes/BCGen/HBC/SerializedLiteralGenerator.h"
#include "hermes/VM/HermesValue.h"
#include "hermes/VM/RuntimeModule.h"
#include "llvh/Support/Endian.h"
namespace hermes {
namespace vm {
using SLG = hermes::hbc::SerializedLiteralGenerator;
HermesValue SerializedLiteralParser::get(Runtime *) {
assert(hasNext() && "Object buffer doesn't have any more values");
if (leftInSeq_ == 0)
parseTagAndSeqLength();
leftInSeq_--;
elemsLeft_--;
// Passing the nullptr instead of a RuntimeModule signifies that
// the generator is generating object key values, and that it should
// just return the StringID rather than find a String Primitive.
switch (lastTag_) {
case SLG::ByteStringTag: {
uint8_t val = llvh::support::endian::read<uint8_t, 1>(
buffer_.data() + currIdx_, llvh::support::endianness::little);
currIdx_ += 1;
return runtimeModule_ == nullptr
? HermesValue::encodeSymbolValue(SymbolID::unsafeCreate(val))
: HermesValue::encodeStringValue(
runtimeModule_->getStringPrimFromStringIDMayAllocate(val));
}
case SLG::ShortStringTag: {
uint16_t val = llvh::support::endian::read<uint16_t, 1>(
buffer_.data() + currIdx_, llvh::support::endianness::little);
currIdx_ += 2;
return runtimeModule_ == nullptr
? HermesValue::encodeSymbolValue(SymbolID::unsafeCreate(val))
: HermesValue::encodeStringValue(
runtimeModule_->getStringPrimFromStringIDMayAllocate(val));
}
case SLG::LongStringTag: {
uint32_t val = llvh::support::endian::read<uint32_t, 1>(
buffer_.data() + currIdx_, llvh::support::endianness::little);
currIdx_ += 4;
return runtimeModule_ == nullptr
? HermesValue::encodeSymbolValue(SymbolID::unsafeCreate(val))
: HermesValue::encodeStringValue(
runtimeModule_->getStringPrimFromStringIDMayAllocate(val));
}
case SLG::NumberTag: {
double val = llvh::support::endian::read<double, 1>(
buffer_.data() + currIdx_, llvh::support::endianness::little);
currIdx_ += 8;
return HermesValue::encodeNumberValue(val);
}
case SLG::IntegerTag: {
int32_t val = llvh::support::endian::read<int32_t, 1>(
buffer_.data() + currIdx_, llvh::support::endianness::little);
currIdx_ += 4;
return HermesValue::encodeNumberValue(val);
}
case SLG::NullTag:
return HermesValue::encodeNullValue();
case SLG::TrueTag:
return HermesValue::encodeBoolValue(true);
case SLG::FalseTag:
return HermesValue::encodeBoolValue(false);
}
llvm_unreachable("No other valid tag");
}
} // namespace vm
} // namespace hermes