typed objects and value types

C. Scott Ananian ecmascript at cscott.net
Wed Apr 2 10:19:58 PDT 2014

Commenting on this same region:
print(points[0].x); // 0 to start
points[0].x = 1;
print(points[0].x); // still 0
points[0] = {x: 1, y: 2};
print(points[0].x); // now 1

There's no reason why we couldn't extend the grammar to handle this
case, in the same way that `a.f()` has different semantics than `f =
a.f; f();`.  That is, use the [Reference Specification Type] to treat
field assignment differently, so that the semantics of:
points[0].x = 1;
are the same as:
points[0] = { x: 1, y: points[0].y }

For my own education, here's a rough draft of how the [runtime
semantics of assignment] might look:

AssignmentExpression[In, Yield] : LeftHandSideExpression[?Yield] =
AssignmentExpression[?In, ?Yield]

1. Let *lref* be the result of evaluating *LeftHandSideExpression*
2. ReturnIfAbrupt(*lref*)
3. If Type(*lref*) is Reference, then
    a. Let *baseref* be GetBaseReference(*lref*)
    b. If *baseref* is a reference to a value type, then
        i. Let *name* be GetReferencedName(*lref*)
        ii. Let *oldval* be GetValue(*baseref*)
        iii. Let *rref* be the result of evaluating *AssignmentExpression*
        iv. ReturnIfAbrupt(*rref*)
        v. Let *rval* be GetValue(*rref*)
        vi. Let *newval* be a value type identical to *oldval* except
that field *name* is set to *rval*
        vii. Let *status* be PutValue(*baseref*, *newval*)
        viii. ReturnIfAbrupt(*status*)
        ix. Return *rval*

Note that in step 3a I need to get a *reference to* the base value,
rather than using GetValue(*lref*).  So the Reference specification
type would have to cover two levels of indirection rather than one.

[Reference Specification Type]:
[runtime semantics of assignment]:

More information about the es-discuss mailing list