Recursive AI

Resource management, iterator additions and Atomics.pause

All oneecma docs

These features come from proposals that reached stage 4 after the 17th edition of ECMA-262. The text of each is in the ES draft: explicit resource management (clauses 7.5, 14.3.1, 14.7.5, 27.4, 27.5, 20.5), iterator chunking and sequencing helpers (27.1.3), joint iteration (27.1.2.1 Iterator.zip, Iterator.zipKeyed) and Atomics.pause (25.4).

using and await using

  • using x = expr; and await using x = expr; declare a block-scoped constant (the binding behaves as const) and register the value for disposal when the scope exits. await using needs an async function, an async generator or a module body.
  • Allowed in blocks, function bodies, module bodies, for heads (for (using x = ...;;)), for-of and for await-of heads. Not allowed directly as the body of if, while, a case clause or a labelled statement, and not at a script's top level (a module's is fine). The parser reports these with V8's headline (Unexpected identifier 'x' or Unexpected token 'using') and a label and help line that explain what to change.
  • using stays an identifier where the grammar needs one: using[x] = 1, using in obj, using\nx (a line break after using ends the expression), for (using of of xs), for (using of = 0;;). An escaped spelling (us\u0069ng) is an identifier, as ECMA-262 5.1.5 requires; V8 accepts it as the keyword, a documented difference.
  • Resources are disposed in reverse order of declaration on every exit: normal completion, break, continue, return, throw, a generator's return() and a module's end. Null and undefined register nothing (for await using they still cost one await).
  • A value that is not an object, or an object without [Symbol.dispose]() (for await using: neither [Symbol.asyncDispose]() nor [Symbol.dispose]()), throws a TypeError when the declaration runs. A non-callable method is also a TypeError.
  • If disposal throws while an error is already propagating, the later error becomes the error of a SuppressedError whose suppressed is the earlier one. Several failures nest in the order they happened.

Objects

  • SuppressedError(error, suppressed, message): error and suppressed are own data properties; message is an own property only when given. When the engine makes one during disposal its message is V8's An error was suppressed during disposal.
  • DisposableStack and AsyncDisposableStack: use, adopt, defer, move, dispose / disposeAsync, disposed, [Symbol.dispose] / [Symbol.asyncDispose]. After disposal their methods throw ReferenceError: Cannot ... on an already-disposed ... in V8's wording (including V8's DisposableStack wording for an AsyncDisposableStack).
  • Symbol.dispose and Symbol.asyncDispose; Iterator.prototype[Symbol.dispose] calls return(); AsyncIterator.prototype[Symbol.asyncDispose] awaits it.

Iterator additions

  • Iterator.prototype.chunks(n) yields arrays of n items (the last may be shorter). windows(n, undersized) yields sliding arrays of n; undersized is "only-full" (default) or "allow-partial". n must be an integer from 1 to 4294967295 (RangeError otherwise, TypeError for a non-number). A bad argument closes the receiver first.
  • Iterator.prototype.includes(value, skippedElements) uses SameValueZero; skippedElements is a non-negative integer or Infinity.
  • Iterator.prototype.join(separator) joins like Array.prototype.join (nullish items are empty).
  • Iterator.zip(iterables, { mode, padding }) and Iterator.zipKeyed(object, options): mode is "shortest" (default), "longest" or "strict"; "strict" throws a TypeError when the inputs differ in length; padding (longest only) supplies the filler values. All inputs are closed when the result is closed or throws.
  • Messages for these have no V8 counterpart in the Node release used as reference; they are plain statements of what was expected and what arrived, in the same style as the others.

Atomics.pause

Atomics.pause() returns undefined. The specification text takes no parameter (a hint, in the proposal, is an integer); the engine ignores arguments, where V8 throws a TypeError for a non-integer. There is nothing to wait for in a single agent, so no time passes.

Limits and replay

  • Disposal runs inside the same fuel, depth and clock accounting as other script code; a [Symbol.dispose]() that never returns ends in a Fuel limit report. Each scope that holds resources is charged against the allocation limit when its record is made.
  • Disposal is plain script execution, so record and replay need nothing extra: a replay re-runs the same disposals.

Reports

  • A rejected declaration puts its label on the offending value (`conn` is number 42, but `using` needs an object) and a help line that says what to write instead.
  • An error thrown by a dispose method has a note thrown while disposingname, and, where the declaration is in the reported source, a secondary label on it ( name is disposed when its scope exits ``).
  • A SuppressedError report lists the chain, earlier errors first, one line each with the error name and message, the resource and the first script position: disposal failed N times; the errors, in the order they happened:. At most 20 entries are listed. The help line says where .error and .suppressed are.