JavaNPObjectV8.cpp [plain text]
#include "config.h"
#include "JavaNPObjectV8.h"
#if ENABLE(JAVA_BRIDGE)
#include "JNIUtilityPrivate.h"
#include "JavaClassV8.h"
#include "JavaFieldV8.h"
#include "JavaInstanceV8.h"
#include "JavaMethod.h"
#include "JavaValueV8.h"
#include "npruntime_impl.h"
namespace JSC {
namespace Bindings {
static NPObject* AllocJavaNPObject(NPP, NPClass*)
{
JavaNPObject* obj = static_cast<JavaNPObject*>(malloc(sizeof(JavaNPObject)));
if (!obj)
return 0;
memset(obj, 0, sizeof(JavaNPObject));
return reinterpret_cast<NPObject*>(obj);
}
static void FreeJavaNPObject(NPObject* npobj)
{
JavaNPObject* obj = reinterpret_cast<JavaNPObject*>(npobj);
obj->m_instance = 0; free(obj);
}
static NPClass JavaNPObjectClass = {
NP_CLASS_STRUCT_VERSION,
AllocJavaNPObject, FreeJavaNPObject, 0, JavaNPObjectHasMethod,
JavaNPObjectInvoke,
0, JavaNPObjectHasProperty,
JavaNPObjectGetProperty,
0, 0, 0, 0 };
NPObject* JavaInstanceToNPObject(JavaInstance* instance)
{
JavaNPObject* object = reinterpret_cast<JavaNPObject*>(_NPN_CreateObject(0, &JavaNPObjectClass));
object->m_instance = instance;
return reinterpret_cast<NPObject*>(object);
}
JavaInstance* ExtractJavaInstance(NPObject* obj)
{
if (obj->_class == &JavaNPObjectClass)
return reinterpret_cast<JavaNPObject*>(obj)->m_instance.get();
return 0;
}
bool JavaNPObjectHasMethod(NPObject* obj, NPIdentifier identifier)
{
JavaInstance* instance = ExtractJavaInstance(obj);
if (!instance)
return false;
NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
if (!name)
return false;
instance->begin();
bool result = (instance->getClass()->methodsNamed(name).size() > 0);
instance->end();
free(name);
return result;
}
bool JavaNPObjectInvoke(NPObject* obj, NPIdentifier identifier, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
JavaInstance* instance = ExtractJavaInstance(obj);
if (!instance)
return false;
NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
if (!name)
return false;
instance->begin();
MethodList methodList = instance->getClass()->methodsNamed(name);
free(name);
size_t numMethods = methodList.size();
JavaMethod* aMethod;
JavaMethod* jMethod = 0;
for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) {
aMethod = methodList[methodIndex];
if (aMethod->numParameters() == static_cast<int>(argCount)) {
jMethod = aMethod;
break;
}
}
if (!jMethod)
return false;
JavaValue* jArgs = new JavaValue[argCount];
for (unsigned int i = 0; i < argCount; i++)
jArgs[i] = convertNPVariantToJavaValue(args[i], jMethod->parameterAt(i));
JavaValue jResult = instance->invokeMethod(jMethod, jArgs);
instance->end();
delete[] jArgs;
VOID_TO_NPVARIANT(*result);
convertJavaValueToNPVariant(jResult, result);
return true;
}
bool JavaNPObjectHasProperty(NPObject* obj, NPIdentifier identifier)
{
JavaInstance* instance = ExtractJavaInstance(obj);
if (!instance)
return false;
NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
if (!name)
return false;
instance->begin();
bool result = instance->getClass()->fieldNamed(name);
instance->end();
free(name);
return result;
}
bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result)
{
VOID_TO_NPVARIANT(*result);
JavaInstance* instance = ExtractJavaInstance(obj);
if (!instance)
return false;
NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
if (!name)
return false;
instance->begin();
JavaField* field = instance->getClass()->fieldNamed(name);
free(name); if (!field)
return false;
JavaValue value = instance->getField(field);
instance->end();
convertJavaValueToNPVariant(value, result);
return true;
}
}
}
#endif // ENABLE(JAVA_BRIDGE)