Commit 4181e3c9 authored by Marja Hölttä's avatar Marja Hölttä Committed by V8 LUCI CQ

[web snapshots] Add two-byte string support

Bug: v8:11525
Change-Id: I7dee1987160d3811054a7be43280993630bca5be
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3320426Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78295}
parent a45e354a
...@@ -398,8 +398,13 @@ void WebSnapshotSerializer::SerializeString(Handle<String> string, ...@@ -398,8 +398,13 @@ void WebSnapshotSerializer::SerializeString(Handle<String> string,
string_serializer_.WriteRawBytes(chars.begin(), string_serializer_.WriteRawBytes(chars.begin(),
chars.length() * sizeof(uint8_t)); chars.length() * sizeof(uint8_t));
} else if (flat.IsTwoByte()) { } else if (flat.IsTwoByte()) {
// TODO(v8:11525): Support two-byte strings. v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate_);
UNREACHABLE(); v8::Local<v8::String> api_string = Utils::ToLocal(string);
int length = api_string->Utf8Length(v8_isolate);
std::unique_ptr<char[]> buffer(new char[length]);
api_string->WriteUtf8(v8_isolate, buffer.get(), length);
string_serializer_.WriteUint32(length);
string_serializer_.WriteRawBytes(buffer.get(), length * sizeof(uint8_t));
} else { } else {
UNREACHABLE(); UNREACHABLE();
} }
...@@ -1127,8 +1132,7 @@ void WebSnapshotDeserializer::DeserializeStrings() { ...@@ -1127,8 +1132,7 @@ void WebSnapshotDeserializer::DeserializeStrings() {
STATIC_ASSERT(kMaxItemCount <= FixedArray::kMaxLength); STATIC_ASSERT(kMaxItemCount <= FixedArray::kMaxLength);
strings_ = isolate_->factory()->NewFixedArray(string_count_); strings_ = isolate_->factory()->NewFixedArray(string_count_);
for (uint32_t i = 0; i < string_count_; ++i) { for (uint32_t i = 0; i < string_count_; ++i) {
// TODO(v8:11525): Read strings as UTF-8. MaybeHandle<String> maybe_string = deserializer_->ReadUtf8String();
MaybeHandle<String> maybe_string = deserializer_->ReadOneByteString();
Handle<String> string; Handle<String> string;
if (!maybe_string.ToHandle(&string)) { if (!maybe_string.ToHandle(&string)) {
Throw("Malformed string"); Throw("Malformed string");
......
...@@ -99,6 +99,34 @@ function takeAndUseWebSnapshot(createObjects, exports) { ...@@ -99,6 +99,34 @@ function takeAndUseWebSnapshot(createObjects, exports) {
assertFalse(b); assertFalse(b);
})(); })();
(function TestStringWithNull() {
function createObjects() {
globalThis.s = 'l\0l';
}
const { s } = takeAndUseWebSnapshot(createObjects, ['s']);
assertEquals(108, s.charCodeAt(0));
assertEquals(0, s.charCodeAt(1));
assertEquals(108, s.charCodeAt(2));
})();
(function TestTwoByteString() {
function createObjects() {
globalThis.s = '\u{1F600}';
}
const { s } = takeAndUseWebSnapshot(createObjects, ['s']);
assertEquals('\u{1F600}', s);
})();
(function TestTwoByteStringWithNull() {
function createObjects() {
globalThis.s = 'l\0l\u{1F600}';
}
const { s } = takeAndUseWebSnapshot(createObjects, ['s']);
assertEquals(108, s.charCodeAt(0));
assertEquals(0, s.charCodeAt(1));
assertEquals(108, s.charCodeAt(2));
})();
(function TestFunction() { (function TestFunction() {
function createObjects() { function createObjects() {
globalThis.foo = { globalThis.foo = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment