servo: Merge #2668 - Handle JSAPI OOM failures by task failure as soon as possible (from Ms2ger:CreateInterfaceObjects-failures)
authorMs2ger <ms2ger@gmail.com>
Fri, 20 Jun 2014 09:28:59 +0100
changeset 334531 31066deb82e1aae0c2c6c3341e6291f0f9d38a02
parent 334530 037a6bee285854da962334e64ba9e0f2993c6747
child 334532 245a11d99729df49a7dca1b1f6a5dcdec8008077
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
servo: Merge #2668 - Handle JSAPI OOM failures by task failure as soon as possible (from Ms2ger:CreateInterfaceObjects-failures) Source-Repo: https://github.com/servo/servo Source-Revision: 9eff8b458fc4e1c1f6e4798a1bf2e22728a2cc6f
servo/src/components/script/dom/bindings/codegen/CodegenRust.py
servo/src/components/script/dom/bindings/utils.rs
--- a/servo/src/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/servo/src/components/script/dom/bindings/codegen/CodegenRust.py
@@ -1872,20 +1872,18 @@ class CGCreateInterfaceObjectsMethod(CGA
                               toBindingNamespace(parentProtoName))
 
         needInterfaceObject = self.descriptor.interface.hasInterfaceObject()
         needInterfacePrototypeObject = self.descriptor.interface.hasInterfacePrototypeObject()
 
         # if we don't need to create anything, why are we generating this?
         assert needInterfaceObject or needInterfacePrototypeObject
 
-        getParentProto = ("let parentProto: *mut JSObject = %s;\n" +
-                          "if parentProto.is_null() {\n" +
-                          "  return ptr::mut_null();\n" +
-                          "}\n") % getParentProto
+        getParentProto = ("let parentProto: *mut JSObject = %s;\n"
+                          "assert!(parentProto.is_not_null());\n") % getParentProto
 
         if self.descriptor.interface.ctor():
             constructHook = CONSTRUCT_HOOK_NAME
             constructArgs = methodLength(self.descriptor.interface.ctor())
         else:
             constructHook = "ThrowingConstructor"
             constructArgs = 0
 
