Merge pull request #4145 from tc39/chromium-export-99eec8f4a2

[explicit-resource-management] Add await using to the bytecode generator
This commit is contained in:
Rezvan Mahdavi Hezaveh 2024-08-28 09:58:46 -07:00 committed by GitHub
commit 6c1116d45f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,33 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in async function.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// FunctionBody --------------
asyncTest(async function() {
let functionBodyValues = [];
async function TestUsingInFunctionBody() {
await using x = {
value: 1,
[Symbol.asyncDispose]() {
functionBodyValues.push(42);
}
};
await using y = {
value: 2,
[Symbol.asyncDispose]() {
functionBodyValues.push(43);
}
};
}
functionBodyValues = [];
await TestUsingInFunctionBody();
assert.compareArray(functionBodyValues, [43, 42]);
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in async function.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// FunctionBody --------------
asyncTest(async function() {
let functionBodyValues = [];
async function TestUsingInFunctionBody() {
await using x = {
value: 1,
[Symbol.asyncDispose]() {
functionBodyValues.push(42);
}
};
await using y = {
value: 2,
[Symbol.asyncDispose]() {
functionBodyValues.push(43);
}
};
}
TestUsingInFunctionBody();
assert.compareArray(functionBodyValues, [43]);
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in a block.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// Block ----------------
asyncTest(async function() {
let blockValues = [];
{
await using x = {
value: 1,
[Symbol.asyncDispose]() {
blockValues.push(42);
}
};
await using y = {
value: 1,
[Symbol.asyncDispose]() {
blockValues.push(43);
}
};
blockValues.push(44);
}
assert.compareArray(blockValues, [44, 43, 42]);
});

View File

@ -0,0 +1,26 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in for-in statement.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// ForInStatement --------------
asyncTest(async function() {
let forInStatementValues = [];
for (let i in [0, 1]) {
await using x = {
value: i,
[Symbol.asyncDispose]() {
forInStatementValues.push(this.value);
}
};
}
forInStatementValues.push('2');
assert.compareArray(forInStatementValues, ['0', '1', '2']);
});

View File

@ -0,0 +1,27 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in for-of statement.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// ForOfStatement --------------
asyncTest(async function() {
let forOfStatementValues = [];
for (let i of [0, 1]) {
await using x = {
value: i,
[Symbol.asyncDispose]() {
forOfStatementValues.push(this.value);
}
};
}
forOfStatementValues.push(2);
assert.compareArray(forOfStatementValues, [0, 1, 2]);
});

View File

@ -0,0 +1,26 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in for statements.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// ForStatement --------------
asyncTest(async function() {
let forStatementValues = [];
for (let i = 0; i < 3; i++) {
await using x = {
value: i,
[Symbol.asyncDispose]() {
forStatementValues.push(this.value);
}
};
}
forStatementValues.push(3);
assert.compareArray(forStatementValues, [0, 1, 2, 3]);
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly in switch cases.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// CaseBlock --------------
asyncTest(async function() {
let caseBlockValues = [];
let label = 1;
switch (label) {
case 1:
await using x = {
value: 1,
[Symbol.asyncDispose]() {
caseBlockValues.push(42);
}
};
}
caseBlockValues.push(43);
assert.compareArray(caseBlockValues, [42, 43]);
});

View File

@ -0,0 +1,23 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if exception is throwon when dispose method is missing.
includes: [asyncHelpers.js]
flags: [async]
features: [explicit-resource-management]
---*/
// Lack of dispose method --------
asyncTest(async function() {
let values = [];
async function TestUsingWithoutDisposeMethod() {
{
await using x = {value: 1};
values.push(43);
}
};
await assert.throwsAsync(
TypeError, () => TestUsingWithoutDisposeMethod(), 'No dispose method');
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if Symbol.dispose is called correctly.
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// sync dispose method ----------------
asyncTest(async function() {
let syncMethodValues = [];
{
await using x = {
value: 1,
[Symbol.dispose]() {
syncMethodValues.push(42);
}
};
await using y = {
value: 1,
[Symbol.dispose]() {
syncMethodValues.push(43);
}
};
syncMethodValues.push(44);
}
assert.compareArray(syncMethodValues, [44, 43, 42]);
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Test if disposed methods are called correctly with mixed resources
includes: [asyncHelpers.js, compareArray.js]
flags: [async]
features: [explicit-resource-management]
---*/
// Mix of sync and async resources ----------
asyncTest(async function() {
let mixValues = [];
{
await using x = {
value: 1,
[Symbol.asyncDispose]() {
mixValues.push(42);
}
};
using y = {
value: 1,
[Symbol.dispose]() {
mixValues.push(43);
}
};
mixValues.push(44);
}
assert.compareArray(mixValues, [44, 43, 42]);
});