forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildMetadata.cpp
More file actions
69 lines (59 loc) · 1.89 KB
/
BuildMetadata.cpp
File metadata and controls
69 lines (59 loc) · 1.89 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
/*
* 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/BuildMetadata.h"
#include "hermes/VM/CellKind.h"
#include "llvh/Support/Debug.h"
#include <cstdint>
#define DEBUG_TYPE "metadata"
namespace hermes {
namespace vm {
Metadata buildMetadata(CellKind kind, BuildMetadataCallback *builder) {
const GCCell *base;
#ifdef HERMES_UBSAN
// Using members on a nullptr is UB, but using a pointer to static memory is
// not.
static const int64_t buf[128]{};
base = reinterpret_cast<const GCCell *>(&buf);
#else
// Use nullptr when not building with UBSAN to ensure fast failures.
base = nullptr;
#endif
// This memory should not be read or written to
Metadata::Builder mb(base);
builder(base, mb);
Metadata meta = mb.build();
LLVM_DEBUG(
llvh::dbgs() << "Initialized metadata for cell kind " << cellKindStr(kind)
<< ": " << meta << "\n");
return meta;
}
void buildMetadataTable() {
// Once the storage is initialized, also initialize the global array of VTable
// pointers using the new metadata.
static std::once_flag flag;
std::call_once(flag, [] {
// For each class of object, initialize its metadata
// Only run this once per class, not once per Runtime instantiation.
Metadata::metadataTable = {
#define CELL_KIND(name) buildMetadata(CellKind::name##Kind, name##BuildMeta),
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
};
assert(
!VTable::vtableArray[0] &&
"VTable array should not be initialized before this point.");
VTable::vtableArray = {
#define CELL_KIND(name) \
Metadata::metadataTable[static_cast<uint8_t>(CellKind::name##Kind)].vtp,
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
};
});
}
} // namespace vm
} // namespace hermes
#undef DEBUG_TYPE