v8/test/mjsunit/wasm/shared-memory-worker-simple-gc.js
Ulan Degenbaev 55c48820f8 [api] Add [Shared]ArrayBuffer::GetBackingStore()
This adds an additional V8 API to get the backing store of an array
buffer. Unlike the existing API, the backing store comes wrapped
in a std::shared_ptr, making lifetime management with the embedder
explicit. This obviates the need for the old GetContents() and
Externalize() APIs, which will be deprecated in a future CL.

Contributed by titzer@chromium.org


Bug: v8:9380
Change-Id: I8a87f5dc141dab684693fe536b636e33f6e45173
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1807354
Reviewed-by: Yang Guo <yangguo@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63883}
2019-09-19 10:03:02 +00:00

86 lines
2.1 KiB
JavaScript

// Copyright 2019 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: --experimental-wasm-threads --expose-gc
const kNumIterations = 10;
function NewWorker() {
let script =
`onmessage = (msg) => {
if (msg.memory) postMessage("ack");
if (msg.quit) postMessage("bye");
gc();
}`;
return new Worker(script, {type: 'string'});
}
function PingWorker(worker, memory) {
worker.postMessage({memory: memory});
assertEquals("ack", worker.getMessage());
worker.postMessage({quit: true});
assertEquals("bye", worker.getMessage());
}
function AllocMemory() {
let pages = 1, max = 1;
return new WebAssembly.Memory({initial : pages, maximum : max, shared : true});
}
function RunSingleWorkerSingleMemoryTest() {
print(arguments.callee.name);
let worker = NewWorker();
let first = AllocMemory();
for (let i = 0; i < kNumIterations; i++) {
print(`iteration ${i}`);
PingWorker(worker, first);
gc();
}
worker.terminate();
}
function RunSingleWorkerTwoMemoryTest() {
print(arguments.callee.name);
let worker = NewWorker();
let first = AllocMemory(), second = AllocMemory();
for (let i = 0; i < kNumIterations; i++) {
print(`iteration ${i}`);
PingWorker(worker, first);
PingWorker(worker, second);
gc();
}
worker.terminate();
}
function RunSingleWorkerMultipleMemoryTest() {
print(arguments.callee.name);
let worker = NewWorker();
let first = AllocMemory();
for (let i = 0; i < kNumIterations; i++) {
print(`iteration ${i}`);
PingWorker(worker, first);
PingWorker(worker, AllocMemory());
gc();
}
worker.terminate();
}
function RunMultipleWorkerMultipleMemoryTest() {
print(arguments.callee.name);
let first = AllocMemory();
for (let i = 0; i < kNumIterations; i++) {
print(`iteration ${i}`);
let worker = NewWorker();
PingWorker(worker, first);
PingWorker(worker, AllocMemory());
worker.terminate();
gc();
}
}
RunSingleWorkerSingleMemoryTest();
RunSingleWorkerTwoMemoryTest();
RunSingleWorkerMultipleMemoryTest();
RunMultipleWorkerMultipleMemoryTest();