@@ -1948,16 +1946,17 @@ class CGGetPerInterfaceObject(CGAbstract
 
   assert!(((*JS_GetClass(aGlobal)).flags & JSCLASS_DOM_GLOBAL) != 0);
 
   /* Check to see whether the interface objects are already installed */
   let protoOrIfaceArray = GetProtoOrIfaceArray(aGlobal);
   let cachedObject: *mut JSObject = *protoOrIfaceArray.offset(%s as int);
   if cachedObject.is_null() {
     let tmp: *mut JSObject = CreateInterfaceObjects(aCx, aGlobal, aReceiver);
+    assert!(tmp.is_not_null());
     *protoOrIfaceArray.offset(%s as int) = tmp;
     tmp
   } else {
     cachedObject
   }""" % (self.id, self.id)
 
 class CGGetProtoObjectMethod(CGGetPerInterfaceObject):
     """
--- a/servo/src/components/script/dom/bindings/utils.rs
+++ b/servo/src/components/script/dom/bindings/utils.rs
@@ -225,36 +225,30 @@ pub fn CreateInterfaceObjects2(cx: *mut 
                                constants: Option<&'static [ConstantSpec]>,
                                staticMethods: Option<&'static [JSFunctionSpec]>,
                                name: &str) -> *mut JSObject {
     let mut proto = ptr::mut_null();
     if protoClass.is_not_null() {
         proto = CreateInterfacePrototypeObject(cx, global, protoProto,
                                                protoClass, methods,
                                                properties, constants);
-        if proto.is_null() {
-            return ptr::mut_null();
-        }
 
         unsafe {
             JS_SetReservedSlot(proto, DOM_PROTO_INSTANCE_CLASS_SLOT,
                                PrivateValue(domClass as *libc::c_void));
         }
     }
 
     let mut interface = ptr::mut_null();
     if constructor.is_some() {
         interface = name.to_c_str().with_ref(|s| {
             CreateInterfaceObject(cx, global, receiver,
                                   constructor, ctorNargs, proto,
                                   staticMethods, constants, s)
         });
-        if interface.is_null() {
-            return ptr::mut_null();
-        }
     }
 
     if protoClass.is_not_null() {
         proto
     } else {
         interface
     }
 }
@@ -263,126 +257,100 @@ fn CreateInterfaceObject(cx: *mut JSCont
                          constructorNative: JSNative,
                          ctorNargs: u32, proto: *mut JSObject,
                          staticMethods: Option<&'static [JSFunctionSpec]>,
                          constants: Option<&'static [ConstantSpec]>,
                          name: *libc::c_char) -> *mut JSObject {
     unsafe {
         let fun = JS_NewFunction(cx, constructorNative, ctorNargs,
                                  JSFUN_CONSTRUCTOR, global, name);
-        if fun.is_null() {
-            return ptr::mut_null();
-        }
+        assert!(fun.is_not_null());
 
         let constructor = JS_GetFunctionObject(fun);
         assert!(constructor.is_not_null());
 
         match staticMethods {
-            Some(staticMethods) => {
-                if !DefineMethods(cx, constructor, staticMethods) {
-                    return ptr::mut_null();
-                }
-            },
+            Some(staticMethods) => DefineMethods(cx, constructor, staticMethods),
             _ => (),
         }
 
         match constants {
-            Some(constants) => {
-                if !DefineConstants(cx, constructor, constants) {
-                    return ptr::mut_null();
-                }
-            },
+            Some(constants) => DefineConstants(cx, constructor, constants),
             _ => (),
         }
 
-        if proto.is_not_null() && JS_LinkConstructorAndPrototype(cx, constructor, proto) == 0 {
-            return ptr::mut_null();
+        if proto.is_not_null() {
+            assert!(JS_LinkConstructorAndPrototype(cx, constructor, proto) != 0);
         }
 
         let mut alreadyDefined = 0;
-        if JS_AlreadyHasOwnProperty(cx, receiver, name, &mut alreadyDefined) == 0 {
-            return ptr::mut_null();
-        }
+        assert!(JS_AlreadyHasOwnProperty(cx, receiver, name, &mut alreadyDefined) != 0);
 
-        if alreadyDefined == 0 &&
-            JS_DefineProperty(cx, receiver, name, ObjectValue(&*constructor),
-                              None, None, 0) == 0 {
-            return ptr::mut_null();
+        if alreadyDefined == 0 {
+            assert!(JS_DefineProperty(cx, receiver, name,
+                                      ObjectValue(&*constructor),
+                                      None, None, 0) != 0);
         }
 
         return constructor;
     }
 }
 
-fn DefineConstants(cx: *mut JSContext, obj: *mut JSObject, constants: &'static [ConstantSpec]) -> bool {
-    constants.iter().all(|spec| {
+fn DefineConstants(cx: *mut JSContext, obj: *mut JSObject, constants: &'static [ConstantSpec]) {
+    for spec in constants.iter() {
         let jsval = match spec.value {
             NullVal => NullValue(),
             IntVal(i) => Int32Value(i),
             UintVal(u) => UInt32Value(u),
             DoubleVal(d) => DoubleValue(d),
             BoolVal(b) => BooleanValue(b),
             VoidVal => UndefinedValue(),
         };
         unsafe {
-            JS_DefineProperty(cx, obj, spec.name.as_ptr() as *libc::c_char,
-                              jsval, None, None,
-                              JSPROP_ENUMERATE | JSPROP_READONLY |
-                              JSPROP_PERMANENT) != 0
+            assert!(JS_DefineProperty(cx, obj, spec.name.as_ptr() as *libc::c_char,
+                                      jsval, None, None,
+                                      JSPROP_ENUMERATE | JSPROP_READONLY |
+                                      JSPROP_PERMANENT) != 0);
         }
-    })
-}
-
-fn DefineMethods(cx: *mut JSContext, obj: *mut JSObject, methods: &'static [JSFunctionSpec]) -> bool {
-    unsafe {
-        JS_DefineFunctions(cx, obj, methods.as_ptr()) != 0
     }
 }
 
-fn DefineProperties(cx: *mut JSContext, obj: *mut JSObject, properties: &'static [JSPropertySpec]) -> bool {
+fn DefineMethods(cx: *mut JSContext, obj: *mut JSObject, methods: &'static [JSFunctionSpec]) {
     unsafe {
-        JS_DefineProperties(cx, obj, properties.as_ptr()) != 0
+        assert!(JS_DefineFunctions(cx, obj, methods.as_ptr()) != 0);
+    }
+}
+
+fn DefineProperties(cx: *mut JSContext, obj: *mut JSObject, properties: &'static [JSPropertySpec]) {
+    unsafe {
+        assert!(JS_DefineProperties(cx, obj, properties.as_ptr()) != 0);
     }
 }
 
 fn CreateInterfacePrototypeObject(cx: *mut JSContext, global: *mut JSObject,
                                   parentProto: *mut JSObject, protoClass: *JSClass,
                                   methods: Option<&'static [JSFunctionSpec]>,
                                   properties: Option<&'static [JSPropertySpec]>,
                                   constants: Option<&'static [ConstantSpec]>) -> *mut JSObject {
     unsafe {
         let ourProto = JS_NewObjectWithUniqueType(cx, protoClass, parentProto, global);
-        if ourProto.is_null() {
-            return ptr::mut_null();
-        }
+        assert!(ourProto.is_not_null());
 
         match methods {
-            Some(methods) => {
-                if !DefineMethods(cx, ourProto, methods) {
-                    return ptr::mut_null();
-                }
-            },
+            Some(methods) => DefineMethods(cx, ourProto, methods),
             _ => (),
         }
 
         match properties {
-            Some(properties) => {
-                if !DefineProperties(cx, ourProto, properties) {
-                    return ptr::mut_null();
-                }
-            },
+            Some(properties) => DefineProperties(cx, ourProto, properties),
             _ => (),
         }
 
         match constants {
-            Some(constants) => {
-                if !DefineConstants(cx, ourProto, constants) {
-                    return ptr::mut_null();
-                }
-            },
+            Some(constants) => DefineConstants(cx, ourProto, constants),
             _ => (),
         }
 
         return ourProto;
     }
 }
 
 pub extern fn ThrowingConstructor(_cx: *mut JSContext, _argc: c_uint, _vp: *mut JSVal) -> JSBool {