we (web engine): Experimental web browser project to understand the limits of Claude

JS Iterators, generators, and destructuring #103

open opened by pierrelf.com

Implement the iterator protocol, generator functions, and destructuring syntax.

Scope#

Iterator Protocol#

  • Symbol.iterator method on iterables (Array, String, Map, Set, etc.)
  • Iterator objects: { next() → { value, done } }
  • for...of loop consuming iterators
  • Spread operator (...) in array literals and function calls
  • Array.from() consuming iterables

Generator Functions#

  • function* declaration and expression syntax
  • yield expression — pause execution and produce value
  • Generator objects conform to both iterator and iterable protocols
  • generator.next(value) — resume with value
  • generator.return(value) — force completion
  • generator.throw(error) — throw into generator
  • yield* delegation to sub-iterators

Destructuring#

  • Array destructuring: let [a, b, ...rest] = arr
  • Object destructuring: let { x, y: alias, ...rest } = obj
  • Default values: let [a = 1] = [], let { x = 2 } = {}
  • Nested destructuring: let { a: { b } } = obj
  • Destructuring in function parameters
  • Destructuring in for...of: for (let [key, val] of map)
  • Computed property names in destructuring: let { [expr]: val } = obj

Acceptance Criteria#

  • for...of works with arrays, strings, Maps, Sets
  • Custom iterables work (objects with Symbol.iterator)
  • Spread operator works in arrays and function calls
  • Generator functions pause and resume correctly
  • yield* delegates to sub-iterators
  • generator.next(value) passes value into generator
  • generator.return and generator.throw work
  • Array destructuring with rest and defaults
  • Object destructuring with aliases, rest, and defaults
  • Nested destructuring works
  • Destructuring in function parameters
  • Unit tests for iterators, generators, and destructuring

Phase 10 — JavaScript Engine (issue 14 of 15). Depends on: JS Functions/closures, Symbol built-in.

sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mhn3peabad2h