gecko/dom/base/nsJSUtils.cpp
Ehsan Akhgari 8c296bbcd4 Bug 579517 - Part 1: Automated conversion of NSPR numeric types to stdint types in Gecko; r=bsmedberg
This patch was generated by a script.  Here's the source of the script for
future reference:

function convert() {
echo "Converting $1 to $2..."
find . ! -wholename "*nsprpub*" \
       ! -wholename "*security/nss*" \
       ! -wholename "*/.hg*" \
       ! -wholename "obj-ff-dbg*" \
       ! -name nsXPCOMCID.h \
       ! -name prtypes.h \
         -type f \
      \( -iname "*.cpp" \
         -o -iname "*.h" \
         -o -iname "*.c" \
         -o -iname "*.cc" \
         -o -iname "*.idl" \
         -o -iname "*.ipdl" \
         -o -iname "*.ipdlh" \
         -o -iname "*.mm" \) | \
    xargs -n 1 sed -i -e "s/\b$1\b/$2/g"
}

convert PRInt8 int8_t
convert PRUint8 uint8_t
convert PRInt16 int16_t
convert PRUint16 uint16_t
convert PRInt32 int32_t
convert PRUint32 uint32_t
convert PRInt64 int64_t
convert PRUint64 uint64_t

convert PRIntn int
convert PRUintn unsigned

convert PRSize size_t

convert PROffset32 int32_t
convert PROffset64 int64_t

convert PRPtrdiff ptrdiff_t

convert PRFloat64 double
2012-08-22 11:56:38 -04:00

137 lines
3.9 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et tw=78: */
/* 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/. */
/**
* This is not a generated file. It contains common utility functions
* invoked from the JavaScript code generated from IDL interfaces.
* The goal of the utility functions is to cut down on the size of
* the generated code itself.
*/
#include "nsJSUtils.h"
#include "jsapi.h"
#include "jsdbgapi.h"
#include "prprf.h"
#include "nsIScriptContext.h"
#include "nsIScriptGlobalObject.h"
#include "nsIServiceManager.h"
#include "nsIXPConnect.h"
#include "nsCOMPtr.h"
#include "nsIScriptSecurityManager.h"
#include "nsPIDOMWindow.h"
#include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
#include "mozilla/dom/BindingUtils.h"
JSBool
nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
uint32_t* aLineno)
{
JSScript* script = nullptr;
unsigned lineno = 0;
if (!JS_DescribeScriptedCaller(aContext, &script, &lineno)) {
return JS_FALSE;
}
*aFilename = ::JS_GetScriptFilename(aContext, script);
*aLineno = lineno;
return JS_TRUE;
}
nsIScriptGlobalObject *
nsJSUtils::GetStaticScriptGlobal(JSContext* aContext, JSObject* aObj)
{
JSClass* clazz;
JSObject* glob = aObj; // starting point for search
if (!glob)
return nullptr;
glob = JS_GetGlobalForObject(aContext, glob);
NS_ABORT_IF_FALSE(glob, "Infallible returns null");
clazz = JS_GetClass(glob);
// Whenever we end up with globals that are JSCLASS_IS_DOMJSCLASS
// and have an nsISupports DOM object, we will need to modify this
// check here.
MOZ_ASSERT(!(clazz->flags & JSCLASS_IS_DOMJSCLASS));
nsISupports* supports;
if (!(clazz->flags & JSCLASS_HAS_PRIVATE) ||
!(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
!(supports = (nsISupports*)::JS_GetPrivate(glob))) {
return nullptr;
}
// We might either have a window directly (e.g. if the global is a
// sandbox whose script object principal pointer is a window), or an
// XPCWrappedNative for a window. We could also have other
// sandbox-related script object principals, but we can't do much
// about those short of trying to walk the proto chain of |glob|
// looking for a window or something.
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(supports));
if (!sgo) {
nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
NS_ENSURE_TRUE(wrapper, nullptr);
sgo = do_QueryWrappedNative(wrapper);
}
// We're returning a pointer to something that's about to be
// released, but that's ok here.
return sgo;
}
nsIScriptContext *
nsJSUtils::GetStaticScriptContext(JSContext* aContext, JSObject* aObj)
{
nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
if (!nativeGlobal)
return nullptr;
return nativeGlobal->GetScriptContext();
}
nsIScriptGlobalObject *
nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
{
nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
if (!scriptCX)
return nullptr;
return scriptCX->GetGlobalObject();
}
nsIScriptContext *
nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
{
return GetScriptContextFromJSContext(aContext);
}
uint64_t
nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext)
{
if (!aContext)
return 0;
uint64_t innerWindowID = 0;
JSObject *jsGlobal = JS_GetGlobalForScopeChain(aContext);
if (jsGlobal) {
nsIScriptGlobalObject *scriptGlobal = GetStaticScriptGlobal(aContext,
jsGlobal);
if (scriptGlobal) {
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(scriptGlobal);
if (win)
innerWindowID = win->WindowID();
}
}
return innerWindowID;
}