Bug 208121 Some String properties of a Java object are read as 'null' by Javascript

r=bsmedberg, sr=brendan, a=asa
Using nsDataHashtable instead of nsHashtable for caching class member.
This commit is contained in:
kyle.yuan%sun.com 2003-12-16 01:23:23 +00:00
Родитель 44327a20b7
Коммит babfece7ff
1 изменённых файлов: 75 добавлений и 20 удалений

Просмотреть файл

@ -49,6 +49,7 @@
#include "ProxyClassLoader.h" #include "ProxyClassLoader.h"
#include "ProxyJNI.h" #include "ProxyJNI.h"
#include "nsDataHashtable.h"
#ifdef DEBUG #ifdef DEBUG
#include "nsDebug.h" #include "nsDebug.h"
@ -222,6 +223,41 @@ jvalue* JNIMethod::marshallArgs(va_list args)
return jargs; return jargs;
} }
struct JavaClassMember {
jclass clazz;
void* memberID;
JavaClassMember(jclass cl, void* mID)
{ clazz = cl; memberID = mID;}
};
class JavaClassMemberKey : public PLDHashEntryHdr
{
public:
typedef const JavaClassMember& KeyType;
typedef const JavaClassMember* KeyTypePointer;
JavaClassMemberKey(KeyTypePointer aKey) : mValue(*aKey) { }
JavaClassMemberKey(const JavaClassMemberKey& toCopy) : mValue(toCopy.mValue) { }
~JavaClassMemberKey() { }
KeyType GetKey() const { return mValue; }
KeyTypePointer GetKeyPointer() const { return &mValue; }
PRBool KeyEquals(KeyTypePointer aKey) const { return aKey->clazz == mValue.clazz && aKey->memberID == mValue.memberID; }
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey)
{
PRUint32 v1 = NS_PTR_TO_INT32(aKey->clazz);
PRUint32 v2 = NS_PTR_TO_INT32(aKey->memberID);
return v1 ^ v2;
}
enum { ALLOW_MEMMOVE = PR_TRUE };
private:
const JavaClassMember mValue;
};
/** /**
* Marshalls a va_list into a jvalue array, and destructor automatically * Marshalls a va_list into a jvalue array, and destructor automatically
* deletes when the args go out of scope. * deletes when the args go out of scope.
@ -242,7 +278,7 @@ static jvalue kErrorValue;
class ProxyJNIEnv : public JNIEnv { class ProxyJNIEnv : public JNIEnv {
private: private:
static JNINativeInterface_ theFuncs; static JNINativeInterface_ theFuncs;
static nsHashtable* theIDTable; static nsDataHashtable<JavaClassMemberKey, void*>* theIDTable;
nsISecureEnv* mSecureEnv; nsISecureEnv* mSecureEnv;
nsISecurityContext* mContext; nsISecurityContext* mContext;
jbool mInProxyFindClass; jbool mInProxyFindClass;
@ -509,11 +545,13 @@ private:
jmethodID outMethodID = NULL; jmethodID outMethodID = NULL;
nsresult result = secureEnv->GetMethodID(clazz, name, sig, &outMethodID); nsresult result = secureEnv->GetMethodID(clazz, name, sig, &outMethodID);
if (result == NS_OK && outMethodID != NULL) { if (result == NS_OK && outMethodID != NULL) {
nsVoidKey key(outMethodID); JavaClassMember key(clazz, outMethodID);
JNIMethod* method = (JNIMethod*) theIDTable->Get(&key); JNIMethod* method;
if (method == NULL) { PRBool bFound = theIDTable && theIDTable->Get(key, (void **)&method);
if (!bFound) {
method = new JNIMethod(name, sig, outMethodID); method = new JNIMethod(name, sig, outMethodID);
theIDTable->Put(&key, method); if (theIDTable)
theIDTable->Put(key, method);
} }
outMethodID = jmethodID(method); outMethodID = jmethodID(method);
} }
@ -725,11 +763,13 @@ private:
jfieldID outFieldID = NULL; jfieldID outFieldID = NULL;
nsresult result = secureEnv->GetFieldID(clazz, name, sig, &outFieldID); nsresult result = secureEnv->GetFieldID(clazz, name, sig, &outFieldID);
if (result == NS_OK && outFieldID != NULL) { if (result == NS_OK && outFieldID != NULL) {
nsVoidKey key(outFieldID); JavaClassMember key(clazz, outFieldID);
JNIField* field = (JNIField*) theIDTable->Get(&key); JNIField* field;
if (field == NULL) { PRBool bFound = theIDTable && theIDTable->Get(key, (void **)&field);
if (!bFound) {
field = new JNIField(name, sig, outFieldID); field = new JNIField(name, sig, outFieldID);
theIDTable->Put(&key, field); if (theIDTable)
theIDTable->Put(key, field);
} }
outFieldID = jfieldID(field); outFieldID = jfieldID(field);
} }
@ -803,11 +843,13 @@ private:
jmethodID outMethodID = NULL; jmethodID outMethodID = NULL;
nsresult result = secureEnv->GetStaticMethodID(clazz, name, sig, &outMethodID); nsresult result = secureEnv->GetStaticMethodID(clazz, name, sig, &outMethodID);
if (result == NS_OK && outMethodID != NULL) { if (result == NS_OK && outMethodID != NULL) {
nsVoidKey key(outMethodID); JavaClassMember key(clazz, outMethodID);
JNIMethod* method = (JNIMethod*) theIDTable->Get(&key); JNIMethod* method;
if (method == NULL) { PRBool bFound = theIDTable && theIDTable->Get(key, (void **)&method);
if (!bFound) {
method = new JNIMethod(name, sig, outMethodID); method = new JNIMethod(name, sig, outMethodID);
theIDTable->Put(&key, method); if (theIDTable)
theIDTable->Put(key, method);
} }
outMethodID = jmethodID(method); outMethodID = jmethodID(method);
} }
@ -910,11 +952,13 @@ private:
jfieldID outFieldID = NULL; jfieldID outFieldID = NULL;
nsresult result = secureEnv->GetStaticFieldID(clazz, name, sig, &outFieldID); nsresult result = secureEnv->GetStaticFieldID(clazz, name, sig, &outFieldID);
if (result == NS_OK && outFieldID != NULL) { if (result == NS_OK && outFieldID != NULL) {
nsVoidKey key(outFieldID); JavaClassMember key(clazz, outFieldID);
JNIField* field = (JNIField*) theIDTable->Get(&key); JNIField* field;
if (field == NULL) { PRBool bFound = theIDTable && theIDTable->Get(key, (void **)field);
if (!bFound) {
field = new JNIField(name, sig, outFieldID); field = new JNIField(name, sig, outFieldID);
theIDTable->Put(&key, field); if (theIDTable)
theIDTable->Put(key, field);
} }
outFieldID = jfieldID(field); outFieldID = jfieldID(field);
} }
@ -1657,14 +1701,25 @@ JNINativeInterface_ ProxyJNIEnv::theFuncs = {
&GetJavaVM &GetJavaVM
}; };
nsHashtable* ProxyJNIEnv::theIDTable = NULL; nsDataHashtable<JavaClassMemberKey, void*>* ProxyJNIEnv::theIDTable = NULL;
ProxyJNIEnv::ProxyJNIEnv(nsIJVMPlugin* jvmPlugin, nsISecureEnv* secureEnv) ProxyJNIEnv::ProxyJNIEnv(nsIJVMPlugin* jvmPlugin, nsISecureEnv* secureEnv)
: mSecureEnv(secureEnv), mContext(NULL), mInProxyFindClass(JNI_FALSE) : mSecureEnv(secureEnv), mContext(NULL), mInProxyFindClass(JNI_FALSE)
{ {
this->functions = &theFuncs; this->functions = &theFuncs;
if (theIDTable == NULL) if (theIDTable == NULL) {
theIDTable = new nsHashtable(); theIDTable = new nsDataHashtable<JavaClassMemberKey, void*>;
if (theIDTable) {
if (!theIDTable->Init(16)) {
delete theIDTable;
theIDTable = NULL;
NS_WARNING("theIDTable initialization FAILED");
}
}
else {
NS_WARNING("theIDTable allocation FAILED");
}
}
// Ask the JVM for a new nsISecureEnv, if none provided. // Ask the JVM for a new nsISecureEnv, if none provided.
if (secureEnv == NULL) { if (secureEnv == NULL) {