Back to index

lightning-sunbird  0.9+nobinonly
Static Public Member Functions
nsJSUtils Class Reference

#include <nsJSUtils.h>

List of all members.

Static Public Member Functions

static JSBool GetCallingLocation (JSContext *aContext, const char **aFilename, PRUint32 *aLineno, nsIPrincipal *aPrincipal)
 This is not a generated file.
static jsval ConvertStringToJSVal (const nsString &aProp, JSContext *aContext)
static PRBool ConvertJSValToXPCObject (nsISupports **aSupports, REFNSIID aIID, JSContext *aContext, jsval aValue)
static void ConvertJSValToString (nsAString &aString, JSContext *aContext, jsval aValue)
static PRBool ConvertJSValToUint32 (PRUint32 *aProp, JSContext *aContext, jsval aValue)
static nsIScriptGlobalObjectGetStaticScriptGlobal (JSContext *aContext, JSObject *aObj)
static nsIScriptContextGetStaticScriptContext (JSContext *aContext, JSObject *aObj)
static nsIScriptGlobalObjectGetDynamicScriptGlobal (JSContext *aContext)
static nsIScriptContextGetDynamicScriptContext (JSContext *aContext)

Detailed Description

Definition at line 60 of file nsJSUtils.h.


Member Function Documentation

void nsJSUtils::ConvertJSValToString ( nsAString &  aString,
JSContext aContext,
jsval  aValue 
) [static]

Definition at line 158 of file nsJSUtils.cpp.

{
  JSString *jsstring;
  if ((jsstring = ::JS_ValueToString(aContext, aValue)) != nsnull) {
    aString.Assign(NS_REINTERPRET_CAST(const PRUnichar*,
                                       ::JS_GetStringChars(jsstring)),
                   ::JS_GetStringLength(jsstring));
  }
  else {
    aString.Truncate();
  }
}

Here is the call graph for this function:

PRBool nsJSUtils::ConvertJSValToUint32 ( PRUint32 aProp,
JSContext aContext,
jsval  aValue 
) [static]

Definition at line 173 of file nsJSUtils.cpp.

{
  uint32 temp;
  if (::JS_ValueToECMAUint32(aContext, aValue, &temp)) {
    *aProp = (PRUint32)temp;
  }
  else {
    ::JS_ReportError(aContext, "Parameter must be an integer");
    return JS_FALSE;
  }

  return JS_TRUE;
}

Here is the call graph for this function:

PRBool nsJSUtils::ConvertJSValToXPCObject ( nsISupports **  aSupports,
REFNSIID  aIID,
JSContext aContext,
jsval  aValue 
) [static]

Definition at line 138 of file nsJSUtils.cpp.

{
  *aSupports = nsnull;
  if (JSVAL_IS_NULL(aValue)) {
    return JS_TRUE;
  }

  if (JSVAL_IS_OBJECT(aValue)) {
    // WrapJS does all the work to recycle an existing wrapper and/or do a QI
    nsresult rv = nsContentUtils::XPConnect()->
      WrapJS(aContext, JSVAL_TO_OBJECT(aValue), aIID, (void**)aSupports);

    return NS_SUCCEEDED(rv);
  }

  return JS_FALSE;
}

Here is the call graph for this function:

jsval nsJSUtils::ConvertStringToJSVal ( const nsString aProp,
JSContext aContext 
) [static]

Definition at line 126 of file nsJSUtils.cpp.

{
  JSString *jsstring =
    ::JS_NewUCStringCopyN(aContext, NS_REINTERPRET_CAST(const jschar*,
                                                        aProp.get()),
                          aProp.Length());

  // set the return value
  return STRING_TO_JSVAL(jsstring);
}

Here is the call graph for this function:

JSBool nsJSUtils::GetCallingLocation ( JSContext aContext,
const char **  aFilename,
PRUint32 aLineno,
nsIPrincipal aPrincipal 
) [static]

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.

Definition at line 66 of file nsJSUtils.cpp.

{
  // Get the current filename and line number
  JSStackFrame* frame = nsnull;
  JSScript* script = nsnull;
  do {
    frame = ::JS_FrameIterator(aContext, &frame);

    if (frame) {
      script = ::JS_GetFrameScript(aContext, frame);
    }
  } while (frame && !script);

  if (script) {
    // If aPrincipals is non-null then our caller is asking us to ensure
    // that the filename we return does not have elevated privileges.
    if (aPrincipal) {
      uint32 flags = JS_GetScriptFilenameFlags(script);

      // Use the principal for the filename if it shouldn't be receiving
      // implicit XPCNativeWrappers.
      if (flags & JSFILENAME_SYSTEM) {
        nsIScriptSecurityManager *ssm = nsContentUtils::GetSecurityManager();

        nsCOMPtr<nsIPrincipal> sysPrin;
        ssm->GetSystemPrincipal(getter_AddRefs(sysPrin));

        if (aPrincipal != sysPrin) {
          JSPrincipals* jsprins;
          aPrincipal->GetJSPrincipals(aContext, &jsprins);

          *aFilename = jsprins->codebase;
          *aLineno = 0;
          JSPRINCIPALS_DROP(aContext, jsprins);
          return JS_TRUE;
        }
      }
    }

    const char* filename = ::JS_GetScriptFilename(aContext, script);

    if (filename) {
      PRUint32 lineno = 0;
      jsbytecode* bytecode = ::JS_GetFramePC(aContext, frame);

      if (bytecode) {
        lineno = ::JS_PCToLineNumber(aContext, script, bytecode);
      }

      *aFilename = filename;
      *aLineno = lineno;
      return JS_TRUE;
    }
  }

  return JS_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 241 of file nsJSUtils.cpp.

{
  return GetScriptContextFromJSContext(aContext);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 232 of file nsJSUtils.cpp.

{
  nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
  if (!scriptCX)
    return nsnull;
  return scriptCX->GetGlobalObject();
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsIScriptContext * nsJSUtils::GetStaticScriptContext ( JSContext aContext,
JSObject aObj 
) [static]

Definition at line 222 of file nsJSUtils.cpp.

{
  nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
  if (!nativeGlobal)
    return nsnull;

  return nativeGlobal->GetContext();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 189 of file nsJSUtils.cpp.

{
  nsISupports* supports;
  JSClass* clazz;
  JSObject* parent;
  JSObject* glob = aObj; // starting point for search

  if (!glob)
    return nsnull;

  while ((parent = ::JS_GetParent(aContext, glob)))
    glob = parent;

  clazz = JS_GET_CLASS(aContext, glob);

  if (!clazz ||
      !(clazz->flags & JSCLASS_HAS_PRIVATE) ||
      !(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
      !(supports = (nsISupports*)::JS_GetPrivate(aContext, glob))) {
    return nsnull;
  }

  nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
  NS_ENSURE_TRUE(wrapper, nsnull);

  nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryWrappedNative(wrapper));

  // We're returning a pointer to something that's about to be
  // released, but that's ok here.
  return sgo;
}

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files: