diff options
Diffstat (limited to 'WebCore/bridge')
45 files changed, 2144 insertions, 1297 deletions
diff --git a/WebCore/bridge/Bridge.h b/WebCore/bridge/Bridge.h new file mode 100644 index 0000000..d92cd15 --- /dev/null +++ b/WebCore/bridge/Bridge.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003, 2008, 2009 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: + * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 Bridge_h +#define Bridge_h + +#include "BridgeJSC.h" + +namespace JSC { + +namespace Bindings { + +class Method : public Noncopyable { +public: + virtual int numParameters() const = 0; + + virtual ~Method() { } +}; + +} // namespace Bindings + +} // namespace JSC + +#endif diff --git a/WebCore/bridge/c/c_class.h b/WebCore/bridge/c/c_class.h index 7f20af4..52db2b9 100644 --- a/WebCore/bridge/c/c_class.h +++ b/WebCore/bridge/c/c_class.h @@ -28,8 +28,8 @@ #if ENABLE(NETSCAPE_PLUGIN_API) +#include "Bridge.h" #include "npruntime_internal.h" -#include "runtime.h" #include <wtf/HashMap.h> namespace JSC { diff --git a/WebCore/bridge/c/c_instance.cpp b/WebCore/bridge/c/c_instance.cpp index 77b5966..1b05259 100644 --- a/WebCore/bridge/c/c_instance.cpp +++ b/WebCore/bridge/c/c_instance.cpp @@ -138,7 +138,7 @@ JSValue CInstance::invokeMethod(ExecState* exec, const MethodList& methodList, c for (i = 0; i < count; i++) _NPN_ReleaseVariantValue(&cArgs[i]); - JSValue resultValue = convertNPVariantToValue(exec, &resultVariant, _rootObject.get()); + JSValue resultValue = convertNPVariantToValue(exec, &resultVariant, m_rootObject.get()); _NPN_ReleaseVariantValue(&resultVariant); return resultValue; } @@ -173,7 +173,7 @@ JSValue CInstance::invokeDefaultMethod(ExecState* exec, const ArgList& args) for (i = 0; i < count; i++) _NPN_ReleaseVariantValue(&cArgs[i]); - JSValue resultValue = convertNPVariantToValue(exec, &resultVariant, _rootObject.get()); + JSValue resultValue = convertNPVariantToValue(exec, &resultVariant, m_rootObject.get()); _NPN_ReleaseVariantValue(&resultVariant); return resultValue; } @@ -212,7 +212,7 @@ JSValue CInstance::invokeConstruct(ExecState* exec, const ArgList& args) for (i = 0; i < count; i++) _NPN_ReleaseVariantValue(&cArgs[i]); - JSValue resultValue = convertNPVariantToValue(exec, &resultVariant, _rootObject.get()); + JSValue resultValue = convertNPVariantToValue(exec, &resultVariant, m_rootObject.get()); _NPN_ReleaseVariantValue(&resultVariant); return resultValue; } diff --git a/WebCore/bridge/c/c_instance.h b/WebCore/bridge/c/c_instance.h index f9e9de3..7109582 100644 --- a/WebCore/bridge/c/c_instance.h +++ b/WebCore/bridge/c/c_instance.h @@ -28,9 +28,9 @@ #if ENABLE(NETSCAPE_PLUGIN_API) -#include "runtime.h" -#include <wtf/PassRefPtr.h> +#include "Bridge.h" #include "runtime_root.h" +#include <wtf/PassRefPtr.h> typedef struct NPObject NPObject; diff --git a/WebCore/bridge/c/c_runtime.h b/WebCore/bridge/c/c_runtime.h index e797342..5355934 100644 --- a/WebCore/bridge/c/c_runtime.h +++ b/WebCore/bridge/c/c_runtime.h @@ -28,8 +28,8 @@ #if ENABLE(NETSCAPE_PLUGIN_API) +#include "Bridge.h" #include "npruntime_internal.h" -#include "runtime.h" namespace JSC { namespace Bindings { diff --git a/WebCore/bridge/jni/JNIBridge.cpp b/WebCore/bridge/jni/JNIBridge.cpp new file mode 100644 index 0000000..627d227 --- /dev/null +++ b/WebCore/bridge/jni/JNIBridge.cpp @@ -0,0 +1,576 @@ +/* + * Copyright (C) 2003, 2004, 2005, 2007, 2009 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: + * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 "JNIBridge.h" + +#if ENABLE(MAC_JAVA_BRIDGE) + +#include "CString.h" +#include "JNIUtility.h" +#include "JNIUtilityPrivate.h" +#include "StringBuilder.h" +#include "runtime_array.h" +#include "runtime_object.h" +#include "runtime_root.h" +#include <runtime/Error.h> +#include <runtime/JSLock.h> + +#ifdef NDEBUG +#define JS_LOG(formatAndArgs...) ((void)0) +#else +#define JS_LOG(formatAndArgs...) { \ + fprintf(stderr, "%s:%d -- %s: ", __FILE__, __LINE__, __FUNCTION__); \ + fprintf(stderr, formatAndArgs); \ +} +#endif + +using namespace JSC; +using namespace JSC::Bindings; +using namespace WebCore; + + +JavaParameter::JavaParameter(JNIEnv* env, jstring type) +{ + m_type = JavaString(env, type); + m_JNIType = JNITypeFromClassName(m_type.UTF8String()); +} + +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.UTF8String()); + + // 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); +} + +JSValue JavaArray::convertJObjectToArray(ExecState* exec, jobject anObject, const char* type, PassRefPtr<RootObject> rootObject) +{ + if (type[0] != '[') + return jsUndefined(); + + return new (exec) RuntimeArray(exec, new JavaArray(anObject, type, rootObject)); +} + +jvalue JavaField::dispatchValueFromInstance(ExecState* exec, const JavaInstance* instance, const char* name, const char* sig, JNIType returnType) const +{ + jobject jinstance = instance->javaInstance(); + jobject fieldJInstance = m_field->m_instance; + JNIEnv* env = getJNIEnv(); + jvalue result; + + bzero(&result, sizeof(jvalue)); + jclass cls = env->GetObjectClass(fieldJInstance); + if (cls) { + jmethodID mid = env->GetMethodID(cls, name, sig); + if (mid) { + RootObject* rootObject = instance->rootObject(); + if (rootObject && rootObject->nativeHandle()) { + JSValue exceptionDescription; + jvalue args[1]; + + args[0].l = jinstance; + dispatchJNICall(exec, rootObject->nativeHandle(), fieldJInstance, false, returnType, mid, args, result, 0, exceptionDescription); + if (exceptionDescription) + throwError(exec, GeneralError, exceptionDescription.toString(exec)); + } + } + } + return result; +} + +JSValue JavaField::valueFromInstance(ExecState* exec, const Instance* i) const +{ + const JavaInstance* instance = static_cast<const JavaInstance*>(i); + + JSValue jsresult = jsUndefined(); + + switch (m_JNIType) { + case array_type: + case object_type: + { + jvalue result = dispatchValueFromInstance(exec, instance, "get", "(Ljava/lang/Object;)Ljava/lang/Object;", object_type); + jobject anObject = result.l; + + const char* arrayType = type(); + if (arrayType[0] == '[') + jsresult = JavaArray::convertJObjectToArray(exec, anObject, arrayType, instance->rootObject()); + else if (anObject) + jsresult = JavaInstance::create(anObject, instance->rootObject())->createRuntimeObject(exec); + } + break; + + case boolean_type: + jsresult = jsBoolean(dispatchValueFromInstance(exec, instance, "getBoolean", "(Ljava/lang/Object;)Z", boolean_type).z); + break; + + case byte_type: + case char_type: + case short_type: + + case int_type: + { + jint value; + jvalue result = dispatchValueFromInstance(exec, instance, "getInt", "(Ljava/lang/Object;)I", int_type); + value = result.i; + jsresult = jsNumber(exec, static_cast<int>(value)); + } + break; + + case long_type: + case float_type: + case double_type: + { + jdouble value; + jvalue result = dispatchValueFromInstance(exec, instance, "getDouble", "(Ljava/lang/Object;)D", double_type); + value = result.i; + jsresult = jsNumber(exec, static_cast<double>(value)); + } + break; + default: + break; + } + + JS_LOG("getting %s = %s\n", UString(name()).UTF8String().c_str(), jsresult.toString(exec).ascii()); + + return jsresult; +} + +void JavaField::dispatchSetValueToInstance(ExecState* exec, const JavaInstance* instance, jvalue javaValue, const char* name, const char* sig) const +{ + jobject jinstance = instance->javaInstance(); + jobject fieldJInstance = m_field->m_instance; + JNIEnv* env = getJNIEnv(); + + jclass cls = env->GetObjectClass(fieldJInstance); + if (cls) { + jmethodID mid = env->GetMethodID(cls, name, sig); + if (mid) { + RootObject* rootObject = instance->rootObject(); + if (rootObject && rootObject->nativeHandle()) { + JSValue exceptionDescription; + jvalue args[2]; + jvalue result; + + args[0].l = jinstance; + args[1] = javaValue; + dispatchJNICall(exec, rootObject->nativeHandle(), fieldJInstance, false, void_type, mid, args, result, 0, exceptionDescription); + if (exceptionDescription) + throwError(exec, GeneralError, exceptionDescription.toString(exec)); + } + } + } +} + +void JavaField::setValueToInstance(ExecState* exec, const Instance* i, JSValue aValue) const +{ + const JavaInstance* instance = static_cast<const JavaInstance*>(i); + jvalue javaValue = convertValueToJValue(exec, aValue, m_JNIType, type()); + + JS_LOG("setting value %s to %s\n", UString(name()).UTF8String().c_str(), aValue.toString(exec).ascii()); + + switch (m_JNIType) { + case array_type: + case object_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "set", "(Ljava/lang/Object;Ljava/lang/Object;)V"); + } + break; + + case boolean_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setBoolean", "(Ljava/lang/Object;Z)V"); + } + break; + + case byte_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setByte", "(Ljava/lang/Object;B)V"); + } + break; + + case char_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setChar", "(Ljava/lang/Object;C)V"); + } + break; + + case short_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setShort", "(Ljava/lang/Object;S)V"); + } + break; + + case int_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setInt", "(Ljava/lang/Object;I)V"); + } + break; + + case long_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setLong", "(Ljava/lang/Object;J)V"); + } + break; + + case float_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setFloat", "(Ljava/lang/Object;F)V"); + } + break; + + case double_type: + { + dispatchSetValueToInstance(exec, instance, javaValue, "setDouble", "(Ljava/lang/Object;D)V"); + } + break; + default: + break; + } +} + +JavaMethod::JavaMethod(JNIEnv* env, jobject aMethod) +{ + // Get return type + jobject returnType = callJNIMethod<jobject>(aMethod, "getReturnType", "()Ljava/lang/Class;"); + jstring returnTypeName = static_cast<jstring>(callJNIMethod<jobject>(returnType, "getName", "()Ljava/lang/String;")); + m_returnType = JavaString(env, returnTypeName); + m_JNIReturnType = JNITypeFromClassName(m_returnType.UTF8String()); + env->DeleteLocalRef(returnType); + env->DeleteLocalRef(returnTypeName); + + // Get method name + jstring methodName = static_cast<jstring>(callJNIMethod<jobject>(aMethod, "getName", "()Ljava/lang/String;")); + m_name = JavaString(env, methodName); + env->DeleteLocalRef(methodName); + + // Get parameters + jarray jparameters = static_cast<jarray>(callJNIMethod<jobject>(aMethod, "getParameterTypes", "()[Ljava/lang/Class;")); + m_numParameters = env->GetArrayLength(jparameters); + m_parameters = new JavaParameter[m_numParameters]; + + for (int i = 0; i < m_numParameters; i++) { + jobject aParameter = env->GetObjectArrayElement(static_cast<jobjectArray>(jparameters), i); + jstring parameterName = static_cast<jstring>(callJNIMethod<jobject>(aParameter, "getName", "()Ljava/lang/String;")); + m_parameters[i] = JavaParameter(env, parameterName); + env->DeleteLocalRef(aParameter); + env->DeleteLocalRef(parameterName); + } + env->DeleteLocalRef(jparameters); + + // Created lazily. + m_signature = 0; + m_methodID = 0; + + jclass modifierClass = env->FindClass("java/lang/reflect/Modifier"); + int modifiers = callJNIMethod<jint>(aMethod, "getModifiers", "()I"); + m_isStatic = static_cast<bool>(callJNIStaticMethod<jboolean>(modifierClass, "isStatic", "(I)Z", modifiers)); + env->DeleteLocalRef(modifierClass); +} + +JavaMethod::~JavaMethod() +{ + if (m_signature) + free(m_signature); + delete[] m_parameters; +}; + +// JNI method signatures use '/' between components of a class name, but +// we get '.' between components from the reflection API. +static void appendClassName(StringBuilder& builder, const char* className) +{ + ASSERT(JSLock::lockCount() > 0); + + char* c = strdup(className); + + char* result = c; + while (*c) { + if (*c == '.') + *c = '/'; + c++; + } + + builder.append(result); + + free(result); +} + +const char* JavaMethod::signature() const +{ + if (!m_signature) { + JSLock lock(SilenceAssertionsOnly); + + StringBuilder signatureBuilder; + signatureBuilder.append("("); + for (int i = 0; i < m_numParameters; i++) { + JavaParameter* aParameter = parameterAt(i); + JNIType type = aParameter->getJNIType(); + if (type == array_type) + appendClassName(signatureBuilder, aParameter->type()); + else { + signatureBuilder.append(signatureFromPrimitiveType(type)); + if (type == object_type) { + appendClassName(signatureBuilder, aParameter->type()); + signatureBuilder.append(";"); + } + } + } + signatureBuilder.append(")"); + + const char* returnType = m_returnType.UTF8String(); + if (m_JNIReturnType == array_type) + appendClassName(signatureBuilder, returnType); + else { + signatureBuilder.append(signatureFromPrimitiveType(m_JNIReturnType)); + if (m_JNIReturnType == object_type) { + appendClassName(signatureBuilder, returnType); + signatureBuilder.append(";"); + } + } + + String signatureString = signatureBuilder.toString(); + m_signature = strdup(signatureString.utf8().data()); + } + + return m_signature; +} + +JNIType JavaMethod::JNIReturnType() const +{ + return m_JNIReturnType; +} + +jmethodID JavaMethod::methodID(jobject obj) const +{ + if (!m_methodID) + m_methodID = getMethodID(obj, m_name.UTF8String(), signature()); + return m_methodID; +} + + +JavaArray::JavaArray(jobject array, const char* type, PassRefPtr<RootObject> rootObject) + : Array(rootObject) +{ + m_array = new JObjectWrapper(array); + // Java array are fixed length, so we can cache length. + JNIEnv* env = getJNIEnv(); + m_length = env->GetArrayLength(static_cast<jarray>(m_array->m_instance)); + m_type = strdup(type); + m_rootObject = rootObject; +} + +JavaArray::~JavaArray() +{ + free(const_cast<char*>(m_type)); +} + +RootObject* JavaArray::rootObject() const +{ + return m_rootObject && m_rootObject->isValid() ? m_rootObject.get() : 0; +} + +void JavaArray::setValueAt(ExecState* exec, unsigned index, JSValue aValue) const +{ + JNIEnv* env = getJNIEnv(); + char* javaClassName = 0; + + JNIType arrayType = JNITypeFromPrimitiveType(m_type[1]); + if (m_type[1] == 'L') { + // The type of the array will be something like: + // "[Ljava.lang.string;". This is guaranteed, so no need + // for extra sanity checks. + javaClassName = strdup(&m_type[2]); + javaClassName[strchr(javaClassName, ';')-javaClassName] = 0; + } + jvalue aJValue = convertValueToJValue(exec, aValue, arrayType, javaClassName); + + switch (arrayType) { + case object_type: + { + env->SetObjectArrayElement(static_cast<jobjectArray>(javaArray()), index, aJValue.l); + break; + } + + case boolean_type: + { + env->SetBooleanArrayRegion(static_cast<jbooleanArray>(javaArray()), index, 1, &aJValue.z); + break; + } + + case byte_type: + { + env->SetByteArrayRegion(static_cast<jbyteArray>(javaArray()), index, 1, &aJValue.b); + break; + } + + case char_type: + { + env->SetCharArrayRegion(static_cast<jcharArray>(javaArray()), index, 1, &aJValue.c); + break; + } + + case short_type: + { + env->SetShortArrayRegion(static_cast<jshortArray>(javaArray()), index, 1, &aJValue.s); + break; + } + + case int_type: + { + env->SetIntArrayRegion(static_cast<jintArray>(javaArray()), index, 1, &aJValue.i); + break; + } + + case long_type: + { + env->SetLongArrayRegion(static_cast<jlongArray>(javaArray()), index, 1, &aJValue.j); + } + + case float_type: + { + env->SetFloatArrayRegion(static_cast<jfloatArray>(javaArray()), index, 1, &aJValue.f); + break; + } + + case double_type: + { + env->SetDoubleArrayRegion(static_cast<jdoubleArray>(javaArray()), index, 1, &aJValue.d); + break; + } + default: + break; + } + + if (javaClassName) + free(const_cast<char*>(javaClassName)); +} + + +JSValue JavaArray::valueAt(ExecState* exec, unsigned index) const +{ + JNIEnv* env = getJNIEnv(); + JNIType arrayType = JNITypeFromPrimitiveType(m_type[1]); + switch (arrayType) { + case object_type: + { + jobjectArray objectArray = static_cast<jobjectArray>(javaArray()); + jobject anObject; + anObject = env->GetObjectArrayElement(objectArray, index); + + // No object? + if (!anObject) + return jsNull(); + + // Nested array? + if (m_type[1] == '[') + return JavaArray::convertJObjectToArray(exec, anObject, m_type + 1, rootObject()); + // or array of other object type? + return JavaInstance::create(anObject, rootObject())->createRuntimeObject(exec); + } + + case boolean_type: + { + jbooleanArray booleanArray = static_cast<jbooleanArray>(javaArray()); + jboolean aBoolean; + env->GetBooleanArrayRegion(booleanArray, index, 1, &aBoolean); + return jsBoolean(aBoolean); + } + + case byte_type: + { + jbyteArray byteArray = static_cast<jbyteArray>(javaArray()); + jbyte aByte; + env->GetByteArrayRegion(byteArray, index, 1, &aByte); + return jsNumber(exec, aByte); + } + + case char_type: + { + jcharArray charArray = static_cast<jcharArray>(javaArray()); + jchar aChar; + env->GetCharArrayRegion(charArray, index, 1, &aChar); + return jsNumber(exec, aChar); + break; + } + + case short_type: + { + jshortArray shortArray = static_cast<jshortArray>(javaArray()); + jshort aShort; + env->GetShortArrayRegion(shortArray, index, 1, &aShort); + return jsNumber(exec, aShort); + } + + case int_type: + { + jintArray intArray = static_cast<jintArray>(javaArray()); + jint anInt; + env->GetIntArrayRegion(intArray, index, 1, &anInt); + return jsNumber(exec, anInt); + } + + case long_type: + { + jlongArray longArray = static_cast<jlongArray>(javaArray()); + jlong aLong; + env->GetLongArrayRegion(longArray, index, 1, &aLong); + return jsNumber(exec, aLong); + } + + case float_type: + { + jfloatArray floatArray = static_cast<jfloatArray>(javaArray()); + jfloat aFloat; + env->GetFloatArrayRegion(floatArray, index, 1, &aFloat); + return jsNumber(exec, aFloat); + } + + case double_type: + { + jdoubleArray doubleArray = static_cast<jdoubleArray>(javaArray()); + jdouble aDouble; + env->GetDoubleArrayRegion(doubleArray, index, 1, &aDouble); + return jsNumber(exec, aDouble); + } + default: + break; + } + return jsUndefined(); +} + +unsigned int JavaArray::getLength() const +{ + return m_length; +} + +#endif // ENABLE(MAC_JAVA_BRIDGE) diff --git a/WebCore/bridge/jni/JNIBridge.h b/WebCore/bridge/jni/JNIBridge.h new file mode 100644 index 0000000..ce129af --- /dev/null +++ b/WebCore/bridge/jni/JNIBridge.h @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2003, 2004, 2005, 2007, 2009, 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: + * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 JNIBridge_h +#define JNIBridge_h + +#if ENABLE(MAC_JAVA_BRIDGE) + +#include "JNIUtility.h" +#include "JavaInstanceJSC.h" + +#if USE(JSC) +#include "JavaStringJSC.h" +#endif + +namespace JSC { + +namespace Bindings { + +typedef const char* RuntimeType; + +class JavaString { +public: + JavaString() + { + m_impl.init(); + } + + JavaString(JNIEnv* e, jstring s) + { + m_impl.init(e, s); + } + + JavaString(jstring s) + { + m_impl.init(getJNIEnv(), s); + } + + const char* UTF8String() const { return m_impl.UTF8String(); } + const jchar* uchars() const { return m_impl.uchars(); } + int length() const { return m_impl.length(); } +#if USE(JSC) + operator UString() const { return m_impl.uString(); } +#endif + +private: + JavaStringImpl m_impl; +}; + +class JavaParameter { +public: + JavaParameter() : m_JNIType(invalid_type) { } + JavaParameter(JNIEnv*, jstring type); + virtual ~JavaParameter() { } + + RuntimeType type() const { return m_type.UTF8String(); } + JNIType getJNIType() const { return m_JNIType; } + +private: + JavaString m_type; + JNIType m_JNIType; +}; + + +class JavaField : public Field { +public: + JavaField(JNIEnv*, jobject aField); + + virtual JSValue valueFromInstance(ExecState*, const Instance*) const; + virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const; + + const JavaString& name() const { return m_name; } + virtual RuntimeType type() const { return m_type.UTF8String(); } + + JNIType getJNIType() const { return m_JNIType; } + +private: + void dispatchSetValueToInstance(ExecState*, const JavaInstance*, jvalue, const char* name, const char* sig) const; + jvalue dispatchValueFromInstance(ExecState*, const JavaInstance*, const char* name, const char* sig, JNIType returnType) const; + + JavaString m_name; + JavaString m_type; + JNIType m_JNIType; + RefPtr<JObjectWrapper> m_field; +}; + + +class JavaMethod : public Method { +public: + JavaMethod(JNIEnv*, jobject aMethod); + ~JavaMethod(); + + const JavaString& name() const { return m_name; } + RuntimeType returnType() const { return m_returnType.UTF8String(); } + JavaParameter* parameterAt(int i) const { return &m_parameters[i]; } + int numParameters() const { return m_numParameters; } + + const char* signature() const; + JNIType JNIReturnType() const; + + jmethodID methodID(jobject obj) const; + + bool isStatic() const { return m_isStatic; } + +private: + JavaParameter* m_parameters; + int m_numParameters; + JavaString m_name; + mutable char* m_signature; + JavaString m_returnType; + JNIType m_JNIReturnType; + mutable jmethodID m_methodID; + bool m_isStatic; +}; + +class JavaArray : public Array { +public: + JavaArray(jobject array, const char* type, PassRefPtr<RootObject>); + virtual ~JavaArray(); + + RootObject* rootObject() const; + + virtual void setValueAt(ExecState*, unsigned int index, JSValue) const; + virtual JSValue valueAt(ExecState*, unsigned int index) const; + virtual unsigned int getLength() const; + + jobject javaArray() const { return m_array->m_instance; } + + static JSValue convertJObjectToArray(ExecState*, jobject, const char* type, PassRefPtr<RootObject>); + +private: + RefPtr<JObjectWrapper> m_array; + unsigned int m_length; + const char* m_type; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // ENABLE(MAC_JAVA_BRIDGE) + +#endif // JNIBridge_h diff --git a/WebCore/bridge/jni/jni_utility.cpp b/WebCore/bridge/jni/JNIUtility.cpp index faa7a8b..ee45710 100644 --- a/WebCore/bridge/jni/jni_utility.cpp +++ b/WebCore/bridge/jni/JNIUtility.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -20,11 +20,11 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" -#include "jni_utility.h" +#include "JNIUtility.h" #if ENABLE(MAC_JAVA_BRIDGE) @@ -34,7 +34,7 @@ namespace JSC { namespace Bindings { -static jint KJS_GetCreatedJavaVMs(JavaVM** vmBuf, jsize bufLen, jsize* nVMs) +static jint KJSGetCreatedJavaVMs(JavaVM** vmBuf, jsize bufLen, jsize* nVMs) { static void* javaVMFramework = 0; if (!javaVMFramework) @@ -42,43 +42,43 @@ static jint KJS_GetCreatedJavaVMs(JavaVM** vmBuf, jsize bufLen, jsize* nVMs) if (!javaVMFramework) return JNI_ERR; - static jint(*functionPointer)(JavaVM**, jsize, jsize *) = 0; + typedef jint(*FunctionPointerType)(JavaVM**, jsize, jsize*); + static FunctionPointerType functionPointer = 0; if (!functionPointer) - functionPointer = (jint(*)(JavaVM**, jsize, jsize *))dlsym(javaVMFramework, "JNI_GetCreatedJavaVMs"); + functionPointer = reinterpret_cast<FunctionPointerType>(dlsym(javaVMFramework, "JNI_GetCreatedJavaVMs")); if (!functionPointer) return JNI_ERR; return functionPointer(vmBuf, bufLen, nVMs); } -static JavaVM *jvm = 0; +static JavaVM* jvm = 0; // Provide the ability for an outside component to specify the JavaVM to use -// If the jvm value is set, the getJavaVM function below will just return. +// If the jvm value is set, the getJavaVM function below will just return. // In getJNIEnv(), if AttachCurrentThread is called to a VM that is already // attached, the result is a no-op. -void setJavaVM(JavaVM *javaVM) +void setJavaVM(JavaVM* javaVM) { jvm = javaVM; } -JavaVM *getJavaVM() +JavaVM* getJavaVM() { if (jvm) return jvm; - JavaVM *jvmArray[1]; + JavaVM* jvmArray[1]; jsize bufLen = 1; jsize nJVMs = 0; jint jniError = 0; // Assumes JVM is already running ..., one per process - jniError = KJS_GetCreatedJavaVMs(jvmArray, bufLen, &nJVMs); - if ( jniError == JNI_OK && nJVMs > 0 ) { + jniError = KJSGetCreatedJavaVMs(jvmArray, bufLen, &nJVMs); + if (jniError == JNI_OK && nJVMs > 0) jvm = jvmArray[0]; - } - else - fprintf(stderr, "%s: JNI_GetCreatedJavaVMs failed, returned %ld\n", __PRETTY_FUNCTION__, (long)jniError); - + else + fprintf(stderr, "%s: JNI_GetCreatedJavaVMs failed, returned %ld\n", __PRETTY_FUNCTION__, static_cast<long>(jniError)); + return jvm; } @@ -90,29 +90,27 @@ JNIEnv* getJNIEnv() } u; jint jniError = 0; - jniError = (getJavaVM())->AttachCurrentThread(&u.dummy, NULL); + jniError = getJavaVM()->AttachCurrentThread(&u.dummy, 0); if (jniError == JNI_OK) return u.env; - else - fprintf(stderr, "%s: AttachCurrentThread failed, returned %ld\n", __PRETTY_FUNCTION__, (long)jniError); - return NULL; + fprintf(stderr, "%s: AttachCurrentThread failed, returned %ld\n", __PRETTY_FUNCTION__, static_cast<long>(jniError)); + return 0; } -jmethodID getMethodID (jobject obj, const char *name, const char *sig) +jmethodID getMethodID(jobject obj, const char* name, const char* sig) { - JNIEnv *env = getJNIEnv(); + JNIEnv* env = getJNIEnv(); jmethodID mid = 0; - - if ( env != NULL) { - jclass cls = env->GetObjectClass(obj); - if ( cls != NULL ) { + + if (env) { + jclass cls = env->GetObjectClass(obj); + if (cls) { mid = env->GetMethodID(cls, name, sig); if (!mid) { env->ExceptionClear(); mid = env->GetStaticMethodID(cls, name, sig); - if (!mid) { + if (!mid) env->ExceptionClear(); - } } } env->DeleteLocalRef(cls); @@ -120,118 +118,118 @@ jmethodID getMethodID (jobject obj, const char *name, const char *sig) return mid; } -const char *getCharactersFromJString (jstring aJString) +const char* getCharactersFromJString(jstring aJString) { - return getCharactersFromJStringInEnv (getJNIEnv(), aJString); + return getCharactersFromJStringInEnv(getJNIEnv(), aJString); } -void releaseCharactersForJString (jstring aJString, const char *s) +void releaseCharactersForJString(jstring aJString, const char* s) { - releaseCharactersForJStringInEnv (getJNIEnv(), aJString, s); + releaseCharactersForJStringInEnv(getJNIEnv(), aJString, s); } -const char *getCharactersFromJStringInEnv (JNIEnv *env, jstring aJString) +const char* getCharactersFromJStringInEnv(JNIEnv* env, jstring aJString) { jboolean isCopy; - const char *s = env->GetStringUTFChars((jstring)aJString, &isCopy); + const char* s = env->GetStringUTFChars(aJString, &isCopy); if (!s) { env->ExceptionDescribe(); env->ExceptionClear(); - fprintf (stderr, "\n"); + fprintf(stderr, "\n"); } return s; } -void releaseCharactersForJStringInEnv (JNIEnv *env, jstring aJString, const char *s) +void releaseCharactersForJStringInEnv(JNIEnv* env, jstring aJString, const char* s) { - env->ReleaseStringUTFChars (aJString, s); + env->ReleaseStringUTFChars(aJString, s); } -const jchar *getUCharactersFromJStringInEnv (JNIEnv *env, jstring aJString) +const jchar* getUCharactersFromJStringInEnv(JNIEnv* env, jstring aJString) { jboolean isCopy; - const jchar *s = env->GetStringChars((jstring)aJString, &isCopy); + const jchar* s = env->GetStringChars(aJString, &isCopy); if (!s) { env->ExceptionDescribe(); env->ExceptionClear(); - fprintf (stderr, "\n"); + fprintf(stderr, "\n"); } return s; } -void releaseUCharactersForJStringInEnv (JNIEnv *env, jstring aJString, const jchar *s) +void releaseUCharactersForJStringInEnv(JNIEnv* env, jstring aJString, const jchar* s) { - env->ReleaseStringChars (aJString, s); + env->ReleaseStringChars(aJString, s); } -JNIType JNITypeFromClassName(const char *name) +JNIType JNITypeFromClassName(const char* name) { JNIType type; - - if (strcmp("byte",name) == 0) + + if (!strcmp("byte", name)) type = byte_type; - else if (strcmp("short",name) == 0) + else if (!strcmp("short", name)) type = short_type; - else if (strcmp("int",name) == 0) + else if (!strcmp("int", name)) type = int_type; - else if (strcmp("long",name) == 0) + else if (!strcmp("long", name)) type = long_type; - else if (strcmp("float",name) == 0) + else if (!strcmp("float", name)) type = float_type; - else if (strcmp("double",name) == 0) + else if (!strcmp("double", name)) type = double_type; - else if (strcmp("char",name) == 0) + else if (!strcmp("char", name)) type = char_type; - else if (strcmp("boolean",name) == 0) + else if (!strcmp("boolean", name)) type = boolean_type; - else if (strcmp("void",name) == 0) + else if (!strcmp("void", name)) type = void_type; - else if ('[' == name[0]) + else if ('[' == name[0]) type = array_type; else type = object_type; - + return type; } -const char *signatureFromPrimitiveType(JNIType type) +const char* signatureFromPrimitiveType(JNIType type) { - switch (type){ - case void_type: - return "V"; - - case array_type: - return "["; - - case object_type: - return "L"; - - case boolean_type: - return "Z"; - - case byte_type: - return "B"; - - case char_type: - return "C"; - - case short_type: - return "S"; - - case int_type: - return "I"; - - case long_type: - return "J"; - - case float_type: - return "F"; - - case double_type: - return "D"; - - case invalid_type: - default: + switch (type) { + case void_type: + return "V"; + + case array_type: + return "["; + + case object_type: + return "L"; + + case boolean_type: + return "Z"; + + case byte_type: + return "B"; + + case char_type: + return "C"; + + case short_type: + return "S"; + + case int_type: + return "I"; + + case long_type: + return "J"; + + case float_type: + return "F"; + + case double_type: + return "D"; + + case invalid_type: + default: break; } return ""; @@ -239,58 +237,58 @@ const char *signatureFromPrimitiveType(JNIType type) JNIType JNITypeFromPrimitiveType(char type) { - switch (type){ - case 'V': - return void_type; - - case 'L': - return object_type; - - case '[': - return array_type; - - case 'Z': - return boolean_type; - - case 'B': - return byte_type; - - case 'C': - return char_type; - - case 'S': - return short_type; - - case 'I': - return int_type; - - case 'J': - return long_type; - - case 'F': - return float_type; - - case 'D': - return double_type; - - default: + switch (type) { + case 'V': + return void_type; + + case 'L': + return object_type; + + case '[': + return array_type; + + case 'Z': + return boolean_type; + + case 'B': + return byte_type; + + case 'C': + return char_type; + + case 'S': + return short_type; + + case 'I': + return int_type; + + case 'J': + return long_type; + + case 'F': + return float_type; + + case 'D': + return double_type; + + default: break; } return invalid_type; } -jvalue getJNIField( jobject obj, JNIType type, const char *name, const char *signature) +jvalue getJNIField(jobject obj, JNIType type, const char* name, const char* signature) { - JavaVM *jvm = getJavaVM(); - JNIEnv *env = getJNIEnv(); + JavaVM* jvm = getJavaVM(); + JNIEnv* env = getJNIEnv(); jvalue result; - bzero (&result, sizeof(jvalue)); - if ( obj != NULL && jvm != NULL && env != NULL) { + bzero(&result, sizeof(jvalue)); + if (obj && jvm && env) { jclass cls = env->GetObjectClass(obj); - if ( cls != NULL ) { + if (cls) { jfieldID field = env->GetFieldID(cls, name, signature); - if ( field != NULL ) { + if (field) { switch (type) { case array_type: case object_type: @@ -321,29 +319,25 @@ jvalue getJNIField( jobject obj, JNIType type, const char *name, const char *sig result.d = env->functions->GetDoubleField(env, obj, field); break; default: - fprintf(stderr, "%s: invalid field type (%d)\n", __PRETTY_FUNCTION__, (int)type); + fprintf(stderr, "%s: invalid field type (%d)\n", __PRETTY_FUNCTION__, static_cast<int>(type)); } - } - else - { + } else { fprintf(stderr, "%s: Could not find field: %s\n", __PRETTY_FUNCTION__, name); env->ExceptionDescribe(); env->ExceptionClear(); - fprintf (stderr, "\n"); + fprintf(stderr, "\n"); } env->DeleteLocalRef(cls); - } - else { + } else fprintf(stderr, "%s: Could not find class for object\n", __PRETTY_FUNCTION__); - } } return result; } -} // end of namespace Bindings +} // namespace Bindings -} // end of namespace JSC +} // namespace JSC #endif // ENABLE(MAC_JAVA_BRIDGE) diff --git a/WebCore/bridge/jni/jni_utility.h b/WebCore/bridge/jni/JNIUtility.h index 66b3b2f..85c3533 100644 --- a/WebCore/bridge/jni/jni_utility.h +++ b/WebCore/bridge/jni/JNIUtility.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2008, 2009, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -20,11 +20,11 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _JNI_UTILITY_H_ -#define _JNI_UTILITY_H_ +#ifndef JNIUtility_h +#define JNIUtility_h #if ENABLE(MAC_JAVA_BRIDGE) @@ -56,26 +56,26 @@ namespace Bindings { class JavaParameter; -const char *getCharactersFromJString(jstring aJString); -void releaseCharactersForJString(jstring aJString, const char *s); +const char* getCharactersFromJString(jstring); +void releaseCharactersForJString(jstring, const char*); -const char *getCharactersFromJStringInEnv(JNIEnv *env, jstring aJString); -void releaseCharactersForJStringInEnv(JNIEnv *env, jstring aJString, const char *s); -const jchar *getUCharactersFromJStringInEnv(JNIEnv *env, jstring aJString); -void releaseUCharactersForJStringInEnv(JNIEnv *env, jstring aJString, const jchar *s); +const char* getCharactersFromJStringInEnv(JNIEnv*, jstring); +void releaseCharactersForJStringInEnv(JNIEnv*, jstring, const char*); +const jchar* getUCharactersFromJStringInEnv(JNIEnv*, jstring); +void releaseUCharactersForJStringInEnv(JNIEnv*, jstring, const jchar*); -JNIType JNITypeFromClassName(const char *name); +JNIType JNITypeFromClassName(const char* name); JNIType JNITypeFromPrimitiveType(char type); -const char *signatureFromPrimitiveType(JNIType type); +const char* signatureFromPrimitiveType(JNIType); -jvalue getJNIField(jobject obj, JNIType type, const char *name, const char *signature); +jvalue getJNIField(jobject, JNIType, const char* name, const char* signature); -jmethodID getMethodID(jobject obj, const char *name, const char *sig); +jmethodID getMethodID(jobject, const char* name, const char* sig); JNIEnv* getJNIEnv(); JavaVM* getJavaVM(); void setJavaVM(JavaVM*); - - + + template <typename T> struct JNICaller; template<> struct JNICaller<void> { @@ -97,7 +97,7 @@ template<> struct JNICaller<jobject> { static jobject callV(jobject obj, jmethodID mid, va_list args) { return getJNIEnv()->CallObjectMethodV(obj, mid, args); - } + } }; template<> struct JNICaller<jboolean> { @@ -113,7 +113,6 @@ template<> struct JNICaller<jboolean> { { return getJNIEnv()->CallStaticBooleanMethod(cls, mid, args); } - }; template<> struct JNICaller<jbyte> { @@ -135,7 +134,7 @@ template<> struct JNICaller<jchar> { static jchar callV(jobject obj, jmethodID mid, va_list args) { return getJNIEnv()->CallCharMethodV(obj, mid, args); - } + } }; template<> struct JNICaller<jshort> { @@ -197,36 +196,30 @@ template<typename T> T callJNIMethodIDA(jobject obj, jmethodID mid, jvalue *args { return JNICaller<T>::callA(obj, mid, args); } - + template<typename T> -static T callJNIMethodV(jobject obj, const char *name, const char *sig, va_list args) +static T callJNIMethodV(jobject obj, const char* name, const char* sig, va_list args) { - JavaVM *jvm = getJavaVM(); - JNIEnv *env = getJNIEnv(); - - if ( obj != NULL && jvm != NULL && env != NULL) { + JavaVM* jvm = getJavaVM(); + JNIEnv* env = getJNIEnv(); + + if (obj && jvm && env) { jclass cls = env->GetObjectClass(obj); - if ( cls != NULL ) { + if (cls) { jmethodID mid = env->GetMethodID(cls, name, sig); - if ( mid != NULL ) - { + if (mid) { // Avoids references to cls without popping the local frame. env->DeleteLocalRef(cls); return JNICaller<T>::callV(obj, mid, args); } - else - { - fprintf(stderr, "%s: Could not find method: %s for %p\n", __PRETTY_FUNCTION__, name, obj); - env->ExceptionDescribe(); - env->ExceptionClear(); - fprintf (stderr, "\n"); - } + fprintf(stderr, "%s: Could not find method: %s for %p\n", __PRETTY_FUNCTION__, name, obj); + env->ExceptionDescribe(); + env->ExceptionClear(); + fprintf(stderr, "\n"); env->DeleteLocalRef(cls); - } - else { + } else fprintf(stderr, "%s: Could not find class for %p\n", __PRETTY_FUNCTION__, obj); - } } return 0; @@ -237,46 +230,46 @@ T callJNIMethod(jobject obj, const char* methodName, const char* methodSignature { va_list args; va_start(args, methodSignature); - - T result= callJNIMethodV<T>(obj, methodName, methodSignature, args); - + + T result = callJNIMethodV<T>(obj, methodName, methodSignature, args); + va_end(args); - + return result; } - + template<typename T> T callJNIStaticMethod(jclass cls, const char* methodName, const char* methodSignature, ...) { - JavaVM *jvm = getJavaVM(); - JNIEnv *env = getJNIEnv(); + JavaVM* jvm = getJavaVM(); + JNIEnv* env = getJNIEnv(); va_list args; - + va_start(args, methodSignature); - + T result = 0; - - if (cls != NULL && jvm != NULL && env != NULL) { + + if (cls && jvm && env) { jmethodID mid = env->GetStaticMethodID(cls, methodName, methodSignature); - if (mid != NULL) + if (mid) result = JNICaller<T>::callStaticV(cls, mid, args); else { fprintf(stderr, "%s: Could not find method: %s for %p\n", __PRETTY_FUNCTION__, methodName, cls); env->ExceptionDescribe(); env->ExceptionClear(); - fprintf (stderr, "\n"); + fprintf(stderr, "\n"); } } - + va_end(args); - + return result; } - + } // namespace Bindings } // namespace JSC #endif // ENABLE(MAC_JAVA_BRIDGE) -#endif // _JNI_UTILITY_H_ +#endif // JNIUtility_h diff --git a/WebCore/bridge/jni/jni_jsobject.mm b/WebCore/bridge/jni/jni_jsobject.mm index 6683af6..de67711 100644 --- a/WebCore/bridge/jni/jni_jsobject.mm +++ b/WebCore/bridge/jni/jni_jsobject.mm @@ -29,13 +29,13 @@ #if ENABLE(MAC_JAVA_BRIDGE) #include "Frame.h" +#include "JNIBridge.h" +#include "JNIUtility.h" +#include "JNIUtilityPrivate.h" #include "JSDOMBinding.h" #include "ScriptController.h" #include "StringSourceProvider.h" #include "WebCoreFrameView.h" -#include "jni_runtime.h" -#include "jni_utility.h" -#include "jni_utility_private.h" #include "runtime_object.h" #include "runtime_root.h" #include <interpreter/CallFrame.h> diff --git a/WebCore/bridge/jni/jni_objc.mm b/WebCore/bridge/jni/jni_objc.mm index fb2d694..e2e71c5 100644 --- a/WebCore/bridge/jni/jni_objc.mm +++ b/WebCore/bridge/jni/jni_objc.mm @@ -28,8 +28,8 @@ #if ENABLE(MAC_JAVA_BRIDGE) #import <Foundation/Foundation.h> -#import "jni_utility.h" -#import "jni_utility_private.h" +#import "JNIUtility.h" +#import "JNIUtilityPrivate.h" #import "objc_utility.h" #include <runtime/JSLock.h> diff --git a/WebCore/bridge/jni/jni_runtime.cpp b/WebCore/bridge/jni/jni_runtime.cpp deleted file mode 100644 index be11981..0000000 --- a/WebCore/bridge/jni/jni_runtime.cpp +++ /dev/null @@ -1,548 +0,0 @@ -/* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. - * - * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 <jni_runtime.h> - -#if ENABLE(MAC_JAVA_BRIDGE) - -#include "jni_utility.h" -#include "jni_utility_private.h" -#include "runtime_array.h" -#include "runtime_object.h" -#include "runtime_root.h" -#include <runtime/Error.h> -#include <runtime/JSLock.h> - -#ifdef NDEBUG -#define JS_LOG(formatAndArgs...) ((void)0) -#else -#define JS_LOG(formatAndArgs...) { \ - fprintf (stderr, "%s:%d -- %s: ", __FILE__, __LINE__, __FUNCTION__); \ - fprintf(stderr, formatAndArgs); \ -} -#endif - -using namespace JSC; -using namespace JSC::Bindings; - - -JavaParameter::JavaParameter (JNIEnv *env, jstring type) -{ - _type = JavaString (env, type); - _JNIType = JNITypeFromClassName (_type.UTF8String()); -} - -JavaField::JavaField (JNIEnv *env, jobject aField) -{ - // Get field type - jobject fieldType = callJNIMethod<jobject>(aField, "getType", "()Ljava/lang/Class;"); - jstring fieldTypeName = (jstring)callJNIMethod<jobject>(fieldType, "getName", "()Ljava/lang/String;"); - _type = JavaString(env, fieldTypeName); - _JNIType = JNITypeFromClassName (_type.UTF8String()); - - // Get field name - jstring fieldName = (jstring)callJNIMethod<jobject>(aField, "getName", "()Ljava/lang/String;"); - _name = JavaString(env, fieldName); - - _field = new JObjectWrapper(aField); -} - -JSValue JavaArray::convertJObjectToArray(ExecState* exec, jobject anObject, const char* type, PassRefPtr<RootObject> rootObject) -{ - if (type[0] != '[') - return jsUndefined(); - - return new (exec) RuntimeArray(exec, new JavaArray((jobject)anObject, type, rootObject)); -} - -jvalue JavaField::dispatchValueFromInstance(ExecState *exec, const JavaInstance *instance, const char *name, const char *sig, JNIType returnType) const -{ - jobject jinstance = instance->javaInstance(); - jobject fieldJInstance = _field->_instance; - JNIEnv *env = getJNIEnv(); - jvalue result; - - bzero (&result, sizeof(jvalue)); - jclass cls = env->GetObjectClass(fieldJInstance); - if ( cls != NULL ) { - jmethodID mid = env->GetMethodID(cls, name, sig); - if ( mid != NULL ) - { - RootObject* rootObject = instance->rootObject(); - if (rootObject && rootObject->nativeHandle()) { - JSValue exceptionDescription; - jvalue args[1]; - - args[0].l = jinstance; - dispatchJNICall(exec, rootObject->nativeHandle(), fieldJInstance, false, returnType, mid, args, result, 0, exceptionDescription); - if (exceptionDescription) - throwError(exec, GeneralError, exceptionDescription.toString(exec)); - } - } - } - return result; -} - -JSValue JavaField::valueFromInstance(ExecState* exec, const Instance* i) const -{ - const JavaInstance *instance = static_cast<const JavaInstance *>(i); - - JSValue jsresult = jsUndefined(); - - switch (_JNIType) { - case array_type: - case object_type: { - jvalue result = dispatchValueFromInstance (exec, instance, "get", "(Ljava/lang/Object;)Ljava/lang/Object;", object_type); - jobject anObject = result.l; - - const char *arrayType = type(); - if (arrayType[0] == '[') { - jsresult = JavaArray::convertJObjectToArray(exec, anObject, arrayType, instance->rootObject()); - } - else if (anObject != 0){ - jsresult = JavaInstance::create(anObject, instance->rootObject())->createRuntimeObject(exec); - } - } - break; - - case boolean_type: - jsresult = jsBoolean(dispatchValueFromInstance(exec, instance, "getBoolean", "(Ljava/lang/Object;)Z", boolean_type).z); - break; - - case byte_type: - case char_type: - case short_type: - - case int_type: { - jint value; - jvalue result = dispatchValueFromInstance (exec, instance, "getInt", "(Ljava/lang/Object;)I", int_type); - value = result.i; - jsresult = jsNumber(exec, (int)value); - } - break; - - case long_type: - case float_type: - case double_type: { - jdouble value; - jvalue result = dispatchValueFromInstance (exec, instance, "getDouble", "(Ljava/lang/Object;)D", double_type); - value = result.i; - jsresult = jsNumber(exec, (double)value); - } - break; - default: - break; - } - - JS_LOG ("getting %s = %s\n", UString(name()).UTF8String().c_str(), jsresult.toString(exec).ascii()); - - return jsresult; -} - -void JavaField::dispatchSetValueToInstance(ExecState *exec, const JavaInstance *instance, jvalue javaValue, const char *name, const char *sig) const -{ - jobject jinstance = instance->javaInstance(); - jobject fieldJInstance = _field->_instance; - JNIEnv *env = getJNIEnv(); - - jclass cls = env->GetObjectClass(fieldJInstance); - if ( cls != NULL ) { - jmethodID mid = env->GetMethodID(cls, name, sig); - if ( mid != NULL ) - { - RootObject* rootObject = instance->rootObject(); - if (rootObject && rootObject->nativeHandle()) { - JSValue exceptionDescription; - jvalue args[2]; - jvalue result; - - args[0].l = jinstance; - args[1] = javaValue; - dispatchJNICall(exec, rootObject->nativeHandle(), fieldJInstance, false, void_type, mid, args, result, 0, exceptionDescription); - if (exceptionDescription) - throwError(exec, GeneralError, exceptionDescription.toString(exec)); - } - } - } -} - -void JavaField::setValueToInstance(ExecState* exec, const Instance* i, JSValue aValue) const -{ - const JavaInstance *instance = static_cast<const JavaInstance *>(i); - jvalue javaValue = convertValueToJValue (exec, aValue, _JNIType, type()); - - JS_LOG ("setting value %s to %s\n", UString(name()).UTF8String().c_str(), aValue.toString(exec).ascii()); - - switch (_JNIType) { - case array_type: - case object_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "set", "(Ljava/lang/Object;Ljava/lang/Object;)V"); - } - break; - - case boolean_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setBoolean", "(Ljava/lang/Object;Z)V"); - } - break; - - case byte_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setByte", "(Ljava/lang/Object;B)V"); - } - break; - - case char_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setChar", "(Ljava/lang/Object;C)V"); - } - break; - - case short_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setShort", "(Ljava/lang/Object;S)V"); - } - break; - - case int_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setInt", "(Ljava/lang/Object;I)V"); - } - break; - - case long_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setLong", "(Ljava/lang/Object;J)V"); - } - break; - - case float_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setFloat", "(Ljava/lang/Object;F)V"); - } - break; - - case double_type: { - dispatchSetValueToInstance (exec, instance, javaValue, "setDouble", "(Ljava/lang/Object;D)V"); - } - break; - default: - break; - } -} - -JavaMethod::JavaMethod (JNIEnv *env, jobject aMethod) -{ - // Get return type - jobject returnType = callJNIMethod<jobject>(aMethod, "getReturnType", "()Ljava/lang/Class;"); - jstring returnTypeName = (jstring)callJNIMethod<jobject>(returnType, "getName", "()Ljava/lang/String;"); - _returnType =JavaString (env, returnTypeName); - _JNIReturnType = JNITypeFromClassName (_returnType.UTF8String()); - env->DeleteLocalRef (returnType); - env->DeleteLocalRef (returnTypeName); - - // Get method name - jstring methodName = (jstring)callJNIMethod<jobject>(aMethod, "getName", "()Ljava/lang/String;"); - _name = JavaString (env, methodName); - env->DeleteLocalRef (methodName); - - // Get parameters - jarray jparameters = (jarray)callJNIMethod<jobject>(aMethod, "getParameterTypes", "()[Ljava/lang/Class;"); - _numParameters = env->GetArrayLength (jparameters); - _parameters = new JavaParameter[_numParameters]; - - int i; - for (i = 0; i < _numParameters; i++) { - jobject aParameter = env->GetObjectArrayElement ((jobjectArray)jparameters, i); - jstring parameterName = (jstring)callJNIMethod<jobject>(aParameter, "getName", "()Ljava/lang/String;"); - _parameters[i] = JavaParameter(env, parameterName); - env->DeleteLocalRef (aParameter); - env->DeleteLocalRef (parameterName); - } - env->DeleteLocalRef (jparameters); - - // Created lazily. - _signature = 0; - _methodID = 0; - - jclass modifierClass = env->FindClass("java/lang/reflect/Modifier"); - int modifiers = callJNIMethod<jint>(aMethod, "getModifiers", "()I"); - _isStatic = (bool)callJNIStaticMethod<jboolean>(modifierClass, "isStatic", "(I)Z", modifiers); - env->DeleteLocalRef(modifierClass); -} - -JavaMethod::~JavaMethod() -{ - if (_signature) - free(_signature); - delete [] _parameters; -}; - -// JNI method signatures use '/' between components of a class name, but -// we get '.' between components from the reflection API. -static void appendClassName(UString& aString, const char* className) -{ - ASSERT(JSLock::lockCount() > 0); - - char *result, *cp = strdup(className); - - result = cp; - while (*cp) { - if (*cp == '.') - *cp = '/'; - cp++; - } - - aString.append(result); - - free (result); -} - -const char *JavaMethod::signature() const -{ - if (!_signature) { - JSLock lock(SilenceAssertionsOnly); - - UString signatureBuilder("("); - for (int i = 0; i < _numParameters; i++) { - JavaParameter* aParameter = parameterAt(i); - JNIType _JNIType = aParameter->getJNIType(); - if (_JNIType == array_type) - appendClassName(signatureBuilder, aParameter->type()); - else { - signatureBuilder.append(signatureFromPrimitiveType(_JNIType)); - if (_JNIType == object_type) { - appendClassName(signatureBuilder, aParameter->type()); - signatureBuilder.append(";"); - } - } - } - signatureBuilder.append(")"); - - const char *returnType = _returnType.UTF8String(); - if (_JNIReturnType == array_type) { - appendClassName(signatureBuilder, returnType); - } else { - signatureBuilder.append(signatureFromPrimitiveType(_JNIReturnType)); - if (_JNIReturnType == object_type) { - appendClassName(signatureBuilder, returnType); - signatureBuilder.append(";"); - } - } - - _signature = strdup(signatureBuilder.ascii()); - } - - return _signature; -} - -JNIType JavaMethod::JNIReturnType() const -{ - return _JNIReturnType; -} - -jmethodID JavaMethod::methodID (jobject obj) const -{ - if (_methodID == 0) { - _methodID = getMethodID (obj, _name.UTF8String(), signature()); - } - return _methodID; -} - - -JavaArray::JavaArray(jobject array, const char* type, PassRefPtr<RootObject> rootObject) - : Array(rootObject) -{ - _array = new JObjectWrapper(array); - // Java array are fixed length, so we can cache length. - JNIEnv *env = getJNIEnv(); - _length = env->GetArrayLength((jarray)_array->_instance); - _type = strdup(type); - _rootObject = rootObject; -} - -JavaArray::~JavaArray () -{ - free ((void *)_type); -} - -RootObject* JavaArray::rootObject() const -{ - return _rootObject && _rootObject->isValid() ? _rootObject.get() : 0; -} - -void JavaArray::setValueAt(ExecState* exec, unsigned index, JSValue aValue) const -{ - JNIEnv *env = getJNIEnv(); - char *javaClassName = 0; - - JNIType arrayType = JNITypeFromPrimitiveType(_type[1]); - if (_type[1] == 'L'){ - // The type of the array will be something like: - // "[Ljava.lang.string;". This is guaranteed, so no need - // for extra sanity checks. - javaClassName = strdup(&_type[2]); - javaClassName[strchr(javaClassName, ';')-javaClassName] = 0; - } - jvalue aJValue = convertValueToJValue (exec, aValue, arrayType, javaClassName); - - switch (arrayType) { - case object_type: { - env->SetObjectArrayElement((jobjectArray)javaArray(), index, aJValue.l); - break; - } - - case boolean_type: { - env->SetBooleanArrayRegion((jbooleanArray)javaArray(), index, 1, &aJValue.z); - break; - } - - case byte_type: { - env->SetByteArrayRegion((jbyteArray)javaArray(), index, 1, &aJValue.b); - break; - } - - case char_type: { - env->SetCharArrayRegion((jcharArray)javaArray(), index, 1, &aJValue.c); - break; - } - - case short_type: { - env->SetShortArrayRegion((jshortArray)javaArray(), index, 1, &aJValue.s); - break; - } - - case int_type: { - env->SetIntArrayRegion((jintArray)javaArray(), index, 1, &aJValue.i); - break; - } - - case long_type: { - env->SetLongArrayRegion((jlongArray)javaArray(), index, 1, &aJValue.j); - } - - case float_type: { - env->SetFloatArrayRegion((jfloatArray)javaArray(), index, 1, &aJValue.f); - break; - } - - case double_type: { - env->SetDoubleArrayRegion((jdoubleArray)javaArray(), index, 1, &aJValue.d); - break; - } - default: - break; - } - - if (javaClassName) - free ((void *)javaClassName); -} - - -JSValue JavaArray::valueAt(ExecState* exec, unsigned index) const -{ - JNIEnv *env = getJNIEnv(); - JNIType arrayType = JNITypeFromPrimitiveType(_type[1]); - switch (arrayType) { - case object_type: { - jobjectArray objectArray = (jobjectArray)javaArray(); - jobject anObject; - anObject = env->GetObjectArrayElement(objectArray, index); - - // No object? - if (!anObject) { - return jsNull(); - } - - // Nested array? - if (_type[1] == '[') { - return JavaArray::convertJObjectToArray(exec, anObject, _type+1, rootObject()); - } - // or array of other object type? - return JavaInstance::create(anObject, rootObject())->createRuntimeObject(exec); - } - - case boolean_type: { - jbooleanArray booleanArray = (jbooleanArray)javaArray(); - jboolean aBoolean; - env->GetBooleanArrayRegion(booleanArray, index, 1, &aBoolean); - return jsBoolean(aBoolean); - } - - case byte_type: { - jbyteArray byteArray = (jbyteArray)javaArray(); - jbyte aByte; - env->GetByteArrayRegion(byteArray, index, 1, &aByte); - return jsNumber(exec, aByte); - } - - case char_type: { - jcharArray charArray = (jcharArray)javaArray(); - jchar aChar; - env->GetCharArrayRegion(charArray, index, 1, &aChar); - return jsNumber(exec, aChar); - break; - } - - case short_type: { - jshortArray shortArray = (jshortArray)javaArray(); - jshort aShort; - env->GetShortArrayRegion(shortArray, index, 1, &aShort); - return jsNumber(exec, aShort); - } - - case int_type: { - jintArray intArray = (jintArray)javaArray(); - jint anInt; - env->GetIntArrayRegion(intArray, index, 1, &anInt); - return jsNumber(exec, anInt); - } - - case long_type: { - jlongArray longArray = (jlongArray)javaArray(); - jlong aLong; - env->GetLongArrayRegion(longArray, index, 1, &aLong); - return jsNumber(exec, aLong); - } - - case float_type: { - jfloatArray floatArray = (jfloatArray)javaArray(); - jfloat aFloat; - env->GetFloatArrayRegion(floatArray, index, 1, &aFloat); - return jsNumber(exec, aFloat); - } - - case double_type: { - jdoubleArray doubleArray = (jdoubleArray)javaArray(); - jdouble aDouble; - env->GetDoubleArrayRegion(doubleArray, index, 1, &aDouble); - return jsNumber(exec, aDouble); - } - default: - break; - } - return jsUndefined(); -} - -unsigned int JavaArray::getLength() const -{ - return _length; -} - -#endif // ENABLE(MAC_JAVA_BRIDGE) diff --git a/WebCore/bridge/jni/jni_runtime.h b/WebCore/bridge/jni/jni_runtime.h deleted file mode 100644 index 81484ff..0000000 --- a/WebCore/bridge/jni/jni_runtime.h +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. - * - * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 _JNI_RUNTIME_H_ -#define _JNI_RUNTIME_H_ - -#if ENABLE(MAC_JAVA_BRIDGE) - -#include <jni_utility.h> -#include <jni_instance.h> -#include <runtime/JSLock.h> - - -namespace JSC -{ - -namespace Bindings -{ - -typedef const char* RuntimeType; - -class JavaString -{ -public: - JavaString() - { - JSLock lock(SilenceAssertionsOnly); - _rep = UString().rep(); - } - - void _commonInit (JNIEnv *e, jstring s) - { - int _size = e->GetStringLength (s); - const jchar *uc = getUCharactersFromJStringInEnv (e, s); - { - JSLock lock(SilenceAssertionsOnly); - _rep = UString(reinterpret_cast<const UChar*>(uc), _size).rep(); - } - releaseUCharactersForJStringInEnv (e, s, uc); - } - - JavaString (JNIEnv *e, jstring s) { - _commonInit (e, s); - } - - JavaString (jstring s) { - _commonInit (getJNIEnv(), s); - } - - ~JavaString() - { - JSLock lock(SilenceAssertionsOnly); - _rep = 0; - } - - const char *UTF8String() const { - if (_utf8String.c_str() == 0) { - JSLock lock(SilenceAssertionsOnly); - _utf8String = UString(_rep).UTF8String(); - } - return _utf8String.c_str(); - } - const jchar *uchars() const { return (const jchar *)_rep->data(); } - int length() const { return _rep->size(); } - operator UString() const { return UString(_rep); } - -private: - RefPtr<UString::Rep> _rep; - mutable CString _utf8String; -}; - -class JavaParameter -{ -public: - JavaParameter () : _JNIType(invalid_type) {}; - JavaParameter (JNIEnv *env, jstring type); - virtual ~JavaParameter() { } - - RuntimeType type() const { return _type.UTF8String(); } - JNIType getJNIType() const { return _JNIType; } - -private: - JavaString _type; - JNIType _JNIType; -}; - - -class JavaField : public Field -{ -public: - JavaField (JNIEnv *env, jobject aField); - - virtual JSValue valueFromInstance(ExecState *exec, const Instance *instance) const; - virtual void setValueToInstance(ExecState *exec, const Instance *instance, JSValue aValue) const; - - UString::Rep* name() const { return ((UString)_name).rep(); } - virtual RuntimeType type() const { return _type.UTF8String(); } - - JNIType getJNIType() const { return _JNIType; } - -private: - void dispatchSetValueToInstance(ExecState *exec, const JavaInstance *instance, jvalue javaValue, const char *name, const char *sig) const; - jvalue dispatchValueFromInstance(ExecState *exec, const JavaInstance *instance, const char *name, const char *sig, JNIType returnType) const; - - JavaString _name; - JavaString _type; - JNIType _JNIType; - RefPtr<JObjectWrapper> _field; -}; - - -class JavaMethod : public Method -{ -public: - JavaMethod(JNIEnv* env, jobject aMethod); - ~JavaMethod(); - - UString::Rep* name() const { return ((UString)_name).rep(); } - RuntimeType returnType() const { return _returnType.UTF8String(); }; - JavaParameter* parameterAt(int i) const { return &_parameters[i]; }; - int numParameters() const { return _numParameters; }; - - const char *signature() const; - JNIType JNIReturnType() const; - - jmethodID methodID (jobject obj) const; - - bool isStatic() const { return _isStatic; } - -private: - JavaParameter* _parameters; - int _numParameters; - JavaString _name; - mutable char* _signature; - JavaString _returnType; - JNIType _JNIReturnType; - mutable jmethodID _methodID; - bool _isStatic; -}; - -class JavaArray : public Array -{ -public: - JavaArray(jobject array, const char* type, PassRefPtr<RootObject>); - virtual ~JavaArray(); - - RootObject* rootObject() const; - - virtual void setValueAt(ExecState *exec, unsigned int index, JSValue aValue) const; - virtual JSValue valueAt(ExecState *exec, unsigned int index) const; - virtual unsigned int getLength() const; - - jobject javaArray() const { return _array->_instance; } - - static JSValue convertJObjectToArray (ExecState* exec, jobject anObject, const char* type, PassRefPtr<RootObject>); - -private: - RefPtr<JObjectWrapper> _array; - unsigned int _length; - const char *_type; -}; - -} // namespace Bindings - -} // namespace JSC - -#endif // ENABLE(MAC_JAVA_BRIDGE) - -#endif // _JNI_RUNTIME_H_ diff --git a/WebCore/bridge/jni/jsc/jni_utility_private.cpp b/WebCore/bridge/jni/jsc/JNIUtilityPrivate.cpp index 7f8afd5..e89d0e0 100644 --- a/WebCore/bridge/jni/jsc/jni_utility_private.cpp +++ b/WebCore/bridge/jni/jsc/JNIUtilityPrivate.cpp @@ -25,11 +25,11 @@ */ #include "config.h" -#include "jni_utility_private.h" +#include "JNIUtilityPrivate.h" #if ENABLE(MAC_JAVA_BRIDGE) -#include "jni_runtime.h" +#include "JNIBridge.h" #include "runtime_array.h" #include "runtime_object.h" #include <runtime/JSArray.h> @@ -189,7 +189,7 @@ jvalue convertValueToJValue(ExecState* exec, JSValue value, JNIType jniType, con if (instance) result.l = instance->javaInstance(); } else if (objectImp->classInfo() == &RuntimeArray::s_info) { - // Input is a JavaScript Array that was originally created from a Java Array + // Input is a JavaScript Array that was originally created from a Java Array RuntimeArray* imp = static_cast<RuntimeArray*>(objectImp); JavaArray* array = static_cast<JavaArray*>(imp->getConcreteArray()); result.l = array->javaArray(); @@ -284,7 +284,7 @@ jvalue convertValueToJValue(ExecState* exec, JSValue value, JNIType jniType, con return result; } -} // end of namespace Bindings +} // end of namespace Bindings } // end of namespace JSC diff --git a/WebCore/bridge/jni/jsc/jni_utility_private.h b/WebCore/bridge/jni/jsc/JNIUtilityPrivate.h index 647852e..0297f97 100644 --- a/WebCore/bridge/jni/jsc/jni_utility_private.h +++ b/WebCore/bridge/jni/jsc/JNIUtilityPrivate.h @@ -24,12 +24,12 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _JNI_UTILITY_PRIVATE_H_ -#define _JNI_UTILITY_PRIVATE_H_ +#ifndef JNIUtilityPrivate_h +#define JNIUtilityPrivate_h #if ENABLE(MAC_JAVA_BRIDGE) -#include "jni_utility.h" +#include "JNIUtility.h" #include <runtime/JSValue.h> namespace JSC { @@ -48,4 +48,4 @@ bool dispatchJNICall(ExecState*, const void* targetAppletView, jobject obj, bool #endif // ENABLE(MAC_JAVA_BRIDGE) -#endif // _JNI_UTILITY_H_ +#endif // JNIUtilityPrivate_h diff --git a/WebCore/bridge/jni/jni_class.cpp b/WebCore/bridge/jni/jsc/JavaClassJSC.cpp index 6728e7b..350c8ae 100644 --- a/WebCore/bridge/jni/jni_class.cpp +++ b/WebCore/bridge/jni/jsc/JavaClassJSC.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -20,48 +20,47 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" -#include "jni_class.h" +#include "JavaClassJSC.h" #if ENABLE(MAC_JAVA_BRIDGE) +#include "JNIUtility.h" #include "JSDOMWindow.h" #include <runtime/Identifier.h> #include <runtime/JSLock.h> -#include "jni_utility.h" -#include "jni_runtime.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; } - + jstring className = (jstring)callJNIMethod<jobject>(aClass, "getName", "()Ljava/lang/String;"); - const char *classNameC = getCharactersFromJString(className); - _name = strdup(classNameC); + const char* classNameC = getCharactersFromJString(className); + m_name = strdup(classNameC); releaseCharactersForJString(className, classNameC); int i; - JNIEnv *env = getJNIEnv(); + JNIEnv* env = getJNIEnv(); // Get the fields jarray fields = (jarray)callJNIMethod<jobject>(aClass, "getFields", "()[Ljava/lang/reflect/Field;"); - int numFields = env->GetArrayLength(fields); + int numFields = env->GetArrayLength(fields); for (i = 0; i < numFields; i++) { jobject aJField = env->GetObjectArrayElement((jobjectArray)fields, i); - JavaField *aField = new JavaField(env, aJField); // deleted in the JavaClass destructor + JavaField* aField = new JavaField(env, aJField); // deleted in the JavaClass destructor { JSLock lock(SilenceAssertionsOnly); - _fields.set(aField->name(), aField); + m_fields.set(((UString)aField->name()).rep(), aField); } env->DeleteLocalRef(aJField); } @@ -71,75 +70,76 @@ JavaClass::JavaClass(jobject anInstance) int numMethods = env->GetArrayLength(methods); for (i = 0; i < numMethods; i++) { jobject aJMethod = env->GetObjectArrayElement((jobjectArray)methods, i); - JavaMethod *aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor + JavaMethod* aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor MethodList* methodList; { JSLock lock(SilenceAssertionsOnly); - methodList = _methods.get(aMethod->name()); + methodList = m_methods.get(((UString)aMethod->name()).rep()); if (!methodList) { methodList = new MethodList(); - _methods.set(aMethod->name(), methodList); + m_methods.set(((UString)aMethod->name()).rep(), methodList); } } methodList->append(aMethod); env->DeleteLocalRef(aJMethod); - } + } env->DeleteLocalRef(fields); env->DeleteLocalRef(methods); env->DeleteLocalRef(aClass); } -JavaClass::~JavaClass() { - free((void *)_name); +JavaClass::~JavaClass() +{ + free(const_cast<char*>(m_name)); JSLock lock(SilenceAssertionsOnly); - deleteAllValues(_fields); - _fields.clear(); + deleteAllValues(m_fields); + m_fields.clear(); - MethodListMap::const_iterator end = _methods.end(); - for (MethodListMap::const_iterator it = _methods.begin(); it != end; ++it) { + 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; } - _methods.clear(); + m_methods.clear(); } MethodList JavaClass::methodsNamed(const Identifier& identifier, Instance*) const { - MethodList *methodList = _methods.get(identifier.ustring().rep()); - + MethodList* methodList = m_methods.get(identifier.ustring().rep()); + if (methodList) return *methodList; return MethodList(); } -Field *JavaClass::fieldNamed(const Identifier& identifier, Instance*) const +Field* JavaClass::fieldNamed(const Identifier& identifier, Instance*) const { - return _fields.get(identifier.ustring().rep()); + return m_fields.get(identifier.ustring().rep()); } bool JavaClass::isNumberClass() const { - return ((strcmp(_name, "java.lang.Byte") == 0 || - strcmp(_name, "java.lang.Short") == 0 || - strcmp(_name, "java.lang.Integer") == 0 || - strcmp(_name, "java.lang.Long") == 0 || - strcmp(_name, "java.lang.Float") == 0 || - strcmp(_name, "java.lang.Double") == 0) ); + return (!strcmp(m_name, "java.lang.Byte") + || !strcmp(m_name, "java.lang.Short") + || !strcmp(m_name, "java.lang.Integer") + || !strcmp(m_name, "java.lang.Long") + || !strcmp(m_name, "java.lang.Float") + || !strcmp(m_name, "java.lang.Double")); } bool JavaClass::isBooleanClass() const { - return strcmp(_name, "java.lang.Boolean") == 0; + return !strcmp(m_name, "java.lang.Boolean"); } bool JavaClass::isStringClass() const { - return strcmp(_name, "java.lang.String") == 0; + return !strcmp(m_name, "java.lang.String"); } #endif // ENABLE(MAC_JAVA_BRIDGE) diff --git a/WebCore/bridge/jni/jni_class.h b/WebCore/bridge/jni/jsc/JavaClassJSC.h index 890b4d3..b20781b 100644 --- a/WebCore/bridge/jni/jni_class.h +++ b/WebCore/bridge/jni/jsc/JavaClassJSC.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2007, 2009, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -20,15 +20,15 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef JNI_CLASS_H_ -#define JNI_CLASS_H_ +#ifndef JavaClassJSC_h +#define JavaClassJSC_h #if ENABLE(MAC_JAVA_BRIDGE) -#include <jni_runtime.h> +#include "JNIBridge.h" #include <wtf/HashMap.h> namespace JSC { @@ -37,20 +37,20 @@ namespace Bindings { class JavaClass : public Class { public: - JavaClass (jobject anInstance); - ~JavaClass (); + JavaClass(jobject); + ~JavaClass(); + + virtual MethodList methodsNamed(const Identifier&, Instance*) const; + virtual Field* fieldNamed(const Identifier&, Instance*) const; - virtual MethodList methodsNamed(const Identifier&, Instance* instance) const; - virtual Field *fieldNamed(const Identifier&, Instance* instance) const; - bool isNumberClass() const; bool isBooleanClass() const; bool isStringClass() const; - + private: - const char *_name; - FieldMap _fields; - MethodListMap _methods; + const char* m_name; + FieldMap m_fields; + MethodListMap m_methods; }; } // namespace Bindings @@ -59,4 +59,4 @@ private: #endif // ENABLE(MAC_JAVA_BRIDGE) -#endif // JNI_CLASS_H_ +#endif // JavaClassJSC_h diff --git a/WebCore/bridge/jni/jni_instance.cpp b/WebCore/bridge/jni/jsc/JavaInstanceJSC.cpp index 3783d10..d9e0f60 100644 --- a/WebCore/bridge/jni/jni_instance.cpp +++ b/WebCore/bridge/jni/jsc/JavaInstanceJSC.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -20,18 +20,18 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" -#include "jni_instance.h" +#include "JavaInstanceJSC.h" #if ENABLE(MAC_JAVA_BRIDGE) -#include "jni_class.h" -#include "jni_runtime.h" -#include "jni_utility.h" -#include "jni_utility_private.h" +#include "JNIBridge.h" +#include "JNIUtility.h" +#include "JNIUtilityPrivate.h" +#include "JavaClassJSC.h" #include "runtime_object.h" #include "runtime_root.h" #include <runtime/ArgList.h> @@ -46,83 +46,83 @@ #define JS_LOG(formatAndArgs...) ((void)0) #else #define JS_LOG(formatAndArgs...) { \ - fprintf (stderr, "%s:%d -- %s: ", __FILE__, __LINE__, __FUNCTION__); \ + fprintf(stderr, "%s:%d -- %s: ", __FILE__, __LINE__, __FUNCTION__); \ fprintf(stderr, formatAndArgs); \ } #endif - + using namespace JSC::Bindings; using namespace JSC; -JavaInstance::JavaInstance (jobject instance, PassRefPtr<RootObject> rootObject) +JavaInstance::JavaInstance(jobject instance, PassRefPtr<RootObject> rootObject) : Instance(rootObject) { - _instance = new JObjectWrapper (instance); - _class = 0; + m_instance = new JObjectWrapper(instance); + m_class = 0; } -JavaInstance::~JavaInstance () +JavaInstance::~JavaInstance() { - delete _class; + delete m_class; } #define NUM_LOCAL_REFS 64 void JavaInstance::virtualBegin() { - getJNIEnv()->PushLocalFrame (NUM_LOCAL_REFS); + getJNIEnv()->PushLocalFrame(NUM_LOCAL_REFS); } void JavaInstance::virtualEnd() { - getJNIEnv()->PopLocalFrame (NULL); + getJNIEnv()->PopLocalFrame(0); } -Class *JavaInstance::getClass() const +Class* JavaInstance::getClass() const { - if (_class == 0) - _class = new JavaClass (_instance->_instance); - return _class; + if (!m_class) + m_class = new JavaClass (m_instance->m_instance); + return m_class; } JSValue JavaInstance::stringValue(ExecState* exec) const { JSLock lock(SilenceAssertionsOnly); - - jstring stringValue = (jstring)callJNIMethod<jobject>(_instance->_instance, "toString", "()Ljava/lang/String;"); - JNIEnv *env = getJNIEnv(); - const jchar *c = getUCharactersFromJStringInEnv(env, stringValue); - UString u((const UChar *)c, (int)env->GetStringLength(stringValue)); + + jstring stringValue = (jstring)callJNIMethod<jobject>(m_instance->m_instance, "toString", "()Ljava/lang/String;"); + JNIEnv* env = getJNIEnv(); + const jchar* c = getUCharactersFromJStringInEnv(env, stringValue); + UString u((const UChar*)c, (int)env->GetStringLength(stringValue)); releaseUCharactersForJStringInEnv(env, stringValue, c); return jsString(exec, u); } JSValue JavaInstance::numberValue(ExecState* exec) const { - jdouble doubleValue = callJNIMethod<jdouble>(_instance->_instance, "doubleValue", "()D"); + jdouble doubleValue = callJNIMethod<jdouble>(m_instance->m_instance, "doubleValue", "()D"); return jsNumber(exec, doubleValue); } JSValue JavaInstance::booleanValue() const { - jboolean booleanValue = callJNIMethod<jboolean>(_instance->_instance, "booleanValue", "()Z"); + jboolean booleanValue = callJNIMethod<jboolean>(m_instance->m_instance, "booleanValue", "()Z"); return jsBoolean(booleanValue); } -JSValue JavaInstance::invokeMethod (ExecState *exec, const MethodList &methodList, const ArgList &args) +JSValue JavaInstance::invokeMethod(ExecState* exec, const MethodList& methodList, const ArgList &args) { int i, count = args.size(); - jvalue *jArgs; + jvalue* jArgs; JSValue resultValue; - Method *method = 0; + Method* method = 0; size_t numMethods = methodList.size(); - - // Try to find a good match for the overloaded method. The + + // Try to find a good match for the overloaded method. The // fundamental problem is that JavaScript doesn have the - // notion of method overloading and Java does. We could + // 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. - Method *aMethod; + Method* aMethod; for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) { aMethod = methodList[methodIndex]; if (aMethod->numParameters() == count) { @@ -130,26 +130,25 @@ JSValue JavaInstance::invokeMethod (ExecState *exec, const MethodList &methodLis break; } } - if (method == 0) { - JS_LOG ("unable to find an appropiate method\n"); + if (!method) { + JS_LOG("unable to find an appropiate method\n"); return jsUndefined(); } - - const JavaMethod *jMethod = static_cast<const JavaMethod*>(method); - JS_LOG ("call %s %s on %p\n", UString(jMethod->name()).UTF8String().c_str(), jMethod->signature(), _instance->_instance); - - if (count > 0) { - jArgs = (jvalue *)malloc (count * sizeof(jvalue)); - } + + const JavaMethod* jMethod = static_cast<const JavaMethod*>(method); + JS_LOG("call %s %s on %p\n", UString(jMethod->name()).UTF8String().c_str(), jMethod->signature(), m_instance->m_instance); + + if (count > 0) + jArgs = (jvalue*)malloc(count * sizeof(jvalue)); else jArgs = 0; - + for (i = 0; i < count; i++) { JavaParameter* aParameter = jMethod->parameterAt(i); jArgs[i] = convertValueToJValue(exec, args.at(i), aParameter->getJNIType(), aParameter->type()); JS_LOG("arg[%d] = %s\n", i, args.at(i).toString(exec).ascii()); } - + jvalue result; // Try to use the JNI abstraction first, otherwise fall back to @@ -161,129 +160,136 @@ JSValue JavaInstance::invokeMethod (ExecState *exec, const MethodList &methodLis bool handled = false; if (rootObject->nativeHandle()) { - jobject obj = _instance->_instance; + jobject obj = m_instance->m_instance; JSValue exceptionDescription; - const char *callingURL = 0; // FIXME, need to propagate calling URL to Java + const char *callingURL = 0; // FIXME, need to propagate calling URL to Java handled = dispatchJNICall(exec, rootObject->nativeHandle(), obj, jMethod->isStatic(), jMethod->JNIReturnType(), jMethod->methodID(obj), jArgs, result, callingURL, exceptionDescription); if (exceptionDescription) { throwError(exec, GeneralError, exceptionDescription.toString(exec)); - free (jArgs); + free(jArgs); return jsUndefined(); } } - + // 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. - if (!handled) { - jobject obj = _instance->_instance; - 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; + if (!handled) { + jobject obj = m_instance->m_instance; + 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; } } - - switch (jMethod->JNIReturnType()){ - case void_type: { + + switch (jMethod->JNIReturnType()) { + case void_type: + { resultValue = jsUndefined(); } break; - - case object_type: { - if (result.l != 0) { - const char *arrayType = jMethod->returnType(); - if (arrayType[0] == '[') { + + case object_type: + { + if (result.l) { + const char* arrayType = jMethod->returnType(); + if (arrayType[0] == '[') resultValue = JavaArray::convertJObjectToArray(exec, result.l, arrayType, rootObject); - } - else { + else resultValue = JavaInstance::create(result.l, rootObject)->createRuntimeObject(exec); - } - } - else { + } else resultValue = jsUndefined(); - } } break; - - case boolean_type: { + + case boolean_type: + { resultValue = jsBoolean(result.z); } break; - - case byte_type: { + + case byte_type: + { resultValue = jsNumber(exec, result.b); } break; - - case char_type: { + + case char_type: + { resultValue = jsNumber(exec, result.c); } break; - - case short_type: { + + case short_type: + { resultValue = jsNumber(exec, result.s); } break; - - case int_type: { + + case int_type: + { resultValue = jsNumber(exec, result.i); } break; - - case long_type: { + + case long_type: + { resultValue = jsNumber(exec, result.j); } break; - - case float_type: { + + case float_type: + { resultValue = jsNumber(exec, result.f); } break; - - case double_type: { + + case double_type: + { resultValue = jsNumber(exec, result.d); } break; - case invalid_type: - default: { + case invalid_type: + default: + { resultValue = jsUndefined(); } break; } - free (jArgs); + free(jArgs); return resultValue; } @@ -294,7 +300,7 @@ JSValue JavaInstance::defaultValue(ExecState* exec, PreferredPrimitiveType hint) return stringValue(exec); if (hint == PreferNumber) return numberValue(exec); - JavaClass *aClass = static_cast<JavaClass*>(getClass()); + JavaClass* aClass = static_cast<JavaClass*>(getClass()); if (aClass->isStringClass()) return stringValue(exec); if (aClass->isNumberClass()) @@ -304,32 +310,32 @@ JSValue JavaInstance::defaultValue(ExecState* exec, PreferredPrimitiveType hint) return valueOf(exec); } -JSValue JavaInstance::valueOf(ExecState* exec) const +JSValue JavaInstance::valueOf(ExecState* exec) const { return stringValue(exec); } JObjectWrapper::JObjectWrapper(jobject instance) -: _refCount(0) + : m_refCount(0) { - assert (instance != 0); + assert(instance); // Cache the JNIEnv used to get the global ref for this java instanace. // It'll be used to delete the reference. - _env = getJNIEnv(); - - _instance = _env->NewGlobalRef (instance); - - JS_LOG ("new global ref %p for %p\n", _instance, instance); - - if (_instance == NULL) { - fprintf (stderr, "%s: could not get GlobalRef for %p\n", __PRETTY_FUNCTION__, instance); - } + m_env = getJNIEnv(); + + m_instance = m_env->NewGlobalRef(instance); + + JS_LOG("new global ref %p for %p\n", m_instance, instance); + + if (!m_instance) + fprintf(stderr, "%s: could not get GlobalRef for %p\n", __PRETTY_FUNCTION__, instance); } -JObjectWrapper::~JObjectWrapper() { - JS_LOG ("deleting global ref %p\n", _instance); - _env->DeleteGlobalRef (_instance); +JObjectWrapper::~JObjectWrapper() +{ + JS_LOG("deleting global ref %p\n", m_instance); + m_env->DeleteGlobalRef(m_instance); } #endif // ENABLE(MAC_JAVA_BRIDGE) diff --git a/WebCore/bridge/jni/jni_instance.h b/WebCore/bridge/jni/jsc/JavaInstanceJSC.h index 049bfdb..d7b6102 100644 --- a/WebCore/bridge/jni/jni_instance.h +++ b/WebCore/bridge/jni/jsc/JavaInstanceJSC.h @@ -20,15 +20,15 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _JNI_INSTANCE_H_ -#define _JNI_INSTANCE_H_ +#ifndef JavaInstanceJSC_h +#define JavaInstanceJSC_h #if ENABLE(MAC_JAVA_BRIDGE) -#include "runtime.h" +#include "Bridge.h" #include "runtime_root.h" #include <JavaVM/jni.h> @@ -39,8 +39,7 @@ namespace Bindings { class JavaClass; -class JObjectWrapper -{ +class JObjectWrapper { friend class RefPtr<JObjectWrapper>; friend class JavaArray; friend class JavaField; @@ -48,46 +47,45 @@ friend class JavaInstance; friend class JavaMethod; public: - jobject instance() const { return _instance; } - void setInstance(jobject instance) { _instance = instance; } + jobject instance() const { return m_instance; } + void setInstance(jobject instance) { m_instance = instance; } protected: - JObjectWrapper(jobject instance); + JObjectWrapper(jobject instance); ~JObjectWrapper(); - - void ref() { _refCount++; } - void deref() - { - if (--_refCount == 0) - delete this; + + void ref() { m_refCount++; } + void deref() + { + if (!(--m_refCount)) + delete this; } - jobject _instance; + jobject m_instance; private: - JNIEnv *_env; - unsigned int _refCount; + JNIEnv* m_env; + unsigned int m_refCount; }; -class JavaInstance : public Instance -{ +class JavaInstance : public Instance { public: - static PassRefPtr<JavaInstance> create(jobject instance, PassRefPtr<RootObject> rootObject) + static PassRefPtr<JavaInstance> create(jobject instance, PassRefPtr<RootObject> rootObject) { return adoptRef(new JavaInstance(instance, rootObject)); } - + ~JavaInstance(); - - virtual Class *getClass() const; - + + virtual Class* getClass() const; + virtual JSValue valueOf(ExecState*) const; virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const; virtual JSValue invokeMethod(ExecState* exec, const MethodList& method, const ArgList& args); - jobject javaInstance() const { return _instance->_instance; } - + jobject javaInstance() const { return m_instance->m_instance; } + JSValue stringValue(ExecState*) const; JSValue numberValue(ExecState*) const; JSValue booleanValue() const; @@ -97,8 +95,8 @@ protected: virtual void virtualBegin(); virtual void virtualEnd(); - RefPtr<JObjectWrapper> _instance; - mutable JavaClass *_class; + RefPtr<JObjectWrapper> m_instance; + mutable JavaClass* m_class; }; } // namespace Bindings @@ -107,4 +105,4 @@ protected: #endif // ENABLE(MAC_JAVA_BRIDGE) -#endif // _JNI_INSTANCE_H_ +#endif // JavaInstanceJSC_h diff --git a/WebCore/bridge/jni/jsc/JavaStringJSC.h b/WebCore/bridge/jni/jsc/JavaStringJSC.h new file mode 100644 index 0000000..720f887 --- /dev/null +++ b/WebCore/bridge/jni/jsc/JavaStringJSC.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2010 Apple Computer, Inc. All rights reserved. + * + * 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 JavaStringJSC_h +#define JavaStringJSC_h + +#include "JNIUtility.h" +#include "JavaInstanceJSC.h" +#include <runtime/JSLock.h> + + +namespace JSC { + +namespace Bindings { + +class JavaStringImpl { +public: + ~JavaStringImpl() + { + JSLock lock(SilenceAssertionsOnly); + m_rep = 0; + } + + void init() + { + JSLock lock(SilenceAssertionsOnly); + m_rep = UString().rep(); + } + + void init(JNIEnv* e, jstring s) + { + int size = e->GetStringLength(s); + const jchar* uc = getUCharactersFromJStringInEnv(e, s); + { + JSLock lock(SilenceAssertionsOnly); + m_rep = UString(reinterpret_cast<const UChar*>(uc), size).rep(); + } + releaseUCharactersForJStringInEnv(e, s, uc); + } + + const char* UTF8String() const + { + if (!m_utf8String.c_str()) { + JSLock lock(SilenceAssertionsOnly); + m_utf8String = UString(m_rep).UTF8String(); + } + return m_utf8String.c_str(); + } + const jchar* uchars() const { return (const jchar*)m_rep->data(); } + int length() const { return m_rep->size(); } + UString uString() const { return UString(m_rep); } + +private: + RefPtr<UString::Rep> m_rep; + mutable CString m_utf8String; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaStringJSC_h diff --git a/WebCore/bridge/jni/v8/jni_utility_private.cpp b/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp index 6c8f018..a817bc0 100644 --- a/WebCore/bridge/jni/v8/jni_utility_private.cpp +++ b/WebCore/bridge/jni/v8/JNIUtilityPrivate.cpp @@ -24,10 +24,10 @@ */ #include "config.h" -#include "jni_utility_private.h" +#include "JNIUtilityPrivate.h" +#include "JavaNPObjectV8.h" #include "jni_runtime.h" -#include "jni_npobject.h" namespace JSC { @@ -42,7 +42,7 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case array_type: case object_type: { - result.l = (jobject)0; + result.l = static_cast<jobject>(0); // First see if we have a Java instance. if (type == NPVariantType_Object) { @@ -53,25 +53,25 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja // 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 == 0 && strcmp(javaClassName, "java.lang.String") == 0) { + if (!result.l && !strcmp(javaClassName, "java.lang.String")) { #ifdef CONVERT_NULL_TO_EMPTY_STRING if (type == NPVariantType_Null) { - JNIEnv *env = getJNIEnv(); + JNIEnv* env = getJNIEnv(); jchar buf[2]; - jobject javaString = env->functions->NewString (env, buf, 0); + jobject javaString = env->functions->NewString(env, buf, 0); result.l = javaString; - } - else + } else #else - if (type == NPVariantType_String) { + if (type == NPVariantType_String) #endif + { NPString src = NPVARIANT_TO_STRING(value); - JNIEnv *env = getJNIEnv(); + JNIEnv* env = getJNIEnv(); jobject javaString = env->NewStringUTF(src.UTF8Characters); result.l = javaString; } - } else if (result.l == 0) - bzero (&result, sizeof(jvalue)); // Handle it the same as a void case + } else if (!result.l) + bzero(&result, sizeof(jvalue)); // Handle it the same as a void case } break; @@ -80,14 +80,14 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja if (type == NPVariantType_Bool) result.z = NPVARIANT_TO_BOOLEAN(value); else - bzero(&result, sizeof(jvalue)); // as void case + bzero(&result, sizeof(jvalue)); // as void case } break; case byte_type: { if (type == NPVariantType_Int32) - result.b = (char)NPVARIANT_TO_INT32(value); + result.b = static_cast<char>(NPVARIANT_TO_INT32(value)); else bzero(&result, sizeof(jvalue)); } @@ -96,7 +96,7 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case char_type: { if (type == NPVariantType_Int32) - result.c = (char)NPVARIANT_TO_INT32(value); + result.c = static_cast<char>(NPVARIANT_TO_INT32(value)); else bzero(&result, sizeof(jvalue)); } @@ -105,7 +105,7 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case short_type: { if (type == NPVariantType_Int32) - result.s = (jshort)NPVARIANT_TO_INT32(value); + result.s = static_cast<jshort>(NPVARIANT_TO_INT32(value)); else bzero(&result, sizeof(jvalue)); } @@ -114,7 +114,7 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case int_type: { if (type == NPVariantType_Int32) - result.i = (jint)NPVARIANT_TO_INT32(value); + result.i = static_cast<jint>(NPVARIANT_TO_INT32(value)); else bzero(&result, sizeof(jvalue)); } @@ -123,9 +123,9 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case long_type: { if (type == NPVariantType_Int32) - result.j = (jlong)NPVARIANT_TO_INT32(value); + result.j = static_cast<jlong>(NPVARIANT_TO_INT32(value)); else if (type == NPVariantType_Double) - result.j = (jlong)NPVARIANT_TO_DOUBLE(value); + result.j = static_cast<jlong>(NPVARIANT_TO_DOUBLE(value)); else bzero(&result, sizeof(jvalue)); } @@ -134,9 +134,9 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case float_type: { if (type == NPVariantType_Int32) - result.j = (jfloat)NPVARIANT_TO_INT32(value); + result.j = static_cast<jfloat>(NPVARIANT_TO_INT32(value)); else if (type == NPVariantType_Double) - result.j = (jfloat)NPVARIANT_TO_DOUBLE(value); + result.j = static_cast<jfloat>(NPVARIANT_TO_DOUBLE(value)); else bzero(&result, sizeof(jvalue)); } @@ -145,9 +145,9 @@ jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* ja case double_type: { if (type == NPVariantType_Int32) - result.j = (jdouble)NPVARIANT_TO_INT32(value); + result.j = static_cast<jdouble>(NPVARIANT_TO_INT32(value)); else if (type == NPVariantType_Double) - result.j = (jdouble)NPVARIANT_TO_DOUBLE(value); + result.j = static_cast<jdouble>(NPVARIANT_TO_DOUBLE(value)); else bzero(&result, sizeof(jvalue)); } @@ -178,20 +178,18 @@ void convertJValueToNPVariant(jvalue value, JNIType jniType, const char* javaTyp case object_type: { - if (value.l != 0) { - if (strcmp(javaTypeName, "java.lang.String") == 0) { - const char* v = getCharactersFromJString((jstring)value.l); + 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((jstring)value.l, v); + releaseCharactersForJString(static_cast<jstring>(value.l), v); STRINGZ_TO_NPVARIANT(s, *result); - } else { + } else OBJECT_TO_NPVARIANT(JavaInstanceToNPObject(new JavaInstance(value.l)), *result); - } } - else { + else VOID_TO_NPVARIANT(*result); - } } break; diff --git a/WebCore/bridge/jni/v8/jni_utility_private.h b/WebCore/bridge/jni/v8/JNIUtilityPrivate.h index 397de7b..f20cfb2 100644 --- a/WebCore/bridge/jni/v8/jni_utility_private.h +++ b/WebCore/bridge/jni/v8/JNIUtilityPrivate.h @@ -23,10 +23,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _JNI_UTILITY_PRIVATE_H_ -#define _JNI_UTILITY_PRIVATE_H_ +#ifndef JNIUtilityPrivate_h +#define JNIUtilityPrivate_h -#include "jni_utility.h" +#include "JNIUtility.h" #include "npruntime.h" namespace JSC { @@ -40,4 +40,4 @@ void convertJValueToNPVariant(jvalue, JNIType, const char* javaClassName, NPVari } // namespace JSC -#endif // _JNI_UTILITY_H_ +#endif // JNIUtilityPrivate_h diff --git a/WebCore/bridge/jni/v8/JavaClassV8.cpp b/WebCore/bridge/jni/v8/JavaClassV8.cpp new file mode 100644 index 0000000..c29cf86 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaClassV8.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2010 Apple Computer, 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 "JavaClassV8.h" + +#include "JNIUtility.h" +#include "jni_runtime.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; + } + + jstring className = (jstring)callJNIMethod<jobject>(aClass, "getName", "()Ljava/lang/String;"); + const char* classNameC = getCharactersFromJString(className); + m_name = strdup(classNameC); + releaseCharactersForJString(className, classNameC); + + int i; + JNIEnv* env = getJNIEnv(); + + // Get the fields + jarray fields = (jarray)callJNIMethod<jobject>(aClass, "getFields", "()[Ljava/lang/reflect/Field;"); + int numFields = env->GetArrayLength(fields); + for (i = 0; i < numFields; i++) { + jobject aJField = env->GetObjectArrayElement((jobjectArray)fields, i); + JavaField* aField = new JavaField(env, aJField); // deleted in the JavaClass destructor + { + m_fields.set(aField->name().UTF8String(), aField); + } + env->DeleteLocalRef(aJField); + } + + // Get the methods + jarray methods = (jarray)callJNIMethod<jobject>(aClass, "getMethods", "()[Ljava/lang/reflect/Method;"); + int numMethods = env->GetArrayLength(methods); + for (i = 0; i < numMethods; i++) { + jobject aJMethod = env->GetObjectArrayElement((jobjectArray)methods, i); + JavaMethod* aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor + MethodList* methodList; + { + methodList = m_methods.get(aMethod->name().UTF8String()); + if (!methodList) { + methodList = new MethodList(); + m_methods.set(aMethod->name().UTF8String(), methodList); + } + } + methodList->append(aMethod); + env->DeleteLocalRef(aJMethod); + } + env->DeleteLocalRef(fields); + env->DeleteLocalRef(methods); + env->DeleteLocalRef(aClass); +} + +JavaClass::~JavaClass() +{ + free((void*)m_name); + + 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/WebCore/bridge/jni/v8/JavaClassV8.h b/WebCore/bridge/jni/v8/JavaClassV8.h new file mode 100644 index 0000000..12cdf93 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaClassV8.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2010 Apple Computer, 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 JavaClassV8_h +#define JavaClassV8_h + +#include "PlatformString.h" +#include "StringHash.h" +#include "jni_runtime.h" +#include <wtf/HashMap.h> +#include <wtf/Vector.h> + +namespace JSC { + +namespace Bindings { + +typedef Vector<JavaMethod*> MethodList; +typedef HashMap<WebCore::String, MethodList*> MethodListMap; +typedef HashMap<WebCore::String, JavaField*> FieldMap; + +class JavaClass { +public: + JavaClass(jobject anInstance); + ~JavaClass(); + + MethodList methodsNamed(const char* name) const; + JavaField* fieldNamed(const char* name) const; + +private: + const char* m_name; + MethodListMap m_methods; + FieldMap m_fields; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaClassV8_h diff --git a/WebCore/bridge/jni/v8/JavaInstanceV8.cpp b/WebCore/bridge/jni/v8/JavaInstanceV8.cpp new file mode 100644 index 0000000..13d4984 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaInstanceV8.cpp @@ -0,0 +1,168 @@ +/* + * 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: + * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 "JNIUtility.h" +#include "JavaClassV8.h" +#include "jni_runtime.h" + +#include <assert.h> +#include <utils/Log.h> + +#define LOG_TAG "v8binding" + +using namespace JSC::Bindings; + +JavaInstance::JavaInstance(jobject instance) +{ + m_instance = new JObjectWrapper(instance); + m_class = 0; +} + +JavaInstance::~JavaInstance() +{ + m_instance = 0; + delete m_class; +} + +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 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) { + LOGW("unable to find an appropiate method\n"); + return false; + } + + const JavaMethod* jMethod = static_cast<const JavaMethod*>(method); + + jvalue* jArgs = 0; + if (count > 0) + jArgs = (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); + + LOGV("new global ref %p for %p\n", m_instance, instance); + + if (!m_instance) + fprintf (stderr, "%s: could not get GlobalRef for %p\n", __PRETTY_FUNCTION__, instance); +} + +JObjectWrapper::~JObjectWrapper() { + LOGV("deleting global ref %p\n", m_instance); + m_env->DeleteGlobalRef(m_instance); +} diff --git a/WebCore/bridge/jni/v8/JavaInstanceV8.h b/WebCore/bridge/jni/v8/JavaInstanceV8.h new file mode 100644 index 0000000..dcd51e8 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaInstanceV8.h @@ -0,0 +1,99 @@ +/* + * 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: + * 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 APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 "JNIUtilityPrivate.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; } + +protected: + JObjectWrapper(jobject instance); + ~JObjectWrapper(); + + void ref() { m_refCount++; } + void deref() + { + if (!(--m_refCount)) + delete this; + } + + 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/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp b/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp new file mode 100644 index 0000000..636d075 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaNPObjectV8.cpp @@ -0,0 +1,166 @@ +/* + * Copyright 2009, 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 APPLE COMPUTER, INC. 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 "JNIUtility.h" +#include "JavaClassV8.h" +#include "JavaInstanceV8.h" +#include "jni_runtime.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; + bzero(obj, 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 + JavaNPObject_HasMethod, + JavaNPObject_Invoke, + 0, // invokeDefault, + JavaNPObject_HasProperty, + JavaNPObject_GetProperty, + 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 JavaNPObject_HasMethod(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 JavaNPObject_Invoke(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 JavaNPObject_HasProperty(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 JavaNPObject_GetProperty(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; + + jvalue value = getJNIField(instance->javaInstance(), + field->getJNIType(), + field->name().UTF8String(), + field->type()); + + convertJValueToNPVariant(value, field->getJNIType(), field->type(), result); + + return true; +} + +} // namespace Bindings + +} // namespace JSC diff --git a/WebCore/bridge/jni/v8/JavaNPObjectV8.h b/WebCore/bridge/jni/v8/JavaNPObjectV8.h new file mode 100644 index 0000000..c36ef70 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaNPObjectV8.h @@ -0,0 +1,58 @@ +/* + * 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 APPLE COMPUTER, INC. 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 "jni_runtime.h" +#include "npruntime.h" +#include <JavaVM/jni.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 JavaNPObject_HasMethod(NPObject*, NPIdentifier name); +bool JavaNPObject_Invoke(NPObject*, NPIdentifier methodName, const NPVariant* args, uint32_t argCount, NPVariant* result); +bool JavaNPObject_HasProperty(NPObject*, NPIdentifier name); +bool JavaNPObject_GetProperty(NPObject*, NPIdentifier name, NPVariant* result); + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaNPObjectV8_h diff --git a/WebCore/bridge/jni/v8/JavaStringV8.h b/WebCore/bridge/jni/v8/JavaStringV8.h new file mode 100644 index 0000000..8788e58 --- /dev/null +++ b/WebCore/bridge/jni/v8/JavaStringV8.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2010 Apple Computer, Inc. All rights reserved. + * + * 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 "CString.h" +#include "JNIUtility.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 = WebCore::CString(cs, size); + } + releaseCharactersForJStringInEnv(e, s, cs); + } + + const char* UTF8String() const { return m_utf8String.data(); } + const jchar* uchars() const { return 0; } // Not implemented + int length() const { return m_utf8String.length(); } + +private: + WebCore::CString m_utf8String; +}; + +} // namespace Bindings + +} // namespace JSC + +#endif // JavaStringV8_h diff --git a/WebCore/bridge/runtime.cpp b/WebCore/bridge/jsc/BridgeJSC.cpp index eac8586..ed582d3 100644 --- a/WebCore/bridge/runtime.cpp +++ b/WebCore/bridge/jsc/BridgeJSC.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2003, 2006, 2008 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 @@ -20,11 +21,11 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" -#include "runtime.h" +#include "BridgeJSC.h" #include "runtime_object.h" #include "runtime_root.h" @@ -34,12 +35,14 @@ #include "qt_instance.h" #endif -namespace JSC { namespace Bindings { +namespace JSC { + +namespace Bindings { Array::Array(PassRefPtr<RootObject> rootObject) - : _rootObject(rootObject) + : m_rootObject(rootObject) { - ASSERT(_rootObject); + ASSERT(m_rootObject); } Array::~Array() @@ -47,10 +50,10 @@ Array::~Array() } Instance::Instance(PassRefPtr<RootObject> rootObject) - : _rootObject(rootObject) + : m_rootObject(rootObject) , m_runtimeObject(0) { - ASSERT(_rootObject); + ASSERT(m_rootObject); } Instance::~Instance() @@ -82,28 +85,28 @@ void Instance::end() RuntimeObjectImp* Instance::createRuntimeObject(ExecState* exec) { - ASSERT(_rootObject); - ASSERT(_rootObject->isValid()); + ASSERT(m_rootObject); + ASSERT(m_rootObject->isValid()); if (m_runtimeObject) return m_runtimeObject; JSLock lock(SilenceAssertionsOnly); m_runtimeObject = newRuntimeObject(exec); - _rootObject->addRuntimeObject(m_runtimeObject); + m_rootObject->addRuntimeObject(m_runtimeObject); return m_runtimeObject; } RuntimeObjectImp* Instance::newRuntimeObject(ExecState* exec) { JSLock lock(SilenceAssertionsOnly); - return new (exec) RuntimeObjectImp(exec, this); + return new (exec)RuntimeObjectImp(exec, this); } void Instance::willDestroyRuntimeObject() { - ASSERT(_rootObject); - ASSERT(_rootObject->isValid()); + ASSERT(m_rootObject); + ASSERT(m_rootObject->isValid()); ASSERT(m_runtimeObject); - _rootObject->removeRuntimeObject(m_runtimeObject); + m_rootObject->removeRuntimeObject(m_runtimeObject); m_runtimeObject = 0; } @@ -113,9 +116,11 @@ void Instance::willInvalidateRuntimeObject() m_runtimeObject = 0; } -RootObject* Instance::rootObject() const -{ - return _rootObject && _rootObject->isValid() ? _rootObject.get() : 0; +RootObject* Instance::rootObject() const +{ + return m_rootObject && m_rootObject->isValid() ? m_rootObject.get() : 0; } -} } // namespace JSC::Bindings +} // namespace Bindings + +} // namespace JSC diff --git a/WebCore/bridge/runtime.h b/WebCore/bridge/jsc/BridgeJSC.h index 6682a97..8e2cb2b 100644 --- a/WebCore/bridge/runtime.h +++ b/WebCore/bridge/jsc/BridgeJSC.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2003, 2008, 2009 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 @@ -20,11 +21,13 @@ * 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. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef JAVASCRIPTCORE_BINDINGS_RUNTIME_H -#define JAVASCRIPTCORE_BINDINGS_RUNTIME_H +#ifndef BridgeJSC_h +#define BridgeJSC_h + +#if USE(JSC) #include <runtime/JSString.h> #include <wtf/HashMap.h> @@ -55,13 +58,6 @@ public: virtual ~Field() { } }; -class Method : public Noncopyable { -public: - virtual int numParameters() const = 0; - - virtual ~Method() { } -}; - class Class : public Noncopyable { public: virtual MethodList methodsNamed(const Identifier&, Instance*) const = 0; @@ -85,12 +81,12 @@ public: // any needed state. void begin(); void end(); - + virtual Class* getClass() const = 0; RuntimeObjectImp* createRuntimeObject(ExecState*); void willInvalidateRuntimeObject(); void willDestroyRuntimeObject(); - + // Returns false if the value was not set successfully. virtual bool setValueOfUndefinedField(ExecState*, const Identifier&, JSValue) { return false; } @@ -98,18 +94,18 @@ public: virtual bool supportsInvokeDefaultMethod() const { return false; } virtual JSValue invokeDefaultMethod(ExecState*, const ArgList&) { return jsUndefined(); } - + virtual bool supportsConstruct() const { return false; } virtual JSValue invokeConstruct(ExecState*, const ArgList&) { return JSValue(); } - + virtual void getPropertyNames(ExecState*, PropertyNameArray&) { } virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const = 0; - + virtual JSValue valueOf(ExecState* exec) const = 0; - + RootObject* rootObject() const; - + virtual ~Instance(); virtual bool getOwnPropertySlot(JSObject*, ExecState*, const Identifier&, PropertySlot&) { return false; } @@ -121,7 +117,7 @@ protected: virtual void virtualEnd() { } virtual RuntimeObjectImp* newRuntimeObject(ExecState*); - RefPtr<RootObject> _rootObject; + RefPtr<RootObject> m_rootObject; private: RuntimeObjectImp* m_runtimeObject; @@ -131,23 +127,25 @@ class Array : public Noncopyable { public: Array(PassRefPtr<RootObject>); virtual ~Array(); - - virtual void setValueAt(ExecState *, unsigned index, JSValue) const = 0; - virtual JSValue valueAt(ExecState *, unsigned index) const = 0; + + virtual void setValueAt(ExecState*, unsigned index, JSValue) const = 0; + virtual JSValue valueAt(ExecState*, unsigned index) const = 0; virtual unsigned int getLength() const = 0; protected: - RefPtr<RootObject> _rootObject; + RefPtr<RootObject> m_rootObject; }; -const char *signatureForParameters(const ArgList&); +const char* signatureForParameters(const ArgList&); typedef HashMap<RefPtr<UString::Rep>, MethodList*> MethodListMap; -typedef HashMap<RefPtr<UString::Rep>, Method*> MethodMap; -typedef HashMap<RefPtr<UString::Rep>, Field*> FieldMap; - +typedef HashMap<RefPtr<UString::Rep>, Method*> MethodMap; +typedef HashMap<RefPtr<UString::Rep>, Field*> FieldMap; + } // namespace Bindings } // namespace JSC +#endif // USE(JSC) + #endif diff --git a/WebCore/bridge/objc/objc_instance.mm b/WebCore/bridge/objc/objc_instance.mm index 9f2e3bb..25da8b9 100644 --- a/WebCore/bridge/objc/objc_instance.mm +++ b/WebCore/bridge/objc/objc_instance.mm @@ -277,7 +277,7 @@ JSValue ObjcInstance::invokeMethod(ExecState* exec, const MethodList &methodList if (*type != 'v') { [invocation getReturnValue:buffer]; - result = convertObjcValueToValue(exec, buffer, objcValueType, _rootObject.get()); + result = convertObjcValueToValue(exec, buffer, objcValueType, m_rootObject.get()); } } @catch(NSException* localException) { } @@ -329,7 +329,7 @@ JSValue ObjcInstance::invokeDefaultMethod(ExecState* exec, const ArgList &args) // OK with 32 here. char buffer[32]; [invocation getReturnValue:buffer]; - result = convertObjcValueToValue(exec, buffer, objcValueType, _rootObject.get()); + result = convertObjcValueToValue(exec, buffer, objcValueType, m_rootObject.get()); } @catch(NSException* localException) { } moveGlobalExceptionToExecState(exec); @@ -383,7 +383,7 @@ JSValue ObjcInstance::getValueOfUndefinedField(ExecState* exec, const Identifier @try { id objcValue = [targetObject valueForUndefinedKey:[NSString stringWithCString:property.ascii() encoding:NSASCIIStringEncoding]]; - result = convertObjcValueToValue(exec, &objcValue, ObjcObjectType, _rootObject.get()); + result = convertObjcValueToValue(exec, &objcValue, ObjcObjectType, m_rootObject.get()); } @catch(NSException* localException) { // Do nothing. Class did not override valueForUndefinedKey:. } diff --git a/WebCore/bridge/objc/objc_runtime.h b/WebCore/bridge/objc/objc_runtime.h index 63f3254..4de1ce6 100644 --- a/WebCore/bridge/objc/objc_runtime.h +++ b/WebCore/bridge/objc/objc_runtime.h @@ -26,8 +26,8 @@ #ifndef KJS_BINDINGS_OBJC_RUNTIME_H #define KJS_BINDINGS_OBJC_RUNTIME_H +#include "Bridge.h" #include "objc_header.h" -#include "runtime.h" #include <runtime/JSGlobalObject.h> #include <wtf/RetainPtr.h> diff --git a/WebCore/bridge/objc/objc_runtime.mm b/WebCore/bridge/objc/objc_runtime.mm index 5efc865..772695c 100644 --- a/WebCore/bridge/objc/objc_runtime.mm +++ b/WebCore/bridge/objc/objc_runtime.mm @@ -174,7 +174,7 @@ JSValue ObjcArray::valueAt(ExecState* exec, unsigned int index) const @try { id obj = [_array.get() objectAtIndex:index]; if (obj) - return convertObjcValueToValue (exec, &obj, ObjcObjectType, _rootObject.get()); + return convertObjcValueToValue (exec, &obj, ObjcObjectType, m_rootObject.get()); } @catch(NSException* localException) { return throwError(exec, GeneralError, "Objective-C exception."); } diff --git a/WebCore/bridge/qt/qt_class.h b/WebCore/bridge/qt/qt_class.h index dc6b130..b7a04cf 100644 --- a/WebCore/bridge/qt/qt_class.h +++ b/WebCore/bridge/qt/qt_class.h @@ -20,8 +20,7 @@ #ifndef BINDINGS_QT_CLASS_H_ #define BINDINGS_QT_CLASS_H_ -#include "runtime.h" - +#include "Bridge.h" #include "qglobal.h" QT_BEGIN_NAMESPACE diff --git a/WebCore/bridge/qt/qt_instance.h b/WebCore/bridge/qt/qt_instance.h index 0afc6c7..1fc253a 100644 --- a/WebCore/bridge/qt/qt_instance.h +++ b/WebCore/bridge/qt/qt_instance.h @@ -20,11 +20,11 @@ #ifndef BINDINGS_QT_INSTANCE_H_ #define BINDINGS_QT_INSTANCE_H_ -#include <QtScript/qscriptengine.h> -#include "runtime.h" +#include "Bridge.h" #include "runtime_root.h" -#include <qpointer.h> +#include <QtScript/qscriptengine.h> #include <qhash.h> +#include <qpointer.h> #include <qset.h> namespace JSC { diff --git a/WebCore/bridge/qt/qt_runtime.cpp b/WebCore/bridge/qt/qt_runtime.cpp index 94749b4..c711baa 100644 --- a/WebCore/bridge/qt/qt_runtime.cpp +++ b/WebCore/bridge/qt/qt_runtime.cpp @@ -46,7 +46,6 @@ #include "qvarlengtharray.h" #include <JSFunction.h> #include <limits.h> -#include <runtime.h> #include <runtime/Error.h> #include <runtime_array.h> #include <runtime_object.h> @@ -1744,7 +1743,7 @@ template <typename T> QtArray<T>::~QtArray () template <typename T> RootObject* QtArray<T>::rootObject() const { - return _rootObject && _rootObject->isValid() ? _rootObject.get() : 0; + return m_rootObject && m_rootObject->isValid() ? m_rootObject.get() : 0; } template <typename T> void QtArray<T>::setValueAt(ExecState* exec, unsigned index, JSValue aValue) const diff --git a/WebCore/bridge/qt/qt_runtime.h b/WebCore/bridge/qt/qt_runtime.h index dc55f61..d9450eb 100644 --- a/WebCore/bridge/qt/qt_runtime.h +++ b/WebCore/bridge/qt/qt_runtime.h @@ -20,9 +20,9 @@ #ifndef BINDINGS_QT_RUNTIME_H_ #define BINDINGS_QT_RUNTIME_H_ +#include "Bridge.h" #include "Completion.h" #include "Protect.h" -#include "runtime.h" #include "runtime_method.h" #include <qbytearray.h> diff --git a/WebCore/bridge/runtime_array.h b/WebCore/bridge/runtime_array.h index 1218b8c..eeaa337 100644 --- a/WebCore/bridge/runtime_array.h +++ b/WebCore/bridge/runtime_array.h @@ -26,7 +26,7 @@ #ifndef RUNTIME_ARRAY_H_ #define RUNTIME_ARRAY_H_ -#include "runtime.h" +#include "Bridge.h" #include <runtime/JSGlobalObject.h> namespace JSC { diff --git a/WebCore/bridge/runtime_method.h b/WebCore/bridge/runtime_method.h index 9676048..148be32 100644 --- a/WebCore/bridge/runtime_method.h +++ b/WebCore/bridge/runtime_method.h @@ -26,7 +26,7 @@ #ifndef RUNTIME_FUNCTION_H_ #define RUNTIME_FUNCTION_H_ -#include "runtime.h" +#include "Bridge.h" #include <runtime/InternalFunction.h> #include <runtime/JSGlobalObject.h> #include <wtf/OwnPtr.h> diff --git a/WebCore/bridge/runtime_object.h b/WebCore/bridge/runtime_object.h index 391e078..7d658fa 100644 --- a/WebCore/bridge/runtime_object.h +++ b/WebCore/bridge/runtime_object.h @@ -26,7 +26,7 @@ #ifndef KJS_RUNTIME_OBJECT_H #define KJS_RUNTIME_OBJECT_H -#include "runtime.h" +#include "Bridge.h" #include <runtime/JSGlobalObject.h> namespace JSC { diff --git a/WebCore/bridge/runtime_root.cpp b/WebCore/bridge/runtime_root.cpp index 0e9bf74..143c3ad 100644 --- a/WebCore/bridge/runtime_root.cpp +++ b/WebCore/bridge/runtime_root.cpp @@ -26,7 +26,7 @@ #include "config.h" #include "runtime_root.h" -#include "runtime.h" +#include "Bridge.h" #include "runtime_object.h" #include <runtime/JSGlobalObject.h> #include <wtf/HashCountedSet.h> diff --git a/WebCore/bridge/testbindings.cpp b/WebCore/bridge/testbindings.cpp index e4bbc7e..bcba115 100644 --- a/WebCore/bridge/testbindings.cpp +++ b/WebCore/bridge/testbindings.cpp @@ -19,19 +19,17 @@ * */ #include "config.h" -#include <assert.h> -#include <stdio.h> -#include <string.h> -#include "JSValue.h" +#include "Bridge.h" #include "JSObject.h" -#include "types.h" +#include "JSValue.h" #include "interpreter.h" - #include "npruntime_internal.h" - -#include "runtime.h" #include "runtime_object.h" +#include "types.h" +#include <assert.h> +#include <stdio.h> +#include <string.h> #define LOG(formatAndArgs...) { \ diff --git a/WebCore/bridge/testbindings.mm b/WebCore/bridge/testbindings.mm index ca70e17..31564a8 100644 --- a/WebCore/bridge/testbindings.mm +++ b/WebCore/bridge/testbindings.mm @@ -24,20 +24,18 @@ */ #include "config.h" -#include <Foundation/Foundation.h> +#include "Bridge.h" +#include <Foundation/Foundation.h> +#include "JSObject.h" +#include "JSValue.h" #import <WebKit/WebScriptObject.h> - +#include "interpreter.h" +#include "runtime_object.h" #include <stdio.h> #include <string.h> - -#include "JSValue.h" -#include "JSObject.h" #include "types.h" -#include "interpreter.h" -#include "runtime.h" -#include "runtime_object.h" #define LOG(formatAndArgs...) { \ fprintf (stderr, "%s: ", __PRETTY_FUNCTION__); \ diff --git a/WebCore/bridge/testqtbindings.cpp b/WebCore/bridge/testqtbindings.cpp index 41a9a3a..73df155 100644 --- a/WebCore/bridge/testqtbindings.cpp +++ b/WebCore/bridge/testqtbindings.cpp @@ -23,21 +23,18 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" -#include <assert.h> -#include <stdio.h> -#include <string.h> -#include "JSValue.h" +#include "Bridge.h" #include "JSObject.h" -#include "types.h" +#include "JSValue.h" #include "interpreter.h" - -#include "qobject.h" #include "qdebug.h" - -#include "runtime.h" +#include "qobject.h" #include "runtime_object.h" - +#include "types.h" +#include <assert.h> +#include <stdio.h> +#include <string.h> class MyObject : public QObject |
