summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/compiled-module-management.js
blob: 19446403aba6fcc7d4ecd9623e93f49bce9ec62d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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 instances=0");
  assertEquals(0, %WasmGetNumberOfInstances(module));
  instance1 = new WebAssembly.Instance(module, {"": {getValue: () => 1}});

  print("Initial instances=1");
  assertEquals(1, %WasmGetNumberOfInstances(module));
  instance2 = new WebAssembly.Instance(module, {"": {getValue: () => 2}});

  print("Initial instances=2");
  assertEquals(2, %WasmGetNumberOfInstances(module));
  instance3 = new WebAssembly.Instance(module, {"": {getValue: () => 3}});

  print("Initial instances=3");
  assertEquals(3, %WasmGetNumberOfInstances(module));
})();

(function CompiledModuleInstancesClear1() {
  assertEquals(1, instance1.exports.f());
  instance1 = null;
})();

// Note that two GC's are required because weak slots clearing is deferred.
gc();
gc();
print("After gc instances=2");
assertEquals(2, %WasmGetNumberOfInstances(module));

(function CompiledModuleInstancesClear3() {
  assertEquals(3, instance3.exports.f());
  instance3 = null;
})();

// Note that two GC's are required because weak slots clearing is deferred.
gc();
gc();
print("After gc instances=1");
assertEquals(1, %WasmGetNumberOfInstances(module));

(function CompiledModuleInstancesClear2() {
  assertEquals(2, instance2.exports.f());
  instance2 = null;
})();

// Note that two GC's are required because weak slots clearing is deferred.
gc();
gc();
print("After gc instances=0");
assertEquals(0, %WasmGetNumberOfInstances(module));

(function CompiledModuleInstancesInitialize4AndClearModule() {
  instance4 = new WebAssembly.Instance(module, {"": {getValue: () => 4}});
  assertEquals(4, instance4.exports.f());
  module = null;
})();

// Note that two GC's are required because weak slots clearing is deferred.
gc();
gc();