Skip to content

Commit

Permalink
Bug 1286795 part 6 - Replace JS_NewRuntime/JS_DestroyRuntime with JS_…
Browse files Browse the repository at this point in the history
…NewContext/JS_DestroyContext. r=luke,bz
  • Loading branch information
jandem committed Jul 23, 2016
1 parent d5c8efb commit 8746191
Show file tree
Hide file tree
Showing 26 changed files with 230 additions and 250 deletions.
108 changes: 52 additions & 56 deletions dom/indexedDB/ActorsParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3674,13 +3674,13 @@ UpgradeSchemaFrom18_0To19_0(mozIStorageConnection* aConnection)

#if !defined(MOZ_B2G)

class NormalJSRuntime;
class NormalJSContext;

class UpgradeFileIdsFunction final
: public mozIStorageFunction
{
RefPtr<FileManager> mFileManager;
nsAutoPtr<NormalJSRuntime> mRuntime;
nsAutoPtr<NormalJSContext> mContext;

public:
UpgradeFileIdsFunction()
Expand Down Expand Up @@ -7832,7 +7832,7 @@ class CreateIndexOp final
{
friend class VersionChangeTransaction;

class ThreadLocalJSRuntime;
class ThreadLocalJSContext;
class UpdateIndexDataValuesFunction;

static const unsigned int kBadThreadLocalIndex =
Expand Down Expand Up @@ -7868,19 +7868,18 @@ class CreateIndexOp final
DoDatabaseWork(DatabaseConnection* aConnection) override;
};

class NormalJSRuntime
class NormalJSContext
{
friend class nsAutoPtr<NormalJSRuntime>;
friend class nsAutoPtr<NormalJSContext>;

static const JSClass sGlobalClass;
static const uint32_t kRuntimeHeapSize = 768 * 1024;
static const uint32_t kContextHeapSize = 768 * 1024;

JSRuntime* mRuntime;
JSContext* mContext;
JSObject* mGlobal;

public:
static NormalJSRuntime*
static NormalJSContext*
Create();

JSContext*
Expand All @@ -7896,46 +7895,45 @@ class NormalJSRuntime
}

protected:
NormalJSRuntime()
: mRuntime(nullptr)
, mContext(nullptr)
NormalJSContext()
: mContext(nullptr)
, mGlobal(nullptr)
{
MOZ_COUNT_CTOR(NormalJSRuntime);
MOZ_COUNT_CTOR(NormalJSContext);
}

~NormalJSRuntime()
~NormalJSContext()
{
MOZ_COUNT_DTOR(NormalJSRuntime);
MOZ_COUNT_DTOR(NormalJSContext);

if (mRuntime) {
JS_DestroyRuntime(mRuntime);
if (mContext) {
JS_DestroyContext(mContext);
}
}

bool
Init();
};

class CreateIndexOp::ThreadLocalJSRuntime final
: public NormalJSRuntime
class CreateIndexOp::ThreadLocalJSContext final
: public NormalJSContext
{
friend class CreateIndexOp;
friend class nsAutoPtr<ThreadLocalJSRuntime>;
friend class nsAutoPtr<ThreadLocalJSContext>;

public:
static ThreadLocalJSRuntime*
static ThreadLocalJSContext*
GetOrCreate();

private:
ThreadLocalJSRuntime()
ThreadLocalJSContext()
{
MOZ_COUNT_CTOR(CreateIndexOp::ThreadLocalJSRuntime);
MOZ_COUNT_CTOR(CreateIndexOp::ThreadLocalJSContext);
}

~ThreadLocalJSRuntime()
~ThreadLocalJSContext()
{
MOZ_COUNT_DTOR(CreateIndexOp::ThreadLocalJSRuntime);
MOZ_COUNT_DTOR(CreateIndexOp::ThreadLocalJSContext);
}
};

Expand Down Expand Up @@ -18751,13 +18749,13 @@ UpgradeFileIdsFunction::Init(nsIFile* aFMDirectory,
return rv;
}

nsAutoPtr<NormalJSRuntime> runtime(NormalJSRuntime::Create());
if (NS_WARN_IF(!runtime)) {
nsAutoPtr<NormalJSContext> context(NormalJSContext::Create());
if (NS_WARN_IF(!context)) {
return NS_ERROR_FAILURE;
}

mFileManager.swap(fileManager);
mRuntime = runtime;
mContext = context;
return NS_OK;
}

Expand All @@ -18770,7 +18768,7 @@ UpgradeFileIdsFunction::OnFunctionCall(mozIStorageValueArray* aArguments,
MOZ_ASSERT(aArguments);
MOZ_ASSERT(aResult);
MOZ_ASSERT(mFileManager);
MOZ_ASSERT(mRuntime);
MOZ_ASSERT(mContext);

PROFILER_LABEL("IndexedDB",
"UpgradeFileIdsFunction::OnFunctionCall",
Expand All @@ -18794,9 +18792,9 @@ UpgradeFileIdsFunction::OnFunctionCall(mozIStorageValueArray* aArguments,
mFileManager,
&cloneInfo);

JSContext* cx = mRuntime->Context();
JSContext* cx = mContext->Context();
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, mRuntime->Global());
JSAutoCompartment ac(cx, mContext->Global());

JS::Rooted<JS::Value> clone(cx);
if (NS_WARN_IF(!IDBObjectStore::DeserializeUpgradeValue(cx, cloneInfo,
Expand Down Expand Up @@ -23826,15 +23824,15 @@ CreateIndexOp::InsertDataFromObjectStore(DatabaseConnection* aConnection)
aConnection->GetStorageConnection();
MOZ_ASSERT(storageConnection);

ThreadLocalJSRuntime* runtime = ThreadLocalJSRuntime::GetOrCreate();
if (NS_WARN_IF(!runtime)) {
ThreadLocalJSContext* context = ThreadLocalJSContext::GetOrCreate();
if (NS_WARN_IF(!context)) {
IDB_REPORT_INTERNAL_ERR();
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}

JSContext* cx = runtime->Context();
JSContext* cx = context->Context();
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, runtime->Global());
JSAutoCompartment ac(cx, context->Global());

RefPtr<UpdateIndexDataValuesFunction> updateFunction =
new UpdateIndexDataValuesFunction(this, aConnection, cx);
Expand Down Expand Up @@ -23909,7 +23907,7 @@ CreateIndexOp::Init(TransactionBase* aTransaction)
static void
Destroy(void* aThreadLocal)
{
delete static_cast<ThreadLocalJSRuntime*>(aThreadLocal);
delete static_cast<ThreadLocalJSContext*>(aThreadLocal);
}
};

Expand Down Expand Up @@ -24069,7 +24067,7 @@ CreateIndexOp::DoDatabaseWork(DatabaseConnection* aConnection)
return NS_OK;
}

static const JSClassOps sNormalJSRuntimeGlobalClassOps = {
static const JSClassOps sNormalJSContextGlobalClassOps = {
/* addProperty */ nullptr,
/* delProperty */ nullptr,
/* getProperty */ nullptr,
Expand All @@ -24084,24 +24082,22 @@ static const JSClassOps sNormalJSRuntimeGlobalClassOps = {
/* trace */ JS_GlobalObjectTraceHook
};

const JSClass NormalJSRuntime::sGlobalClass = {
const JSClass NormalJSContext::sGlobalClass = {
"IndexedDBTransactionThreadGlobal",
JSCLASS_GLOBAL_FLAGS,
&sNormalJSRuntimeGlobalClassOps
&sNormalJSContextGlobalClassOps
};

bool
NormalJSRuntime::Init()
NormalJSContext::Init()
{
MOZ_ASSERT(!IsOnBackgroundThread());

mRuntime = JS_NewRuntime(kRuntimeHeapSize);
if (NS_WARN_IF(!mRuntime)) {
mContext = JS_NewContext(kContextHeapSize);
if (NS_WARN_IF(!mContext)) {
return false;
}

mContext = JS_GetContext(mRuntime);

// Not setting this will cause JS_CHECK_RECURSION to report false positives.
JS_SetNativeStackQuota(mContext, 128 * sizeof(size_t) * 1024);

Expand All @@ -24122,46 +24118,46 @@ NormalJSRuntime::Init()
}

// static
NormalJSRuntime*
NormalJSRuntime::Create()
NormalJSContext*
NormalJSContext::Create()
{
MOZ_ASSERT(!IsOnBackgroundThread());

nsAutoPtr<NormalJSRuntime> newRuntime(new NormalJSRuntime());
nsAutoPtr<NormalJSContext> newContext(new NormalJSContext());

if (NS_WARN_IF(!newRuntime->Init())) {
if (NS_WARN_IF(!newContext->Init())) {
return nullptr;
}

return newRuntime.forget();
return newContext.forget();
}

// static
auto
CreateIndexOp::
ThreadLocalJSRuntime::GetOrCreate() -> ThreadLocalJSRuntime*
ThreadLocalJSContext::GetOrCreate() -> ThreadLocalJSContext*
{
MOZ_ASSERT(!IsOnBackgroundThread());
MOZ_ASSERT(CreateIndexOp::kBadThreadLocalIndex !=
CreateIndexOp::sThreadLocalIndex);

auto* runtime = static_cast<ThreadLocalJSRuntime*>(
auto* context = static_cast<ThreadLocalJSContext*>(
PR_GetThreadPrivate(CreateIndexOp::sThreadLocalIndex));
if (runtime) {
return runtime;
if (context) {
return context;
}

nsAutoPtr<ThreadLocalJSRuntime> newRuntime(new ThreadLocalJSRuntime());
nsAutoPtr<ThreadLocalJSContext> newContext(new ThreadLocalJSContext());

if (NS_WARN_IF(!newRuntime->Init())) {
if (NS_WARN_IF(!newContext->Init())) {
return nullptr;
}

DebugOnly<PRStatus> status =
PR_SetThreadPrivate(CreateIndexOp::sThreadLocalIndex, newRuntime);
PR_SetThreadPrivate(CreateIndexOp::sThreadLocalIndex, newContext);
MOZ_ASSERT(status == PR_SUCCESS);

return newRuntime.forget();
return newContext.forget();
}

NS_IMPL_ISUPPORTS(CreateIndexOp::UpdateIndexDataValuesFunction,
Expand Down
21 changes: 9 additions & 12 deletions dom/workers/RuntimeService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,20 +875,17 @@ class WorkerJSRuntime : public mozilla::CycleCollectedJSRuntime
mWorkerPrivate = nullptr;
}

nsresult Initialize(JSRuntime* aParentRuntime)
nsresult Initialize(JSContext* aParentContext)
{
nsresult rv =
CycleCollectedJSRuntime::Initialize(aParentRuntime,
CycleCollectedJSRuntime::Initialize(aParentContext,
WORKER_DEFAULT_RUNTIME_HEAPSIZE,
WORKER_DEFAULT_NURSERY_SIZE);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

JSRuntime* rt = Runtime();
MOZ_ASSERT(rt);

JSContext* cx = JS_GetContext(rt);
JSContext* cx = Context();

JS_SetContextPrivate(cx, new WorkerThreadContextPrivate(mWorkerPrivate));

Expand Down Expand Up @@ -987,7 +984,7 @@ class WorkerThreadPrimaryRunnable final : public Runnable
{
WorkerPrivate* mWorkerPrivate;
RefPtr<WorkerThread> mThread;
JSRuntime* mParentRuntime;
JSContext* mParentContext;

class FinishedRunnable final : public Runnable
{
Expand All @@ -1012,8 +1009,8 @@ class WorkerThreadPrimaryRunnable final : public Runnable
public:
WorkerThreadPrimaryRunnable(WorkerPrivate* aWorkerPrivate,
WorkerThread* aThread,
JSRuntime* aParentRuntime)
: mWorkerPrivate(aWorkerPrivate), mThread(aThread), mParentRuntime(aParentRuntime)
JSContext* aParentContext)
: mWorkerPrivate(aWorkerPrivate), mThread(aThread), mParentContext(aParentContext)
{
MOZ_ASSERT(aWorkerPrivate);
MOZ_ASSERT(aThread);
Expand Down Expand Up @@ -1623,10 +1620,10 @@ RuntimeService::ScheduleWorker(WorkerPrivate* aWorkerPrivate)
NS_WARNING("Could not set the thread's priority!");
}

JSRuntime* rt = CycleCollectedJSRuntime::Get()->Runtime();
JSContext* cx = CycleCollectedJSRuntime::Get()->Context();
nsCOMPtr<nsIRunnable> runnable =
new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread,
JS_GetParentRuntime(rt));
JS_GetParentContext(cx));
if (NS_FAILED(thread->DispatchPrimaryRunnable(friendKey, runnable.forget()))) {
UnregisterWorker(aWorkerPrivate);
return false;
Expand Down Expand Up @@ -2561,7 +2558,7 @@ WorkerThreadPrimaryRunnable::Run()
nsCycleCollector_startup();

WorkerJSRuntime runtime(mWorkerPrivate);
nsresult rv = runtime.Initialize(mParentRuntime);
nsresult rv = runtime.Initialize(mParentContext);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
Expand Down
3 changes: 1 addition & 2 deletions js/src/gdb/gdb-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ int
main(int argc, const char** argv)
{
if (!JS_Init()) return 1;
JSRuntime* runtime = checkPtr(JS_NewRuntime(1024 * 1024));
JSContext* cx = JS_GetContext(runtime);
JSContext* cx = checkPtr(JS_NewContext(1024 * 1024));

JS_SetGCParameter(cx, JSGC_MAX_BYTES, 0xffffffff);
JS_SetNativeStackQuota(cx, 5000000);
Expand Down
2 changes: 1 addition & 1 deletion js/src/jsapi-tests/testAssemblerBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ BEGIN_TEST(testAssemblerBuffer_ARM64)
js::LifoAlloc lifo(4096);
TempAllocator alloc(&lifo);
JitContext jc(cx, &alloc);
rt->getJitRuntime(cx);
cx->getJitRuntime(cx);
MacroAssembler masm;

// Branches to an unbound label.
Expand Down
12 changes: 6 additions & 6 deletions js/src/jsapi-tests/testCloneScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ struct Principals final : public JSPrincipals

class AutoDropPrincipals
{
JSRuntime* rt;
JSContext* cx;
JSPrincipals* principals;

public:
AutoDropPrincipals(JSRuntime* rt, JSPrincipals* principals)
: rt(rt), principals(principals)
AutoDropPrincipals(JSContext* cx, JSPrincipals* principals)
: cx(cx), principals(principals)
{
JS_HoldPrincipals(principals);
}

~AutoDropPrincipals()
{
JS_DropPrincipals(JS_GetContext(rt), principals);
JS_DropPrincipals(cx, principals);
}
};

Expand All @@ -95,9 +95,9 @@ BEGIN_TEST(test_cloneScriptWithPrincipals)
JS_InitDestroyPrincipalsCallback(cx, DestroyPrincipals);

JSPrincipals* principalsA = new Principals();
AutoDropPrincipals dropA(rt, principalsA);
AutoDropPrincipals dropA(cx, principalsA);
JSPrincipals* principalsB = new Principals();
AutoDropPrincipals dropB(rt, principalsB);
AutoDropPrincipals dropB(cx, principalsB);

JS::RootedObject A(cx, createGlobal(principalsA));
JS::RootedObject B(cx, createGlobal(principalsB));
Expand Down
Loading

0 comments on commit 8746191

Please sign in to comment.