Overview

The semantics of enumeration in existing editions of ECMA-262 is very loosely specified. This strawman proposes more fully specifying the semantics of property enumeration.

Versioning

The semantics below is incompatible with existing web behavior, and would be enabled only through opting into Harmony. For compatibility, code run in legacy JavaScript versions may need to preserve the previous enumeration behavior.

For-in loops

The semantics of iteration uses the iterate proxy trap to drive iteration if it exists, but falls back to using the enumeration behavior of this proposal.

Semantics

The following operation produces an eagerly-computed sequence of the own-properties of an object.

Operation EnumerateProperties(obj)

Execution Error propagation
Let suppress = ∅
Let props = []
While (obj != empty)
    If IsTrappingProxy(obj)
        Let handler = obj.[[Handler]]
        Let enum = handler.[[Get]](”enumerate”) If IsError(enum) Return enum
        If !IsCallable(enum)
            Return (type=error, value=TypeError, target=empty)
        Let rest = enum.value.[[Call]](handler, []) If IsError(rest) Return rest
        Return [ props, ..., rest, ... ]
    Let own = OwnProperties(obj)
    For each i in 0 ... own.length - 1
        Let P = own[i]
        If P.attributes.enumerable && P ∉ props && P ∉ suppress
            props := [ props, ..., P.name ]
        If !P.attributes.enumerable
            suppress := suppress ∪ { P }
    obj := obj.[[Prototype]]
Return props

Operation OwnProperties(obj)

We could specify this in a number of different ways. Conceptually, this operation should produce a sequence of property descriptors, in the following order:

  1. index properties (see definition below) in ascending numeric order
  2. all other properties, in the order in which they were created

An index, as defined in 15.4, is a property name P such that ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2**32-1.

Several specification approaches:

  • all objects have two sequential property tables:
    • properties with uint32 names, kept in integer order
    • all other properties, kept in creation order
  • objects have one sequential property table, kept in creation order; OwnProperties then filters out uint32 properties
  • properties in property tables include an internal creation-order attribute

See the thread starting at this message from Charles Kendrick for criticisms (some aesthetic or theoretical) and compatibility concerns (these look significant to me) about enumerating own indexed properties first for non-Array objects.

Brendan Eich 2011/03/13 20:04

 
strawman/enumeration.txt · Last modified: 2011/03/13 20:09 by brendan
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki