forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deno::Basename and deno::BinaryContentAsC utilies.
These are prep for code sharing with the asset code soon to land.
- Loading branch information
Showing
4 changed files
with
104 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright 2018 Ryan Dahl <[email protected]> | ||
// 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()); | ||
} |