forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteObjectsTable.cpp
More file actions
146 lines (111 loc) · 2.86 KB
/
RemoteObjectsTable.cpp
File metadata and controls
146 lines (111 loc) · 2.86 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* 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 "RemoteObjectsTable.h"
#include <cstdlib>
#include <folly/Conv.h>
namespace {
bool isScopeId(int64_t id) {
return id < 0;
}
bool isValueId(int64_t id) {
return id > 0;
}
std::string toObjId(int64_t id) {
return folly::to<std::string>(id);
}
int64_t toId(const std::string &objId) {
return atoll(objId.c_str());
}
} // namespace
namespace facebook {
namespace hermes {
namespace inspector {
namespace chrome {
const char *BacktraceObjectGroup = "backtrace";
const char *ConsoleObjectGroup = "console";
RemoteObjectsTable::RemoteObjectsTable() = default;
RemoteObjectsTable::~RemoteObjectsTable() = default;
std::string RemoteObjectsTable::addScope(
std::pair<uint32_t, uint32_t> frameAndScopeIndex,
const std::string &objectGroup) {
int64_t id = scopeId_--;
scopes_[id] = frameAndScopeIndex;
if (!objectGroup.empty()) {
idToGroup_[id] = objectGroup;
groupToIds_[objectGroup].push_back(id);
}
return toObjId(id);
}
std::string RemoteObjectsTable::addValue(
::facebook::jsi::Value value,
const std::string &objectGroup) {
int64_t id = valueId_++;
values_[id] = std::move(value);
if (!objectGroup.empty()) {
idToGroup_[id] = objectGroup;
groupToIds_[objectGroup].push_back(id);
}
return toObjId(id);
}
const std::pair<uint32_t, uint32_t> *RemoteObjectsTable::getScope(
const std::string &objId) const {
int64_t id = toId(objId);
if (!isScopeId(id)) {
return nullptr;
}
auto it = scopes_.find(id);
if (it == scopes_.end()) {
return nullptr;
}
return &it->second;
}
const ::facebook::jsi::Value *RemoteObjectsTable::getValue(
const std::string &objId) const {
int64_t id = toId(objId);
if (!isValueId(id)) {
return nullptr;
}
auto it = values_.find(id);
if (it == values_.end()) {
return nullptr;
}
return &it->second;
}
std::string RemoteObjectsTable::getObjectGroup(const std::string &objId) const {
int64_t id = toId(objId);
auto it = idToGroup_.find(id);
if (it == idToGroup_.end()) {
return "";
}
return it->second;
}
void RemoteObjectsTable::releaseObject(int64_t id) {
if (isScopeId(id)) {
scopes_.erase(id);
} else if (isValueId(id)) {
values_.erase(id);
}
}
void RemoteObjectsTable::releaseObject(const std::string &objId) {
int64_t id = toId(objId);
releaseObject(id);
}
void RemoteObjectsTable::releaseObjectGroup(const std::string &objectGroup) {
auto it = groupToIds_.find(objectGroup);
if (it == groupToIds_.end()) {
return;
}
const auto &ids = it->second;
for (int64_t id : ids) {
releaseObject(id);
}
groupToIds_.erase(it);
}
} // namespace chrome
} // namespace inspector
} // namespace hermes
} // namespace facebook