mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
Add deno::Basename and deno::BinaryContentAsC utilies.
These are prep for code sharing with the asset code soon to land.
This commit is contained in:
parent
08606e40a8
commit
c6ae3f7abc
4 changed files with 104 additions and 82 deletions
107
src/file_util.cc
107
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;
|
||||
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());
|
||||
}
|
||||
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;
|
||||
}
|
||||
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
|
||||
|
|
|
@ -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_
|
||||
|
|
|
@ -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) { }
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue