From 069c540faa7ae84c35376868fd70235c6b03a51e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 24 Apr 2024 08:16:15 +0200 Subject: [PATCH] src: add `string_view` overload to snapshot FromBlob MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `const std::vector&` 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: https://github.com/nodejs/node/pull/52595 Reviewed-By: Yagiz Nizipli Reviewed-By: Joyee Cheung --- src/api/embed_helpers.cc | 5 +++++ src/node.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/api/embed_helpers.cc b/src/api/embed_helpers.cc index 6fac48d1b534d2..14d2a20c1c18d9 100644 --- a/src/api/embed_helpers.cc +++ b/src/api/embed_helpers.cc @@ -302,6 +302,11 @@ EmbedderSnapshotData::Pointer EmbedderSnapshotData::BuiltinSnapshotData() { EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromBlob( const std::vector& 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{ diff --git a/src/node.h b/src/node.h index b041a20318145b..58c021f67e92c3 100644 --- a/src/node.h +++ b/src/node.h @@ -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& 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.