metano-runtime 3.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ import type { InterfaceType } from "./reflection/runtime-type.ts";
2
+ /**
3
+ * Runs `dispose` on `resource`: the body of the `[Symbol.dispose]()` alias every generated
4
+ * `IDisposable` implementer declares (spec FR-111), which passes the method that implements
5
+ * `Dispose()` under its emitted name (`disposeResource(this, this.dispose)`).
6
+ *
7
+ * The binding is the polyfill anchor. Building it installs `Symbol.dispose` first when the
8
+ * engine has none (stable Safari), as `Symbol.for("Symbol.dispose")`, the key TypeScript's
9
+ * downlevel `using` helpers then read. The ordering rests on the ESM graph: a generated
10
+ * module imports this one, and this module never imports generated code, so even in an
11
+ * import cycle between generated modules it is evaluated before any class whose
12
+ * `[Symbol.dispose]` key it defines; a bundle that keeps an implementer keeps this
13
+ * initializer (the alias reads it), and one that keeps none keeps nothing (spec 011 FR-043).
14
+ */
15
+ export declare const disposeResource: <T>(resource: T, dispose: (this: T) => void) => void;
16
+ /**
17
+ * C# `System.IDisposable` (spec FR-111): `Dispose()` lowers to `dispose()`, and a C# `using`
18
+ * lowers to a TypeScript `using` declaration, which calls `[Symbol.dispose]()`. Every
19
+ * generated implementer declares that member as an alias of `dispose()`, so hand-written
20
+ * TypeScript can hold an `IDisposable` in a `using` declaration too.
21
+ */
22
+ export interface IDisposable {
23
+ dispose(): void;
24
+ [Symbol.dispose](): void;
25
+ }
26
+ /**
27
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
28
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
29
+ * `dispose` method.
30
+ */
31
+ export declare const IDisposable$runtimeType: InterfaceType<IDisposable>;
@@ -0,0 +1,43 @@
1
+ import { implementsInterface } from "./reflection/implements-interface.js";
2
+ /**
3
+ * Runs `dispose` on `resource`: the body of the `[Symbol.dispose]()` alias every generated
4
+ * `IDisposable` implementer declares (spec FR-111), which passes the method that implements
5
+ * `Dispose()` under its emitted name (`disposeResource(this, this.dispose)`).
6
+ *
7
+ * The binding is the polyfill anchor. Building it installs `Symbol.dispose` first when the
8
+ * engine has none (stable Safari), as `Symbol.for("Symbol.dispose")`, the key TypeScript's
9
+ * downlevel `using` helpers then read. The ordering rests on the ESM graph: a generated
10
+ * module imports this one, and this module never imports generated code, so even in an
11
+ * import cycle between generated modules it is evaluated before any class whose
12
+ * `[Symbol.dispose]` key it defines; a bundle that keeps an implementer keeps this
13
+ * initializer (the alias reads it), and one that keeps none keeps nothing (spec 011 FR-043).
14
+ */
15
+ export const disposeResource =
16
+ // Pure for bundlers: kept, and run at load, only while a generated alias reads it.
17
+ /* @__PURE__ */ createResourceDisposer();
18
+ function createResourceDisposer() {
19
+ installSymbolDispose();
20
+ return disposeThrough;
21
+ }
22
+ /** Defines `Symbol.dispose` as `Symbol.for("Symbol.dispose")` on an engine without it. */
23
+ function installSymbolDispose() {
24
+ const symbols = Symbol;
25
+ symbols.dispose ??= Symbol.for("Symbol.dispose");
26
+ }
27
+ function disposeThrough(resource, dispose) {
28
+ dispose.call(resource);
29
+ }
30
+ /**
31
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
32
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
33
+ * `dispose` method.
34
+ */
35
+ export const IDisposable$runtimeType = {
36
+ kind: "interface",
37
+ name: "IDisposable",
38
+ fullName: "System.IDisposable",
39
+ namespace: "System",
40
+ baseInterfaces: [],
41
+ plainImplementers: [],
42
+ test: (value) => implementsInterface(value, IDisposable$runtimeType),
43
+ };
@@ -13,8 +13,12 @@ import { Exception } from "./exception.ts";
13
13
  * A wrapper keeps the original's `message` and `stack`, and holds the original
14
14
  * as its `innerException` (`cause`). A `TypeError` thrown on purpose by
15
15
  * third-party JavaScript is also reported as a `NullReferenceException`.
16
+ *
17
+ * A `SuppressedError` (what a `using` declaration throws when its body and its
18
+ * disposal both fail) is unwrapped to its `error`, the disposal's exception,
19
+ * which is the one a C# `finally` lets through (spec FR-111).
16
20
  */
