#include #include #include "mlp_java_priv.h" mlp_value* _mlp_java_object_new(mlp_context *ctx, mlp_runtime *rt, const char *class, mlp_value **args, int nb_args, const char *hint){ mlp_java_runtime *jrt = (mlp_java_runtime *)rt->runtime ; JNIEnv *env = get_env(ctx, jrt) ; RETURN_NULL_IF_NULL(env) ; jstring jclazz = jstring_new(ctx, env, class) ; RETURN_NULL_IF_NULL(jclazz) ; jstring jhint = NULL ; if (hint != NULL){ jhint = jstring_new(ctx, env, hint) ; RETURN_NULL_IF_NULL(jhint) ; } jobjectArray jargs = NULL ; if (nb_args > 0){ jargs = java_MLPValue_array_new(ctx, rt, env, args, nb_args) ; RETURN_NULL_IF_NULL(jargs) ; } jobject ret = (*(env))->CallStaticObjectMethod(env, jrt->api, jrt->api_newObject, jclazz, jargs, jhint) ; if (process_exception(ctx, jrt, env, MLP_RUNTIME_ERROR, "Can't call method newObject(...) of class 'mlp.MLPJavaAPI'")){ return NULL ; } mlp_value *val = mlp_value_new(ctx, rt, env, ret) ; RETURN_NULL_IF_NULL(val) ; /* TODO: Check for exceptions */ if (jargs != NULL){ java_MLPValue_array_delete(env, jargs, nb_args) ; } if (jhint != NULL){ jstring_delete(env, jhint) ; } jstring_delete(env, jclazz) ; return val ; } mlp_value* _mlp_java_object_call_method(mlp_context *ctx, mlp_runtime *rt, mlp_object *obj, const char *method, mlp_value **args, int nb_args, const char *hint){ mlp_java_runtime *jrt = (mlp_java_runtime *)rt->runtime ; JNIEnv *env = get_env(ctx, jrt) ; RETURN_NULL_IF_NULL(env) ; jstring jmethod = jstring_new(ctx, env, method) ; RETURN_NULL_IF_NULL(jmethod) ; jstring jhint = NULL ; if (hint != NULL){ jhint = jstring_new(ctx, env, hint) ; RETURN_NULL_IF_NULL(jhint) ; } jobject jo = java_MLPJavaObject_new(ctx, rt, env, obj) ; RETURN_NULL_IF_NULL(jo) ; jobjectArray jargs = NULL ; if (nb_args > 0){ jargs = java_MLPValue_array_new(ctx, rt, env, args, nb_args) ; RETURN_NULL_IF_NULL(jargs) ; } jobject ret = (*(env))->CallStaticObjectMethod(env, jrt->api, jrt->api_callMethod, jo, jmethod, jargs, jhint) ; if (process_exception(ctx, jrt, env, MLP_RUNTIME_ERROR, "Can't call method callMethod(...) of class 'mlp.MLPJavaAPI'")){ return NULL ; } mlp_value *val = mlp_value_new(ctx, rt, env, ret) ; RETURN_NULL_IF_NULL(val) ; /* TODO: Check for exceptions */ if (jargs != NULL){ java_MLPValue_array_delete(env, jargs, nb_args) ; } if (jhint != NULL){ jstring_delete(env, jhint) ; } jstring_delete(env, jmethod) ; return val ; } void _mlp_java_object_delete(mlp_context *ctx, mlp_object *obj){ mlp_java_runtime *jrt = (mlp_java_runtime *)(mlp_object_get_runtime(obj)->runtime) ; JNIEnv *env = get_env(ctx, jrt) ; if (env != NULL){ jobject jo = (jobject)mlp_object_get_handle(obj) ; (*env)->DeleteGlobalRef(env, jo) ; } }