Bug 483751 - Switch to use the new JSPropertyDescriptor API. Also fix it to return values on the prototype chain (which was sort of the point of its existance...). r+sr=jst
authorBlake Kaplan <mrbkap@gmail.com>
Fri, 20 Mar 2009 14:24:24 -0700
changeset 26428 61440f2eb1f58093a9ae4814f2547f7d92c895f4
parent 26427 f45743a633feb8420f669d1073c9b6b68c0a23eb
child 26429 514e4089924d889c7a2dcecd0e081ab55bd4e2e2
push id6063
push usermrbkap@mozilla.com
push dateFri, 20 Mar 2009 21:24:53 +0000
treeherdermozilla-central@61440f2eb1f5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs483751
milestone1.9.2a1pre
Bug 483751 - Switch to use the new JSPropertyDescriptor API. Also fix it to return values on the prototype chain (which was sort of the point of its existance...). r+sr=jst
js/src/jsapi.cpp
js/src/xpconnect/src/XPCWrapper.cpp
js/src/xpconnect/src/XPCWrapper.h
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -3575,17 +3575,17 @@ JS_LookupPropertyWithFlagsById(JSContext
 }
 
 JS_PUBLIC_API(JSBool)
 JS_GetPropertyDescriptorById(JSContext *cx, JSObject *obj, jsid id, uintN flags,
                              JSPropertyDescriptor *desc)
 {
     CHECK_REQUEST(cx);
 
-    return GetPropertyAttributesById(cx, obj, id, flags, JS_TRUE, desc);
+    return GetPropertyAttributesById(cx, obj, id, flags, JS_FALSE, desc);
 }
 
 JS_PUBLIC_API(JSBool)
 JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp)
 {
     JSAtom *atom;
 
     CHECK_REQUEST(cx);
--- a/js/src/xpconnect/src/XPCWrapper.cpp
+++ b/js/src/xpconnect/src/XPCWrapper.cpp
@@ -201,31 +201,27 @@ XPCWrapper::AddProperty(JSContext *cx, J
 {
   jsid interned_id;
   if (!::JS_ValueToId(cx, id, &interned_id)) {
     return JS_FALSE;
   }
 
   JSBool isXOW = (STOBJ_GET_CLASS(wrapperObj) == &sXPC_XOW_JSClass.base);
 
-  JSObject *wrapperObjp;
-  uintN attrs = JSPROP_ENUMERATE;
-  JSPropertyOp getter = nsnull;
-  JSPropertyOp setter = nsnull;
-
-  if (!GetPropertyAttrs(cx, wrapperObj, interned_id, &wrapperObjp, isXOW,
-                        JSRESOLVE_QUALIFIED, &attrs, &getter, &setter, vp)) {
+  JSPropertyDescriptor desc;
+  if (!GetPropertyAttrs(cx, wrapperObj, interned_id, JSRESOLVE_QUALIFIED,
+                        isXOW, &desc)) {
     return JS_FALSE;
   }
 
-  NS_ASSERTION(wrapperObjp == wrapperObj,
+  NS_ASSERTION(desc.obj == wrapperObj,
                "What weird wrapper are we using?");
 
-  return DefineProperty(cx, innerObj, interned_id, isXOW, *vp,
-                        getter, setter, attrs);
+  return JS_DefinePropertyById(cx, innerObj, interned_id, desc.value,
+                               desc.getter, desc.setter, desc.attrs);
 }
 
 // static
 JSBool
 XPCWrapper::DelProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
 {
   if (JSVAL_IS_STRING(id)) {
     JSString *str = JSVAL_TO_STRING(id);
@@ -291,51 +287,45 @@ XPCWrapper::Enumerate(JSContext *cx, JSO
 }
 
 // static
 JSBool
 XPCWrapper::NewResolve(JSContext *cx, JSObject *wrapperObj,
                        JSObject *innerObj, jsval id, uintN flags,
                        JSObject **objp, JSBool preserveVal)
 {
-  jsval v = JSVAL_VOID;
-
   jsid interned_id;
   if (!::JS_ValueToId(cx, id, &interned_id)) {
     return JS_FALSE;
   }
 
   JSBool isXOW = (STOBJ_GET_CLASS(wrapperObj) == &sXPC_XOW_JSClass.base);
-  JSObject *innerObjp;
-  uintN attrs = JSPROP_ENUMERATE;
-  JSPropertyOp getter = nsnull;
-  JSPropertyOp setter = nsnull;
 
-  if (!GetPropertyAttrs(cx, innerObj, interned_id, &innerObjp, isXOW, flags,
-                        &attrs, &getter, &setter, &v)) {
+  JSPropertyDescriptor desc;
+  if (!GetPropertyAttrs(cx, innerObj, interned_id, flags, isXOW, &desc)) {
     return JS_FALSE;
   }
 
-  if (!innerObjp) {
+  if (!desc.obj) {
     // Nothing to define.
     return JS_TRUE;
   }
 
   if (!preserveVal) {
-    v = JSVAL_VOID;
+    desc.value = JSVAL_VOID;
   }
 
   jsval oldSlotVal;
   if (!::JS_GetReservedSlot(cx, wrapperObj, sResolvingSlot, &oldSlotVal) ||
       !::JS_SetReservedSlot(cx, wrapperObj, sResolvingSlot, JSVAL_TRUE)) {
     return JS_FALSE;
   }
 
-  JSBool ok = DefineProperty(cx, wrapperObj, interned_id, isXOW, v,
-                             getter, setter, attrs);
+  JSBool ok = JS_DefinePropertyById(cx, wrapperObj, interned_id, desc.value,
+                                    desc.getter, desc.setter, desc.attrs);
 
   if (ok && (ok = ::JS_SetReservedSlot(cx, wrapperObj, sResolvingSlot,
                                        oldSlotVal))) {
     *objp = wrapperObj;
   }
 
   return ok;
 }
@@ -773,67 +763,43 @@ XPCWrapper::NativeToString(JSContext *cx
   NS_ENSURE_TRUE(str, JS_FALSE);
 
   *rval = STRING_TO_JSVAL(str);
   return JS_TRUE;
 }
 
 // static
 JSBool
-XPCWrapper::GetPropertyAttrs(JSContext *cx, JSObject *obj,
-                             jsid interned_id, JSObject **objp,
-                             JSBool wantDetails, uintN flags, uintN *attrsp,
-                             JSPropertyOp *getterp, JSPropertyOp *setterp,
-                             jsval *vp)
+XPCWrapper::GetPropertyAttrs(JSContext *cx, JSObject *obj, jsid interned_id,
+                             uintN flags, JSBool wantDetails,
+                             JSPropertyDescriptor *desc)
 {
-  // NB: All parameters must be initialized by this point.
-  if (!JS_LookupPropertyWithFlagsById(cx, obj, interned_id, flags,
-                                      objp, vp)) {
+  if (!JS_GetPropertyDescriptorById(cx, obj, interned_id, flags, desc)) {
     return JS_FALSE;
   }
 
-  if (!*objp) {
-    // Nothing to define.
-    return JS_TRUE;
-  }
-
-  if (!wantDetails) {
-    *vp = JSVAL_VOID;
-  } else {
-    JSBool found;
-    if (!JS_GetPropertyAttrsGetterAndSetterById(cx, *objp, interned_id,
-                                                attrsp, &found,
-                                                getterp, setterp)) {
-      return JS_FALSE;
-    }
-
-    // JS_GetPropertyAttrsGetterAndSetterById returns non scripted getters and
-    // setters, we don't want those.
-    uintN attrs = *attrsp;
-    if (!(attrs & JSPROP_GETTER)) {
-      *getterp = nsnull;
-    }
-    if (!(attrs & JSPROP_SETTER)) {
-      *setterp = nsnull;
-    }
-  }
-
-  return JS_TRUE;
-}
-
-// static
-JSBool
-XPCWrapper::DefineProperty(JSContext *cx, JSObject *obj, jsid interned_id,
-                           JSBool haveDetails, jsval v,
-                           JSPropertyOp getter, JSPropertyOp setter,
-                           uintN attrs)
-{
-  const uintN interesting_attrs = haveDetails
+  const uintN interesting_attrs = wantDetails
                                   ? (JSPROP_ENUMERATE |
                                      JSPROP_READONLY  |
                                      JSPROP_PERMANENT |
                                      JSPROP_SHARED    |
                                      JSPROP_GETTER    |
                                      JSPROP_SETTER)
                                   : JSPROP_ENUMERATE;
-  return JS_DefinePropertyById(cx, obj, interned_id, v, getter, setter,
-                               (attrs & interesting_attrs));
+  desc->attrs &= interesting_attrs;
+
+  if (wantDetails) {
+    // JS_GetPropertyDescriptorById returns non scripted getters and setters.
+    // If wantDetails is true, then we need to censor them.
+    if (!(desc->attrs & JSPROP_GETTER)) {
+      desc->getter = nsnull;
+    }
+    if (!(desc->attrs & JSPROP_SETTER)) {
+      desc->setter = nsnull;
+    }
+  } else {
+    // Clear out all but attrs and obj.
+    desc->getter = desc->setter = nsnull;
+    desc->value = JSVAL_VOID;
+  }
+
+  return JS_TRUE;
 }
--- a/js/src/xpconnect/src/XPCWrapper.h
+++ b/js/src/xpconnect/src/XPCWrapper.h
@@ -335,26 +335,15 @@ public:
 private:
   /**
    * Looks up a property on obj. If it exists, then the parameters are filled
    * in with useful values.
    *
    * NB: All parameters must be initialized before the call.
    */
   static JSBool GetPropertyAttrs(JSContext *cx, JSObject *obj,
-                                 jsid interned_id, JSObject **objp,
-                                 JSBool wantDetails, uintN flags, uintN *attrsp,
-                                 JSPropertyOp *getterp, JSPropertyOp *setterp,
-                                 jsval *vp);
-
-  /**
-   * Works in conjunction with GetPropertyAttrs to define the looked-up
-   * property on another object, preserving interesting attributes, if
-   * desired.
-   */
-  static JSBool DefineProperty(JSContext *cx, JSObject *obj, jsid interned_id,
-                               JSBool haveDetails, jsval v,
-                               JSPropertyOp getter, JSPropertyOp setter,
-                               uintN attrs);
+                                 jsid interned_id, uintN flags,
+                                 JSBool wantDetails,
+                                 JSPropertyDescriptor *desc);
 };
 
 
 #endif