Back to index

lightning-sunbird  0.9+nobinonly
Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions
nsJSObjWrapper Class Reference

#include <nsJSNPRuntime.h>

Inheritance diagram for nsJSObjWrapper:
Inheritance graph
[legend]
Collaboration diagram for nsJSObjWrapper:
Collaboration graph
[legend]

List of all members.

Static Public Member Functions

static NPObjectGetNewOrUsed (NPP npp, JSContext *cx, JSObject *obj)

Public Attributes

NPClass_class
uint32_t referenceCount
JSObjectmJSObj
const NPP mNpp

Static Public Attributes

static NPClass sJSObjWrapperNPClass

Protected Member Functions

 nsJSObjWrapper (NPP npp)
 ~nsJSObjWrapper ()

Static Protected Member Functions

static NPObjectNP_Allocate (NPP npp, NPClass *aClass)
static void NP_Deallocate (NPObject *obj)
static void NP_Invalidate (NPObject *obj)
static bool NP_HasMethod (NPObject *, NPIdentifier identifier)
static bool NP_Invoke (NPObject *obj, NPIdentifier method, const NPVariant *args, uint32_t argCount, NPVariant *result)
static bool NP_InvokeDefault (NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
static bool NP_HasProperty (NPObject *obj, NPIdentifier property)
static bool NP_GetProperty (NPObject *obj, NPIdentifier property, NPVariant *result)
static bool NP_SetProperty (NPObject *obj, NPIdentifier property, const NPVariant *value)
static bool NP_RemoveProperty (NPObject *obj, NPIdentifier property)

Detailed Description

Definition at line 66 of file nsJSNPRuntime.h.


Constructor & Destructor Documentation

nsJSObjWrapper::nsJSObjWrapper ( NPP  npp) [protected]

Definition at line 445 of file nsJSNPRuntime.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 451 of file nsJSNPRuntime.cpp.

{
  // Invalidate first, since it relies on sJSRuntime and sJSObjWrappers.
  NP_Invalidate(this);

  OnWrapperDestroyed();
}

Here is the call graph for this function:


Member Function Documentation

NPObject * nsJSObjWrapper::GetNewOrUsed ( NPP  npp,
JSContext cx,
JSObject obj 
) [static]

Definition at line 791 of file nsJSNPRuntime.cpp.

{
  if (!npp) {
    NS_ERROR("Null NPP passed to nsJSObjWrapper::GetNewOrUsed()!");

    return nsnull;
  }

  if (!cx) {
    cx = GetJSContext(npp);

    if (!cx) {
      NS_ERROR("Unable to find a JSContext in "
               "nsJSObjWrapper::GetNewOrUsed()!");

      return nsnull;
    }
  }

  JSClass *clazz = JS_GET_CLASS(cx, obj);

  if (clazz == &sNPObjectJSWrapperClass) {
    // obj is one of our own, its private data is the NPObject we're
    // looking for.

    NPObject *npobj = (NPObject *)::JS_GetPrivate(cx, obj);

    return _retainobject(npobj);
  }

  if (!sJSObjWrappers.ops) {
    // No hash yet (or any more), initalize it.

    static PLDHashTableOps ops =
      {
        PL_DHashAllocTable,
        PL_DHashFreeTable,
        JSObjWrapperHashGetKey,
        JSObjWrapperHash,
        JSObjWrapperHashMatchEntry,
        PL_DHashMoveEntryStub,
        PL_DHashClearEntryStub,
        PL_DHashFinalizeStub
      };

    if (!PL_DHashTableInit(&sJSObjWrappers, &ops, nsnull,
                           sizeof(JSObjWrapperHashEntry), 16)) {
      NS_ERROR("Error initializing PLDHashTable!");

      return nsnull;
    }
  }

  nsJSObjWrapperKey key(obj, npp);

  JSObjWrapperHashEntry *entry =
    NS_STATIC_CAST(JSObjWrapperHashEntry *,
                   PL_DHashTableOperate(&sJSObjWrappers, &key, PL_DHASH_ADD));
  if (!entry) {
    // Out of memory.
    return nsnull;
  }

  if (PL_DHASH_ENTRY_IS_BUSY(entry) && entry->mJSObjWrapper) {
    // Found a live nsJSObjWrapper, return it.

    return _retainobject(entry->mJSObjWrapper);
  }

  // No existing nsJSObjWrapper, create one.

  nsJSObjWrapper *wrapper =
    (nsJSObjWrapper *)_createobject(npp, &sJSObjWrapperNPClass);

  if (!wrapper) {
    // OOM? Remove the stale entry from the hash.

    PL_DHashTableRawRemove(&sJSObjWrappers, entry);

    return nsnull;
  }

  wrapper->mJSObj = obj;

  entry->mJSObjWrapper = wrapper;

  NS_ASSERTION(wrapper->mNpp == npp, "nsJSObjWrapper::mNpp not initialized!");

  // Root the JSObject, its lifetime is now tied to that of the
  // NPObject.
  if (!::JS_AddNamedRoot(cx, &wrapper->mJSObj, "nsJSObjWrapper::mJSObject")) {
    NS_ERROR("Failed to root JSObject!");

    _releaseobject(wrapper);

    PL_DHashTableRawRemove(&sJSObjWrappers, entry);

    return nsnull;
  }

  return wrapper;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NPObject * nsJSObjWrapper::NP_Allocate ( NPP  npp,
NPClass aClass 
) [static, protected]

Definition at line 461 of file nsJSNPRuntime.cpp.

{
  NS_ASSERTION(aClass == &sJSObjWrapperNPClass,
               "Huh, wrong class passed to NP_Allocate()!!!");

  return new nsJSObjWrapper(npp);
}

Here is the call graph for this function:

void nsJSObjWrapper::NP_Deallocate ( NPObject obj) [static, protected]

Definition at line 471 of file nsJSNPRuntime.cpp.

{
  // nsJSObjWrapper::~nsJSObjWrapper() will call NP_Invalidate().
  delete (nsJSObjWrapper *)npobj;
}
bool nsJSObjWrapper::NP_GetProperty ( NPObject obj,
NPIdentifier  property,
NPVariant result 
) [static, protected]

Definition at line 662 of file nsJSNPRuntime.cpp.

{
  NPP npp = NPPStack::Peek();
  JSContext *cx = GetJSContext(npp);

  if (!cx || !npobj)
    return PR_FALSE;

  nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;

  AutoCXPusher pusher(cx);

  jsval v;
  return (GetProperty(cx, npjsobj->mJSObj, identifier, &v) &&
          JSValToNPVariant(npp, cx, v, result));
}

Here is the call graph for this function:

bool nsJSObjWrapper::NP_HasMethod ( NPObject npobj,
NPIdentifier  identifier 
) [static, protected]

Definition at line 520 of file nsJSNPRuntime.cpp.

{
  NPP npp = NPPStack::Peek();
  JSContext *cx = GetJSContext(npp);

  if (!cx || !npobj) {
    return PR_FALSE;
  }

  nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
  jsval v;
  JSBool ok = GetProperty(cx, npjsobj->mJSObj, identifier, &v);

  return ok && !JSVAL_IS_PRIMITIVE(v) &&
    ::JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(v));
}

Here is the call graph for this function:

bool nsJSObjWrapper::NP_HasProperty ( NPObject obj,
NPIdentifier  property 
) [static, protected]

Definition at line 631 of file nsJSNPRuntime.cpp.

{
  NPP npp = NPPStack::Peek();
  JSContext *cx = GetJSContext(npp);

  if (!cx || !npobj) {
    // XXX: Throw null ptr exception

    return PR_FALSE;
  }

  nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
  jsval id = (jsval)identifier;
  JSBool found, ok = JS_FALSE;

  if (JSVAL_IS_STRING(id)) {
    JSString *str = JSVAL_TO_STRING(id);

    ok = ::JS_HasUCProperty(cx, npjsobj->mJSObj, ::JS_GetStringChars(str),
                            ::JS_GetStringLength(str), &found);
  } else {
    NS_ASSERTION(JSVAL_IS_INT(id), "id must be either string or int!\n");

    ok = ::JS_HasElement(cx, npjsobj->mJSObj, JSVAL_TO_INT(id), &found);
  }

  return ok && found;
}

Here is the call graph for this function:

void nsJSObjWrapper::NP_Invalidate ( NPObject obj) [static, protected]

Definition at line 479 of file nsJSNPRuntime.cpp.

{
  nsJSObjWrapper *jsnpobj = (nsJSObjWrapper *)npobj;

  if (jsnpobj && jsnpobj->mJSObj) {
    // Unroot the object's JSObject
    ::JS_RemoveRootRT(sJSRuntime, &jsnpobj->mJSObj);

    if (sJSObjWrappers.ops) {
      // Remove the wrapper from the hash

      nsJSObjWrapperKey key(jsnpobj->mJSObj, jsnpobj->mNpp);
      PL_DHashTableOperate(&sJSObjWrappers, &key, PL_DHASH_REMOVE);
    }

    // Forget our reference to the JSObject.
    jsnpobj->mJSObj = nsnull;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool nsJSObjWrapper::NP_Invoke ( NPObject obj,
NPIdentifier  method,
const NPVariant args,
uint32_t  argCount,
NPVariant result 
) [static, protected]

Definition at line 610 of file nsJSNPRuntime.cpp.

{
  if ((jsval)method == JSVAL_VOID) {
    return PR_FALSE;
  }

  return doInvoke(npobj, method, args, argCount, result);
}

Here is the call graph for this function:

bool nsJSObjWrapper::NP_InvokeDefault ( NPObject obj,
const NPVariant args,
uint32_t  argCount,
NPVariant result 
) [static, protected]

Definition at line 623 of file nsJSNPRuntime.cpp.

{
  return doInvoke(npobj, (NPIdentifier)JSVAL_VOID, args, argCount, result);
}

Here is the call graph for this function:

bool nsJSObjWrapper::NP_RemoveProperty ( NPObject obj,
NPIdentifier  property 
) [static, protected]

Definition at line 718 of file nsJSNPRuntime.cpp.

{
  NPP npp = NPPStack::Peek();
  JSContext *cx = GetJSContext(npp);

  if (!cx || !npobj)
    return PR_FALSE;

  nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
  jsval id = (jsval)identifier;
  JSBool ok = JS_FALSE;

  AutoCXPusher pusher(cx);

  if (JSVAL_IS_STRING(id)) {
    JSString *str = JSVAL_TO_STRING(id);

    jsval unused;
    ok = ::JS_DeleteUCProperty2(cx, npjsobj->mJSObj, ::JS_GetStringChars(str),
                                ::JS_GetStringLength(str), &unused);
  } else {
    NS_ASSERTION(JSVAL_IS_INT(id), "id must be either string or int!\n");

    ok = ::JS_DeleteElement(cx, npjsobj->mJSObj, JSVAL_TO_INT(id));
  }

  // return ok == JS_TRUE to quiet down compiler warning, even if
  // return ok is what we really want.
  return ok == JS_TRUE;
}

Here is the call graph for this function:

bool nsJSObjWrapper::NP_SetProperty ( NPObject obj,
NPIdentifier  property,
const NPVariant value 
) [static, protected]

Definition at line 682 of file nsJSNPRuntime.cpp.

{
  NPP npp = NPPStack::Peek();
  JSContext *cx = GetJSContext(npp);

  if (!cx || !npobj)
    return PR_FALSE;

  nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
  jsval id = (jsval)identifier;
  JSBool ok = JS_FALSE;

  AutoCXPusher pusher(cx);

  jsval v = NPVariantToJSVal(npp, cx, value);
  JSAutoTempValueRooter tvr(cx, v);

  if (JSVAL_IS_STRING(id)) {
    JSString *str = JSVAL_TO_STRING(id);

    ok = ::JS_SetUCProperty(cx, npjsobj->mJSObj, ::JS_GetStringChars(str),
                            ::JS_GetStringLength(str), &v);
  } else {
    NS_ASSERTION(JSVAL_IS_INT(id), "id must be either string or int!\n");

    ok = ::JS_SetElement(cx, npjsobj->mJSObj, JSVAL_TO_INT(id), &v);
  }

  // return ok == JS_TRUE to quiet down compiler warning, even if
  // return ok is what we really want.
  return ok == JS_TRUE;
}

Here is the call graph for this function:


Member Data Documentation

NPClass* NPObject::_class [inherited]

Definition at line 332 of file npruntime.h.

Definition at line 61 of file nsJSNPRuntime.h.

Definition at line 63 of file nsJSNPRuntime.h.

Definition at line 333 of file npruntime.h.


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