-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[explicit-resource-management] Add await using to the bytecode generator
This CL adds subtypes (SyncJSDisposableStack and AsyncDisposableStack) to JSDisposableStack as well as `await using` to the bytecode generator. Currently async generators are broken and the fix is left as a TODO in this CL. Also, exception handling (promise rejections) will be completed in a follow up CL. Bug: 42203814 Change-Id: I303a380b57fb4ab4662e4f55fb4dc9b14d18cd2a Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5569647 Reviewed-by: Shu-yu Guo <syg@chromium.org> Commit-Queue: Rezvan Mahdavi Hezaveh <rezvan@chromium.org> Cr-Commit-Position: refs/heads/main@{#94944}
- Loading branch information
Showing
10 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
test/staging/explicit-resource-management/await-using-in-async-function-call-with-await.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
32 changes: 32 additions & 0 deletions
32
.../staging/explicit-resource-management/await-using-in-async-function-call-without-await.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
32 changes: 32 additions & 0 deletions
32
test/staging/explicit-resource-management/await-using-in-block.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
26 changes: 26 additions & 0 deletions
26
test/staging/explicit-resource-management/await-using-in-for-in-statement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']); | ||
}); |
27 changes: 27 additions & 0 deletions
27
test/staging/explicit-resource-management/await-using-in-for-of-statement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
26 changes: 26 additions & 0 deletions
26
test/staging/explicit-resource-management/await-using-in-for-statement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
28 changes: 28 additions & 0 deletions
28
test/staging/explicit-resource-management/await-using-in-switch-case-block.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
23 changes: 23 additions & 0 deletions
23
test/staging/explicit-resource-management/await-using-with-no-async-dispose-method.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); |
32 changes: 32 additions & 0 deletions
32
test/staging/explicit-resource-management/await-using-with-sync-dispose-method.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |
32 changes: 32 additions & 0 deletions
32
test/staging/explicit-resource-management/mixed-call-dispose-methods.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); |