JSBool WrapObject(JSContext *cx, JSObject *parent, jsval v, jsval *vp) { if (JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(v))) { return WrapFunction(cx, parent, JSVAL_TO_OBJECT(v), vp); } JSObject *wrapperObj = JS_NewObjectWithGivenProto(cx, &COWClass.base, NULL, parent); if (!wrapperObj) { return JS_FALSE; } *vp = OBJECT_TO_JSVAL(wrapperObj); jsval exposedProps = JSVAL_VOID; JSAutoTempValueRooter tvr(cx, 1, &exposedProps); if (!GetExposedProperties(cx, JSVAL_TO_OBJECT(v), &exposedProps)) { return JS_FALSE; } if (!JS_SetReservedSlot(cx, wrapperObj, XPCWrapper::sWrappedObjSlot, v) || !JS_SetReservedSlot(cx, wrapperObj, XPCWrapper::sFlagsSlot, JSVAL_ZERO) || !JS_SetReservedSlot(cx, wrapperObj, sExposedPropsSlot, exposedProps)) { return JS_FALSE; } return JS_TRUE; }
JSBool RewrapIfNeeded(JSContext *cx, JSObject *outerObj, jsval *vp) { // Don't need to wrap primitive values. if (JSVAL_IS_PRIMITIVE(*vp)) { return JS_TRUE; } JSObject *obj = JSVAL_TO_OBJECT(*vp); if (JS_ObjectIsFunction(cx, obj)) { return WrapFunction(cx, outerObj, obj, vp); } XPCWrappedNative *wn = nsnull; if (obj->getClass() == &XOWClass && outerObj->getParent() != obj->getParent()) { *vp = OBJECT_TO_JSVAL(GetWrappedObject(cx, obj)); } else if (!(wn = XPCWrappedNative::GetAndMorphWrappedNativeOfJSObject(cx, obj))) { return JS_TRUE; } return WrapObject(cx, JS_GetGlobalForObject(cx, outerObj), vp, wn); }