Bug 740469 - JSNative->JSFunction fixup should happen in DefinePropertyById, not DefineProperty. r=luke

This commit is contained in:
Bobby Holley 2012-03-29 13:34:36 -07:00
parent 86dea005e5
commit dbae036f37
3 changed files with 30 additions and 21 deletions

View File

@ -3635,6 +3635,34 @@ DefinePropertyById(JSContext *cx, JSObject *obj, jsid id, const Value &value,
if (attrs & (JSPROP_GETTER | JSPROP_SETTER))
attrs &= ~JSPROP_READONLY;
/*
* When we use DefineProperty, we need full scriptable Function objects rather
* than JSNatives. However, we might be pulling this property descriptor off
* of something with JSNative property descriptors. If we are, wrap them in
* JS Function objects.
*/
if (attrs & JSPROP_NATIVE_ACCESSORS) {
RootId idRoot(cx, &id);
JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
attrs &= ~JSPROP_NATIVE_ACCESSORS;
if (getter) {
JSObject *getobj = JS_NewFunction(cx, (Native) getter, 0, 0, &obj->global(), NULL);
if (!getobj)
return false;
getter = JS_DATA_TO_FUNC_PTR(PropertyOp, getobj);
attrs |= JSPROP_GETTER;
}
if (setter) {
JSObject *setobj = JS_NewFunction(cx, (Native) setter, 1, 0, &obj->global(), NULL);
if (!setobj)
return false;
setter = JS_DATA_TO_FUNC_PTR(StrictPropertyOp, setobj);
attrs |= JSPROP_SETTER;
}
}
AssertNoGC(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, obj, id, value,
@ -3694,27 +3722,6 @@ DefineProperty(JSContext *cx, JSObject *obj, const char *name, const Value &valu
id = ATOM_TO_JSID(atom);
}
if (attrs & JSPROP_NATIVE_ACCESSORS) {
RootId idRoot(cx, &id);
JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
attrs &= ~JSPROP_NATIVE_ACCESSORS;
if (getter) {
JSObject *getobj = JS_NewFunction(cx, (Native) getter, 0, 0, &obj->global(), NULL);
if (!getobj)
return false;
getter = JS_DATA_TO_FUNC_PTR(PropertyOp, getobj);
attrs |= JSPROP_GETTER;
}
if (setter) {
JSObject *setobj = JS_NewFunction(cx, (Native) setter, 1, 0, &obj->global(), NULL);
if (!setobj)
return false;
setter = JS_DATA_TO_FUNC_PTR(StrictPropertyOp, setobj);
attrs |= JSPROP_SETTER;
}
}
return DefinePropertyById(cx, obj, id, value, getter, setter, attrs, flags, tinyid);
}

View File

@ -4453,6 +4453,7 @@ DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, const Value &value_,
{
JS_ASSERT((defineHow & ~(DNP_CACHE_RESULT | DNP_DONT_PURGE |
DNP_SKIP_TYPE)) == 0);
JS_ASSERT(!(attrs & JSPROP_NATIVE_ACCESSORS));
RootObject objRoot(cx, &obj);
RootId idRoot(cx, &id);

View File

@ -1029,6 +1029,7 @@ JSObject::defineGeneric(JSContext *cx, jsid id, const js::Value &value,
JSStrictPropertyOp setter /* = JS_StrictPropertyStub */,
unsigned attrs /* = JSPROP_ENUMERATE */)
{
JS_ASSERT(!(attrs & JSPROP_NATIVE_ACCESSORS));
js::DefineGenericOp op = getOps()->defineGeneric;
return (op ? op : js_DefineProperty)(cx, this, id, &value, getter, setter, attrs);
}