diff options
Diffstat (limited to 'Source/WebCore/bridge/jni/v8')
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JNIBridgeV8.cpp | 44 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JNIBridgeV8.h | 56 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp | 481 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h | 49 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaClassV8.cpp | 103 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaClassV8.h | 60 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp | 173 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaInstanceV8.h | 100 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp | 176 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaNPObjectV8.h | 56 | ||||
-rw-r--r-- | Source/WebCore/bridge/jni/v8/JavaStringV8.h | 61 |
11 files changed, 1359 insertions, 0 deletions
diff --git a/Source/WebCore/bridge/jni/v8/JNIBridgeV8.cpp b/Source/WebCore/bridge/jni/v8/JNIBridgeV8.cpp new file mode 100644 index 0000000..35775dc --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JNIBridgeV8.cpp @@ -0,0 +1,44 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + */ + +#include "config.h" +#include "JNIBridgeV8.h" + +using namespace JSC::Bindings; + +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()); + + // Get field name + jstring fieldName = static_cast<jstring>(callJNIMethod<jobject>(aField, "getName", "()Ljava/lang/String;")); + m_name = JavaString(env, fieldName); + + m_field = new JObjectWrapper(aField); +} diff --git a/Source/WebCore/bridge/jni/v8/JNIBridgeV8.h b/Source/WebCore/bridge/jni/v8/JNIBridgeV8.h new file mode 100644 index 0000000..46cbd56 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JNIBridgeV8.h @@ -0,0 +1,56 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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 JNIBridgeV8_h +#define JNIBridgeV8_h + +#include "JNIBridge.h" // For JavaString +#include "JavaInstanceV8.h" // For JObjectWrapper + +namespace JSC { + +namespace Bindings { + +class JavaField { +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; } + +private: + JavaString m_name; + JavaString m_type; + JNIType m_JNIType; + RefPtr<JObjectWrapper> m_field; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JNIBridgeV8_h diff --git a/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp new file mode 100644 index 0000000..f104e65 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp @@ -0,0 +1,481 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + */ + +#include "config.h" +#include "JNIUtilityPrivate.h" + +#include "JavaInstanceV8.h" +#include "JavaNPObjectV8.h" +#include "npruntime_impl.h" + +namespace JSC { + +namespace Bindings { + +jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* javaClassName) +{ + jvalue result; + NPVariantType type = value.type; + + switch (jniType) { + case array_type: + { + JNIEnv* env = getJNIEnv(); + jobject javaArray; + NPObject* object = NPVARIANT_IS_OBJECT(value) ? NPVARIANT_TO_OBJECT(value) : 0; + NPVariant npvLength; + bool success = _NPN_GetProperty(0, object, _NPN_GetStringIdentifier("length"), &npvLength); + if (!success) { + // No length property so we don't know how many elements to put into the array. + // Treat this as an error. +#ifdef EMULATE_JSC_BINDINGS + // JSC sends null for an array that is not an array of strings or basic types, + // do this also in the unknown length case. + memset(&result, 0, sizeof(jvalue)); +#else + // Sending NULL as JSC does seems dangerous. (Imagine the java method that asks + // for the length of the array it was passed). Here we send a 0 length array. + jclass objectClass = env->FindClass("java/lang/Object"); + javaArray = env->NewObjectArray(0, objectClass, 0); + env->DeleteLocalRef(objectClass); +#endif + break; + } + + jsize length = 0; + if (NPVARIANT_IS_INT32(npvLength)) + length = static_cast<jsize>(NPVARIANT_TO_INT32(npvLength)); + else if (NPVARIANT_IS_DOUBLE(npvLength)) + length = static_cast<jsize>(NPVARIANT_TO_DOUBLE(npvLength)); + + if (!strcmp(javaClassName, "[Ljava.lang.String;")) { + // Match JSC behavior by only allowing Object arrays if they are Strings. + jclass stringClass = env->FindClass("java/lang/String"); + javaArray = env->NewObjectArray(length, stringClass, 0); + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + if(NPVARIANT_IS_STRING(npvValue)) { + NPString str = NPVARIANT_TO_STRING(npvValue); + env->SetObjectArrayElement(static_cast<jobjectArray>(javaArray), i, env->NewStringUTF(str.UTF8Characters)); + } + } + + env->DeleteLocalRef(stringClass); + } else if (!strcmp(javaClassName, "[B")) { + // array of bytes + javaArray = env->NewByteArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + jbyte bVal = 0; + if (NPVARIANT_IS_INT32(npvValue)) { + bVal = static_cast<jbyte>(NPVARIANT_TO_INT32(npvValue)); + } else if (NPVARIANT_IS_DOUBLE(npvValue)) { + bVal = static_cast<jbyte>(NPVARIANT_TO_DOUBLE(npvValue)); + } + env->SetByteArrayRegion(static_cast<jbyteArray>(javaArray), i, 1, &bVal); + } + } else if (!strcmp(javaClassName, "[C")) { + // array of chars + javaArray = env->NewCharArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + jchar cVal = 0; + if (NPVARIANT_IS_INT32(npvValue)) { + cVal = static_cast<jchar>(NPVARIANT_TO_INT32(npvValue)); + } else if (NPVARIANT_IS_STRING(npvValue)) { + NPString str = NPVARIANT_TO_STRING(npvValue); + cVal = str.UTF8Characters[0]; + } + env->SetCharArrayRegion(static_cast<jcharArray>(javaArray), i, 1, &cVal); + } + } else if (!strcmp(javaClassName, "[D")) { + // array of doubles + javaArray = env->NewDoubleArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + if (NPVARIANT_IS_DOUBLE(npvValue)) { + jdouble dVal = NPVARIANT_TO_DOUBLE(npvValue); + env->SetDoubleArrayRegion(static_cast<jdoubleArray>(javaArray), i, 1, &dVal); + } + } + } else if (!strcmp(javaClassName, "[F")) { + // array of floats + javaArray = env->NewFloatArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + if (NPVARIANT_IS_DOUBLE(npvValue)) { + jfloat fVal = static_cast<jfloat>(NPVARIANT_TO_DOUBLE(npvValue)); + env->SetFloatArrayRegion(static_cast<jfloatArray>(javaArray), i, 1, &fVal); + } + } + } else if (!strcmp(javaClassName, "[I")) { + // array of ints + javaArray = env->NewIntArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + jint iVal = 0; + if (NPVARIANT_IS_INT32(npvValue)) { + iVal = NPVARIANT_TO_INT32(npvValue); + } else if (NPVARIANT_IS_DOUBLE(npvValue)) { + iVal = static_cast<jint>(NPVARIANT_TO_DOUBLE(npvValue)); + } + env->SetIntArrayRegion(static_cast<jintArray>(javaArray), i, 1, &iVal); + } + } else if (!strcmp(javaClassName, "[J")) { + // array of longs + javaArray = env->NewLongArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + jlong jVal = 0; + if (NPVARIANT_IS_INT32(npvValue)) { + jVal = static_cast<jlong>(NPVARIANT_TO_INT32(npvValue)); + } else if (NPVARIANT_IS_DOUBLE(npvValue)) { + jVal = static_cast<jlong>(NPVARIANT_TO_DOUBLE(npvValue)); + } + env->SetLongArrayRegion(static_cast<jlongArray>(javaArray), i, 1, &jVal); + } + } else if (!strcmp(javaClassName, "[S")) { + // array of shorts + javaArray = env->NewShortArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + jshort sVal = 0; + if (NPVARIANT_IS_INT32(npvValue)) { + sVal = static_cast<jshort>(NPVARIANT_TO_INT32(npvValue)); + } else if (NPVARIANT_IS_DOUBLE(npvValue)) { + sVal = static_cast<jshort>(NPVARIANT_TO_DOUBLE(npvValue)); + } + env->SetShortArrayRegion(static_cast<jshortArray>(javaArray), i, 1, &sVal); + } + } else if (!strcmp(javaClassName, "[Z")) { + // array of booleans + javaArray = env->NewBooleanArray(length); + // Now iterate over each element and add to the array. + for (jsize i = 0; i < length; i++) { + NPVariant npvValue; + _NPN_GetProperty(0, object, _NPN_GetIntIdentifier(i), &npvValue); + if (NPVARIANT_IS_BOOLEAN(npvValue)) { + jboolean zVal = NPVARIANT_TO_BOOLEAN(npvValue); + env->SetBooleanArrayRegion(static_cast<jbooleanArray>(javaArray), i, 1, &zVal); + } + } + } else { +#ifdef EMULATE_JSC_BINDINGS + // JSC sends null for an array that is not an array of strings or basic types. + memset(&result, 0, sizeof(jvalue)); + break; +#else + // Sending NULL as JSC does seems dangerous. (Imagine the java method that asks + // for the length of the array it was passed). Here we send a 0 length array. + jclass objectClass = env->FindClass("java/lang/Object"); + javaArray = env->NewObjectArray(0, objectClass, 0); + env->DeleteLocalRef(objectClass); +#endif + } + + result.l = javaArray; + } + break; + + case object_type: + { + JNIEnv* env = getJNIEnv(); + result.l = static_cast<jobject>(0); + jobject javaString; + + // First 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(); + } + + // 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, "java.lang.String")) { +#ifdef CONVERT_NULL_TO_EMPTY_STRING + if (type == NPVariantType_Null) { + jchar buf[2]; + jobject javaString = env->functions->NewString(env, buf, 0); + result.l = javaString; + } else +#else + if (type == NPVariantType_String) +#endif + { + NPString src = NPVARIANT_TO_STRING(value); + javaString = env->NewStringUTF(src.UTF8Characters); + result.l = javaString; + } else if (type == NPVariantType_Int32) { + jint src = NPVARIANT_TO_INT32(value); + jclass integerClass = env->FindClass("java/lang/Integer"); + jmethodID toString = env->GetStaticMethodID(integerClass, "toString", "(I)Ljava/lang/String;"); + javaString = env->CallStaticObjectMethod(integerClass, toString, src); + result.l = javaString; + env->DeleteLocalRef(integerClass); + } else if (type == NPVariantType_Bool) { + jboolean src = NPVARIANT_TO_BOOLEAN(value); + jclass booleanClass = env->FindClass("java/lang/Boolean"); + jmethodID toString = env->GetStaticMethodID(booleanClass, "toString", "(Z)Ljava/lang/String;"); + javaString = env->CallStaticObjectMethod(booleanClass, toString, src); + result.l = javaString; + env->DeleteLocalRef(booleanClass); + } else if (type == NPVariantType_Double) { + jdouble src = NPVARIANT_TO_DOUBLE(value); + jclass doubleClass = env->FindClass("java/lang/Double"); + jmethodID toString = env->GetStaticMethodID(doubleClass, "toString", "(D)Ljava/lang/String;"); + javaString = env->CallStaticObjectMethod(doubleClass, toString, src); + result.l = javaString; + env->DeleteLocalRef(doubleClass); + } +#ifdef EMULATE_JSC_BINDINGS + // For the undefined value, JSC sends the String "undefined". Feels to me like we + // should send null in this case. + else if (!NPVARIANT_IS_NULL(value)) { + javaString = env->NewStringUTF("undefined"); + result.l = javaString; + } +#endif + } else if (!result.l) + memset(&result, 0, sizeof(jvalue)); // Handle it the same as a void case + } + break; + + case boolean_type: + { + if (type == NPVariantType_Bool) + result.z = NPVARIANT_TO_BOOLEAN(value); + else + memset(&result, 0, sizeof(jvalue)); // as void case + } + break; + + case byte_type: + { + if (type == NPVariantType_Int32) + result.b = static_cast<jbyte>(NPVARIANT_TO_INT32(value)); + else if (type == NPVariantType_Double) + result.b = static_cast<jbyte>(NPVARIANT_TO_DOUBLE(value)); + else + memset(&result, 0, sizeof(jvalue)); + } + break; + + case char_type: + { + if (type == NPVariantType_Int32) + result.c = static_cast<char>(NPVARIANT_TO_INT32(value)); +#ifndef EMULATE_JSC_BINDINGS + // There is no char type in JavaScript - just strings 1 character + // long. So just converting it to an int above doesn't work. Again, + // we emulate the behavior for now for maximum compatability. + else if (type == NPVariantType_String) { + NPString str = NPVARIANT_TO_STRING(value); + result.c = str.UTF8Characters[0]; + } +#endif + else + memset(&result, 0, sizeof(jvalue)); + } + break; + + case short_type: + { + if (type == NPVariantType_Int32) + result.s = static_cast<jshort>(NPVARIANT_TO_INT32(value)); + else if (type == NPVariantType_Double) + result.s = static_cast<jshort>(NPVARIANT_TO_DOUBLE(value)); + else + memset(&result, 0, sizeof(jvalue)); + } + break; + + case int_type: + { + if (type == NPVariantType_Int32) + result.i = static_cast<jint>(NPVARIANT_TO_INT32(value)); + else if (type == NPVariantType_Double) + result.i = static_cast<jint>(NPVARIANT_TO_DOUBLE(value)); + else + memset(&result, 0, sizeof(jvalue)); + } + break; + + case long_type: + { + if (type == NPVariantType_Int32) + result.j = static_cast<jlong>(NPVARIANT_TO_INT32(value)); + else if (type == NPVariantType_Double) + result.j = static_cast<jlong>(NPVARIANT_TO_DOUBLE(value)); + else + memset(&result, 0, sizeof(jvalue)); + } + break; + + case float_type: + { + if (type == NPVariantType_Int32) + result.f = static_cast<jfloat>(NPVARIANT_TO_INT32(value)); + else if (type == NPVariantType_Double) + result.f = static_cast<jfloat>(NPVARIANT_TO_DOUBLE(value)); + else + memset(&result, 0, sizeof(jvalue)); + } + break; + + case double_type: + { + if (type == NPVariantType_Int32) + result.d = static_cast<jdouble>(NPVARIANT_TO_INT32(value)); + else if (type == NPVariantType_Double) + result.d = static_cast<jdouble>(NPVARIANT_TO_DOUBLE(value)); + else + memset(&result, 0, sizeof(jvalue)); + } + 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) +{ + switch (jniType) { + case void_type: + { + VOID_TO_NPVARIANT(*result); + } + break; + + case object_type: + { + 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 + VOID_TO_NPVARIANT(*result); + } + break; + + case boolean_type: + { + BOOLEAN_TO_NPVARIANT(value.z, *result); + } + break; + + case byte_type: + { + INT32_TO_NPVARIANT(value.b, *result); + } + break; + + case char_type: + { +#ifndef EMULATE_JSC_BINDINGS + // There is no char type in JavaScript - just strings 1 character + // long. So just converting it to an int above doesn't work. Again, + // we emulate the behavior for now for maximum compatability. + if (!strcmp(javaTypeName, "char")) { + const char c = value.c; + const char* v = strndup(&c, 1); + STRINGZ_TO_NPVARIANT(v, *result); + } else +#endif + INT32_TO_NPVARIANT(value.c, *result); + } + break; + + case short_type: + { + INT32_TO_NPVARIANT(value.s, *result); + } + break; + + case int_type: + { + INT32_TO_NPVARIANT(value.i, *result); + } + break; + + // TODO: Check if cast to double is needed. + case long_type: + { + DOUBLE_TO_NPVARIANT(value.j, *result); + } + break; + + case float_type: + { + DOUBLE_TO_NPVARIANT(value.f, *result); + } + break; + + case double_type: + { + DOUBLE_TO_NPVARIANT(value.d, *result); + } + break; + + case invalid_type: + default: + { + VOID_TO_NPVARIANT(*result); + } + break; + } +} + +} // end of namespace Bindings + +} // end of namespace JSC diff --git a/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h new file mode 100644 index 0000000..da7a24c --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JNIUtilityPrivate.h @@ -0,0 +1,49 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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 JNIUtilityPrivate_h +#define JNIUtilityPrivate_h + +#include "JNIUtility.h" +#include "npruntime.h" + +// FIXME: While fully implementing the bindings I noticed some differences between what +// I wrote and seemed intuitive and what JSC does. Need to verify if my intuition is wrong +// or there are bugs in the JSC bindings. For now, this macro makes the V8 bindings do the +// same as the JSC bindings. +#define EMULATE_JSC_BINDINGS 1 + +namespace JSC { + +namespace Bindings { + +jvalue convertNPVariantToJValue(NPVariant, JNIType, const char* javaClassName); +void convertJValueToNPVariant(jvalue, JNIType, const char* javaClassName, NPVariant*); + +} // namespace Bindings + +} // namespace JSC + +#endif // JNIUtilityPrivate_h diff --git a/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp b/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp new file mode 100644 index 0000000..1d381af --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaClassV8.cpp @@ -0,0 +1,103 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + */ + +#include "config.h" +#include "JavaClassV8.h" + +using namespace JSC::Bindings; + +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); + return; + } + + int i; + JNIEnv* env = getJNIEnv(); + + // Get the fields + jarray fields = static_cast<jarray>(callJNIMethod<jobject>(aClass, "getFields", "()[Ljava/lang/reflect/Field;")); + int numFields = env->GetArrayLength(fields); + for (i = 0; i < numFields; i++) { + jobject aJField = env->GetObjectArrayElement(static_cast<jobjectArray>(fields), i); + JavaField* aField = new JavaField(env, aJField); // deleted in the JavaClass destructor + { + m_fields.set(aField->name().utf8(), aField); + } + env->DeleteLocalRef(aJField); + } + + // Get the methods + jarray methods = static_cast<jarray>(callJNIMethod<jobject>(aClass, "getMethods", "()[Ljava/lang/reflect/Method;")); + 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 + MethodList* methodList; + { + methodList = m_methods.get(aMethod->name().utf8()); + if (!methodList) { + methodList = new MethodList(); + m_methods.set(aMethod->name().utf8(), methodList); + } + } + methodList->append(aMethod); + env->DeleteLocalRef(aJMethod); + } + env->DeleteLocalRef(fields); + env->DeleteLocalRef(methods); + env->DeleteLocalRef(aClass); +} + +JavaClass::~JavaClass() +{ + deleteAllValues(m_fields); + m_fields.clear(); + + MethodListMap::const_iterator end = m_methods.end(); + for (MethodListMap::const_iterator it = m_methods.begin(); it != end; ++it) { + const MethodList* methodList = it->second; + deleteAllValues(*methodList); + delete methodList; + } + m_methods.clear(); +} + +MethodList JavaClass::methodsNamed(const char* name) const +{ + MethodList* methodList = m_methods.get(name); + + if (methodList) + return *methodList; + return MethodList(); +} + +JavaField* JavaClass::fieldNamed(const char* name) const +{ + return m_fields.get(name); +} diff --git a/Source/WebCore/bridge/jni/v8/JavaClassV8.h b/Source/WebCore/bridge/jni/v8/JavaClassV8.h new file mode 100644 index 0000000..99137f1 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaClassV8.h @@ -0,0 +1,60 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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 JavaClassV8_h +#define JavaClassV8_h + +#include "JNIBridgeV8.h" +#include "PlatformString.h" +#include <wtf/HashMap.h> +#include <wtf/Vector.h> +#include <wtf/text/StringHash.h> + +namespace JSC { + +namespace Bindings { + +typedef Vector<JavaMethod*> MethodList; +typedef HashMap<WTF::String, MethodList*> MethodListMap; +typedef HashMap<WTF::String, JavaField*> FieldMap; + +class JavaClass { +public: + JavaClass(jobject anInstance); + ~JavaClass(); + + MethodList methodsNamed(const char* name) const; + JavaField* fieldNamed(const char* name) const; + +private: + MethodListMap m_methods; + FieldMap m_fields; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaClassV8_h diff --git a/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp new file mode 100644 index 0000000..27adca3 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved. + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + */ + +#include "config.h" +#include "JavaInstanceV8.h" + +#include "JNIBridge.h" +#include "JNIUtilityPrivate.h" +#include "JavaClassV8.h" + +#include <assert.h> + +using namespace JSC::Bindings; + +JavaInstance::JavaInstance(jobject instance) +{ + m_instance = new JObjectWrapper(instance); + m_class = 0; +} + +JavaInstance::~JavaInstance() +{ + m_instance = 0; + delete m_class; +} + +#define NUM_LOCAL_REFS 64 + +void JavaInstance::virtualBegin() +{ + getJNIEnv()->PushLocalFrame(NUM_LOCAL_REFS); +} + +void JavaInstance::virtualEnd() +{ + getJNIEnv()->PopLocalFrame(0); +} + +JavaClass* JavaInstance::getClass() const +{ + if (!m_class) + m_class = new JavaClass(javaInstance()); + return m_class; +} + +bool JavaInstance::invokeMethod(const char* methodName, const NPVariant* args, int count, NPVariant* resultValue) +{ + 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++) { + JavaParameter* aParameter = jMethod->parameterAt(i); + jArgs[i] = convertNPVariantToJValue(args[i], aParameter->getJNIType(), aParameter->type()); + } + + 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); + + return true; +} + +JObjectWrapper::JObjectWrapper(jobject instance) + : m_refCount(0) +{ + assert(instance); + + // Cache the JNIEnv used to get the global ref for this java instanace. + // It'll be used to delete the reference. + m_env = getJNIEnv(); + + m_instance = m_env->NewGlobalRef(instance); + + if (!m_instance) + fprintf(stderr, "%s: could not get GlobalRef for %p\n", __PRETTY_FUNCTION__, instance); +} + +JObjectWrapper::~JObjectWrapper() +{ + m_env->DeleteGlobalRef(m_instance); +} diff --git a/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h new file mode 100644 index 0000000..4f009a5 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaInstanceV8.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved. + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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 JavaInstanceV8_h +#define JavaInstanceV8_h + +#include "npruntime.h" + +#include <JavaVM/jni.h> +#include <wtf/RefCounted.h> +#include <wtf/RefPtr.h> + +using namespace WTF; + +namespace JSC { + +namespace Bindings { + +class JavaClass; + +class JObjectWrapper { +friend class RefPtr<JObjectWrapper>; +friend class JavaField; +friend class JavaInstance; + +public: + jobject instance() const { return m_instance; } + void setInstance(jobject instance) { m_instance = instance; } + + void ref() { m_refCount++; } + void deref() + { + if (!(--m_refCount)) + delete this; + } + +protected: + JObjectWrapper(jobject); + ~JObjectWrapper(); + + jobject m_instance; + +private: + JNIEnv* m_env; + unsigned int m_refCount; +}; + +class JavaInstance : public RefCounted<JavaInstance> { +public: + JavaInstance(jobject instance); + virtual ~JavaInstance(); + + JavaClass* getClass() const; + + bool invokeMethod(const char* name, const NPVariant* args, int argsCount, NPVariant* result); + + jobject javaInstance() const { return m_instance->m_instance; } + + // These functions are called before and after the main entry points into + // the native implementations. They can be used to establish and cleanup + // any needed state. + void begin() { virtualBegin(); } + void end() { virtualEnd(); } + +protected: + RefPtr<JObjectWrapper> m_instance; + mutable JavaClass* m_class; + + virtual void virtualBegin(); + virtual void virtualEnd(); +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaInstanceV8_h diff --git a/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp b/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp new file mode 100644 index 0000000..3bb8e27 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp @@ -0,0 +1,176 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + */ + + +#include "config.h" +#include "JavaNPObjectV8.h" + +#include "JNIUtilityPrivate.h" +#include "JavaClassV8.h" +#include "JavaInstanceV8.h" +#include "npruntime_impl.h" + +namespace JSC { + +namespace Bindings { + +static NPObject* AllocJavaNPObject(NPP, NPClass*) +{ + JavaNPObject* obj = static_cast<JavaNPObject*>(malloc(sizeof(JavaNPObject))); + if (!obj) + return 0; + memset(obj, 0, sizeof(JavaNPObject)); + return reinterpret_cast<NPObject*>(obj); +} + +static void FreeJavaNPObject(NPObject* npobj) +{ + JavaNPObject* obj = reinterpret_cast<JavaNPObject*>(npobj); + obj->m_instance = 0; // free does not call the destructor + free(obj); +} + +static NPClass JavaNPObjectClass = { + NP_CLASS_STRUCT_VERSION, + AllocJavaNPObject, // allocate, + FreeJavaNPObject, // free, + 0, // invalidate + JavaNPObjectHasMethod, + JavaNPObjectInvoke, + 0, // invokeDefault, + JavaNPObjectHasProperty, + JavaNPObjectGetProperty, + 0, // setProperty + 0, // removeProperty + 0, // enumerate + 0 // construct +}; + +NPObject* JavaInstanceToNPObject(JavaInstance* instance) +{ + JavaNPObject* object = reinterpret_cast<JavaNPObject*>(_NPN_CreateObject(0, &JavaNPObjectClass)); + object->m_instance = instance; + return reinterpret_cast<NPObject*>(object); +} + +// Returns null if obj is not a wrapper of JavaInstance +JavaInstance* ExtractJavaInstance(NPObject* obj) +{ + if (obj->_class == &JavaNPObjectClass) + return reinterpret_cast<JavaNPObject*>(obj)->m_instance.get(); + return 0; +} + +bool JavaNPObjectHasMethod(NPObject* obj, NPIdentifier identifier) +{ + JavaInstance* instance = ExtractJavaInstance(obj); + if (!instance) + return false; + NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); + if (!name) + return false; + + instance->begin(); + bool result = (instance->getClass()->methodsNamed(name).size() > 0); + instance->end(); + + // TODO: use NPN_MemFree + free(name); + + return result; +} + +bool JavaNPObjectInvoke(NPObject* obj, NPIdentifier identifier, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + JavaInstance* instance = ExtractJavaInstance(obj); + if (!instance) + return false; + NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); + if (!name) + return false; + + instance->begin(); + bool r = instance->invokeMethod(name, args, argCount, result); + instance->end(); + + // TODO: use NPN_MemFree + free(name); + return r; +} + +bool JavaNPObjectHasProperty(NPObject* obj, NPIdentifier identifier) +{ + JavaInstance* instance = ExtractJavaInstance(obj); + if (!instance) + return false; + NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); + if (!name) + return false; + instance->begin(); + bool result = instance->getClass()->fieldNamed(name); + instance->end(); + free(name); + return result; +} + +bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result) +{ + VOID_TO_NPVARIANT(*result); + JavaInstance* instance = ExtractJavaInstance(obj); + if (!instance) + return false; + NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); + if (!name) + return false; + + instance->begin(); + JavaField* field = instance->getClass()->fieldNamed(name); + instance->end(); + free(name); // TODO: use NPN_MemFree + + if (!field) + return false; + +#ifdef EMULATE_JSC_BINDINGS + // JSC does not seem to support returning object properties so we emulate that + // behaviour here. + jvalue value; +#else + // FIXME: Note here that field->type() refers to the Java class name and NOT the + // JNI signature i.e. "int" as opposed to "I". This means that the field lookup + // will fail. + jvalue value = getJNIField(instance->javaInstance(), + field->getJNIType(), + field->name().utf8(), + field->type()); +#endif + convertJValueToNPVariant(value, field->getJNIType(), field->type(), result); + + return true; +} + +} // namespace Bindings + +} // namespace JSC diff --git a/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.h b/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.h new file mode 100644 index 0000000..31b0ac7 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaNPObjectV8.h @@ -0,0 +1,56 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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 JavaNPObjectV8_h +#define JavaNPObjectV8_h + +#include "npruntime.h" +#include <wtf/RefPtr.h> + + +namespace JSC { + +namespace Bindings { + +class JavaInstance; + +struct JavaNPObject { + NPObject m_object; + RefPtr<JavaInstance> m_instance; +}; + +NPObject* JavaInstanceToNPObject(JavaInstance*); +JavaInstance* ExtractJavaInstance(NPObject*); + +bool JavaNPObjectHasMethod(NPObject*, NPIdentifier name); +bool JavaNPObjectInvoke(NPObject*, NPIdentifier methodName, const NPVariant* args, uint32_t argCount, NPVariant* result); +bool JavaNPObjectHasProperty(NPObject*, NPIdentifier name); +bool JavaNPObjectGetProperty(NPObject*, NPIdentifier name, NPVariant* result); + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaNPObjectV8_h diff --git a/Source/WebCore/bridge/jni/v8/JavaStringV8.h b/Source/WebCore/bridge/jni/v8/JavaStringV8.h new file mode 100644 index 0000000..827d9f5 --- /dev/null +++ b/Source/WebCore/bridge/jni/v8/JavaStringV8.h @@ -0,0 +1,61 @@ +/* + * Copyright 2010, 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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 JavaStringV8_h +#define JavaStringV8_h + +#include "JNIUtility.h" +#include <wtf/text/CString.h> + + +namespace JSC { + +namespace Bindings { + +class JavaStringImpl { +public: + void init() {} + + void init(JNIEnv* e, jstring s) + { + int size = e->GetStringLength(s); + const char* cs = getCharactersFromJStringInEnv(e, s); + m_utf8String = WTF::CString(cs, size); + releaseCharactersForJStringInEnv(e, s, cs); + } + + const char* utf8() const { return m_utf8String.data(); } + const jchar* uchars() const { return 0; } // Not implemented + int length() const { return m_utf8String.length(); } + +private: + WTF::CString m_utf8String; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaStringV8_h |