Coverage Report

Created: 2025-12-12 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hermes/lib/VM/PredefinedStringIDs.cpp
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
#include "hermes/VM/PredefinedStringIDs.h"
9
#include "hermes/VM/Predefined.h"
10
#include "hermes/VM/SymbolID.h"
11
12
#include "llvh/ADT/DenseMap.h"
13
14
#include <array>
15
#include <tuple>
16
17
namespace {
18
19
using namespace hermes::vm;
20
using StringIDMap = llvh::DenseMap<llvh::StringRef, SymbolID>;
21
22
0
StringIDMap createPredefinedStringSet() {
23
0
  namespace P = Predefined;
24
0
  auto lengths = predefStringLengths;
25
0
  const size_t NumPredefStrings = P::NumStrings;
26
27
0
  static const Predefined::Str ids[] = {
28
0
#define STR(name, string) P::name,
29
0
#include "hermes/VM/PredefinedStrings.def"
30
0
  };
31
32
0
  StringIDMap predefined;
33
34
0
  assert(
35
0
      NumPredefStrings == sizeof(ids) / sizeof(Predefined::Str) &&
36
0
      "Mismatched count of predefined strings.");
37
0
  const char *chars = predefStringAndSymbolChars.begin();
38
0
  for (uint32_t i = 0; i < NumPredefStrings; chars += lengths[i++]) {
39
0
    auto res = predefined.try_emplace(
40
0
        {chars, lengths[i]}, Predefined::getSymbolID(ids[i]));
41
0
    assert(res.second && "Duplicate predefined string.");
42
0
    (void)res;
43
0
  }
44
45
0
  return predefined;
46
0
}
47
48
} // namespace
49
50
namespace hermes {
51
namespace vm {
52
53
0
llvh::Optional<SymbolID> getPredefinedStringID(llvh::StringRef str) {
54
0
  static const auto predefined = createPredefinedStringSet();
55
0
  auto it = predefined.find(str);
56
0
  if (it == predefined.end()) {
57
0
    return {};
58
0
  }
59
60
0
  return it->second;
61
0
}
62
63
// Use local constexpr arrays to avoid complaint about global constructors.
64
static constexpr uint8_t _predefPropertyLengths[] = {
65
#define PROP(id) sizeof("(InternalProperty" #id ")") - 1,
66
#define NAMED_PROP(name) sizeof("(InternalProperty" #name ")") - 1,
67
#include "hermes/VM/InternalProperties.def"
68
};
69
70
static constexpr uint8_t _predefStringLengths[] = {
71
#define STR(name, string) sizeof(string) - 1,
72
#include "hermes/VM/PredefinedStrings.def"
73
};
74
75
static constexpr uint8_t _predefSymbolLengths[] = {
76
#define SYM(name, symbol) sizeof(symbol) - 1,
77
#include "hermes/VM/PredefinedSymbols.def"
78
};
79
80
const llvh::ArrayRef<uint8_t> predefPropertyLengths = _predefPropertyLengths;
81
const llvh::ArrayRef<uint8_t> predefSymbolLengths = _predefSymbolLengths;
82
const llvh::ArrayRef<uint8_t> predefStringLengths = _predefStringLengths;
83
84
const llvh::ArrayRef<char> predefStringAndSymbolChars =
85
// One buffer contains all strings.
86
// This ensures that all the strings live together in memory,
87
// and that we don't touch multiple separate pages on startup.
88
#define PROP(id) "(InternalProperty" #id ")"
89
#define NAMED_PROP(name) "(InternalProperty" #name ")"
90
#include "hermes/VM/InternalProperties.def"
91
#define STR(name, string) string
92
#include "hermes/VM/PredefinedStrings.def"
93
#define SYM(name, symbol) symbol
94
#include "hermes/VM/PredefinedSymbols.def"
95
    ;
96
97
} // namespace vm
98
} // namespace hermes