17
- export declare function toException(value: unknown): Exception;
21
+ export declare function toException(thrown: unknown): Exception;
18
22
  /**
19
23
  * Runs a `when` filter of a `catch` clause. A filter that throws counts as
20
24
  * `false`, as in C#: the exception is dropped and the next clause is tried.
@@ -6,6 +6,10 @@ import { TaskCanceledException } from "./task-canceled-exception.js";
6
6
  * rejects with.
7
7
  */
8
8
  const ABORT_ERROR_NAME = "AbortError";
9
+ /** The `name` of the error a `using` declaration throws when its disposal hides another error. */
10
+ const SUPPRESSED_ERROR_NAME = "SuppressedError";
11
+ /** How many nested `SuppressedError`s `toException` unwraps at most. */
12
+ const MAX_SUPPRESSION_DEPTH = 64;
9
13
  /**
10
14
  * Normalizes whatever a JavaScript `catch` caught to a `System.Exception`, as
11
15
  * the typed clauses of a C# `try` see it:
@@ -20,8 +24,13 @@ const ABORT_ERROR_NAME = "AbortError";
20
24
  * A wrapper keeps the original's `message` and `stack`, and holds the original
21
25
  * as its `innerException` (`cause`). A `TypeError` thrown on purpose by
22
26
  * third-party JavaScript is also reported as a `NullReferenceException`.
27
+ *
28
+ * A `SuppressedError` (what a `using` declaration throws when its body and its
29
+ * disposal both fail) is unwrapped to its `error`, the disposal's exception,
30
+ * which is the one a C# `finally` lets through (spec FR-111).
23
31
  */
