1 |
|
|
#include "cache_builder.h" |
2 |
|
|
#include "node_native_module.h" |
3 |
|
|
#include "util.h" |
4 |
|
|
|
5 |
|
|
#include <iostream> |
6 |
|
|
#include <map> |
7 |
|
|
#include <sstream> |
8 |
|
|
#include <vector> |
9 |
|
|
#include <cstdlib> |
10 |
|
|
|
11 |
|
|
namespace node { |
12 |
|
|
namespace native_module { |
13 |
|
|
|
14 |
|
|
using v8::Context; |
15 |
|
|
using v8::Function; |
16 |
|
|
using v8::Isolate; |
17 |
|
|
using v8::Local; |
18 |
|
|
using v8::MaybeLocal; |
19 |
|
|
using v8::ScriptCompiler; |
20 |
|
|
|
21 |
|
324 |
static std::string GetDefName(const std::string& id) { |
22 |
|
324 |
char buf[64] = {0}; |
23 |
|
324 |
size_t size = id.size(); |
24 |
✗✓ |
324 |
CHECK_LT(size, sizeof(buf)); |
25 |
✓✓ |
6162 |
for (size_t i = 0; i < size; ++i) { |
26 |
|
5838 |
char ch = id[i]; |
27 |
✓✓✓✓
|
5838 |
buf[i] = (ch == '-' || ch == '/') ? '_' : ch; |
28 |
|
|
} |
29 |
|
324 |
return buf; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
static std::string FormatSize(size_t size) { |
33 |
|
|
char buf[64] = {0}; |
34 |
|
|
if (size < 1024) { |
35 |
|
|
snprintf(buf, sizeof(buf), "%.2fB", static_cast<double>(size)); |
36 |
|
|
} else if (size < 1024 * 1024) { |
37 |
|
|
snprintf(buf, sizeof(buf), "%.2fKB", static_cast<double>(size / 1024)); |
38 |
|
|
} else { |
39 |
|
|
snprintf( |
40 |
|
|
buf, sizeof(buf), "%.2fMB", static_cast<double>(size / 1024 / 1024)); |
41 |
|
|
} |
42 |
|
|
return buf; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
162 |
static std::string GetDefinition(const std::string& id, |
46 |
|
|
size_t size, |
47 |
|
|
const uint8_t* data) { |
48 |
|
162 |
std::stringstream ss; |
49 |
|
162 |
ss << "static const uint8_t " << GetDefName(id) << "[] = {\n"; |
50 |
✓✓ |
719970 |
for (size_t i = 0; i < size; ++i) { |
51 |
|
719808 |
uint8_t ch = data[i]; |
52 |
✓✓ |
719808 |
ss << std::to_string(ch) << (i == size - 1 ? '\n' : ','); |
53 |
|
|
} |
54 |
|
162 |
ss << "};"; |
55 |
|
162 |
return ss.str(); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
162 |
static void GetInitializer(const std::string& id, std::stringstream& ss) { |
59 |
|
162 |
std::string def_name = GetDefName(id); |
60 |
|
162 |
ss << " code_cache.emplace(\n"; |
61 |
|
162 |
ss << " \"" << id << "\",\n"; |
62 |
|
162 |
ss << " std::make_unique<v8::ScriptCompiler::CachedData>(\n"; |
63 |
|
162 |
ss << " " << def_name << ",\n"; |
64 |
|
162 |
ss << " static_cast<int>(arraysize(" << def_name << ")), policy\n"; |
65 |
|
162 |
ss << " )\n"; |
66 |
|
162 |
ss << " );"; |
67 |
|
162 |
} |
68 |
|
|
|
69 |
|
1 |
static std::string GenerateCodeCache( |
70 |
|
|
const std::map<std::string, ScriptCompiler::CachedData*>& data, |
71 |
|
|
bool log_progress) { |
72 |
|
1 |
std::stringstream ss; |
73 |
|
|
ss << R"(#include <cinttypes> |
74 |
|
|
#include "node_native_module_env.h" |
75 |
|
|
|
76 |
|
|
// This file is generated by mkcodecache (tools/code_cache/mkcodecache.cc) |
77 |
|
|
|
78 |
|
|
namespace node { |
79 |
|
|
namespace native_module { |
80 |
|
|
|
81 |
|
|
const bool has_code_cache = true; |
82 |
|
|
|
83 |
|
1 |
)"; |
84 |
|
|
|
85 |
|
1 |
size_t total = 0; |
86 |
✓✓ |
163 |
for (const auto& x : data) { |
87 |
|
162 |
const std::string& id = x.first; |
88 |
|
162 |
ScriptCompiler::CachedData* cached_data = x.second; |
89 |
|
162 |
total += cached_data->length; |
90 |
|
162 |
std::string def = GetDefinition(id, cached_data->length, cached_data->data); |
91 |
|
162 |
ss << def << "\n\n"; |
92 |
✗✓ |
162 |
if (log_progress) { |
93 |
|
|
std::cout << "Generated cache for " << id |
94 |
|
|
<< ", size = " << FormatSize(cached_data->length) |
95 |
|
|
<< ", total = " << FormatSize(total) << "\n"; |
96 |
|
|
} |
97 |
|
162 |
} |
98 |
|
|
|
99 |
|
|
ss << R"(void NativeModuleEnv::InitializeCodeCache() { |
100 |
|
|
NativeModuleCacheMap& code_cache = |
101 |
|
|
*NativeModuleLoader::GetInstance()->code_cache(); |
102 |
|
|
CHECK(code_cache.empty()); |
103 |
|
|
auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned; |
104 |
|
1 |
)"; |
105 |
|
|
|
106 |
✓✓ |
163 |
for (const auto& x : data) { |
107 |
|
162 |
GetInitializer(x.first, ss); |
108 |
|
162 |
ss << "\n\n"; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
ss << R"( |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
} // namespace native_module |
115 |
|
|
} // namespace node |
116 |
|
1 |
)"; |
117 |
|
1 |
return ss.str(); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
1 |
std::string CodeCacheBuilder::Generate(Local<Context> context) { |
121 |
|
1 |
NativeModuleLoader* loader = NativeModuleLoader::GetInstance(); |
122 |
|
1 |
std::vector<std::string> ids = loader->GetModuleIds(); |
123 |
|
|
|
124 |
|
2 |
std::map<std::string, ScriptCompiler::CachedData*> data; |
125 |
|
|
|
126 |
✓✓ |
205 |
for (const auto& id : ids) { |
127 |
|
|
// TODO(joyeecheung): we can only compile the modules that can be |
128 |
|
|
// required here because the parameters for other types of builtins |
129 |
|
|
// are still very flexible. We should look into auto-generating |
130 |
|
|
// the parameters from the source somehow. |
131 |
✓✓ |
204 |
if (loader->CanBeRequired(id.c_str())) { |
132 |
|
|
NativeModuleLoader::Result result; |
133 |
|
162 |
USE(loader->CompileAsModule(context, id.c_str(), &result)); |
134 |
|
|
ScriptCompiler::CachedData* cached_data = |
135 |
|
162 |
loader->GetCodeCache(id.c_str()); |
136 |
✗✓ |
162 |
if (cached_data == nullptr) { |
137 |
|
|
// TODO(joyeecheung): display syntax errors |
138 |
|
|
std::cerr << "Failed to compile " << id << "\n"; |
139 |
|
|
} else { |
140 |
|
162 |
data.emplace(id, cached_data); |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
char env_buf[32]; |
146 |
|
1 |
size_t env_size = sizeof(env_buf); |
147 |
|
1 |
int ret = uv_os_getenv("NODE_DEBUG", env_buf, &env_size); |
148 |
|
1 |
bool log_progress = false; |
149 |
✗✓✗✗
|
1 |
if (ret == 0 && strcmp(env_buf, "mkcodecache") == 0) { |
150 |
|
|
log_progress = true; |
151 |
|
|
} |
152 |
|
2 |
return GenerateCodeCache(data, log_progress); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
} // namespace native_module |
156 |
✓✗✓✗
|
3 |
} // namespace node |