v8/test/mjsunit/shared-memory/shared-string-copy-on-share-large.js
pthier 0621c2d553 [string] Copy when sharing strings that are not in shared space
Usually sharable strings are automatically promoted to shared old space
and can be shared in-place.
There are currently two exceptions:
- When using a non-moving GC (e.g. minor MC)
- Strings in LO space
Account for these exceptions by copying strings to the respective shared
space when sharing them.

Bug: chromium:1400048
Change-Id: I20713b5f32f449c14febd848e289b5767530a257
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4110752
Reviewed-by: Dominik Inführ <dinfuehr@chromium.org>
Commit-Queue: Patrick Thier <pthier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84942}
2022-12-19 16:28:22 +00:00

54 lines
1.7 KiB
JavaScript

// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Flags: --harmony-struct --shared-string-table --allow-natives-syntax
// Flags: --expose-gc
if (this.Worker) {
(function TestSharedStringPostMessage() {
function workerCode() {
let Box = new SharedStructType(['payload']);
let b1 = new Box();
b1.payload = "started";
postMessage(b1);
onmessage = function(box) {
if (!%IsSharedString(box.payload)) {
throw new Error("str isn't shared");
}
let b2 = new Box();
b2.payload = box.payload;
postMessage(b2);
};
}
// Strings referenced in shared structs are serialized by sharing.
let worker = new Worker(workerCode, { type: 'function' });
let started = worker.getMessage();
assertTrue(%IsSharedString(started.payload));
assertEquals("started", started.payload);
let Box = new SharedStructType(['payload']);
let box_to_send = new Box();
// Create a string in large object space.
let payload = %FlattenString('a'.repeat(1024 * 256));
assertTrue(%InLargeObjectSpace(payload));
// Trigger a gc to move the object to old space.
gc();
assertFalse(%InYoungGeneration(payload));
box_to_send.payload = payload;
assertTrue(%IsSharedString(box_to_send.payload));
worker.postMessage(box_to_send);
let box_received = worker.getMessage();
assertTrue(%IsSharedString(box_received.payload));
assertFalse(box_to_send === box_received);
// Object.is and === won't check pointer equality of Strings.
assertTrue(%IsSameHeapObject(box_to_send.payload, box_received.payload));
worker.terminate();
})();
}