archives

« Bugzilla Issues Index

#3544 — new Object(value), for value != null, is no longer equivalent to ToObject(value)


19.1.1.1 Object([ value ])

As specced in Rev 31, `new Object(foo)`, for foo != null, is now just equivalent to `new Object()` instead of ToObject(foo).

The simplest fix is to revert to the definition of Rev 30, that is removing the newly added step 1.

Otherwise, one might want to produce an object with NewTarget.prototype as its [[Prototype]] internal slot: That may be done when `value` is a primitive; but when `value` is an object, it clashes with the ES1-5 semantics of `new Object(foo) === foo`.


In case one is interested to produce an object with NewTarget.prototype as its [[Prototype]] when possible (that is, except when `value` is already an object), here is a proposed patch.


19.1.1.1 Object(value)

1. If value is provided and is neither undefined nor null, return ToObject(value, NewTarget).
2. If NewTarget is not null, return OrdinaryCreateFromConstructor(NewTarget, "%ObjectPrototype").
3. Return ObjectCreate(%ObjectPrototype%).


7.1.13. ToObject(argument [, constructor])

1. If argument is undefined or null, throw a TypeError.
2. If Type(argument) is Object, return argument.
3. If Type(argument) is Boolean, then
a. Let dataSlot be [[BooleanData]].
b. Let intrinsicDefaultProto be "%BooleanPrototype%".
4. Else, if Type(argument) is Number, then
a. Let dataSlot be [[NumberData]].
b. Let intrinsicDefaultProto be "%NumberPrototype%".
5. Else, if Type(argument) is String, then
a. Let dataSlot be [[StringData]].
b. Let intrinsicDefaultProto be "%StringPrototype%".
6. Else, Type(argument) is Symbol.
a. Let dataSlot be [[SymbolData]].
b. Let intrinsicDefaultProto be "%SymbolPrototype%".
7. If constructor is provided as is not null, then
a. Let O be OrdinaryCreateFromConstructor(constructor, intrinsicDefaultProto, «dataSlot»).
b. ReturnIfAbrupt(O).
8. Else
a. Let currentRealm be the running execution context’s Realm.
b. Let proto be currentRealm’s intrinsic object named intrinsicDefaultProto.
c. Let O be ObjectCreate(proto, «dataSlot»).
9. Set the value of the dataSlot internal slot of O to argument.
10. Return O.


Typo in Comment 1:

7. If constructor is provided *and* is not null, then


Sorry, the idea of Comment 1 is bad, because new Object(3) must produce an object with [[Prototype]] equal to %NumberPrototype%, not %ObjectPrototype%.

One should just revert to algorithm of Rev 30 (deleting step 1).


[creating a new bug, removing the noise]

*** This bug has been marked as a duplicate of bug 3550 ***