Bug 755186 - add JSCompartment::global (r=jorendorff)

--HG--
extra : rebase_source : 0a54699e01350611f1de69845cfe05b1506c40cb
This commit is contained in:
Luke Wagner 2012-06-20 08:57:29 -07:00
parent 0c274df8bd
commit bbae81d96a
7 changed files with 37 additions and 20 deletions

View File

@ -1389,14 +1389,8 @@ JS_EnterCrossCompartmentCallScript(JSContext *cx, JSScript *target)
{
AssertNoGC(cx);
CHECK_REQUEST(cx);
GlobalObject *global = target->getGlobalObjectOrNull();
if (!global) {
SwitchToCompartment sc(cx, target->compartment());
global = GlobalObject::create(cx, &dummy_class);
if (!global)
return NULL;
}
return JS_EnterCrossCompartmentCall(cx, global);
GlobalObject &global = target->compartment()->global();
return JS_EnterCrossCompartmentCall(cx, &global);
}
JS_PUBLIC_API(JSCrossCompartmentCall *)

View File

@ -224,8 +224,13 @@ class CompartmentChecker
JSCompartment *compartment;
public:
explicit CompartmentChecker(JSContext *cx) : context(cx), compartment(cx->compartment) {
check(cx->hasfp() ? JS_GetGlobalForScopeChain(cx) : cx->globalObject);
explicit CompartmentChecker(JSContext *cx)
: context(cx), compartment(cx->compartment)
{
if (cx->compartment) {
GlobalObject *global = GetGlobalForScopeChain(cx);
JS_ASSERT(cx->compartment->global() == *global);
}
}
/*

View File

@ -40,6 +40,7 @@ using namespace js::gc;
JSCompartment::JSCompartment(JSRuntime *rt)
: rt(rt),
principals(NULL),
global_(NULL),
needsBarrier_(false),
gcState(NoGCScheduled),
gcPreserveCode(false),

View File

@ -116,6 +116,20 @@ struct JSCompartment
JSRuntime *rt;
JSPrincipals *principals;
private:
js::GlobalObject *global_;
public:
js::GlobalObject &global() const {
JS_ASSERT(global_->compartment() == this);
return *global_;
}
void initGlobal(js::GlobalObject &global) {
JS_ASSERT(!global_);
global_ = &global;
}
public:
js::gc::ArenaLists arenas;
private:

View File

@ -2802,6 +2802,7 @@ NewObject(JSContext *cx, Class *clasp, types::TypeObject *type_, JSObject *paren
JS_ASSERT(clasp != &ArrayClass);
JS_ASSERT_IF(clasp == &FunctionClass,
kind == JSFunction::FinalizeKind || kind == JSFunction::ExtendedFinalizeKind);
JS_ASSERT_IF(parent, parent->global() == cx->compartment->global());
RootedTypeObject type(cx, type_);

View File

@ -1223,6 +1223,7 @@ JSObject::global() const
JSObject *obj = const_cast<JSObject *>(this);
while (JSObject *parent = obj->getParent())
obj = parent;
JS_ASSERT(obj->asGlobal() == compartment()->global());
return obj->asGlobal();
}

View File

@ -242,24 +242,25 @@ GlobalObject::create(JSContext *cx, Class *clasp)
{
JS_ASSERT(clasp->flags & JSCLASS_IS_GLOBAL);
Rooted<GlobalObject*> obj(cx);
JSObject *obj_ = NewObjectWithGivenProto(cx, clasp, NULL, NULL);
if (!obj_)
JSObject *obj = NewObjectWithGivenProto(cx, clasp, NULL, NULL);
if (!obj)
return NULL;
obj = &obj_->asGlobal();
if (!obj->setSingletonType(cx) || !obj->setVarObj(cx))
Rooted<GlobalObject *> global(cx, &obj->asGlobal());
cx->compartment->initGlobal(*global);
if (!global->setSingletonType(cx) || !global->setVarObj(cx))
return NULL;
/* Construct a regexp statics object for this global object. */
JSObject *res = RegExpStatics::create(cx, obj);
JSObject *res = RegExpStatics::create(cx, global);
if (!res)
return NULL;
obj->initSlot(REGEXP_STATICS, ObjectValue(*res));
obj->initFlags(0);
global->initSlot(REGEXP_STATICS, ObjectValue(*res));
global->initFlags(0);
return obj;
return global;
}
/* static */ bool