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 24989 9517f50933c50ef605ad0eaa12698b8e32b93025
parent 24988 a81ce137b9443e7a8d24834b11fd7ef4d1ba9f77
child 24990 b96cdb8210feb9b3d41e19351271932ac3ddcf05
push id1308
push usermrbkap@mozilla.com
push dateThu, 23 Apr 2009 08:56:45 +0000
bugs483751
milestone1.9.1b4pre
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 * * * Fix bad merge * * * More merge fixing
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
@@ -3516,17 +3516,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
@@ -36,29 +36,68 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "XPCWrapper.h"
+#include "XPCNativeWrapper.h"
 
 const PRUint32
 XPCWrapper::sWrappedObjSlot = 1;
 
 const PRUint32
-XPCWrapper::sResolvingSlot = 0;
+XPCWrapper::sFlagsSlot = 0;
 
 const PRUint32
 XPCWrapper::sNumSlots = 2;
 
 JSNative
 XPCWrapper::sEvalNative = nsnull;
 
+// static
+JSObject *
+XPCWrapper::Unwrap(JSContext *cx, JSObject *wrapper)
+{
+  JSClass *clasp = STOBJ_GET_CLASS(wrapper);
+  if (clasp == &sXPC_XOW_JSClass.base) {
+    return UnwrapXOW(cx, wrapper);
+  }
+
+  if (XPCNativeWrapper::IsNativeWrapperClass(clasp)) {
+    XPCWrappedNative *wrappedObj;
+    if (!XPCNativeWrapper::GetWrappedNative(cx, wrapper, &wrappedObj) ||
+        !wrappedObj) {
+      return nsnull;
+    }
+
+    return wrappedObj->GetFlatJSObject();
+  }
+
+  if (clasp == &sXPC_SJOW_JSClass.base) {
+    JSObject *wrappedObj = STOBJ_GET_PARENT(wrapper);
+
+    if (NS_FAILED(CanAccessWrapper(cx, wrappedObj))) {
+      JS_ClearPendingException(cx);
+
+      return nsnull;
+    }
+
+    return wrappedObj;
+  }
+
+  if (clasp == &sXPC_SOW_JSClass.base) {
+    return UnwrapSOW(cx, wrapper);
+  }
+
+  return nsnull;
+}
+
 static void
 IteratorFinalize(JSContext *cx, JSObject *obj)
 {
   jsval v;
   JS_GetReservedSlot(cx, obj, 0, &v);
 
   JSIdArray *ida = reinterpret_cast<JSIdArray *>(JSVAL_TO_PRIVATE(v));
   if (ida) {
@@ -196,31 +235,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);
@@ -286,51 +321,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;
 }
@@ -505,17 +534,17 @@ XPCWrapper::ResolveNativeProperty(JSCont
 
   if (!::JS_DefineUCProperty(cx, wrapperObj, ::JS_GetStringChars(str),
                             ::JS_GetStringLength(str), v, nsnull, nsnull,
                             attrs)) {
     return JS_FALSE;
   }
 
   if (!isNativeWrapper &&
-      !::JS_SetReservedSlot(cx, wrapperObj, sResolvingSlot, oldFlags)) {
+      !::JS_SetReservedSlot(cx, wrapperObj, sFlagsSlot, oldFlags)) {
     return JS_FALSE;
   }
 
   *objp = wrapperObj;
 
   return JS_TRUE;
 }
 
@@ -762,67 +791,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
@@ -327,26 +327,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