forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredefinedStringIDs.cpp
More file actions
88 lines (69 loc) · 2.41 KB
/
PredefinedStringIDs.cpp
File metadata and controls
88 lines (69 loc) · 2.41 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
84
85
86
87
88
/*
* 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/PredefinedStringIDs.h"
#include "hermes/VM/Predefined.h"
#include "hermes/VM/SymbolID.h"
#include "llvh/ADT/DenseMap.h"
#include <array>
#include <tuple>
namespace {
using namespace hermes::vm;
using StringIDMap = llvh::DenseMap<llvh::StringRef, SymbolID>;
StringIDMap createPredefinedStringSet() {
namespace P = Predefined;
auto lengths = predefStringLengths;
const size_t NumPredefStrings = P::NumStrings;
static const Predefined::Str ids[] = {
#define STR(name, string) P::name,
#include "hermes/VM/PredefinedStrings.def"
};
StringIDMap predefined;
assert(
NumPredefStrings == sizeof(ids) / sizeof(Predefined::Str) &&
"Mismatched count of predefined strings.");
const char *chars = predefStringAndSymbolChars.begin();
for (uint32_t i = 0; i < NumPredefStrings; chars += lengths[i++]) {
auto res = predefined.try_emplace(
{chars, lengths[i]}, Predefined::getSymbolID(ids[i]));
assert(res.second && "Duplicate predefined string.");
(void)res;
}
return predefined;
}
} // namespace
namespace hermes {
namespace vm {
llvh::Optional<SymbolID> getPredefinedStringID(llvh::StringRef str) {
static const auto predefined = createPredefinedStringSet();
auto it = predefined.find(str);
if (it == predefined.end()) {
return {};
}
return it->second;
}
// Use local constexpr arrays to avoid complaint about global constructors.
static constexpr uint8_t _predefStringLengths[] = {
#define STR(name, string) sizeof(string) - 1,
#include "hermes/VM/PredefinedStrings.def"
};
static constexpr uint8_t _predefSymbolLengths[] = {
#define SYM(name, symbol) sizeof(symbol) - 1,
#include "hermes/VM/PredefinedSymbols.def"
};
const llvh::ArrayRef<uint8_t> predefSymbolLengths = _predefSymbolLengths;
const llvh::ArrayRef<uint8_t> predefStringLengths = _predefStringLengths;
const llvh::ArrayRef<char> predefStringAndSymbolChars =
// One buffer contains all strings.
// This ensures that all the strings live together in memory,
// and that we don't touch multiple separate pages on startup.
#define STR(name, string) string
#include "hermes/VM/PredefinedStrings.def"
#define SYM(name, symbol) symbol
#include "hermes/VM/PredefinedSymbols.def"
;
} // namespace vm
} // namespace hermes