JavaInstanceJSC.cpp [plain text]
#include "config.h"
#include "JavaInstanceJSC.h"
#if ENABLE(JAVA_BRIDGE)
#include "JavaRuntimeObject.h"
#include "JNIUtilityPrivate.h"
#include "JSDOMBinding.h"
#include "JavaArrayJSC.h"
#include "JavaClassJSC.h"
#include "JavaMethodJSC.h"
#include "JavaStringJSC.h"
#include "Logging.h"
#include "jni_jsobject.h"
#include "runtime_method.h"
#include "runtime_object.h"
#include "runtime_root.h"
#include <runtime/ArgList.h>
#include <runtime/Error.h>
#include <runtime/FunctionPrototype.h>
#include <runtime/JSLock.h>
using namespace JSC::Bindings;
using namespace JSC;
using namespace WebCore;
JavaInstance::JavaInstance(jobject instance, PassRefPtr<RootObject> rootObject)
: Instance(rootObject)
{
m_instance = JobjectWrapper::create(instance);
m_class = 0;
}
JavaInstance::~JavaInstance()
{
delete m_class;
}
RuntimeObject* JavaInstance::newRuntimeObject(ExecState* exec)
{
return JavaRuntimeObject::create(exec, exec->lexicalGlobalObject(), this);
}
#define NUM_LOCAL_REFS 64
void JavaInstance::virtualBegin()
{
getJNIEnv()->PushLocalFrame(NUM_LOCAL_REFS);
}
void JavaInstance::virtualEnd()
{
getJNIEnv()->PopLocalFrame(0);
}
Class* JavaInstance::getClass() const
{
if (!m_class)
m_class = new JavaClass (m_instance->instance());
return m_class;
}
JSValue JavaInstance::stringValue(ExecState* exec) const
{
JSLockHolder lock(exec);
jstring stringValue = (jstring)callJNIMethod<jobject>(m_instance->instance(), "toString", "()Ljava/lang/String;");
if (!stringValue)
return jsString(exec, UString());
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*) const
{
jdouble doubleValue = callJNIMethod<jdouble>(m_instance->instance(), "doubleValue", "()D");
return jsNumber(doubleValue);
}
JSValue JavaInstance::booleanValue() const
{
jboolean booleanValue = callJNIMethod<jboolean>(m_instance->instance(), "booleanValue", "()Z");
return jsBoolean(booleanValue);
}
class JavaRuntimeMethod : public RuntimeMethod {
public:
typedef RuntimeMethod Base;
static JavaRuntimeMethod* create(ExecState* exec, JSGlobalObject* globalObject, const Identifier& name, Bindings::MethodList& list)
{
Structure* domStructure = WebCore::deprecatedGetDOMStructure<JavaRuntimeMethod>(exec);
JavaRuntimeMethod* method = new (NotNull, allocateCell<JavaRuntimeMethod>(*exec->heap())) JavaRuntimeMethod(globalObject, domStructure, list);
method->finishCreation(exec->globalData(), name);
return method;
}
static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(globalData, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
}
static const ClassInfo s_info;
private:
JavaRuntimeMethod(JSGlobalObject* globalObject, Structure* structure, Bindings::MethodList& list)
: RuntimeMethod(globalObject, structure, list)
{
}
void finishCreation(JSGlobalData& globalData, const Identifier& name)
{
Base::finishCreation(globalData, name);
ASSERT(inherits(&s_info));
}
};
const ClassInfo JavaRuntimeMethod::s_info = { "JavaRuntimeMethod", &RuntimeMethod::s_info, 0, 0, CREATE_METHOD_TABLE(JavaRuntimeMethod) };
JSValue JavaInstance::getMethod(ExecState* exec, const Identifier& propertyName)
{
MethodList methodList = getClass()->methodsNamed(propertyName, this);
return JavaRuntimeMethod::create(exec, exec->lexicalGlobalObject(), propertyName, methodList);
}
JSValue JavaInstance::invokeMethod(ExecState* exec, RuntimeMethod* runtimeMethod)
{
if (!asObject(runtimeMethod)->inherits(&JavaRuntimeMethod::s_info))
return throwError(exec, createTypeError(exec, "Attempt to invoke non-Java method on Java object."));
const MethodList& methodList = *runtimeMethod->methods();
int i;
int count = exec->argumentCount();
JSValue resultValue;
Method* method = 0;
size_t numMethods = methodList.size();
for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) {
Method* aMethod = methodList[methodIndex];
if (aMethod->numParameters() == count) {
method = aMethod;
break;
}
}
if (!method) {
LOG(LiveConnect, "JavaInstance::invokeMethod unable to find an appropiate method");
return jsUndefined();
}
const JavaMethod* jMethod = static_cast<const JavaMethod*>(method);
LOG(LiveConnect, "JavaInstance::invokeMethod call %s %s on %p", UString(jMethod->name().impl()).utf8().data(), jMethod->signature(), m_instance->instance());
Vector<jvalue> jArgs(count);
for (i = 0; i < count; i++) {
CString javaClassName = jMethod->parameterAt(i).utf8();
jArgs[i] = convertValueToJValue(exec, m_rootObject.get(), exec->argument(i), javaTypeFromClassName(javaClassName.data()), javaClassName.data());
LOG(LiveConnect, "JavaInstance::invokeMethod arg[%d] = %s", i, exec->argument(i).toString(exec)->value(exec).ascii().data());
}
jvalue result;
RootObject* rootObject = this->rootObject();
if (!rootObject)
return jsUndefined();
bool handled = false;
if (rootObject->nativeHandle()) {
jobject obj = m_instance->instance();
JSValue exceptionDescription;
const char *callingURL = 0; jmethodID methodId = getMethodID(obj, jMethod->name().utf8().data(), jMethod->signature());
handled = dispatchJNICall(exec, rootObject->nativeHandle(), obj, jMethod->isStatic(), jMethod->returnType(), methodId, jArgs.data(), result, callingURL, exceptionDescription);
if (exceptionDescription) {
throwError(exec, createError(exec, exceptionDescription.toString(exec)->value(exec)));
return jsUndefined();
}
}
switch (jMethod->returnType()) {
case JavaTypeVoid:
{
resultValue = jsUndefined();
}
break;
case JavaTypeObject:
{
if (result.l) {
const char* arrayType = jMethod->returnTypeClassName();
if (arrayType[0] == '[')
resultValue = JavaArray::convertJObjectToArray(exec, result.l, arrayType, rootObject);
else {
jobject classOfInstance = callJNIMethod<jobject>(result.l, "getClass", "()Ljava/lang/Class;");
jstring className = static_cast<jstring>(callJNIMethod<jobject>(classOfInstance, "getName", "()Ljava/lang/String;"));
if (!strcmp(JavaString(className).utf8(), "sun.plugin.javascript.webkit.JSObject")) {
JNIEnv* env = getJNIEnv();
jfieldID fieldID = env->GetFieldID(static_cast<jclass>(classOfInstance), "nativeJSObject", "J");
jlong nativeHandle = env->GetLongField(result.l, fieldID);
if (nativeHandle == 1 )
return jsUndefined();
return jlong_to_impptr(nativeHandle);
} else
return JavaInstance::create(result.l, rootObject)->createRuntimeObject(exec);
}
} else
return jsUndefined();
}
break;
case JavaTypeBoolean:
{
resultValue = jsBoolean(result.z);
}
break;
case JavaTypeByte:
{
resultValue = jsNumber(result.b);
}
break;
case JavaTypeChar:
{
resultValue = jsNumber(result.c);
}
break;
case JavaTypeShort:
{
resultValue = jsNumber(result.s);
}
break;
case JavaTypeInt:
{
resultValue = jsNumber(result.i);
}
break;
case JavaTypeLong:
{
resultValue = jsNumber(result.j);
}
break;
case JavaTypeFloat:
{
resultValue = jsNumber(result.f);
}
break;
case JavaTypeDouble:
{
resultValue = jsNumber(result.d);
}
break;
case JavaTypeArray:
{
const char* arrayType = jMethod->returnTypeClassName();
ASSERT(arrayType[0] == '[');
resultValue = JavaArray::convertJObjectToArray(exec, result.l, arrayType, rootObject);
}
break;
case JavaTypeInvalid:
{
resultValue = jsUndefined();
}
break;
}
return resultValue;
}
JSValue JavaInstance::defaultValue(ExecState* exec, PreferredPrimitiveType hint) const
{
if (hint == PreferString)
return stringValue(exec);
if (hint == PreferNumber)
return numberValue(exec);
JavaClass* aClass = static_cast<JavaClass*>(getClass());
if (aClass->isStringClass())
return stringValue(exec);
if (aClass->isNumberClass())
return numberValue(exec);
if (aClass->isBooleanClass())
return booleanValue();
return valueOf(exec);
}
JSValue JavaInstance::valueOf(ExecState* exec) const
{
return stringValue(exec);
}
#endif // ENABLE(JAVA_BRIDGE)