2018-06-14 05:50:35 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
2018-07-01 12:21:03 -04:00
|
|
|
#include <inttypes.h>
|
2018-06-14 05:50:35 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "file_util.h"
|
|
|
|
|
|
|
|
namespace deno {
|
|
|
|
|
|
|
|
bool ReadFileToString(const char* fn, std::string* contents) {
|
|
|
|
std::ifstream file(fn, std::ios::binary);
|
2018-06-15 09:45:45 -04:00
|
|
|
if (file.fail()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-14 05:50:35 -04:00
|
|
|
contents->assign(std::istreambuf_iterator<char>{file}, {});
|
2018-06-15 09:45:45 -04:00
|
|
|
return !file.fail();
|
2018-06-14 05:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2018-07-03 04:15:32 -04:00
|
|
|
file_ << "#include \"third_party/v8/include/v8.h\"\n\n";
|
2018-06-18 10:02:08 -04:00
|
|
|
file_ << "namespace deno { \n\n";
|
2018-06-14 05:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteSuffix() {
|
|
|
|
char buffer[500];
|
2018-06-18 10:02:08 -04:00
|
|
|
snprintf(buffer, sizeof(buffer), "v8::StartupData* StartupBlob_%s() {\n",
|
|
|
|
name_);
|
2018-06-14 05:50:35 -04:00
|
|
|
file_ << buffer;
|
|
|
|
snprintf(buffer, sizeof(buffer), " return &%s_blob;\n", name_);
|
|
|
|
file_ << buffer;
|
|
|
|
file_ << "}\n\n";
|
2018-06-18 10:02:08 -04:00
|
|
|
file_ << "} // namespace deno\n\n";
|
2018-06-14 05:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2018-07-01 12:21:03 -04:00
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"static const int %s_blob_size = %" PRId64 ";\n", name_,
|
|
|
|
static_cast<uint64_t>(data_.size()));
|
2018-06-14 05:50:35 -04:00
|
|
|
file_ << buffer;
|
2018-06-18 10:02:08 -04:00
|
|
|
snprintf(buffer, sizeof(buffer), "static v8::StartupData %s_blob =\n",
|
2018-06-14 05:50:35 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|