Bug 899367 - Add an API to opt out of default compartment objects. r=luke

We need this explicit API because otherwise the default compartment object will
get set implicitly during the first call to InitClasses.
This commit is contained in:
Bobby Holley 2013-09-04 14:06:55 -07:00
parent b9a315924b
commit e0d786d901
4 changed files with 23 additions and 6 deletions

View File

@ -1526,7 +1526,11 @@ JS_StringToVersion(const char *string);
/* JS_BIT(10) is currently unused. */
/* JS_BIT(11) is currently unused. */
#define JSOPTION_NO_DEFAULT_COMPARTMENT_OBJECT JS_BIT(11) /* This JSContext does not use a
default compartment object. Such
an object will not be set implicitly,
and attempts to get or set it will
assert. */
#define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
that a null rval out-param

View File

@ -449,7 +449,10 @@ struct JSContext : public js::ExclusiveContext,
public:
inline void setDefaultCompartmentObject(JSObject *obj);
inline void setDefaultCompartmentObjectIfUnset(JSObject *obj);
JSObject *maybeDefaultCompartmentObject() const { return defaultCompartmentObject_; }
JSObject *maybeDefaultCompartmentObject() const {
JS_ASSERT(!hasOption(JSOPTION_NO_DEFAULT_COMPARTMENT_OBJECT));
return defaultCompartmentObject_;
}
/* Wrap cx->exception for the current compartment. */
void wrapPendingException();

View File

@ -438,14 +438,18 @@ JSContext::setPendingException(js::Value v) {
inline void
JSContext::setDefaultCompartmentObject(JSObject *obj)
{
JS_ASSERT(!hasOption(JSOPTION_NO_DEFAULT_COMPARTMENT_OBJECT));
defaultCompartmentObject_ = obj;
}
inline void
JSContext::setDefaultCompartmentObjectIfUnset(JSObject *obj)
{
if (!defaultCompartmentObject_)
if (!hasOption(JSOPTION_NO_DEFAULT_COMPARTMENT_OBJECT) &&
!defaultCompartmentObject_)
{
setDefaultCompartmentObject(obj);
}
}
inline void

View File

@ -682,12 +682,17 @@ bool
JSRuntime::initSelfHosting(JSContext *cx)
{
JS_ASSERT(!selfHostingGlobal_);
RootedObject savedGlobal(cx, js::DefaultObjectForContextOrNull(cx));
bool receivesDefaultObject = !cx->hasOption(JSOPTION_NO_DEFAULT_COMPARTMENT_OBJECT);
RootedObject savedGlobal(cx, receivesDefaultObject
? js::DefaultObjectForContextOrNull(cx)
: NULL);
if (!(selfHostingGlobal_ = JS_NewGlobalObject(cx, &self_hosting_global_class,
NULL, JS::DontFireOnNewGlobalHook)))
return false;
JSAutoCompartment ac(cx, selfHostingGlobal_);
js::SetDefaultObjectForContext(cx, selfHostingGlobal_);
if (receivesDefaultObject)
js::SetDefaultObjectForContext(cx, selfHostingGlobal_);
Rooted<GlobalObject*> shg(cx, &selfHostingGlobal_->as<GlobalObject>());
/*
* During initialization of standard classes for the self-hosting global,
@ -756,7 +761,8 @@ JSRuntime::initSelfHosting(JSContext *cx)
ok = Evaluate(cx, shg, options, src, srcLen, &rv);
}
JS_SetErrorReporter(cx, oldReporter);
js::SetDefaultObjectForContext(cx, savedGlobal);
if (receivesDefaultObject)
js::SetDefaultObjectForContext(cx, savedGlobal);
return ok;
}