Repository navigation
Expand file tree
/
Copy pathV8Flags.cpp
More file actions
100 lines (93 loc) · 2.71 KB
/
Copy pathV8Flags.cpp
File metadata and controls
100 lines (93 loc) · 2.71 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
#include "ns/V8Flags.h"
#include <algorithm>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
namespace nativescript::common {
std::vector<std::string> V8FlagArgs(const std::string& flags) {
static constexpr const char* kSpace = " \t\n\v\f\r";
std::vector<std::string> args;
size_t pos = 0;
while (pos < flags.size()) {
const size_t start = flags.find_first_not_of(kSpace, pos);
if (start == std::string::npos) {
break;
}
size_t end = flags.find_first_of(kSpace, start);
if (end == std::string::npos) {
end = flags.size();
}
pos = end;
std::string arg = flags.substr(start, end - start);
if (arg == "--") {
break;
}
args.push_back(std::move(arg));
}
return args;
}
bool V8FlagsDisableLazyCompile(const std::string& flags) {
bool lazy = true;
for (std::string arg : V8FlagArgs(flags)) {
if (arg.size() < 2 || arg[0] != '-') {
continue;
}
std::replace(arg.begin(), arg.end(), '_', '-');
size_t name = arg[1] == '-' ? 2 : 1;
bool negated = false;
if (arg.compare(name, 2, "no") == 0) {
name += 2;
if (name < arg.size() && arg[name] == '-') {
name++;
}
negated = true;
}
if (arg.compare(name, std::string::npos, "lazy") == 0 ||
arg.compare(name, 5, "lazy=") == 0) {
lazy = !negated;
}
}
return !lazy;
}
size_t V8FlagsStackSizeKb(const std::string& flags) {
const std::vector<std::string> args = V8FlagArgs(flags);
size_t kb = 0;
for (size_t i = 0; i < args.size(); i++) {
std::string arg = args[i];
if (arg.size() < 2 || arg[0] != '-') {
continue;
}
std::replace(arg.begin(), arg.end(), '_', '-');
const size_t name = arg[1] == '-' ? 2 : 1;
std::string value;
if (arg.compare(name, std::string::npos, "stack-size") == 0) {
if (i + 1 >= args.size()) {
break;
}
value = args[++i];
} else if (arg.compare(name, 11, "stack-size=") == 0) {
value = arg.substr(name + 11);
} else {
continue;
}
char* end = nullptr;
errno = 0;
const long long parsed = std::strtoll(value.c_str(), &end, 10);
if (errno == 0 && end != value.c_str() && *end == '\0' && parsed > 0) {
kb = static_cast<size_t>(parsed);
}
}
return kb;
}
size_t BackgroundCompileStackBytes(const std::string& flags) {
constexpr size_t kHeadroomBytes = 1024 * 1024;
constexpr size_t kMinStackBytes = 2 * 1024 * 1024;
const size_t stackKb = V8FlagsStackSizeKb(flags);
if (stackKb == 0) {
return kMinStackBytes;
}
const size_t maxKb = (SIZE_MAX - kHeadroomBytes) / 1024;
return std::max(kMinStackBytes,
std::min(stackKb, maxKb) * 1024 + kHeadroomBytes);
}
} // namespace nativescript::common