Skip to content

Commit

Permalink
[explicit-resource-management] Add await using to the bytecode generator
Browse files Browse the repository at this point in the history
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
rmahdav authored and v8-test262-autoroller@chromium.org committed Jul 10, 2024
1 parent 16322d3 commit 97e0bef
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 0 deletions.
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]);
});
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 test/staging/explicit-resource-management/await-using-in-block.js
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]);
});
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']);
});
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]);
});
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]);
});
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]);
});
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');
});
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]);
});
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]);
});

0 comments on commit 97e0bef

Please sign in to comment.