Bug 989528 - Introduce a mechanism to explicitly enter a null compartment. r=luke

This commit is contained in:
Bobby Holley 2014-04-14 20:26:59 -07:00
parent 20f62f5f5a
commit bae8ff964e
4 changed files with 32 additions and 1 deletions

View File

@ -984,6 +984,19 @@ JSAutoCompartment::~JSAutoCompartment()
cx_->leaveCompartment(oldCompartment_);
}
JSAutoNullCompartment::JSAutoNullCompartment(JSContext *cx)
: cx_(cx),
oldCompartment_(cx->compartment())
{
AssertHeapIsIdleOrIterating(cx_);
cx_->enterNullCompartment();
}
JSAutoNullCompartment::~JSAutoNullCompartment()
{
cx_->leaveCompartment(oldCompartment_);
}
JS_PUBLIC_API(void)
JS_SetCompartmentPrivate(JSCompartment *compartment, void *data)
{

View File

@ -1677,6 +1677,15 @@ class JS_PUBLIC_API(JSAutoCompartment)
~JSAutoCompartment();
};
class JS_PUBLIC_API(JSAutoNullCompartment)
{
JSContext *cx_;
JSCompartment *oldCompartment_;
public:
JSAutoNullCompartment(JSContext *cx);
~JSAutoNullCompartment();
};
/* NB: This API is infallible; a nullptr return value does not indicate error. */
extern JS_PUBLIC_API(JSCompartment *)
JS_EnterCompartment(JSContext *cx, JSObject *target);

View File

@ -350,6 +350,7 @@ class ExclusiveContext : public ThreadSafeContext
#endif
inline void enterCompartment(JSCompartment *c);
inline void enterNullCompartment();
inline void leaveCompartment(JSCompartment *oldCompartment);
void setWorkerThread(WorkerThread *workerThread);

View File

@ -407,6 +407,13 @@ js::ExclusiveContext::enterCompartment(JSCompartment *c)
setCompartment(c);
}
inline void
js::ExclusiveContext::enterNullCompartment()
{
enterCompartmentDepth_++;
setCompartment(nullptr);
}
inline void
js::ExclusiveContext::leaveCompartment(JSCompartment *oldCompartment)
{
@ -417,7 +424,8 @@ js::ExclusiveContext::leaveCompartment(JSCompartment *oldCompartment)
// compartment.
JSCompartment *startingCompartment = compartment_;
setCompartment(oldCompartment);
startingCompartment->leave();
if (startingCompartment)
startingCompartment->leave();
}
inline void