#include "cache_builder.h" #include "node_native_module.h" #include "util.h" #include #include #include #include #include namespace node { namespace native_module { using v8::Context; using v8::Function; using v8::Isolate; using v8::Local; using v8::MaybeLocal; using v8::ScriptCompiler; static std::string GetDefName(const std::string& id) { char buf[64] = {0}; size_t size = id.size(); CHECK_LT(size, sizeof(buf)); for (size_t i = 0; i < size; ++i) { char ch = id[i]; buf[i] = (ch == '-' || ch == '/') ? '_' : ch; } return buf; } static std::string FormatSize(size_t size) { char buf[64] = {0}; if (size < 1024) { snprintf(buf, sizeof(buf), "%.2fB", static_cast(size)); } else if (size < 1024 * 1024) { snprintf(buf, sizeof(buf), "%.2fKB", static_cast(size / 1024)); } else { snprintf( buf, sizeof(buf), "%.2fMB", static_cast(size / 1024 / 1024)); } return buf; } static std::string GetDefinition(const std::string& id, size_t size, const uint8_t* data) { std::stringstream ss; ss << "static const uint8_t " << GetDefName(id) << "[] = {\n"; for (size_t i = 0; i < size; ++i) { uint8_t ch = data[i]; ss << std::to_string(ch) << (i == size - 1 ? '\n' : ','); } ss << "};"; return ss.str(); } static void GetInitializer(const std::string& id, std::stringstream& ss) { std::string def_name = GetDefName(id); ss << " code_cache.emplace(\n"; ss