1/* 2 * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved. 3 * Copyright 2010, The Android Open Source Project 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "config.h" 28#include "JavaInstanceJobjectV8.h" 29 30#if ENABLE(JAVA_BRIDGE) 31 32#include "JNIUtilityPrivate.h" 33#include "JavaClassJobjectV8.h" 34#include "JavaFieldV8.h" 35#include "JavaMethod.h" 36 37#include <wtf/OwnArrayPtr.h> 38#include <wtf/PassOwnPtr.h> 39#include <wtf/text/CString.h> 40#include <wtf/text/StringBuilder.h> 41 42using namespace JSC::Bindings; 43 44JavaInstanceJobject::JavaInstanceJobject(jobject instance) 45 : m_instance(new JobjectWrapper(instance)) 46{ 47} 48 49#define NUM_LOCAL_REFS 64 50 51void JavaInstanceJobject::begin() 52{ 53 getJNIEnv()->PushLocalFrame(NUM_LOCAL_REFS); 54} 55 56void JavaInstanceJobject::end() 57{ 58 getJNIEnv()->PopLocalFrame(0); 59} 60 61JavaClass* JavaInstanceJobject::getClass() const 62{ 63 if (!m_class) 64 m_class = adoptPtr(new JavaClassJobject(javaInstance())); 65 return m_class.get(); 66} 67 68// ANDROID 69JavaValue JavaInstanceJobject::invokeMethod(const JavaMethod* method, JavaValue* args, bool& didRaiseUncaughtException) 70{ 71 didRaiseUncaughtException = false; 72// END ANDROID 73 74 ASSERT(getClass()->methodsNamed(method->name().utf8().data()).find(method) != notFound); 75 unsigned int numParams = method->numParameters(); 76 OwnArrayPtr<jvalue> jvalueArgs = adoptArrayPtr(new jvalue[numParams]); 77 for (unsigned int i = 0; i < numParams; ++i) 78 jvalueArgs[i] = javaValueToJvalue(args[i]); 79 jvalue result = callJNIMethod(javaInstance(), method->returnType(), method->name().utf8().data(), method->signature(), jvalueArgs.get()); 80 81// ANDROID 82 JNIEnv* env = getJNIEnv(); 83 if (env->ExceptionCheck() != JNI_FALSE) { 84 env->ExceptionClear(); 85 didRaiseUncaughtException = true; 86 return JavaValue(); 87 } 88 89 return jvalueToJavaValue(result, method->returnType()); 90// END ANDROID 91} 92 93static void appendClassName(StringBuilder& builder, const char* className) 94{ 95 char* c = fastStrDup(className); 96 char* result = c; 97 while (*c) { 98 if (*c == '.') 99 *c = '/'; 100 c++; 101 } 102 builder.append(result); 103 fastFree(result); 104} 105 106JavaValue JavaInstanceJobject::getField(const JavaField* field) 107{ 108 ASSERT(getClass()->fieldNamed(field->name().utf8().data()) == field); 109 110 StringBuilder signature; 111 signature.append(signatureFromJavaType(field->type())); 112 if (field->type() == JavaTypeObject || field->type() == JavaTypeString) { 113 appendClassName(signature, field->typeClassName()); 114 signature.append(';'); 115 } 116 return jvalueToJavaValue(getJNIField(javaInstance(), field->type(), field->name().utf8().data(), signature.toString().utf8().data()), field->type()); 117} 118 119#endif // ENABLE(JAVA_BRIDGE) 120