1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

feat: explicit resource management in TypeScript (#20506)

This adds support for `using` and `await using` declarations in
TypeScript only. We need to wait for v8 to support it for this to work
in JS.
This commit is contained in:
David Sherret 2023-09-14 14:08:59 -04:00 committed by GitHub
parent e66d3c2c2e
commit 6dc8682b9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 3 deletions

4
Cargo.lock generated
View file

@ -958,9 +958,9 @@ dependencies = [
[[package]]
name = "deno_ast"
version = "0.29.1"
version = "0.29.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8bb902bcaa072210ca7b2f28c391f77bb16f6ef64664331c5d928d99943303c"
checksum = "577ec3850834c2578eb44afa9250f9a807f8497664e6e2aaae19cea0aac2fe3b"
dependencies = [
"anyhow",
"base64 0.13.1",

View file

@ -38,7 +38,7 @@ license = "MIT"
repository = "https://github.com/denoland/deno"
[workspace.dependencies]
deno_ast = { version = "0.29.1", features = ["transpiling"] }
deno_ast = { version = "0.29.3", features = ["transpiling"] }
deno_core = { version = "0.211.0" }

View file

@ -4701,3 +4701,8 @@ console.log(returnsHi());"#,
")
.assert_exit_code(1);
}
itest!(explicit_resource_management {
args: "run --quiet --check run/explicit_resource_management/main.ts",
output: "run/explicit_resource_management/main.out",
});

View file

@ -0,0 +1,5 @@
A
Disposed
B
Async disposed
C

View file

@ -0,0 +1,21 @@
class Resource {
[Symbol.dispose]() {
console.log("Disposed");
}
}
class AsyncResource {
async [Symbol.asyncDispose]() {
await new Promise((resolve) => setTimeout(resolve, 10));
console.log("Async disposed");
}
}
{
using resource = new Resource();
console.log("A");
}
{
await using resource = new AsyncResource();
console.log("B");
}
console.log("C");

View file

@ -71,6 +71,11 @@ import {
workerRuntimeGlobalProperties,
} from "ext:runtime/98_global_scope.js";
// deno-lint-ignore prefer-primordials
Symbol.dispose ??= Symbol("Symbol.dispose");
// deno-lint-ignore prefer-primordials
Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
let windowIsClosing = false;
let globalThis_;