24
- export function toException(value) {
32
+ export function toException(thrown) {
33
+ const value = withoutSuppression(thrown);
25
34
  if (value instanceof Exception) {
26
35
  return value;
27
36
  }
@@ -34,6 +43,25 @@ export function toException(value) {
34
43
  }
35
44
  return wrapper;
36
45
  }
46
+ /**
47
+ * The exception a chain of `SuppressedError`s ends with, following `error`. A chain longer
48
+ * than {@link MAX_SUPPRESSION_DEPTH} (a cyclic `error`) stops at the error it reached.
49
+ */
50
+ function withoutSuppression(value, depth = 0) {
51
+ return isSuppressedError(value) && depth < MAX_SUPPRESSION_DEPTH
52
+ ? withoutSuppression(value.error, depth + 1)
53
+ : value;
54
+ }
55
+ /**
56
+ * Whether `value` is a `SuppressedError`: the native class, or the stand-in TypeScript's
57
+ * downlevel `using` helpers build on an engine without one, which only carries the name.
58
+ */
59
+ function isSuppressedError(value) {
60
+ return (value instanceof Error &&
61
+ !(value instanceof Exception) &&
62
+ value.name === SUPPRESSED_ERROR_NAME &&
63
+ "error" in value);
64
+ }
37
65
  function wrap(error) {
38
66
  if (error instanceof TypeError) {
39
67
  return new NullReferenceException(error.message, error);
@@ -41,3 +41,4 @@ export * from "./char-members.ts";
41
41
  export * from "./string-builder.ts";
42
42
  export * from "./lazy.ts";
43
43
  export * from "./task-completion-source.ts";
44
+ export * from "./disposable.ts";
@@ -41,3 +41,4 @@ export * from "./char-members.js";
41
41
  export * from "./string-builder.js";
42
42
  export * from "./lazy.js";
43
43
  export * from "./task-completion-source.js";
44
+ export * from "./disposable.js";
@@ -0,0 +1,31 @@
1
+ import type { InterfaceType } from "./reflection/runtime-type.ts";
2
+ /**
3
+ * Runs `dispose` on `resource`: the body of the `[Symbol.dispose]()` alias every generated
4
+ * `IDisposable` implementer declares (spec FR-111), which passes the method that implements
5
+ * `Dispose()` under its emitted name (`disposeResource(this, this.dispose)`).
6
+ *
7
+ * The binding is the polyfill anchor. Building it installs `Symbol.dispose` first when the
8
+ * engine has none (stable Safari), as `Symbol.for("Symbol.dispose")`, the key TypeScript's
9
+ * downlevel `using` helpers then read. The ordering rests on the ESM graph: a generated
10
+ * module imports this one, and this module never imports generated code, so even in an
11
+ * import cycle between generated modules it is evaluated before any class whose
12
+ * `[Symbol.dispose]` key it defines; a bundle that keeps an implementer keeps this
13
+ * initializer (the alias reads it), and one that keeps none keeps nothing (spec 011 FR-043).
14
+ */
15
+ export declare const disposeResource: <T>(resource: T, dispose: (this: T) => void) => void;
16
+ /**
17
+ * C# `System.IDisposable` (spec FR-111): `Dispose()` lowers to `dispose()`, and a C# `using`
18
+ * lowers to a TypeScript `using` declaration, which calls `[Symbol.dispose]()`. Every
19
+ * generated implementer declares that member as an alias of `dispose()`, so hand-written
20
+ * TypeScript can hold an `IDisposable` in a `using` declaration too.
21
+ */
22
+ export interface IDisposable {
23
+ dispose(): void;
24
+ [Symbol.dispose](): void;
25
+ }
26
+ /**
27
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
28
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
29
+ * `dispose` method.
30
+ */
31
+ export declare const IDisposable$runtimeType: InterfaceType<IDisposable>;
@@ -0,0 +1,43 @@
1
+ import { implementsInterface } from "./reflection/implements-interface.js";
2
+ /**
3
+ * Runs `dispose` on `resource`: the body of the `[Symbol.dispose]()` alias every generated
4
+ * `IDisposable` implementer declares (spec FR-111), which passes the method that implements
5
+ * `Dispose()` under its emitted name (`disposeResource(this, this.dispose)`).
6
+ *
7
+ * The binding is the polyfill anchor. Building it installs `Symbol.dispose` first when the
8
+ * engine has none (stable Safari), as `Symbol.for("Symbol.dispose")`, the key TypeScript's
9
+ * downlevel `using` helpers then read. The ordering rests on the ESM graph: a generated
10
+ * module imports this one, and this module never imports generated code, so even in an
11
+ * import cycle between generated modules it is evaluated before any class whose
12
+ * `[Symbol.dispose]` key it defines; a bundle that keeps an implementer keeps this
13
+ * initializer (the alias reads it), and one that keeps none keeps nothing (spec 011 FR-043).
14
+ */
15
+ export const disposeResource =
16
+ // Pure for bundlers: kept, and run at load, only while a generated alias reads it.
17
+ /* @__PURE__ */ createResourceDisposer();
18
+ function createResourceDisposer() {
19
+ installSymbolDispose();
20
+ return disposeThrough;
21
+ }
22
+ /** Defines `Symbol.dispose` as `Symbol.for("Symbol.dispose")` on an engine without it. */
23
+ function installSymbolDispose() {
24
+ const symbols = Symbol;
25
+ symbols.dispose ??= Symbol.for("Symbol.dispose");
26
+ }
27
+ function disposeThrough(resource, dispose) {
28
+ dispose.call(resource);
29
+ }
30
+ /**
31
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
32
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
33
+ * `dispose` method.
34
+ */
35
+ export const IDisposable$runtimeType = {
36
+ kind: "interface",
37
+ name: "IDisposable",
38
+ fullName: "System.IDisposable",
39
+ namespace: "System",
40
+ baseInterfaces: [],
41
+ plainImplementers: [],
42
+ test: (value) => implementsInterface(value, IDisposable$runtimeType),
43
+ };
@@ -13,8 +13,12 @@ import { Exception } from "./exception.ts";
13
13
  * A wrapper keeps the original's `message` and `stack`, and holds the original
14
14
  * as its `innerException` (`cause`). A `TypeError` thrown on purpose by
15
15
  * third-party JavaScript is also reported as a `NullReferenceException`.
16
+ *
17
+ * A `SuppressedError` (what a `using` declaration throws when its body and its
18
+ * disposal both fail) is unwrapped to its `error`, the disposal's exception,
19
+ * which is the one a C# `finally` lets through (spec FR-111).
16
20
  */
17
- export declare function toException(value: unknown): Exception;
21
+ export declare function toException(thrown: unknown): Exception;
18
22
  /**
19
23
  * Runs a `when` filter of a `catch` clause. A filter that throws counts as
20
24
  * `false`, as in C#: the exception is dropped and the next clause is tried.
@@ -6,6 +6,10 @@ import { TaskCanceledException } from "./task-canceled-exception.js";
6
6
  * rejects with.
7
7
  */
8
8
  const ABORT_ERROR_NAME = "AbortError";
9
+ /** The `name` of the error a `using` declaration throws when its disposal hides another error. */
10
+ const SUPPRESSED_ERROR_NAME = "SuppressedError";
11
+ /** How many nested `SuppressedError`s `toException` unwraps at most. */
12
+ const MAX_SUPPRESSION_DEPTH = 64;
9
13
  /**
10
14
  * Normalizes whatever a JavaScript `catch` caught to a `System.Exception`, as
11
15
  * the typed clauses of a C# `try` see it:
@@ -20,8 +24,13 @@ const ABORT_ERROR_NAME = "AbortError";
20
24
  * A wrapper keeps the original's `message` and `stack`, and holds the original
21
25
  * as its `innerException` (`cause`). A `TypeError` thrown on purpose by
22
26
  * third-party JavaScript is also reported as a `NullReferenceException`.
27
+ *
28
+ * A `SuppressedError` (what a `using` declaration throws when its body and its
29
+ * disposal both fail) is unwrapped to its `error`, the disposal's exception,
30
+ * which is the one a C# `finally` lets through (spec FR-111).
23
31
  */
24
- export function toException(value) {
32
+ export function toException(thrown) {
33
+ const value = withoutSuppression(thrown);
25
34
  if (value instanceof Exception) {
26
35
  return value;
27
36
  }
@@ -34,6 +43,25 @@ export function toException(value) {
34
43
  }
35
44
  return wrapper;
36
45
  }
46
+ /**
47
+ * The exception a chain of `SuppressedError`s ends with, following `error`. A chain longer
48
+ * than {@link MAX_SUPPRESSION_DEPTH} (a cyclic `error`) stops at the error it reached.
49
+ */
50
+ function withoutSuppression(value, depth = 0) {
51
+ return isSuppressedError(value) && depth < MAX_SUPPRESSION_DEPTH
52
+ ? withoutSuppression(value.error, depth + 1)
53
+ : value;
54
+ }
55
+ /**
56
+ * Whether `value` is a `SuppressedError`: the native class, or the stand-in TypeScript's
57
+ * downlevel `using` helpers build on an engine without one, which only carries the name.
58
+ */
59
+ function isSuppressedError(value) {
60
+ return (value instanceof Error &&
61
+ !(value instanceof Exception) &&
62
+ value.name === SUPPRESSED_ERROR_NAME &&
63
+ "error" in value);
64
+ }
37
65
  function wrap(error) {
38
66
  if (error instanceof TypeError) {
39
67
  return new NullReferenceException(error.message, error);
@@ -41,3 +41,4 @@ export * from "./char-members.ts";
41
41
  export * from "./string-builder.ts";
42
42
  export * from "./lazy.ts";
43
43
  export * from "./task-completion-source.ts";
44
+ export * from "./disposable.ts";
@@ -41,3 +41,4 @@ export * from "./char-members.js";
41
41
  export * from "./string-builder.js";
42
42
  export * from "./lazy.js";
43
43
  export * from "./task-completion-source.js";
44
+ export * from "./disposable.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metano-runtime",
3
- "version": "3.2.0",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,63 @@
1
+ import { implementsInterface } from "./reflection/implements-interface.ts";
2
+ import type { InterfaceType } from "./reflection/runtime-type.ts";
3
+
4
+ /**
5
+ * Runs `dispose` on `resource`: the body of the `[Symbol.dispose]()` alias every generated
6
+ * `IDisposable` implementer declares (spec FR-111), which passes the method that implements
7
+ * `Dispose()` under its emitted name (`disposeResource(this, this.dispose)`).
8
+ *
9
+ * The binding is the polyfill anchor. Building it installs `Symbol.dispose` first when the
10
+ * engine has none (stable Safari), as `Symbol.for("Symbol.dispose")`, the key TypeScript's
11
+ * downlevel `using` helpers then read. The ordering rests on the ESM graph: a generated
12
+ * module imports this one, and this module never imports generated code, so even in an
13
+ * import cycle between generated modules it is evaluated before any class whose
14
+ * `[Symbol.dispose]` key it defines; a bundle that keeps an implementer keeps this
15
+ * initializer (the alias reads it), and one that keeps none keeps nothing (spec 011 FR-043).
16
+ */
17
+ export const disposeResource: <T>(resource: T, dispose: (this: T) => void) => void =
18
+ // Pure for bundlers: kept, and run at load, only while a generated alias reads it.
19
+ /* @__PURE__ */ createResourceDisposer();
20
+
21
+ function createResourceDisposer(): <T>(resource: T, dispose: (this: T) => void) => void {
22
+ installSymbolDispose();
23
+
24
+ return disposeThrough;
25
+ }
26
+
27
+ /** Defines `Symbol.dispose` as `Symbol.for("Symbol.dispose")` on an engine without it. */
28
+ function installSymbolDispose(): void {
29
+ const symbols = Symbol as { dispose?: typeof Symbol.dispose };
30
+
31
+ symbols.dispose ??= Symbol.for("Symbol.dispose") as typeof Symbol.dispose;
32
+ }
33
+
34
+ function disposeThrough<T>(resource: T, dispose: (this: T) => void): void {
35
+ dispose.call(resource);
36
+ }
37
+
38
+ /**
39
+ * C# `System.IDisposable` (spec FR-111): `Dispose()` lowers to `dispose()`, and a C# `using`
40
+ * lowers to a TypeScript `using` declaration, which calls `[Symbol.dispose]()`. Every
41
+ * generated implementer declares that member as an alias of `dispose()`, so hand-written
42
+ * TypeScript can hold an `IDisposable` in a `using` declaration too.
43
+ */
44
+ export interface IDisposable {
45
+ dispose(): void;
46
+ [Symbol.dispose](): void;
47
+ }
48
+
49
+ /**
50
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
51
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
52
+ * `dispose` method.
53
+ */
54
+ export const IDisposable$runtimeType: InterfaceType<IDisposable> = {
55
+ kind: "interface",
56
+ name: "IDisposable",
57
+ fullName: "System.IDisposable",
58
+ namespace: "System",
59
+ baseInterfaces: [],
60
+ plainImplementers: [],
61
+ test: (value: unknown): value is IDisposable =>
62
+ implementsInterface(value, IDisposable$runtimeType),
63
+ };
@@ -8,6 +8,12 @@ import { TaskCanceledException } from "./task-canceled-exception.ts";
8
8
  */
9
9
  const ABORT_ERROR_NAME = "AbortError";
10
10
 
11
+ /** The `name` of the error a `using` declaration throws when its disposal hides another error. */
12
+ const SUPPRESSED_ERROR_NAME = "SuppressedError";
13
+
14
+ /** How many nested `SuppressedError`s `toException` unwraps at most. */
15
+ const MAX_SUPPRESSION_DEPTH = 64;
16
+
11
17
  /**
12
18
  * Normalizes whatever a JavaScript `catch` caught to a `System.Exception`, as
13
19
  * the typed clauses of a C# `try` see it:
@@ -22,8 +28,14 @@ const ABORT_ERROR_NAME = "AbortError";
22
28
  * A wrapper keeps the original's `message` and `stack`, and holds the original
23
29
  * as its `innerException` (`cause`). A `TypeError` thrown on purpose by
24
30
  * third-party JavaScript is also reported as a `NullReferenceException`.
31
+ *
32
+ * A `SuppressedError` (what a `using` declaration throws when its body and its
33
+ * disposal both fail) is unwrapped to its `error`, the disposal's exception,
34
+ * which is the one a C# `finally` lets through (spec FR-111).
25
35
  */
26
- export function toException(value: unknown): Exception {
36
+ export function toException(thrown: unknown): Exception {
37
+ const value = withoutSuppression(thrown);
38
+
27
39
  if (value instanceof Exception) {
28
40
  return value;
29
41
  }
@@ -41,6 +53,29 @@ export function toException(value: unknown): Exception {
41
53
  return wrapper;
42
54
  }
43
55
 
56
+ /**
57
+ * The exception a chain of `SuppressedError`s ends with, following `error`. A chain longer
58
+ * than {@link MAX_SUPPRESSION_DEPTH} (a cyclic `error`) stops at the error it reached.
59
+ */
60
+ function withoutSuppression(value: unknown, depth = 0): unknown {
61
+ return isSuppressedError(value) && depth < MAX_SUPPRESSION_DEPTH
62
+ ? withoutSuppression(value.error, depth + 1)
63
+ : value;
64
+ }
65
+
66
+ /**
67
+ * Whether `value` is a `SuppressedError`: the native class, or the stand-in TypeScript's
68
+ * downlevel `using` helpers build on an engine without one, which only carries the name.
69
+ */
70
+ function isSuppressedError(value: unknown): value is Error & { error: unknown } {
71
+ return (
72
+ value instanceof Error &&
73
+ !(value instanceof Exception) &&
74
+ value.name === SUPPRESSED_ERROR_NAME &&
75
+ "error" in value
76
+ );
77
+ }
78
+
44
79
  function wrap(error: Error): Exception {
45
80
  if (error instanceof TypeError) {
46
81
  return new NullReferenceException(error.message, error);
@@ -41,3 +41,4 @@ export * from "./char-members.ts";
41
41
  export * from "./string-builder.ts";
42
42
  export * from "./lazy.ts";
43
43
  export * from "./task-completion-source.ts";
44
+ export * from "./disposable.ts";