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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// META: global=window,dedicatedworker,jsshell,shadowrealm
// META: script=/wasm/jsapi/memory/assertions.js
test(() => {
const argument = { "initial": 0 };
const memory = new WebAssembly.Memory(argument);
assert_throws_js(TypeError, () => memory.grow());
}, "Missing arguments");
test(t => {
const thisValues = [
undefined,
null,
true,
"",
Symbol(),
1,
{},
WebAssembly.Memory,
WebAssembly.Memory.prototype,
];
const argument = {
valueOf: t.unreached_func("Should not touch the argument (valueOf)"),
toString: t.unreached_func("Should not touch the argument (toString)"),
};
const fn = WebAssembly.Memory.prototype.grow;
for (const thisValue of thisValues) {
assert_throws_js(TypeError, () => fn.call(thisValue, argument), `this=${format_value(thisValue)}`);
}
}, "Branding");
test(() => {
const argument = { "initial": 0 };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 0 }, "Buffer before growing");
const result = memory.grow(2);
assert_equals(result, 0);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "Old buffer after growing");
assert_ArrayBuffer(newMemory, { "size": 2 }, "New buffer after growing");
}, "Zero initial");
test(() => {
const argument = { "initial": { valueOf() { return 0 } } };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 0 }, "Buffer before growing");
const result = memory.grow({ valueOf() { return 2 } });
assert_equals(result, 0);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "Old buffer after growing");
assert_ArrayBuffer(newMemory, { "size": 2 }, "New buffer after growing");
}, "Zero initial with valueOf");
test(() => {
const argument = { "initial": 3 };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 3 }, "Buffer before growing");
const result = memory.grow(2);
assert_equals(result, 3);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "Old buffer after growing");
assert_ArrayBuffer(newMemory, { "size": 5 }, "New buffer after growing");
}, "Non-zero initial");
test(() => {
const argument = { "initial": 0, "maximum": 2 };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 0 }, "Buffer before growing");
const result = memory.grow(2);
assert_equals(result, 0);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "Old buffer after growing");
assert_ArrayBuffer(newMemory, { "size": 2 }, "New buffer after growing");
}, "Zero initial with respected maximum");
test(() => {
const argument = { "initial": 0, "maximum": 2 };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 0 }, "Buffer before growing");
const result = memory.grow(1);
assert_equals(result, 0);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "Old buffer after growing once");
assert_ArrayBuffer(newMemory, { "size": 1 }, "New buffer after growing once");
const result2 = memory.grow(1);
assert_equals(result2, 1);
const newestMemory = memory.buffer;
assert_not_equals(newMemory, newestMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "New buffer after growing twice");
assert_ArrayBuffer(newMemory, { "detached": true }, "New buffer after growing twice");
assert_ArrayBuffer(newestMemory, { "size": 2 }, "Newest buffer after growing twice");
}, "Zero initial with respected maximum grown twice");
test(() => {
const argument = { "initial": 1, "maximum": 2 };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 1 }, "Buffer before growing");
assert_throws_js(RangeError, () => memory.grow(2));
assert_equals(memory.buffer, oldMemory);
assert_ArrayBuffer(memory.buffer, { "size": 1 }, "Buffer before trying to grow");
}, "Zero initial growing too much");
const outOfRangeValues = [
undefined,
NaN,
Infinity,
-Infinity,
-1,
0x100000000,
0x1000000000,
"0x100000000",
{ valueOf() { return 0x100000000; } },
];
for (const value of outOfRangeValues) {
test(() => {
const argument = { "initial": 0 };
const memory = new WebAssembly.Memory(argument);
assert_throws_js(TypeError, () => memory.grow(value));
}, `Out-of-range argument: ${format_value(value)}`);
}
test(() => {
const argument = { "initial": 0 };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 0 }, "Buffer before growing");
const result = memory.grow(2, {});
assert_equals(result, 0);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "detached": true }, "Old buffer after growing");
assert_ArrayBuffer(newMemory, { "size": 2 }, "New buffer after growing");
}, "Stray argument");
test(() => {
const argument = { "initial": 1, "maximum": 2, "shared": true };
const memory = new WebAssembly.Memory(argument);
const oldMemory = memory.buffer;
assert_ArrayBuffer(oldMemory, { "size": 1, "shared": true }, "Buffer before growing");
const result = memory.grow(1);
assert_equals(result, 1);
const newMemory = memory.buffer;
assert_not_equals(oldMemory, newMemory);
assert_ArrayBuffer(oldMemory, { "size": 1, "shared": true }, "Old buffer after growing");
assert_ArrayBuffer(newMemory, { "size": 2, "shared": true }, "New buffer after growing");
// The old and new buffers must have the same value for the
// [[ArrayBufferData]] internal slot.
const oldArray = new Uint8Array(oldMemory);
const newArray = new Uint8Array(newMemory);
assert_equals(oldArray[0], 0, "old first element");
assert_equals(newArray[0], 0, "new first element");
oldArray[0] = 1;
assert_equals(oldArray[0], 1, "old first element");
assert_equals(newArray[0], 1, "new first element");
}, "Growing shared memory does not detach old buffer");
|