diff options
-rw-r--r-- | src/file_util.cc | 107 | ||||
-rw-r--r-- | src/file_util.h | 4 | ||||
-rw-r--r-- | src/file_util_test.cc | 16 | ||||
-rw-r--r-- | src/snapshot_creator.cc | 59 |
4 files changed, 104 insertions, 82 deletions
diff --git a/src/file_util.cc b/src/file_util.cc index 52e0bc204..38e393300 100644 --- a/src/file_util.cc +++ b/src/file_util.cc @@ -10,6 +10,28 @@ namespace deno { +std::string BinaryContentAsC(const char* name, const std::string& data) { + char b[512]; + std::string output; + // Write prefix. + snprintf(b, sizeof(b), "static const char %s_data[] = {\n", name); + output.append(b); + // Write actual data. + for (size_t i = 0; i < data.size(); ++i) { + if ((i & 0x1F) == 0x1F) output.append("\n"); + if (i > 0) output.append(","); + snprintf(b, sizeof(b), "%hhu", static_cast<unsigned char>(data.at(i))); + output.append(b); + } + output.append("\n"); + // Write suffix. + output.append("};\n"); + snprintf(b, sizeof(b), "static const int %s_size = %" PRId64 ";\n", name, + static_cast<uint64_t>(data.size())); + output.append(b); + return output; +} + bool ReadFileToString(const char* fn, std::string* contents) { std::ifstream file(fn, std::ios::binary); if (file.fail()) { @@ -19,87 +41,14 @@ bool ReadFileToString(const char* fn, std::string* contents) { return !file.fail(); } -class StartupDataCppWriter { - public: - StartupDataCppWriter(const char* name, const char* filename, - const std::string& data) - : name_(name), - filename_(filename), - data_(data), - file_(filename_, std::ios::binary) {} - - bool Write() { - if (file_.bad()) { - return false; - } - WritePrefix(); - WriteData(); - WriteSuffix(); - - file_.close(); - // printf("Wrote %s %d %s \n", name_, data_.size(), filename_); - return !file_.bad(); - } - - private: - void WritePrefix() { - file_ << "// Autogenerated snapshot file. Do not edit.\n\n"; - file_ << "#include \"third_party/v8/include/v8.h\"\n\n"; - file_ << "namespace deno { \n\n"; - } - - void WriteSuffix() { - char buffer[500]; - snprintf(buffer, sizeof(buffer), "v8::StartupData* StartupBlob_%s() {\n", - name_); - file_ << buffer; - snprintf(buffer, sizeof(buffer), " return &%s_blob;\n", name_); - file_ << buffer; - file_ << "}\n\n"; - file_ << "} // namespace deno\n\n"; - } - - void WriteBinaryContentsAsCArray() { - char buffer[5]; - for (size_t i = 0; i < data_.size(); i++) { - if ((i & 0x1F) == 0x1F) file_ << "\n"; - if (i > 0) file_ << ","; - snprintf(buffer, sizeof(buffer), "%u", - static_cast<unsigned char>(data_.at(i))); - file_ << buffer; +std::string Basename(std::string const& filename) { + for (auto it = filename.rbegin(); it != filename.rend(); ++it) { + char ch = *it; + if (ch == '\\' || ch == '/') { + return std::string(it.base(), filename.end()); } - file_ << "\n"; } - - void WriteData() { - char buffer[500]; - snprintf(buffer, sizeof(buffer), "static const char %s_blob_data[] = {\n", - name_); - file_ << buffer; - WriteBinaryContentsAsCArray(); - file_ << "};\n"; - snprintf(buffer, sizeof(buffer), - "static const int %s_blob_size = %" PRId64 ";\n", name_, - static_cast<uint64_t>(data_.size())); - file_ << buffer; - snprintf(buffer, sizeof(buffer), "static v8::StartupData %s_blob =\n", - name_); - file_ << buffer; - snprintf(buffer, sizeof(buffer), - "{ (const char*) %s_blob_data, %s_blob_size };\n", name_, name_); - file_ << buffer; - } - - const char* name_; - const char* filename_; - std::string data_; - std::ofstream file_; -}; - -bool WriteDataAsCpp(const char* name, const char* filename, - const std::string& data) { - StartupDataCppWriter writer(name, filename, data); - return writer.Write(); + return filename; } } // namespace deno diff --git a/src/file_util.h b/src/file_util.h index 87e39299d..a2abb5f68 100644 --- a/src/file_util.h +++ b/src/file_util.h @@ -6,9 +6,9 @@ #include <string> namespace deno { -bool WriteDataAsCpp(const char* name, const char* filename, - const std::string& data); bool ReadFileToString(const char* fn, std::string* contents); +std::string Basename(std::string const& filename); +std::string BinaryContentAsC(const char* name, const std::string& data); } // namespace deno #endif // FILE_UTIL_H_ diff --git a/src/file_util_test.cc b/src/file_util_test.cc index 3b1c3e239..33d635881 100644 --- a/src/file_util_test.cc +++ b/src/file_util_test.cc @@ -9,5 +9,21 @@ TEST(FileUtilTest, ReadFileToStringFileNotExist) { EXPECT_FALSE(deno::ReadFileToString("/should_error_out.txt", &output)); } +TEST(FileUtilTest, Basename) { + EXPECT_EQ("foo.txt", deno::Basename("foo.txt")); + EXPECT_EQ("foo.txt", deno::Basename("/foo.txt")); + EXPECT_EQ("", deno::Basename("/")); + EXPECT_EQ("foo.txt", deno::Basename(".\\foo.txt")); + EXPECT_EQ("foo.txt", deno::Basename("/home/ryan/foo.txt")); + EXPECT_EQ("foo.txt", deno::Basename("C:\\home\\ryan\\foo.txt")); +} + +TEST(FileUtilTest, BinaryContentAsC) { + auto c_code = deno::BinaryContentAsC("aaa", std::string("bbb")); + EXPECT_TRUE(c_code.find("static const char aaa_data[]") != std::string::npos); + EXPECT_TRUE(c_code.find("static const int aaa_size = 3;") != + std::string::npos); +} + // TODO(ry) success unit test. Needs a tempfile or fixture. // TEST(FileUtilTest, ReadFileToStringSuccess) { } diff --git a/src/snapshot_creator.cc b/src/snapshot_creator.cc index cb5caf304..3aca7c3a8 100644 --- a/src/snapshot_creator.cc +++ b/src/snapshot_creator.cc @@ -1,6 +1,7 @@ // Copyright 2018 Ryan Dahl <ry@tinyclouds.org> // All rights reserved. MIT License. // Hint: --trace_serializer is a useful debugging flag. +#include <fstream> #include "deno.h" #include "file_util.h" #include "internal.h" @@ -40,6 +41,61 @@ v8::StartupData MakeSnapshot(const char* js_filename, const char* js_source) { return snapshot_blob; } +class StartupDataCppWriter { + public: + StartupDataCppWriter(const char* name, const char* filename, + const std::string& data) + : name_(name), + filename_(filename), + data_(data), + file_(filename_, std::ios::binary) {} + + bool Write() { + if (file_.bad()) { + return false; + } + WritePrefix(); + WriteData(); + WriteSuffix(); + + file_.close(); + // printf("Wrote %s %d %s \n", name_, data_.size(), filename_); + return !file_.bad(); + } + + private: + void WritePrefix() { + file_ << "// Autogenerated snapshot file. Do not edit.\n\n"; + file_ << "#include \"third_party/v8/include/v8.h\"\n\n"; + file_ << "namespace deno { \n\n"; + } + + void WriteSuffix() { + char buffer[500]; + snprintf(buffer, sizeof(buffer), "v8::StartupData* StartupBlob_%s() {\n", + name_); + file_ << buffer; + snprintf(buffer, sizeof(buffer), " return &%s_blob;\n", name_); + file_ << buffer; + file_ << "}\n\n"; + file_ << "} // namespace deno\n\n"; + } + + void WriteData() { + char buffer[500]; + file_ << BinaryContentAsC(name_, data_); + snprintf(buffer, sizeof(buffer), + "static v8::StartupData %s_blob = { %s_data, %s_size };\n", name_, + name_, name_); + file_ << buffer; + } + + const char* name_; + const char* filename_; + std::string data_; + std::ofstream file_; +}; + } // namespace deno int main(int argc, char** argv) { @@ -59,5 +115,6 @@ int main(int argc, char** argv) { auto snapshot_blob = deno::MakeSnapshot(js_fn, js_source.c_str()); std::string snapshot_str(snapshot_blob.data, snapshot_blob.raw_size); - CHECK(deno::WriteDataAsCpp("snapshot", snapshot_out_cc, snapshot_str)); + deno::StartupDataCppWriter writer("snapshot", snapshot_out_cc, snapshot_str); + CHECK(writer.Write()); } |