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