v8/test/mjsunit/shared-memory/shared-string.js
Shu-yu Guo 87ce9fce74 [shared-struct] Rework shared value serializer API again
This CL fixes redesigns the current API, which does not correctly
manage lifetimes of the shared object conveyors.

See design doc at
https://docs.google.com/document/d/1TV6agY9dafVJFvdPrUAGbEvos8wL2WDnsmf84n3OJVU/edit?usp=sharing

This CL also removes the incorrect behavior of serializing all shared
strings by sharing instead of copying. Shared strings may be sent to
another process, which should still work.

Bug: v8:12547
Change-Id: I7413abd2d871fd3d52c9b433445cfa1d03e4a732
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3868713
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Adam Klein <adamk@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83044}
2022-09-07 23:41:26 +00:00

48 lines
1.5 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
if (this.Worker) {
(function TestSharedStringPostMessage() {
let workerScript =
`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(workerScript, { type: 'string' });
let started = worker.getMessage();
assertTrue(%IsSharedString(started.payload));
assertEquals("started", started.payload);
// The string literal appears in source and is internalized, so should
// already be shared.
let Box = new SharedStructType(['payload']);
let box_to_send = new Box();
box_to_send.payload = 'foo';
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();
})();
}