js/xpconnect/src/XPCJSContextStack.cpp
author Jan de Mooij <jdemooij@mozilla.com>
Wed, 22 Jun 2016 09:47:52 +0200
changeset 344439 0ca871e39a20d94c5c8948beb41867d679f3709e
parent 343592 b974e754526b67fbe66f1a1161ce5b8af3292a62
child 344514 5fbb064a7bcbfe3af8f0e8a605331b8cdc684716
permissions -rw-r--r--
Bug 1279295 - Create the runtime's JSContext when we create the runtime. r=luke

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Implement global service to track stack of JSContext. */

#include "xpcprivate.h"
#include "XPCWrapper.h"
#include "nsDOMJSUtils.h"
#include "nsNullPrincipal.h"
#include "mozilla/dom/BindingUtils.h"

using namespace mozilla;
using namespace JS;
using namespace xpc;
using mozilla::dom::DestroyProtoAndIfaceCache;

/***************************************************************************/

XPCJSContextStack::~XPCJSContextStack()
{
    if (mSafeJSContext) {
        delete XPCContext::GetXPCContext(mSafeJSContext);
        mSafeJSContext = nullptr;
    }
}

void
XPCJSContextStack::Pop()
{
    MOZ_ASSERT(!mStack.IsEmpty());

    uint32_t idx = mStack.Length() - 1; // The thing we're popping

    mStack.RemoveElementAt(idx);
    JSContext* newTop;
    // We _could_ probably use mStack.SafeElementAt(idx-1, nullptr) here....
    if (idx == 0) {
        newTop = nullptr;
    } else {
        newTop = mStack[idx-1];
    }
    js::Debug_SetActiveJSContext(mRuntime->Runtime(), newTop);
}

void
XPCJSContextStack::Push(JSContext* cx)
{
    js::Debug_SetActiveJSContext(mRuntime->Runtime(), cx);
    mStack.AppendElement(cx);
}

JSContext*
XPCJSContextStack::GetSafeJSContext()
{
    MOZ_ASSERT(mSafeJSContext);
    return mSafeJSContext;
}

void
XPCJSContextStack::InitSafeJSContext()
{
    MOZ_ASSERT(!mSafeJSContext);

    mSafeJSContext = JS_GetContext(mRuntime->Runtime());
    if (!JS::InitSelfHostedCode(mSafeJSContext))
        MOZ_CRASH("InitSelfHostedCode failed");

    if (!mRuntime->InitXPCContext(mSafeJSContext))
        MOZ_CRASH("InitXPCContext failed");
}