#include <jni_class.h>
#include <jni_instance.h>
#include <jni_runtime.h>
#include <jni_utility.h>
#include <runtime_object.h>
#include <runtime_root.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 KJS::Bindings;
using namespace KJS;
JavaInstance::JavaInstance (jobject instance, const RootObject *r)
{
_instance = new JObjectWrapper (instance);
_class = 0;
setExecutionContext (r);
};
JavaInstance::~JavaInstance ()
{
_instance->deref();
}
JavaInstance::JavaInstance (const JavaInstance &other) : Instance()
{
_instance = other._instance;
_instance->ref();
_class = other._class;
};
#define NUM_LOCAL_REFS 64
void JavaInstance::begin()
{
getJNIEnv()->PushLocalFrame (NUM_LOCAL_REFS);
}
void JavaInstance::end()
{
getJNIEnv()->PopLocalFrame (NULL);
}
Class *JavaInstance::getClass() const
{
if (_class == 0)
_class = JavaClass::classForInstance (_instance->_instance);
return _class;
}
KJS::Value JavaInstance::stringValue() const
{
jstring stringValue = (jstring)callJNIObjectMethod (_instance->_instance, "toString", "()Ljava/lang/String;");
JNIEnv *env = getJNIEnv();
const UChar *c = (const UChar *)getUCharactersFromJStringInEnv (env, stringValue);
UString u(c, (int)env->GetStringLength(stringValue));
String v(u);
releaseUCharactersForJStringInEnv (env, stringValue, (const jchar *)c);
return v;
}
KJS::Value JavaInstance::numberValue() const
{
jdouble doubleValue = callJNIDoubleMethod (_instance->_instance, "doubleValue", "()D");
KJS::Number v(doubleValue);
return v;
}
KJS::Value JavaInstance::booleanValue() const
{
jboolean booleanValue = callJNIBooleanMethod (_instance->_instance, "booleanValue", "()Z");
KJS::Boolean v(booleanValue);
return v;
}
Value JavaInstance::invokeMethod (KJS::ExecState *exec, const MethodList &methodList, const List &args)
{
int i, count = args.size();
jvalue *jArgs;
Value resultValue;
Method *method = 0;
unsigned int numMethods = methodList.length();
unsigned int methodIndex;
Method *aMethod;
for (methodIndex = 0; methodIndex < numMethods; methodIndex++) {
aMethod = methodList.methodAt (methodIndex);
if (aMethod->numParameters() == count) {
method = aMethod;
break;
}
}
if (method == 0) {
JS_LOG ("unable to find an appropiate method\n");
return Undefined();
}
const JavaMethod *jMethod = static_cast<const JavaMethod*>(method);
JS_LOG ("call %s %s on %p\n", method->name(), jMethod->signature(), _instance->_instance);
if (count > 0) {
jArgs = (jvalue *)malloc (count * sizeof(jvalue));
}
else
jArgs = 0;
for (i = 0; i < count; i++) {
JavaParameter *aParameter = static_cast<JavaParameter *>(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;
const RootObject *execContext = executionContext();
bool handled = false;
if (execContext && execContext->nativeHandle()) {
jobject obj = _instance->_instance;
Value exceptionDescription;
const char *callingURL = 0; handled = dispatchJNICall (execContext->nativeHandle(), obj, jMethod->isStatic(), jMethod->JNIReturnType(), jMethod->methodID(obj), jArgs, result, callingURL, exceptionDescription);
if (!exceptionDescription.isNull()) {
Object error = Error::create(exec, GeneralError, exceptionDescription.toString(exec).UTF8String().c_str());
exec->setException(error);
free (jArgs);
return Undefined();
}
}
if (!handled) {
jobject obj = _instance->_instance;
switch (jMethod->JNIReturnType()){
case void_type: {
callJNIVoidMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case object_type: {
result.l = callJNIObjectMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case boolean_type: {
result.z = callJNIBooleanMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case byte_type: {
result.b = callJNIByteMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case char_type: {
result.c = callJNICharMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case short_type: {
result.s = callJNIShortMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case int_type: {
result.i = callJNIIntMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case long_type: {
result.j = callJNILongMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case float_type: {
result.f = callJNIFloatMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case double_type: {
result.d = callJNIDoubleMethodIDA (obj, jMethod->methodID(obj), jArgs);
}
break;
case invalid_type:
default: {
}
break;
}
}
switch (jMethod->JNIReturnType()){
case void_type: {
resultValue = Undefined();
}
break;
case object_type: {
if (result.l != 0) {
const char *arrayType = jMethod->returnType();
if (arrayType[0] == '[') {
resultValue = JavaArray::convertJObjectToArray (exec, result.l, arrayType, executionContext());
}
else {
resultValue = Instance::createRuntimeObject(Instance::JavaLanguage, result.l, executionContext());
}
}
else {
resultValue = Undefined();
}
}
break;
case boolean_type: {
resultValue = KJS::Boolean(result.z);
}
break;
case byte_type: {
resultValue = Number(result.b);
}
break;
case char_type: {
resultValue = Number(result.c);
}
break;
case short_type: {
resultValue = Number(result.s);
}
break;
case int_type: {
resultValue = Number(result.i);
}
break;
case long_type: {
resultValue = Number((long int)result.j);
}
break;
case float_type: {
resultValue = Number(result.f);
}
break;
case double_type: {
resultValue = Number(result.d);
}
break;
case invalid_type:
default: {
resultValue = Undefined();
}
break;
}
free (jArgs);
return resultValue;
}
KJS::Value JavaInstance::invokeDefaultMethod (KJS::ExecState *exec, const KJS::List &args)
{
return Undefined();
}
KJS::Value JavaInstance::defaultValue (KJS::Type hint) const
{
if (hint == StringType) {
return stringValue();
}
else if (hint == NumberType) {
return numberValue();
}
else if (hint == BooleanType) {
return booleanValue();
}
else if (hint == UnspecifiedType) {
JavaClass *aClass = static_cast<JavaClass*>(getClass());
if (aClass->isStringClass()) {
return stringValue();
}
else if (aClass->isNumberClass()) {
return numberValue();
}
else if (aClass->isBooleanClass()) {
return booleanValue();
}
}
return valueOf();
}
KJS::Value JavaInstance::valueOf() const
{
return stringValue();
};
JObjectWrapper::JObjectWrapper(jobject instance)
{
assert (instance != 0);
_ref = 1;
_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);
}
}
JObjectWrapper::~JObjectWrapper() {
JS_LOG ("deleting global ref %p\n", _instance);
_env->DeleteGlobalRef (_instance);
}