From 1efe967bcb79838002857d2c440517fbd16e7c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marja=20H=C3=B6ltt=C3=A4?= Date: Wed, 24 Nov 2021 10:10:24 +0100 Subject: [PATCH] [web snapshot] Support web snapshot magic when the source string is two byte MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: v8:11525 Change-Id: I28548c4eddcc7764be950950e16ac30b12ac8cdd Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3297890 Reviewed-by: Camillo Bruni Reviewed-by: Leszek Swirski Commit-Queue: Marja Hölttä Cr-Commit-Position: refs/heads/main@{#78061} --- src/codegen/compiler.cc | 3 ++- src/web-snapshot/web-snapshot.cc | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/codegen/compiler.cc b/src/codegen/compiler.cc index 40f7639c3e..ecee7a59af 100644 --- a/src/codegen/compiler.cc +++ b/src/codegen/compiler.cc @@ -2804,7 +2804,8 @@ MaybeHandle GetSharedFunctionInfoForScriptImpl( if (V8_UNLIKELY( i::FLAG_experimental_web_snapshots && - (source->IsExternalOneByteString() || source->IsSeqOneByteString()) && + (source->IsExternalOneByteString() || source->IsSeqOneByteString() || + source->IsExternalTwoByteString() || source->IsSeqTwoByteString()) && source_length > 4)) { // Experimental: Treat the script as a web snapshot if it starts with the // magic byte sequence. TODO(v8:11525): Remove this once proper embedder diff --git a/src/web-snapshot/web-snapshot.cc b/src/web-snapshot/web-snapshot.cc index e4355036c5..cdc2c883b3 100644 --- a/src/web-snapshot/web-snapshot.cc +++ b/src/web-snapshot/web-snapshot.cc @@ -973,6 +973,39 @@ bool WebSnapshotDeserializer::UseWebSnapshot( deserializer_.reset( new ValueDeserializer(isolate_, data_copy.get(), length)); return Deserialize(); + } else if (source->IsExternalTwoByteString()) { + // TODO(v8:11525): Implement end-to-end snapshot processing which gets rid + // of the need to copy the data here. + const v8::String::ExternalStringResource* resource = + ExternalTwoByteString::cast(*source).resource(); + auto length = resource->length(); + std::unique_ptr data_copy(new uint8_t[length]); + { + DisallowGarbageCollection no_gc; + const uint16_t* data = resource->data(); + uint8_t* data_copy_ptr = data_copy.get(); + for (size_t i = 0; i < length; ++i) { + data_copy_ptr[i] = static_cast(data[i]); + } + } + deserializer_.reset( + new ValueDeserializer(isolate_, data_copy.get(), length)); + return Deserialize(); + } else if (source->IsSeqTwoByteString()) { + SeqTwoByteString source_as_seq = SeqTwoByteString::cast(*source); + auto length = source_as_seq.length(); + std::unique_ptr data_copy(new uint8_t[length]); + { + DisallowGarbageCollection no_gc; + uint16_t* data = source_as_seq.GetChars(no_gc); + uint8_t* data_copy_ptr = data_copy.get(); + for (int i = 0; i < length; ++i) { + data_copy_ptr[i] = static_cast(data[i]); + } + } + deserializer_.reset( + new ValueDeserializer(isolate_, data_copy.get(), length)); + return Deserialize(); } UNREACHABLE(); }