v8/test/mjsunit/regress/wasm/regress-1067621.js
Camillo Bruni a345a442d3 [d8][mjsunit][tools] Improve d8 file API
- Add d8.file.read() and d8.file.execute() helpers
- Change tools and tests to use new d8.file helper
- Unify error throwing in v8::Shell::ReadFile

Change-Id: I5ef4cb27f217508a367106f01e872a4059d5e399
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2928505
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74883}
2021-06-01 13:37:57 +00:00

83 lines
2.3 KiB
JavaScript

// Copyright 2020 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
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
const kNumberOfWorker = 4;
const workerOnMessage = function(msg) {
if (msg.module) {
let module = msg.module;
let mem = msg.mem;
this.instance = new WebAssembly.Instance(module, {m: {memory: mem}});
postMessage({instantiated: true});
} else {
const kNumberOfRuns = 20;
let result = new Array(kNumberOfRuns);
for (let i = 0; i < kNumberOfRuns; ++i) {
result[i] = instance.exports.grow();
}
postMessage({result: result});
}
};
function spawnWorkers() {
let workers = [];
for (let i = 0; i < kNumberOfWorker; i++) {
let worker = new Worker(
'onmessage = ' + workerOnMessage.toString(), {type: 'string'});
workers.push(worker);
}
return workers;
}
function instantiateModuleInWorkers(workers, module, shared_memory) {
for (let worker of workers) {
worker.postMessage({module: module, mem: shared_memory});
let msg = worker.getMessage();
if (!msg.instantiated) throw 'Worker failed to instantiate';
}
}
function triggerWorkers(workers) {
for (i = 0; i < workers.length; i++) {
let worker = workers[i];
worker.postMessage({});
}
}
(function TestConcurrentGrowMemoryResult() {
let builder = new WasmModuleBuilder();
builder.addImportedMemory('m', 'memory', 1, 500, 'shared');
builder.addFunction('grow', kSig_i_v)
.addBody([kExprI32Const, 1, kExprMemoryGrow, kMemoryZero])
.exportFunc();
const module = builder.toModule();
const shared_memory =
new WebAssembly.Memory({initial: 1, maximum: 500, shared: true});
// Spawn off the workers and run the sequences.
let workers = spawnWorkers();
instantiateModuleInWorkers(workers, module, shared_memory);
triggerWorkers(workers);
let all_results = [];
for (let worker of workers) {
let msg = worker.getMessage();
all_results = all_results.concat(msg.result);
}
all_results.sort((a, b) => a - b);
for (let i = 1; i < all_results.length; ++i) {
assertEquals(all_results[i - 1] + 1, all_results[i]);
}
// Terminate all workers.
for (let worker of workers) {
worker.terminate();
}
})();