11#include " cache_builder.h"
2+ #include " node_native_module.h"
3+ #include " util.h"
4+
25#include < iostream>
36#include < map>
47#include < sstream>
58#include < vector>
69#include < cstdlib>
7- #include " util.h"
8-
9- #include " node_native_module.h"
1010
1111namespace node {
1212namespace native_module {
@@ -55,25 +55,19 @@ static std::string GetDefinition(const std::string& id,
5555 return ss.str ();
5656}
5757
58- static std::string GetInitializer (const std::string& id) {
58+ static void GetInitializer (const std::string& id, std::stringstream& ss ) {
5959 std::string def_name = GetDefName (id);
60- char buf[256 ] = {0 };
61- snprintf (buf,
62- sizeof (buf),
63- " code_cache->emplace(\n "
64- " \" %s\" ,\n "
65- " std::make_unique<v8::ScriptCompiler::CachedData>"
66- " (%s, static_cast<int>(arraysize(%s)), policy)\n "
67- " );" ,
68- id.c_str (),
69- def_name.c_str (),
70- def_name.c_str ());
71- return buf;
60+ ss << " code_cache.emplace(\n " ;
61+ ss << " \" " << id << " \" ,\n " ;
62+ ss << " std::make_unique<v8::ScriptCompiler::CachedData>(\n " ;
63+ ss << " " << def_name << " ,\n " ;
64+ ss << " static_cast<int>(arraysize(" << def_name << " )), policy\n " ;
65+ ss << " )\n " ;
66+ ss << " );" ;
7267}
7368
7469static std::string GenerateCodeCache (
75- std::map<std::string, ScriptCompiler::CachedData*> data,
76- std::vector<std::string> ids,
70+ const std::map<std::string, ScriptCompiler::CachedData*>& data,
7771 bool log_progress) {
7872 std::stringstream ss;
7973 ss << R"( #include <cinttypes>
@@ -101,20 +95,19 @@ namespace native_module {
10195 }
10296
10397 ss << R"( void NativeModuleEnv::InitializeCodeCache() {
104- NativeModuleCacheMap* code_cache =
105- NativeModuleLoader::GetInstance()->code_cache();
106- if (!code_cache->empty()) {
107- return;
108- }
98+ NativeModuleCacheMap& code_cache =
99+ *NativeModuleLoader::GetInstance()->code_cache();
100+ CHECK(code_cache.empty());
109101 auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned;
110102)" ;
111103
112104 for (const auto & x : data) {
113- const std::string& id = x.first ;
114- ss << GetInitializer (id) << " \n\n " ;
105+ GetInitializer ( x.first , ss) ;
106+ ss << " \n\n " ;
115107 }
116108
117- ss << R"( }
109+ ss << R"(
110+ }
118111
119112} // namespace native_module
120113} // namespace node
@@ -126,19 +119,15 @@ std::string CodeCacheBuilder::Generate(Local<Context> context) {
126119 NativeModuleLoader* loader = NativeModuleLoader::GetInstance ();
127120 std::vector<std::string> ids = loader->GetModuleIds ();
128121
129- std::vector<std::string> modules;
130- modules.reserve (ids.size ());
131-
132122 std::map<std::string, ScriptCompiler::CachedData*> data;
133123
134- NativeModuleLoader::Result result;
135124 for (const auto & id : ids) {
136125 // TODO(joyeecheung): we can only compile the modules that can be
137126 // required here because the parameters for other types of builtins
138127 // are still very flexible. We should look into auto-generating
139128 // the paramters from the source somehow.
140129 if (loader->CanBeRequired (id.c_str ())) {
141- modules. push_back (id) ;
130+ NativeModuleLoader::Result result ;
142131 USE (loader->CompileAsModule (context, id.c_str (), &result));
143132 ScriptCompiler::CachedData* cached_data =
144133 loader->GetCodeCache (id.c_str ());
@@ -158,7 +147,7 @@ std::string CodeCacheBuilder::Generate(Local<Context> context) {
158147 if (ret == 0 && strcmp (env_buf, " mkcodecache" ) == 0 ) {
159148 log_progress = true ;
160149 }
161- return GenerateCodeCache (data, modules, log_progress);
150+ return GenerateCodeCache (data, log_progress);
162151}
163152
164153} // namespace native_module
0 commit comments