Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[explicit-resource-management] Add await using to the bytecode generator #4145

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
});
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]);
});
Loading