-
Notifications
You must be signed in to change notification settings - Fork 25
/
gc.test.mvm.js
86 lines (67 loc) · 1.91 KB
/
gc.test.mvm.js
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
/*---
runExportedFunction: 0
nativeOnly: true
testOnly: false
description: Some garbage collection tests
---*/
// WARNING: these tests don't work if MVM_VERY_EXPENSIVE_MEMORY_CHECKS is
// enabled because it triggers garbage collections at unexpected times.
function run() {
garbage = 0;
heap = getHeapUsed();
function1();
}
let garbage;
let globalVariable;
let heap;
function function1() {
// New array with global reference
globalVariable = [0];
checkAllocated(10, 0);
// New array with local reference
let localVariable1A = [1];
checkAllocated(10, 0);
// Resize array
localVariable1A[1] = 42; // See setProperty and growArray
checkAllocated(10, 4);
// New array
let localVariable1B = [2];
checkAllocated(10, 0);
// Make eligible for GC
localVariable1B = undefined;
checkAllocated(0, 10);
nestedFunction();
}
function nestedFunction() {
// New object. Note that new objects are manufactured empty to start with and
// then properties are added.
let localVariable2A = { x: 3 };
checkAllocated(16, 6);
// Extend object
localVariable2A.y = 4;
checkAllocated(10, 6);
// Extend object
localVariable2A.z = 5;
checkAllocated(10, 6);
// New object
let localVariable2B = { x: 6 };
checkAllocated(16, 6);
// Make eligible for GC
localVariable2B = 0;
checkAllocated(0, 10);
// TODO: This tests an explicit run of the GC, but it would also be good to
// test an implicit run because the top frame will be internal rather than
// the host
// checkGC();
}
function checkAllocated(newAllocatedSize, newGarbageSize) {
assertEqual(getHeapUsed() - heap, newAllocatedSize);
heap += newAllocatedSize;
garbage += newGarbageSize; // (will be checked at end)
}
function checkGC() {
checkAllocated(0, 0); // Just check consistency between `heap` and `getHeapUsed`
runGC();
checkAllocated(-garbage, -garbage); // Check that garbage is deallocated
}
vmExport(0, run);