Bug 1022769 - Use PersistentRooted to root NativeJSContainer objects. r=jonco

This commit is contained in:
Jim Chen 2014-06-10 12:46:00 -04:00
Родитель bbadf88bb4
Коммит a2f1f96c3e
1 изменённых файлов: 13 добавлений и 8 удалений

Просмотреть файл

@ -133,7 +133,7 @@ public:
return nullptr; return nullptr;
} }
JSContext* const cx = container->mThreadContext; JSContext* const cx = container->mThreadContext;
JS::RootedObject object(cx, container->mJSObject); JS::RootedObject object(cx, *container->mJSObject);
MOZ_ASSERT(object); MOZ_ASSERT(object);
JSAutoStructuredCloneBuffer buffer; JSAutoStructuredCloneBuffer buffer;
@ -179,9 +179,9 @@ public:
const jint index = env->GetIntField(object, jObjectIndex); const jint index = env->GetIntField(object, jObjectIndex);
if (index < 0) { if (index < 0) {
// -1 for index field means it's the root object of the container // -1 for index field means it's the root object of the container
return container->mJSObject; return *container->mJSObject;
} }
return container->mRootedObjects[index]; return *container->mRootedObjects[index];
} }
static jobject CreateObjectInstance(JNIEnv* env, jobject object, static jobject CreateObjectInstance(JNIEnv* env, jobject object,
@ -197,7 +197,8 @@ public:
return nullptr; return nullptr;
} }
size_t newIndex = container->mRootedObjects.length(); size_t newIndex = container->mRootedObjects.length();
if (!container->mRootedObjects.append(jsObject)) { PersistentObjectPtr rootedJSObject(new PersistentObject(cx, jsObject));
if (!container->mRootedObjects.append(Move(rootedJSObject))) {
AndroidBridge::ThrowException(env, AndroidBridge::ThrowException(env,
"java/lang/OutOfMemoryError", "Cannot allocate object"); "java/lang/OutOfMemoryError", "Cannot allocate object");
return nullptr; return nullptr;
@ -231,7 +232,7 @@ public:
MOZ_ASSERT(mBuffer.data()); MOZ_ASSERT(mBuffer.data());
MOZ_ALWAYS_TRUE(mBuffer.read(mThreadContext, &value)); MOZ_ALWAYS_TRUE(mBuffer.read(mThreadContext, &value));
if (value.isObject()) { if (value.isObject()) {
mJSObject = &value.toObject(); mJSObject = new PersistentObject(mThreadContext, &value.toObject());
} }
if (!mJSObject) { if (!mJSObject) {
AndroidBridge::ThrowException(env, AndroidBridge::ThrowException(env,
@ -278,22 +279,25 @@ private:
return newObject; return newObject;
} }
typedef JS::PersistentRooted<JSObject*> PersistentObject;
typedef ScopedDeletePtr<PersistentObject> PersistentObjectPtr;
// Thread that the object is valid on // Thread that the object is valid on
PRThread* mThread; PRThread* mThread;
// Context that the object is valid in // Context that the object is valid in
JSContext* mThreadContext; JSContext* mThreadContext;
// Deserialized object, or nullptr if object is in serialized form // Deserialized object, or nullptr if object is in serialized form
JS::Heap<JSObject*> mJSObject; PersistentObjectPtr mJSObject;
// Serialized object, or empty if object is in deserialized form // Serialized object, or empty if object is in deserialized form
JSAutoStructuredCloneBuffer mBuffer; JSAutoStructuredCloneBuffer mBuffer;
// Objects derived from mJSObject // Objects derived from mJSObject
Vector<JS::Heap<JSObject*>, 4> mRootedObjects; Vector<PersistentObjectPtr, 0> mRootedObjects;
// Create a new container containing the given deserialized object // Create a new container containing the given deserialized object
NativeJSContainer(JSContext* cx, JS::HandleObject object) NativeJSContainer(JSContext* cx, JS::HandleObject object)
: mThread(PR_GetCurrentThread()) : mThread(PR_GetCurrentThread())
, mThreadContext(cx) , mThreadContext(cx)
, mJSObject(object) , mJSObject(new PersistentObject(cx, object))
{ {
} }
@ -301,6 +305,7 @@ private:
NativeJSContainer(JSContext* cx, JSAutoStructuredCloneBuffer&& buffer) NativeJSContainer(JSContext* cx, JSAutoStructuredCloneBuffer&& buffer)
: mThread(PR_GetCurrentThread()) : mThread(PR_GetCurrentThread())
, mThreadContext(cx) , mThreadContext(cx)
, mJSObject(nullptr)
, mBuffer(Forward<JSAutoStructuredCloneBuffer>(buffer)) , mBuffer(Forward<JSAutoStructuredCloneBuffer>(buffer))
{ {
} }