Backed out 2 changesets (bug 1888310, bug 1884625) for causing failures on browser_ap...
[gecko.git] / js / src / tests / test262 / staging / explicit-resource-management / call-dispose-methods.js
blob55189d21504bf715ebdcf29f444b378c23640250
1 // |reftest| skip -- explicit-resource-management is not supported
2 // Copyright (C) 2024 the V8 project authors. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
5 /*---
6 description: Test if disposed methods are called in correct syntax.
7 includes: [compareArray.js]
8 features: [explicit-resource-management]
9 ---*/
11 // Block ----------------
12 let blockValues = [];
14 (function TestUsingInBlock() {
15   {
16     using x = {
17       value: 1,
18       [Symbol.dispose]() {
19         blockValues.push(42);
20       }
21     };
22     blockValues.push(43);
23   }
24 })();
25 assert.compareArray(blockValues, [43, 42]);
27 // CaseBlock --------------
28 let caseBlockValues = [];
30 (function TestUsingInCaseBlock() {
31   let label = 1;
32   switch (label) {
33     case 1:
34       using x = {
35         value: 1,
36         [Symbol.dispose]() {
37           caseBlockValues.push(42);
38         }
39       };
40   }
41   caseBlockValues.push(43);
42 })();
43 assert.compareArray(caseBlockValues, [42, 43]);
45 // ForStatement --------------
46 let forStatementValues = [];
48 (function TestUsingInForStatement() {
49   for (let i = 0; i < 3; i++) {
50     using x = {
51       value: i,
52       [Symbol.dispose]() {
53         forStatementValues.push(this.value);
54       }
55     };
56   }
57   forStatementValues.push(3);
58 })();
59 assert.compareArray(forStatementValues, [0, 1, 2, 3]);
61 // ForInStatement --------------
62 let forInStatementValues = [];
64 (function TestUsingInForInStatement() {
65   for (let i in [0, 1]) {
66     using x = {
67       value: i,
68       [Symbol.dispose]() {
69         forInStatementValues.push(this.value);
70       }
71     };
72   }
73   forInStatementValues.push('2');
74 })();
75 assert.compareArray(forInStatementValues, ['0', '1', '2']);
77 // ForOfStatement --------------
78 let forOfStatementValues = [];
80 (function TestUsingInForOfStatement() {
81   for (let i of [0, 1]) {
82     using x = {
83       value: i,
84       [Symbol.dispose]() {
85         forOfStatementValues.push(this.value);
86       }
87     };
88   }
89   forOfStatementValues.push(2);
90 })();
91 assert.compareArray(forOfStatementValues, [0, 1, 2]);
93 // FunctionBody --------------
94 let functionBodyValues = [];
96 (function TestUsingInFunctionBody() {
97   using x = {
98     value: 1,
99     [Symbol.dispose]() {
100       functionBodyValues.push(42);
101     }
102   };
103   using y = {
104     value: 2,
105     [Symbol.dispose]() {
106       functionBodyValues.push(43);
107     }
108   };
109 })();
110 assert.compareArray(functionBodyValues, [43, 42]);
112 // GeneratorBody --------------
113 let generatorBodyValues = [];
115 function* gen() {
116   using x = {
117     value: 1,
118     [Symbol.dispose]() {
119       generatorBodyValues.push(42);
120     }
121   };
122   yield x;
125 (function TestUsingInGeneratorBody() {
126   let iter = gen();
127   iter.next();
128   iter.next();
129   generatorBodyValues.push(43);
130 })();
131 assert.compareArray(generatorBodyValues, [42, 43]);
133 // ClassStaticBlockBody --------------
134 let classStaticBlockBodyValues = [];
136 class staticBlockClass {
137   static {
138     using x = {
139       value: 1,
140       [Symbol.dispose]() {
141         classStaticBlockBodyValues.push(42);
142       }
143     };
144   }
147 (function TestUsingInAsyncFunctionBody() {
148   let example = new staticBlockClass();
149 })();
150 assert.compareArray(classStaticBlockBodyValues, [42]);
152 // Derived constructor case
153 let derivedConstructorValues = [];
155 class baseClass {
156   constructor() {
157     derivedConstructorValues.push(43);
158   }
161 class subClass extends baseClass {
162   constructor() {
163     try {
164       using x = {
165         value: 1,
166         [Symbol.dispose]() {
167           derivedConstructorValues.push(42);
168         }
169       };
170     } catch (e) {
171       return;
172     } finally {
173       super();
174     }
175   }
178 (function TestUsingInDerivedConstructor() {
179   let example = new subClass();
180 })();
181 assert.compareArray(derivedConstructorValues, [42, 43]);
183 // Lack of dispose method
184 let values = [];
186 function TestUsingWithoutDisposeMethod() {
187   {
188     using x = {value: 1};
189     values.push(43);
190   }
192 assert.throws(TypeError, TestUsingWithoutDisposeMethod, 'No dispose method');
194 reportCompare(0, 0);