Bug 1107639 - Use PersistentRooted rather than Add/RemoveRoot API is JSAPI tests r=terrence

This commit is contained in:
Jon Coppeard 2015-01-23 10:23:57 +00:00
parent b3238a366a
commit 9ecd974751
5 changed files with 26 additions and 62 deletions

View File

@ -25,8 +25,8 @@ static const JSClass global_class = {
JS_GlobalObjectTraceHook
};
static JS::Heap<JSObject *> trusted_glob;
static JS::Heap<JSObject *> trusted_fun;
static JS::PersistentRootedObject trusted_glob;
static JS::PersistentRootedObject trusted_fun;
static bool
CallTrusted(JSContext *cx, unsigned argc, jsval *vp)
@ -50,12 +50,10 @@ BEGIN_TEST(testChromeBuffer)
{
JS_SetTrustedPrincipals(rt, &system_principals);
trusted_glob = JS_NewGlobalObject(cx, &global_class, &system_principals, JS::FireOnNewGlobalHook);
trusted_glob.init(cx, JS_NewGlobalObject(cx, &global_class, &system_principals,
JS::FireOnNewGlobalHook));
CHECK(trusted_glob);
if (!JS::AddNamedObjectRoot(cx, &trusted_glob, "trusted-global"))
return false;
JS::RootedFunction fun(cx);
/*
@ -68,16 +66,13 @@ BEGIN_TEST(testChromeBuffer)
JSAutoCompartment ac(cx, trusted_glob);
const char *paramName = "x";
const char *bytes = "return x ? 1 + trusted(x-1) : 0";
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(trusted_glob.unsafeGet());
JS::CompileOptions options(cx);
options.setFileAndLine("", 0);
JS::AutoObjectVector emptyScopeChain(cx);
CHECK(JS::CompileFunction(cx, emptyScopeChain, options, "trusted",
1, &paramName, bytes, strlen(bytes), &fun));
CHECK(JS_DefineProperty(cx, global, "trusted", fun, JSPROP_ENUMERATE));
trusted_fun = JS_GetFunctionObject(fun);
if (!JS::AddNamedObjectRoot(cx, &trusted_fun, "trusted-function"))
return false;
CHECK(JS_DefineProperty(cx, trusted_glob, "trusted", fun, JSPROP_ENUMERATE));
trusted_fun.init(cx, JS_GetFunctionObject(fun));
}
JS::RootedValue v(cx, JS::ObjectValue(*trusted_fun));
@ -123,13 +118,12 @@ BEGIN_TEST(testChromeBuffer)
" return 'From trusted: ' + "
" e.name + ': ' + e.message; "
"} ";
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(trusted_glob.unsafeGet());
JS::CompileOptions options(cx);
options.setFileAndLine("", 0);
JS::AutoObjectVector emptyScopeChain(cx);
CHECK(JS::CompileFunction(cx, emptyScopeChain, options, "trusted",
1, &paramName, bytes, strlen(bytes), &fun));
CHECK(JS_DefineProperty(cx, global, "trusted", fun, JSPROP_ENUMERATE));
CHECK(JS_DefineProperty(cx, trusted_glob, "trusted", fun, JSPROP_ENUMERATE));
trusted_fun = JS_GetFunctionObject(fun);
}
@ -164,13 +158,12 @@ BEGIN_TEST(testChromeBuffer)
{
JSAutoCompartment ac(cx, trusted_glob);
const char *bytes = "return 42";
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(trusted_glob.unsafeGet());
JS::CompileOptions options(cx);
options.setFileAndLine("", 0);
JS::AutoObjectVector emptyScopeChain(cx);
CHECK(JS::CompileFunction(cx, emptyScopeChain, options, "trusted",
0, nullptr, bytes, strlen(bytes), &fun));
CHECK(JS_DefineProperty(cx, global, "trusted", fun, JSPROP_ENUMERATE));
CHECK(JS_DefineProperty(cx, trusted_glob, "trusted", fun, JSPROP_ENUMERATE));
trusted_fun = JS_GetFunctionObject(fun);
}
@ -198,11 +191,4 @@ BEGIN_TEST(testChromeBuffer)
return true;
}
virtual void uninit() MOZ_OVERRIDE {
trusted_glob = nullptr;
trusted_fun = nullptr;
JS::RemoveObjectRoot(cx, &trusted_glob);
JS::RemoveObjectRoot(cx, &trusted_fun);
JSAPITest::uninit();
}
END_TEST(testChromeBuffer)

View File

@ -9,25 +9,17 @@
BEGIN_TEST(testNullRoot)
{
obj = nullptr;
CHECK(JS::AddObjectRoot(cx, &obj));
str = nullptr;
CHECK(JS::AddStringRoot(cx, &str));
script = nullptr;
CHECK(JS::AddNamedScriptRoot(cx, &script, "testNullRoot's script"));
obj.init(cx, nullptr);
str.init(cx, nullptr);
script.init(cx, nullptr);
// This used to crash because obj was nullptr.
JS_GC(cx->runtime());
JS::RemoveObjectRoot(cx, &obj);
JS::RemoveStringRoot(cx, &str);
JS::RemoveScriptRoot(cx, &script);
return true;
}
JS::Heap<JSObject *> obj;
JS::Heap<JSString *> str;
JS::Heap<JSScript *> script;
JS::PersistentRootedObject obj;
JS::PersistentRootedString str;
JS::PersistentRootedScript script;
END_TEST(testNullRoot)

View File

@ -25,13 +25,9 @@ BEGIN_TEST(testResolveRecursion)
my_resolve
};
obj1 = obj2 = nullptr;
JS::AddObjectRoot(cx, &obj1);
JS::AddObjectRoot(cx, &obj2);
obj1 = JS_NewObject(cx, &my_resolve_class, JS::NullPtr(), JS::NullPtr());
obj1.init(cx, JS_NewObject(cx, &my_resolve_class, JS::NullPtr(), JS::NullPtr()));
CHECK(obj1);
obj2 = JS_NewObject(cx, &my_resolve_class, JS::NullPtr(), JS::NullPtr());
obj2.init(cx, JS_NewObject(cx, &my_resolve_class, JS::NullPtr(), JS::NullPtr()));
CHECK(obj2);
JS_SetPrivate(obj1, this);
JS_SetPrivate(obj2, this);
@ -53,13 +49,11 @@ BEGIN_TEST(testResolveRecursion)
obj1 = nullptr;
obj2 = nullptr;
JS::RemoveObjectRoot(cx, &obj1);
JS::RemoveObjectRoot(cx, &obj2);
return true;
}
JS::Heap<JSObject *> obj1;
JS::Heap<JSObject *> obj2;
JS::PersistentRootedObject obj1;
JS::PersistentRootedObject obj2;
int resolveEntryCount;
int resolveExitCount;

View File

@ -21,7 +21,8 @@ bool JSAPITest::init()
if (!cx)
return false;
JS_BeginRequest(cx);
JS::RootedObject global(cx, createGlobal());
global.init(rt);
createGlobal();
if (!global)
return false;
JS_EnterCompartment(cx, global);
@ -36,7 +37,6 @@ void JSAPITest::uninit()
}
if (global) {
JS_LeaveCompartment(cx, nullptr);
JS::RemoveObjectRoot(cx, &global);
global = nullptr;
}
if (cx) {
@ -53,7 +53,6 @@ void JSAPITest::uninit()
bool JSAPITest::exec(const char *bytes, const char *filename, int lineno)
{
JS::RootedValue v(cx);
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet());
JS::CompileOptions opts(cx);
opts.setFileAndLine(filename, lineno);
return JS::Evaluate(cx, global, opts, bytes, strlen(bytes), &v) ||
@ -63,7 +62,6 @@ bool JSAPITest::exec(const char *bytes, const char *filename, int lineno)
bool JSAPITest::evaluate(const char *bytes, const char *filename, int lineno,
JS::MutableHandleValue vp)
{
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet());
JS::CompileOptions opts(cx);
opts.setFileAndLine(filename, lineno);
return JS::Evaluate(cx, global, opts, bytes, strlen(bytes), vp) ||
@ -72,7 +70,6 @@ bool JSAPITest::evaluate(const char *bytes, const char *filename, int lineno,
bool JSAPITest::definePrint()
{
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet());
return JS_DefineFunction(cx, global, "print", (JSNative) print, 0, 0);
}
@ -84,16 +81,13 @@ JSObject * JSAPITest::createGlobal(JSPrincipals *principals)
global = JS_NewGlobalObject(cx, getGlobalClass(), principals, JS::FireOnNewGlobalHook, options);
if (!global)
return nullptr;
JS::AddNamedObjectRoot(cx, &global, "test-global");
JS::HandleObject globalHandle = JS::HandleObject::fromMarkedLocation(global.unsafeGet());
JSAutoCompartment ac(cx, globalHandle);
JSAutoCompartment ac(cx, global);
/* Populate the global object with the standard globals, like Object and
Array. */
if (!JS_InitStandardClasses(cx, globalHandle)) {
if (!JS_InitStandardClasses(cx, global))
global = nullptr;
JS::RemoveObjectRoot(cx, &global);
}
return global;
}
@ -124,8 +118,7 @@ int main(int argc, char *argv[])
continue;
}
JS::HandleObject global = JS::HandleObject::fromMarkedLocation(test->global.unsafeGet());
if (test->run(global)) {
if (test->run(test->global)) {
printf("TEST-PASS | %s | ok\n", name);
} else {
JSAPITestString messages = test->messages();

View File

@ -56,13 +56,12 @@ class JSAPITest
JSRuntime *rt;
JSContext *cx;
JS::Heap<JSObject *> global;
JS::PersistentRootedObject global;
bool knownFail;
JSAPITestString msgs;
JSCompartment *oldCompartment;
JSAPITest() : rt(nullptr), cx(nullptr), global(nullptr),
knownFail(false), oldCompartment(nullptr) {
JSAPITest() : rt(nullptr), cx(nullptr), knownFail(false), oldCompartment(nullptr) {
next = list;
list = this;
}