Bug 48911 - Object.defineProperty doesn't create property on Global Object in the presence of a setter in the prototype chain
Summary: Object.defineProperty doesn't create property on Global Object in the presenc...
Status: RESOLVED WORKSFORME
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-03 06:26 PDT by Kent Hansen
Modified: 2012-03-12 18:22 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kent Hansen 2010-11-03 06:26:12 PDT
For normal objects, this works as expected:

o = {};
o.__proto__ = {};
o.__proto__.__defineGetter__("foo", function() { return this._x; });
o.__proto__.__defineSetter__("foo", function(v) { this._x = v; });
o.foo = 123;
o.hasOwnProperty("foo"); // false
Object.defineProperty(o, "foo", { value : 456 });
o.hasOwnProperty("foo"); // true

i.e. even if there's a getter for a property named "foo" in the prototype chain, Object.defineProperty creates a new property on the object itself. This is in accordance with the ES5 spec, so all is good.

However, if instead you do this on the Global Object (this=global):

this.__proto__ = {};
this.__proto__.__defineGetter__("foo", function() { return this._x; });
this.__proto__.__defineSetter__("foo", function(v) { this._x = v; });
this.foo = 123;
this.hasOwnProperty("foo"); // false
Object.defineProperty(this, "foo", { value : 456 });
this.hasOwnProperty("foo"); // still false

the property will _not_ be created on the Global Object, but will instead call the setter in the prototype.

This is because JSGlobalObject::putWithAttributes() calls JSObject::put(), as discussed in https://bugs.webkit.org/show_bug.cgi?id=38636.
Comment 1 Gavin Barraclough 2012-03-12 14:46:52 PDT
This works for me in ToT, was likely fixed in r106783.  Will land a regression test for this.  Please reopen if you're still repro'ing any issues in ToT.

Cheers, G.
Comment 2 Gavin Barraclough 2012-03-12 18:00:31 PDT
Gah, the test case still fails.  It works since in JSC, but fails in WebCore.  Object.defineProperty works fine, it's actually hasOwnProperty that fails, because JSDOMWindow::getOwnPropertySlot erroneously searches the prototype chain, which it shouldn't be doing for an 'own' access.  This is likely related to the sadness that is bug#49739.
Comment 3 Gavin Barraclough 2012-03-12 18:18:06 PDT
Okay, the hasOwnProperty thing is really a separate issue, and defineProperty now works, so to get these tests in I've changed them to use getOwnPropertyDescriptor (which does work correctly in WebCore's global object).  I'll file a new bug to track the problems with hasOwnProperty.

Layout tests landed in r110521.
Comment 4 Gavin Barraclough 2012-03-12 18:22:05 PDT
Tracking the WebCore hasOwnProperty bug here: https://bugs.webkit.org/show_bug.cgi?id=80921