Skip to content

Commit

Permalink
src: add string_view overload to snapshot FromBlob
Browse files Browse the repository at this point in the history
The `const std::vector<char>&` variant only delegates to a method
that converts it to a `std::string_view` anyway, but adding this
capability to the public API as well means that it’s easier to
store snapshot data in embedding applications (because using
`std::vector<>` enforces heap allocation and `std::string_view`
does not).

PR-URL: nodejs#52595
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
addaleax authored and bmeck committed Jun 22, 2024
1 parent 775fcd3 commit e409581
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/api/embed_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ EmbedderSnapshotData::Pointer EmbedderSnapshotData::BuiltinSnapshotData() {

EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromBlob(
const std::vector<char>& in) {
return FromBlob(std::string_view(in.data(), in.size()));
}

EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromBlob(
std::string_view in) {
SnapshotData* snapshot_data = new SnapshotData();
CHECK_EQ(snapshot_data->data_ownership, SnapshotData::DataOwnership::kOwned);
EmbedderSnapshotData::Pointer result{
Expand Down
1 change: 1 addition & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ class EmbedderSnapshotData {
// If the snapshot is invalid, this returns an empty pointer.
static Pointer FromFile(FILE* in);
static Pointer FromBlob(const std::vector<char>& in);
static Pointer FromBlob(std::string_view in);

// Write this EmbedderSnapshotData object to an output file.
// Calling this method will not close the FILE* handle.
Expand Down

0 comments on commit e409581

Please sign in to comment.