tools: refactor mkcodecache · nodejs/node@5ac0308 (original) (raw)

1

1

`#include "cache_builder.h"

`

``

2

`+

#include "node_native_module.h"

`

``

3

`+

#include "util.h"

`

``

4

+

2

5

`#include

`

3

6

`#include

`

4

7

`#include

`

5

8

`#include

`

6

9

`#include

`

7

``

`-

#include "util.h"

`

8

``

-

9

``

`-

#include "node_native_module.h"

`

10

10

``

11

11

`namespace node {

`

12

12

`namespace native_module {

`

`@@ -55,25 +55,19 @@ static std::string GetDefinition(const std::string& id,

`

55

55

`return ss.str();

`

56

56

`}

`

57

57

``

58

``

`-

static std::string GetInitializer(const std::string& id) {

`

``

58

`+

static void GetInitializer(const std::string& id, std::stringstream& ss) {

`

59

59

` 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_uniquev8::ScriptCompiler::CachedData"

`

66

``

`-

"(%s, static_cast(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_uniquev8::ScriptCompiler::CachedData(\n";

`

``

63

`+

ss << " " << def_name << ",\n";

`

``

64

`+

ss << " static_cast(arraysize(" << def_name << ")), policy\n";

`

``

65

`+

ss << " )\n";

`

``

66

`+

ss << " );";

`

72

67

`}

`

73

68

``

74

69

`static std::string GenerateCodeCache(

`

75

``

`-

std::map<std::string, ScriptCompiler::CachedData*> data,

`

76

``

`-

std::vectorstd::string ids,

`

``

70

`+

const std::map<std::string, ScriptCompiler::CachedData*>& data,

`

77

71

`bool log_progress) {

`

78

72

` std::stringstream ss;

`

79

73

` ss << R"(#include

`

`@@ -101,20 +95,19 @@ namespace native_module {

`

101

95

` }

`

102

96

``

103

97

` 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());

`

109

101

` auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned;

`

110

102

`)";

`

111

103

``

112

104

`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";

`

115

107

` }

`

116

108

``

117

``

`-

ss << R"(}

`

``

109

`+

ss << R"(

`

``

110

`+

}

`

118

111

``

119

112

`} // namespace native_module

`

120

113

`} // namespace node

`

`@@ -126,19 +119,15 @@ std::string CodeCacheBuilder::Generate(Local context) {

`

126

119

` NativeModuleLoader* loader = NativeModuleLoader::GetInstance();

`

127

120

` std::vectorstd::string ids = loader->GetModuleIds();

`

128

121

``

129

``

`-

std::vectorstd::string modules;

`

130

``

`-

modules.reserve(ids.size());

`

131

``

-

132

122

` std::map<std::string, ScriptCompiler::CachedData*> data;

`

133

123

``

134

``

`-

NativeModuleLoader::Result result;

`

135

124

`for (const auto& id : ids) {

`

136

125

`// TODO(joyeecheung): we can only compile the modules that can be

`

137

126

`// required here because the parameters for other types of builtins

`

138

127

`// are still very flexible. We should look into auto-generating

`

139

128

`// the paramters from the source somehow.

`

140

129

`if (loader->CanBeRequired(id.c_str())) {

`

141

``

`-

modules.push_back(id);

`

``

130

`+

NativeModuleLoader::Result result;

`

142

131

`USE(loader->CompileAsModule(context, id.c_str(), &result));

`

143

132

` ScriptCompiler::CachedData* cached_data =

`

144

133

` loader->GetCodeCache(id.c_str());

`

`@@ -158,7 +147,7 @@ std::string CodeCacheBuilder::Generate(Local context) {

`

158

147

`if (ret == 0 && strcmp(env_buf, "mkcodecache") == 0) {

`

159

148

` log_progress = true;

`

160

149

` }

`

161

``

`-

return GenerateCodeCache(data, modules, log_progress);

`

``

150

`+

return GenerateCodeCache(data, log_progress);

`

162

151

`}

`

163

152

``

164

153

`} // namespace native_module

`