summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/bridge/jni/v8
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WebCore/bridge/jni/v8')
-rw-r--r--Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp282
-rw-r--r--Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h9
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaClassV8.cpp10
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaFieldV8.cpp4
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaFieldV8.h9
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp97
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaInstanceV8.h10
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp45
-rw-r--r--Source/WebCore/bridge/jni/v8/JavaValueV8.h75
9 files changed, 342 insertions, 199 deletions
diff --git a/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp
index 30e344d..7ac1a93 100644
--- a/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp
+++ b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp
@@ -30,22 +30,28 @@
#include "JavaInstanceV8.h"
#include "JavaNPObjectV8.h"
+<<<<<<< HEAD
#if PLATFORM(ANDROID)
#include "npruntime_impl.h"
#endif // PLATFORM(ANDROID)
+=======
+#include "JavaValueV8.h"
+>>>>>>> webkit.org at r82507
#include <wtf/text/CString.h>
namespace JSC {
namespace Bindings {
-jvalue convertNPVariantToJValue(NPVariant value, const WTF::String& javaType)
+JavaValue convertNPVariantToJavaValue(NPVariant value, const String& javaClass)
{
- CString javaClassName = javaType.utf8();
- JNIType jniType = JNITypeFromClassName(javaClassName.data());
- jvalue result;
+ CString javaClassName = javaClass.utf8();
+ JavaType javaType = javaTypeFromClassName(javaClassName.data());
+ JavaValue result;
+ result.m_type = javaType;
NPVariantType type = value.type;
+<<<<<<< HEAD
switch (jniType) {
case array_type:
#if PLATFORM(ANDROID)
@@ -208,29 +214,31 @@ jvalue convertNPVariantToJValue(NPVariant value, const WTF::String& javaType)
#endif // PLATFORM(ANDROID)
case object_type:
+=======
+ switch (javaType) {
+ case JavaTypeArray:
+ case JavaTypeObject:
+>>>>>>> webkit.org at r82507
{
- result.l = static_cast<jobject>(0);
-
- // First see if we have a Java instance.
+ // See if we have a Java instance.
if (type == NPVariantType_Object) {
NPObject* objectImp = NPVARIANT_TO_OBJECT(value);
- if (JavaInstance* instance = ExtractJavaInstance(objectImp))
- result.l = instance->javaInstance();
+ result.m_objectValue = ExtractJavaInstance(objectImp);
}
+ }
+ break;
- // Now convert value to a string if the target type is a java.lang.string, and we're not
- // converting from a Null.
- if (!result.l && !strcmp(javaClassName.data(), "java.lang.String")) {
+ case JavaTypeString:
+ {
#ifdef CONVERT_NULL_TO_EMPTY_STRING
- if (type == NPVariantType_Null) {
- JNIEnv* env = getJNIEnv();
- jchar buf[2];
- jobject javaString = env->functions->NewString(env, buf, 0);
- result.l = javaString;
- } else
+ if (type == NPVariantType_Null) {
+ result.m_type = JavaTypeString;
+ result.m_stringValue = String::fromUTF8("");
+ } else
#else
- if (type == NPVariantType_String)
+ if (type == NPVariantType_String)
#endif
+<<<<<<< HEAD
{
NPString src = NPVARIANT_TO_STRING(value);
JNIEnv* env = getJNIEnv();
@@ -261,182 +269,167 @@ jvalue convertNPVariantToJValue(NPVariant value, const WTF::String& javaType)
#endif // PLATFORM(ANDROID)
} else if (!result.l)
memset(&result, 0, sizeof(jvalue)); // Handle it the same as a void case
+=======
+ {
+ NPString src = NPVARIANT_TO_STRING(value);
+ result.m_type = JavaTypeString;
+ result.m_stringValue = String::fromUTF8(src.UTF8Characters);
+ }
+>>>>>>> webkit.org at r82507
}
break;
- case boolean_type:
+ case JavaTypeBoolean:
{
if (type == NPVariantType_Bool)
- result.z = NPVARIANT_TO_BOOLEAN(value);
- else
- memset(&result, 0, sizeof(jvalue)); // as void case
+ result.m_booleanValue = NPVARIANT_TO_BOOLEAN(value);
}
break;
- case byte_type:
+ case JavaTypeByte:
{
if (type == NPVariantType_Int32)
- result.b = static_cast<jbyte>(NPVARIANT_TO_INT32(value));
+ result.m_byteValue = static_cast<signed char>(NPVARIANT_TO_INT32(value));
else if (type == NPVariantType_Double)
- result.b = static_cast<jbyte>(NPVARIANT_TO_DOUBLE(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_byteValue = static_cast<signed char>(NPVARIANT_TO_DOUBLE(value));
}
break;
- case char_type:
+ case JavaTypeChar:
{
if (type == NPVariantType_Int32)
- result.c = static_cast<char>(NPVARIANT_TO_INT32(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_charValue = static_cast<unsigned short>(NPVARIANT_TO_INT32(value));
}
break;
- case short_type:
+ case JavaTypeShort:
{
if (type == NPVariantType_Int32)
- result.s = static_cast<jshort>(NPVARIANT_TO_INT32(value));
+ result.m_shortValue = static_cast<short>(NPVARIANT_TO_INT32(value));
else if (type == NPVariantType_Double)
- result.s = static_cast<jshort>(NPVARIANT_TO_DOUBLE(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_shortValue = static_cast<short>(NPVARIANT_TO_DOUBLE(value));
}
break;
- case int_type:
+ case JavaTypeInt:
{
if (type == NPVariantType_Int32)
- result.i = static_cast<jint>(NPVARIANT_TO_INT32(value));
+ result.m_intValue = static_cast<int>(NPVARIANT_TO_INT32(value));
else if (type == NPVariantType_Double)
- result.i = static_cast<jint>(NPVARIANT_TO_DOUBLE(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_intValue = static_cast<int>(NPVARIANT_TO_DOUBLE(value));
}
break;
- case long_type:
+ case JavaTypeLong:
{
if (type == NPVariantType_Int32)
- result.j = static_cast<jlong>(NPVARIANT_TO_INT32(value));
+ result.m_longValue = static_cast<long long>(NPVARIANT_TO_INT32(value));
else if (type == NPVariantType_Double)
- result.j = static_cast<jlong>(NPVARIANT_TO_DOUBLE(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_longValue = static_cast<long long>(NPVARIANT_TO_DOUBLE(value));
}
break;
- case float_type:
+ case JavaTypeFloat:
{
if (type == NPVariantType_Int32)
- result.f = static_cast<jfloat>(NPVARIANT_TO_INT32(value));
+ result.m_floatValue = static_cast<float>(NPVARIANT_TO_INT32(value));
else if (type == NPVariantType_Double)
- result.f = static_cast<jfloat>(NPVARIANT_TO_DOUBLE(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_floatValue = static_cast<float>(NPVARIANT_TO_DOUBLE(value));
}
break;
- case double_type:
+ case JavaTypeDouble:
{
if (type == NPVariantType_Int32)
- result.d = static_cast<jdouble>(NPVARIANT_TO_INT32(value));
+ result.m_doubleValue = static_cast<double>(NPVARIANT_TO_INT32(value));
else if (type == NPVariantType_Double)
- result.d = static_cast<jdouble>(NPVARIANT_TO_DOUBLE(value));
- else
- memset(&result, 0, sizeof(jvalue));
+ result.m_doubleValue = static_cast<double>(NPVARIANT_TO_DOUBLE(value));
}
break;
-
- break;
-
- case invalid_type:
default:
- case void_type:
- {
- memset(&result, 0, sizeof(jvalue));
- }
break;
}
return result;
}
-void convertJValueToNPVariant(jvalue value, JNIType jniType, const char* javaTypeName, NPVariant* result)
+void convertJavaValueToNPVariant(JavaValue value, NPVariant* result)
{
- switch (jniType) {
- case void_type:
+ switch (value.m_type) {
+ case JavaTypeVoid:
{
VOID_TO_NPVARIANT(*result);
}
break;
- case object_type:
+ case JavaTypeObject:
{
- if (value.l) {
- if (!strcmp(javaTypeName, "java.lang.String")) {
- const char* v = getCharactersFromJString(static_cast<jstring>(value.l));
- // s is freed in NPN_ReleaseVariantValue (see npruntime.cpp)
- const char* s = strdup(v);
- releaseCharactersForJString(static_cast<jstring>(value.l), v);
- STRINGZ_TO_NPVARIANT(s, *result);
- } else
- OBJECT_TO_NPVARIANT(JavaInstanceToNPObject(new JavaInstance(value.l)), *result);
- } else
+ // If the JavaValue is a String object, it should have type JavaTypeString.
+ if (value.m_objectValue)
+ OBJECT_TO_NPVARIANT(JavaInstanceToNPObject(value.m_objectValue.get()), *result);
+ else
VOID_TO_NPVARIANT(*result);
}
break;
- case boolean_type:
+ case JavaTypeString:
+ {
+ const char* utf8String = strdup(value.m_stringValue.utf8().data());
+ // The copied string is freed in NPN_ReleaseVariantValue (see npruntime.cpp)
+ STRINGZ_TO_NPVARIANT(utf8String, *result);
+ }
+ break;
+
+ case JavaTypeBoolean:
{
- BOOLEAN_TO_NPVARIANT(value.z, *result);
+ BOOLEAN_TO_NPVARIANT(value.m_booleanValue, *result);
}
break;
- case byte_type:
+ case JavaTypeByte:
{
- INT32_TO_NPVARIANT(value.b, *result);
+ INT32_TO_NPVARIANT(value.m_byteValue, *result);
}
break;
- case char_type:
+ case JavaTypeChar:
{
- INT32_TO_NPVARIANT(value.c, *result);
+ INT32_TO_NPVARIANT(value.m_charValue, *result);
}
break;
- case short_type:
+ case JavaTypeShort:
{
- INT32_TO_NPVARIANT(value.s, *result);
+ INT32_TO_NPVARIANT(value.m_shortValue, *result);
}
break;
- case int_type:
+ case JavaTypeInt:
{
- INT32_TO_NPVARIANT(value.i, *result);
+ INT32_TO_NPVARIANT(value.m_intValue, *result);
}
break;
// TODO: Check if cast to double is needed.
- case long_type:
+ case JavaTypeLong:
{
- DOUBLE_TO_NPVARIANT(value.j, *result);
+ DOUBLE_TO_NPVARIANT(value.m_longValue, *result);
}
break;
- case float_type:
+ case JavaTypeFloat:
{
- DOUBLE_TO_NPVARIANT(value.f, *result);
+ DOUBLE_TO_NPVARIANT(value.m_floatValue, *result);
}
break;
- case double_type:
+ case JavaTypeDouble:
{
- DOUBLE_TO_NPVARIANT(value.d, *result);
+ DOUBLE_TO_NPVARIANT(value.m_doubleValue, *result);
}
break;
- case invalid_type:
+ case JavaTypeInvalid:
default:
{
VOID_TO_NPVARIANT(*result);
@@ -445,6 +438,103 @@ void convertJValueToNPVariant(jvalue value, JNIType jniType, const char* javaTyp
}
}
+JavaValue jvalueToJavaValue(const jvalue& value, const JavaType& type)
+{
+ JavaValue result;
+ result.m_type = type;
+ switch (result.m_type) {
+ case JavaTypeVoid:
+ break;
+ case JavaTypeObject:
+ result.m_objectValue = new JavaInstance(value.l);
+ break;
+ case JavaTypeString:
+ {
+ jstring javaString = static_cast<jstring>(value.l);
+ const UChar* a = getUCharactersFromJStringInEnv(getJNIEnv(), javaString);
+ // We take a copy to allow the Java String to be released.
+ result.m_stringValue = String(a).threadsafeCopy();
+ releaseUCharactersForJStringInEnv(getJNIEnv(), javaString, a);
+ }
+ break;
+ case JavaTypeBoolean:
+ result.m_booleanValue = value.z == JNI_FALSE ? false : true;
+ break;
+ case JavaTypeByte:
+ result.m_byteValue = value.b;
+ break;
+ case JavaTypeChar:
+ result.m_charValue = value.c;
+ break;
+ case JavaTypeShort:
+ result.m_shortValue = value.s;
+ break;
+ case JavaTypeInt:
+ result.m_intValue = value.i;
+ break;
+ case JavaTypeLong:
+ result.m_longValue = value.j;
+ break;
+ case JavaTypeFloat:
+ result.m_floatValue = value.f;
+ break;
+ case JavaTypeDouble:
+ result.m_doubleValue = value.d;
+ break;
+ default:
+ ASSERT(false);
+ }
+ return result;
+}
+
+jvalue javaValueToJvalue(const JavaValue& value)
+{
+ jvalue result;
+ memset(&result, 0, sizeof(jvalue));
+ switch (value.m_type) {
+ case JavaTypeVoid:
+ break;
+ case JavaTypeObject:
+ if (value.m_objectValue)
+ result.l = value.m_objectValue->javaInstance();
+ break;
+ case JavaTypeString:
+ // This creates a local reference to a new String object, which will
+ // be released when the call stack returns to Java. Note that this
+ // may cause leaks if invoked from a native message loop, as is the
+ // case in workers.
+ result.l = getJNIEnv()->NewString(value.m_stringValue.characters(), value.m_stringValue.length());
+ break;
+ case JavaTypeBoolean:
+ result.z = value.m_booleanValue ? JNI_TRUE : JNI_FALSE;
+ break;
+ case JavaTypeByte:
+ result.b = value.m_byteValue;
+ break;
+ case JavaTypeChar:
+ result.c = value.m_charValue;
+ break;
+ case JavaTypeShort:
+ result.s = value.m_shortValue;
+ break;
+ case JavaTypeInt:
+ result.i = value.m_intValue;
+ break;
+ case JavaTypeLong:
+ result.j = value.m_longValue;
+ break;
+ case JavaTypeFloat:
+ result.f = value.m_floatValue;
+ break;
+ case JavaTypeDouble:
+ result.d = value.m_doubleValue;
+ break;
+ default:
+ ASSERT(false);
+ }
+ return result;
+}
+
} // namespace Bindings
} // namespace JSC
diff --git a/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h
index df73a9e..0282904 100644
--- a/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h
+++ b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h
@@ -36,8 +36,13 @@ namespace JSC {
namespace Bindings {
-jvalue convertNPVariantToJValue(NPVariant, const WTF::String& javaType);
-void convertJValueToNPVariant(jvalue, JNIType, const char* javaClassName, NPVariant*);
+class JavaValue;
+
+JavaValue convertNPVariantToJavaValue(NPVariant, const String& javaClass);
+void convertJavaValueToNPVariant(JavaValue, NPVariant*);
+
+JavaValue jvalueToJavaValue(const jvalue&, const JavaType&);
+jvalue javaValueToJvalue(const JavaValue&);
} // namespace Bindings
diff --git a/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp b/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp
index 18377bd..24d05f3 100644
--- a/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp
+++ b/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp
@@ -29,7 +29,7 @@
#if ENABLE(JAVA_BRIDGE)
#include "JavaFieldV8.h"
-#include "JavaMethod.h"
+#include "JavaMethodJobject.h"
using namespace JSC::Bindings;
@@ -38,7 +38,7 @@ JavaClass::JavaClass(jobject anInstance)
jobject aClass = callJNIMethod<jobject>(anInstance, "getClass", "()Ljava/lang/Class;");
if (!aClass) {
- fprintf(stderr, "%s: unable to call getClass on instance %p\n", __PRETTY_FUNCTION__, anInstance);
+ LOG_ERROR("unable to call getClass on instance %p", anInstance);
return;
}
@@ -62,13 +62,13 @@ JavaClass::JavaClass(jobject anInstance)
int numMethods = env->GetArrayLength(methods);
for (i = 0; i < numMethods; i++) {
jobject aJMethod = env->GetObjectArrayElement(static_cast<jobjectArray>(methods), i);
- JavaMethod* aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor
+ JavaMethod* aMethod = new JavaMethodJobject(env, aJMethod); // deleted in the JavaClass destructor
MethodList* methodList;
{
- methodList = m_methods.get(aMethod->name().utf8());
+ methodList = m_methods.get(aMethod->name());
if (!methodList) {
methodList = new MethodList();
- m_methods.set(aMethod->name().utf8(), methodList);
+ m_methods.set(aMethod->name(), methodList);
}
}
methodList->append(aMethod);
diff --git a/Source/WebCore/bridge/jni/v8/JavaFieldV8.cpp b/Source/WebCore/bridge/jni/v8/JavaFieldV8.cpp
index 62081eb..2a6dd0b 100644
--- a/Source/WebCore/bridge/jni/v8/JavaFieldV8.cpp
+++ b/Source/WebCore/bridge/jni/v8/JavaFieldV8.cpp
@@ -35,8 +35,8 @@ JavaField::JavaField(JNIEnv* env, jobject aField)
// Get field type
jobject fieldType = callJNIMethod<jobject>(aField, "getType", "()Ljava/lang/Class;");
jstring fieldTypeName = static_cast<jstring>(callJNIMethod<jobject>(fieldType, "getName", "()Ljava/lang/String;"));
- m_type = JavaString(env, fieldTypeName);
- m_JNIType = JNITypeFromClassName(m_type.utf8());
+ m_typeClassName = JavaString(env, fieldTypeName);
+ m_type = javaTypeFromClassName(m_typeClassName.utf8());
// Get field name
jstring fieldName = static_cast<jstring>(callJNIMethod<jobject>(aField, "getName", "()Ljava/lang/String;"));
diff --git a/Source/WebCore/bridge/jni/v8/JavaFieldV8.h b/Source/WebCore/bridge/jni/v8/JavaFieldV8.h
index caf28bf..defed60 100644
--- a/Source/WebCore/bridge/jni/v8/JavaFieldV8.h
+++ b/Source/WebCore/bridge/jni/v8/JavaFieldV8.h
@@ -40,14 +40,13 @@ public:
JavaField(JNIEnv*, jobject aField);
const JavaString& name() const { return m_name; }
- const char* type() const { return m_type.utf8(); }
-
- JNIType getJNIType() const { return m_JNIType; }
+ const char* typeClassName() const { return m_typeClassName.utf8(); }
+ JavaType type() const { return m_type; }
private:
JavaString m_name;
- JavaString m_type;
- JNIType m_JNIType;
+ JavaString m_typeClassName;
+ JavaType m_type;
RefPtr<JobjectWrapper> m_field;
};
diff --git a/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp
index dbf53f0..d1075ae 100644
--- a/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp
+++ b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp
@@ -29,10 +29,12 @@
#if ENABLE(JAVA_BRIDGE)
-#include "JavaMethod.h"
#include "JNIUtilityPrivate.h"
#include "JavaClassV8.h"
+#include "JavaFieldV8.h"
+#include "JavaMethod.h"
+#include <wtf/OwnArrayPtr.h>
#include <wtf/text/CString.h>
using namespace JSC::Bindings;
@@ -68,88 +70,21 @@ JavaClass* JavaInstance::getClass() const
return m_class;
}
-bool JavaInstance::invokeMethod(const char* methodName, const NPVariant* args, int count, NPVariant* resultValue)
+JavaValue JavaInstance::invokeMethod(const JavaMethod* method, JavaValue* args)
{
- VOID_TO_NPVARIANT(*resultValue);
-
- MethodList methodList = getClass()->methodsNamed(methodName);
-
- size_t numMethods = methodList.size();
-
- // Try to find a good match for the overloaded method. The
- // fundamental problem is that JavaScript doesn't have the
- // notion of method overloading and Java does. We could
- // get a bit more sophisticated and attempt to does some
- // type checking as we as checking the number of parameters.
- JavaMethod* aMethod;
- JavaMethod* method = 0;
- for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) {
- aMethod = methodList[methodIndex];
- if (aMethod->numParameters() == count) {
- method = aMethod;
- break;
- }
- }
- if (!method)
- return false;
-
- const JavaMethod* jMethod = static_cast<const JavaMethod*>(method);
-
- jvalue* jArgs = 0;
- if (count > 0)
- jArgs = static_cast<jvalue*>(malloc(count * sizeof(jvalue)));
-
- for (int i = 0; i < count; i++)
- jArgs[i] = convertNPVariantToJValue(args[i], jMethod->parameterAt(i));
-
- jvalue result;
-
- // The following code can be conditionally removed once we have a Tiger update that
- // contains the new Java plugin. It is needed for builds prior to Tiger.
- {
- jobject obj = javaInstance();
- switch (jMethod->JNIReturnType()) {
- case void_type:
- callJNIMethodIDA<void>(obj, jMethod->methodID(obj), jArgs);
- break;
- case object_type:
- result.l = callJNIMethodIDA<jobject>(obj, jMethod->methodID(obj), jArgs);
- break;
- case boolean_type:
- result.z = callJNIMethodIDA<jboolean>(obj, jMethod->methodID(obj), jArgs);
- break;
- case byte_type:
- result.b = callJNIMethodIDA<jbyte>(obj, jMethod->methodID(obj), jArgs);
- break;
- case char_type:
- result.c = callJNIMethodIDA<jchar>(obj, jMethod->methodID(obj), jArgs);
- break;
- case short_type:
- result.s = callJNIMethodIDA<jshort>(obj, jMethod->methodID(obj), jArgs);
- break;
- case int_type:
- result.i = callJNIMethodIDA<jint>(obj, jMethod->methodID(obj), jArgs);
- break;
-
- case long_type:
- result.j = callJNIMethodIDA<jlong>(obj, jMethod->methodID(obj), jArgs);
- break;
- case float_type:
- result.f = callJNIMethodIDA<jfloat>(obj, jMethod->methodID(obj), jArgs);
- break;
- case double_type:
- result.d = callJNIMethodIDA<jdouble>(obj, jMethod->methodID(obj), jArgs);
- break;
- case invalid_type:
- default:
- break;
- }
- }
-
- convertJValueToNPVariant(result, jMethod->JNIReturnType(), jMethod->returnType(), resultValue);
- free(jArgs);
+ ASSERT(getClass()->methodsNamed(method->name().utf8().data()).find(method) != notFound);
+ unsigned int numParams = method->numParameters();
+ OwnArrayPtr<jvalue> jvalueArgs = adoptArrayPtr(new jvalue[numParams]);
+ for (unsigned int i = 0; i < numParams; ++i)
+ jvalueArgs[i] = javaValueToJvalue(args[i]);
+ jvalue result = callJNIMethod(javaInstance(), method->returnType(), method->name().utf8().data(), method->signature(), jvalueArgs.get());
+ return jvalueToJavaValue(result, method->returnType());
+}
- return true;
+JavaValue JavaInstance::getField(const JavaField* field)
+{
+ ASSERT(getClass()->fieldNamed(field->name().utf8()) == field);
+ return jvalueToJavaValue(getJNIField(javaInstance(), field->type(), field->name().utf8(), field->typeClassName()), field->type());
}
#endif // ENABLE(JAVA_BRIDGE)
diff --git a/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h
index 8ee3195..b1150f8 100644
--- a/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h
+++ b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h
@@ -30,6 +30,7 @@
#if ENABLE(JAVA_BRIDGE)
#include "JNIUtility.h"
+#include "JavaValueV8.h"
#include "JobjectWrapper.h"
#include "npruntime.h"
@@ -43,6 +44,8 @@ namespace JSC {
namespace Bindings {
class JavaClass;
+class JavaField;
+class JavaMethod;
class JavaInstance : public RefCounted<JavaInstance> {
public:
@@ -50,9 +53,10 @@ public:
virtual ~JavaInstance();
JavaClass* getClass() const;
-
- bool invokeMethod(const char* name, const NPVariant* args, int argsCount, NPVariant* result);
-
+ // args must be an array of length greater than or equal to the number of
+ // arguments expected by the method.
+ JavaValue invokeMethod(const JavaMethod*, JavaValue* args);
+ JavaValue getField(const JavaField*);
jobject javaInstance() const { return m_instance->m_instance; }
// These functions are called before and after the main entry points into
diff --git a/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp b/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp
index 7a7adc5..b02d059 100644
--- a/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp
+++ b/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp
@@ -33,6 +33,8 @@
#include "JavaClassV8.h"
#include "JavaFieldV8.h"
#include "JavaInstanceV8.h"
+#include "JavaMethod.h"
+#include "JavaValueV8.h"
#include "npruntime_impl.h"
namespace JSC {
@@ -115,12 +117,40 @@ bool JavaNPObjectInvoke(NPObject* obj, NPIdentifier identifier, const NPVariant*
return false;
instance->begin();
- bool r = instance->invokeMethod(name, args, argCount, result);
- instance->end();
+ MethodList methodList = instance->getClass()->methodsNamed(name);
// TODO: use NPN_MemFree
free(name);
- return r;
+
+ // Try to find a good match for the overloaded method. The
+ // fundamental problem is that JavaScript doesn't have the
+ // notion of method overloading and Java does. We could
+ // get a bit more sophisticated and attempt to do some
+ // type checking as well as checking the number of parameters.
+ size_t numMethods = methodList.size();
+ JavaMethod* aMethod;
+ JavaMethod* jMethod = 0;
+ for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) {
+ aMethod = methodList[methodIndex];
+ if (aMethod->numParameters() == static_cast<int>(argCount)) {
+ jMethod = aMethod;
+ break;
+ }
+ }
+ if (!jMethod)
+ return false;
+
+ JavaValue* jArgs = new JavaValue[argCount];
+ for (unsigned int i = 0; i < argCount; i++)
+ jArgs[i] = convertNPVariantToJavaValue(args[i], jMethod->parameterAt(i));
+
+ JavaValue jResult = instance->invokeMethod(jMethod, jArgs);
+ instance->end();
+ delete[] jArgs;
+
+ VOID_TO_NPVARIANT(*result);
+ convertJavaValueToNPVariant(jResult, result);
+ return true;
}
bool JavaNPObjectHasProperty(NPObject* obj, NPIdentifier identifier)
@@ -150,12 +180,11 @@ bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant*
instance->begin();
JavaField* field = instance->getClass()->fieldNamed(name);
- instance->end();
free(name); // TODO: use NPN_MemFree
-
if (!field)
return false;
+<<<<<<< HEAD
#if PLATFORM(ANDROID)
// JSC does not seem to support returning object properties so we emulate that
// behaviour here.
@@ -167,6 +196,12 @@ bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant*
field->type());
#endif // PLATFORM(ANDROID)
convertJValueToNPVariant(value, field->getJNIType(), field->type(), result);
+=======
+ JavaValue value = instance->getField(field);
+ instance->end();
+
+ convertJavaValueToNPVariant(value, result);
+>>>>>>> webkit.org at r82507
return true;
}
diff --git a/Source/WebCore/bridge/jni/v8/JavaValueV8.h b/Source/WebCore/bridge/jni/v8/JavaValueV8.h
new file mode 100644
index 0000000..3e1c623
--- /dev/null
+++ b/Source/WebCore/bridge/jni/v8/JavaValueV8.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2011, The Android Open Source Project
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef JavaValueV8_h
+#define JavaValueV8_h
+
+#if ENABLE(JAVA_BRIDGE)
+
+#include "JavaType.h"
+
+#include <wtf/text/WTFString.h>
+
+namespace JSC {
+
+namespace Bindings {
+
+class JavaInstance;
+
+// A variant used to represent a Java value, almost identical to the JNI
+// jvalue type. It exists because the logic to convert between JavaScript
+// objects (as JavaNPObject or JSValue) and Java objects should not depend upon
+// JNI, to allow ports to provide a JavaInstance object etc which does not use
+// JNI. This means that the 'object' field of this variant uses JavaInstance,
+// not jobject.
+//
+// Note that this class is independent of the JavaScript engine, but is
+// currently used only with V8.
+// See https://bugs.webkit.org/show_bug.cgi?id=57023.
+struct JavaValue {
+ JavaValue() : m_type(JavaTypeInvalid) {}
+
+ JavaType m_type;
+ // We don't use a union because we want to be able to ref-count some of the
+ // values. This requires types with non-trivial constructors.
+ RefPtr<JavaInstance> m_objectValue;
+ bool m_booleanValue;
+ signed char m_byteValue;
+ unsigned short m_charValue;
+ short m_shortValue;
+ int m_intValue;
+ long long m_longValue;
+ float m_floatValue;
+ double m_doubleValue;
+ String m_stringValue;
+};
+
+} // namespace Bindings
+
+} // namespace JSC
+
+#endif // ENABLE(JAVA_BRIDGE)
+
+#endif // JavaValueV8_h