v8/test/mjsunit/wasm/compiled-module-management.js
Ben L. Titzer a6d974fe00 [wasm] Merge the WasmContext into WasmInstanceObject
This change makes lifetime management of WasmCode much simpler.
By using the WasmInstanceObject as the context for WASM code execution,
including the pointer to the memory base and indirect function tables,
this keeps the instance alive when WASM code is on the stack, since
the instance object is passed as a parameter and spilled onto the stack.
This is in preparation of sharing the code between instances and
isolates.

Bug: v8:7424

R=mstarzinger@chromium.org

Change-Id: Ia35a3ce91a8f6135767fa764e185cde8bbc889f4
Reviewed-on: https://chromium-review.googlesource.com/997932
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52436}
2018-04-06 12:13:26 +00:00

89 lines
2.5 KiB
JavaScript

// Copyright 2016 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: --expose-wasm --expose-gc --allow-natives-syntax
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
// Use global variables for all values where the test wants to maintain strict
// control over value lifetime. Using local variables would not give sufficient
// guarantees of the value lifetime.
var module;
var instance1;
var instance2;
var instance3;
var instance4;
(function CompiledModuleInstancesInitialize1to3() {
var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true);
builder.addImport("", "getValue", kSig_i_v);
builder.addFunction("f", kSig_i_v)
.addBody([
kExprCallFunction, 0
]).exportFunc();
module = new WebAssembly.Module(builder.toBuffer());
print("Initial module");
%ValidateWasmModuleState(module);
print("Initial instances=0");
%ValidateWasmInstancesChain(module, 0);
instance1 = new WebAssembly.Instance(module, {"": {getValue: () => 1}});
print("Initial instances=1");
%ValidateWasmInstancesChain(module, 1);
instance2 = new WebAssembly.Instance(module, {"": {getValue: () => 2}});
print("Initial instances=2");
%ValidateWasmInstancesChain(module, 2);
instance3 = new WebAssembly.Instance(module, {"": {getValue: () => 3}});
print("Initial instances=3");
%ValidateWasmInstancesChain(module, 3);
})();
(function CompiledModuleInstancesClear1() {
assertEquals(1, instance1.exports.f());
instance1 = null;
})();
gc();
print("After gc instances=2");
%ValidateWasmInstancesChain(module, 2);
(function CompiledModuleInstancesClear3() {
assertEquals(3, instance3.exports.f());
instance3 = null;
})();
gc();
print("After gc instances=1");
%ValidateWasmInstancesChain(module, 1);
(function CompiledModuleInstancesClear2() {
assertEquals(2, instance2.exports.f());
instance2 = null;
})();
// Note that two GC's are required because weak cells are not cleared
// in the same cycle that the instance finalizer is run.
gc();
gc();
print("After gc module state");
%ValidateWasmModuleState(module);
(function CompiledModuleInstancesInitialize4AndClearModule() {
instance4 = new WebAssembly.Instance(module, {"": {getValue: () => 4}});
assertEquals(4, instance4.exports.f());
module = null;
})();
gc();
// the first GC will clear the module, the second the instance.
gc();
%ValidateWasmOrphanedInstance(instance4);