Commit 7a4b10cc authored by Paul Sokolovsky's avatar Paul Sokolovsky

unix/modjni: Support static methods.

parent a5deadf0
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <jni.h> #include <jni.h>
#define JJ(call, ...) (*env)->call(env, __VA_ARGS__) #define JJ(call, ...) (*env)->call(env, __VA_ARGS__)
#define JJ1(call) (*env)->call(env)
static JavaVM *jvm; static JavaVM *jvm;
static JNIEnv *env; static JNIEnv *env;
...@@ -49,6 +50,7 @@ static jmethodID Method_toString_mid; ...@@ -49,6 +50,7 @@ static jmethodID Method_toString_mid;
STATIC const mp_obj_type_t jobject_type; STATIC const mp_obj_type_t jobject_type;
STATIC const mp_obj_type_t jmethod_type; STATIC const mp_obj_type_t jmethod_type;
STATIC mp_obj_t new_jobject(jobject jo);
STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, mp_uint_t n_args, const mp_obj_t *args); STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, mp_uint_t n_args, const mp_obj_t *args);
typedef struct _mp_obj_jclass_t { typedef struct _mp_obj_jclass_t {
...@@ -66,6 +68,7 @@ typedef struct _mp_obj_jmethod_t { ...@@ -66,6 +68,7 @@ typedef struct _mp_obj_jmethod_t {
jobject obj; jobject obj;
jmethodID meth; jmethodID meth;
qstr name; qstr name;
bool is_static;
} mp_obj_jmethod_t; } mp_obj_jmethod_t;
// jclass // jclass
...@@ -85,12 +88,21 @@ STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { ...@@ -85,12 +88,21 @@ STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
jstring field_name = JJ(NewStringUTF, attr); jstring field_name = JJ(NewStringUTF, attr);
jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name); jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name);
jfieldID field_id = JJ(FromReflectedField, field); if (!JJ1(ExceptionCheck)) {
jobject obj = JJ(GetStaticObjectField, self->cls, field_id); jfieldID field_id = JJ(FromReflectedField, field);
jobject obj = JJ(GetStaticObjectField, self->cls, field_id);
dest[0] = new_jobject(obj);
return;
}
//JJ1(ExceptionDescribe);
JJ1(ExceptionClear);
mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t); mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
o->base.type = &jobject_type; o->base.type = &jmethod_type;
o->obj = obj; o->name = attr_in;
o->meth = NULL;
o->obj = self->cls;
o->is_static = true;
dest[0] = o; dest[0] = o;
} }
} }
...@@ -142,6 +154,7 @@ STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { ...@@ -142,6 +154,7 @@ STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
o->name = attr_in; o->name = attr_in;
o->meth = NULL; o->meth = NULL;
o->obj = self->obj; o->obj = self->obj;
o->is_static = false;
dest[0] = o; dest[0] = o;
} }
} }
...@@ -310,7 +323,10 @@ STATIC mp_obj_t jmethod_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, ...@@ -310,7 +323,10 @@ STATIC mp_obj_t jmethod_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw,
const char *name = qstr_str(self->name); const char *name = qstr_str(self->name);
// jstring meth_name = JJ(NewStringUTF, name); // jstring meth_name = JJ(NewStringUTF, name);
jclass obj_class = JJ(GetObjectClass, self->obj); jclass obj_class = self->obj;
if (!self->is_static) {
obj_class = JJ(GetObjectClass, self->obj);
}
jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid); jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid);
return call_method(self->obj, name, methods, false, n_args, args); return call_method(self->obj, name, methods, false, n_args, args);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment