Bug 801712 part 3. Make object return values faster by only doing the JS_WrapValue if needed. r=peterv
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 22 Oct 2012 13:08:52 -0400
changeset 111176 982da0cd2711c3365cc6ff62ecbb8b1b30ef34e4
parent 111175 c8ad798b2ced652b26163fdeecadca5b08042419
child 111177 ffa2d8f5b8be06649da504d863bb6c57e2f586d6
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerspeterv
bugs801712
milestone19.0a1
Bug 801712 part 3. Make object return values faster by only doing the JS_WrapValue if needed. r=peterv
dom/bindings/BindingUtils.h
dom/bindings/Codegen.py
--- a/dom/bindings/BindingUtils.h
+++ b/dom/bindings/BindingUtils.h
@@ -397,16 +397,27 @@ JSObject*
 CreateInterfaceObjects(JSContext* cx, JSObject* global, JSObject* receiver,
                        JSObject* protoProto, JSClass* protoClass,
                        JSClass* constructorClass, JSNative constructor,
                        unsigned ctorNargs, const DOMClass* domClass,
                        const NativeProperties* properties,
                        const NativeProperties* chromeProperties,
                        const char* name);
 
+inline bool
+MaybeWrapValue(JSContext* cx, JSObject* obj, JS::Value* vp)
+{
+  if (vp->isObject() &&
+      js::GetObjectCompartment(&vp->toObject()) != js::GetObjectCompartment(obj)) {
+    return JS_WrapValue(cx, vp);
+  }
+
+  return true;
+}
+
 template <class T>
 inline bool
 WrapNewBindingObject(JSContext* cx, JSObject* scope, T* value, JS::Value* vp)
 {
   JSObject* obj = value->GetWrapper();
   if (obj && js::GetObjectCompartment(obj) == js::GetObjectCompartment(scope)) {
     *vp = JS::ObjectValue(*obj);
     return true;
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -2747,27 +2747,27 @@ def getWrapTemplateForType(type, descrip
     """
     haveSuccessCode = successCode is not None
     if not haveSuccessCode:
         successCode = "return true;"
 
     def setValue(value, callWrapValue=False):
         """
         Returns the code to set the jsval to value. If "callWrapValue" is true
-        JS_WrapValue will be called on the jsval.
+        MaybeWrapValue will be called on the jsval.
         """
         if not callWrapValue:
             tail = successCode
         elif haveSuccessCode:
-            tail = ("if (!JS_WrapValue(cx, ${jsvalPtr})) {\n" +
+            tail = ("if (!MaybeWrapValue(cx, ${obj}, ${jsvalPtr})) {\n" +
                     "  return false;\n" +
                     "}\n" +
                     successCode)
         else:
-            tail = "return JS_WrapValue(cx, ${jsvalPtr});"
+            tail = "return MaybeWrapValue(cx, ${obj}, ${jsvalPtr});"
         return ("${jsvalRef} = %s;\n" +
                 tail) % (value)
 
     def wrapAndSetPtr(wrapCall, failureCode=None):
         """
         Returns the code to set the jsval by calling "wrapCall". "failureCode"
         is the code to run if calling "wrapCall" fails 
         """