Skip to content

Commit

Permalink
rewrite invoke() to support kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
ndjensen committed Sep 22, 2017
1 parent b8aa57f commit 0d7d911
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/main/c/Include/Jep.h
Expand Up @@ -63,6 +63,7 @@
#include "java_access/Collections.h"
#include "java_access/Comparable.h"
#include "java_access/Constructor.h"
#include "java_access/Entry.h"
#include "java_access/Field.h"
#include "java_access/Iterable.h"
#include "java_access/Iterator.h"
Expand Down
34 changes: 34 additions & 0 deletions src/main/c/Include/java_access/Entry.h
@@ -0,0 +1,34 @@
/*
jep - Java Embedded Python
Copyright (c) 2017 JEP AUTHORS.
This file is licensed under the the zlib/libpng License.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/

#ifndef _Included_java_util_Map_Entry
#define _Included_java_util_Map_Entry

jobject java_util_Map_Entry_getKey(JNIEnv*, jobject);
jobject java_util_Map_Entry_getValue(JNIEnv*, jobject);

#endif // ndef java_util_Map_Entry
1 change: 1 addition & 0 deletions src/main/c/Include/java_access/Map.h
Expand Up @@ -34,5 +34,6 @@ jobject java_util_Map_keySet(JNIEnv*, jobject);
jobject java_util_Map_put(JNIEnv*, jobject, jobject, jobject);
jobject java_util_Map_remove(JNIEnv*, jobject, jobject);
jint java_util_Map_size(JNIEnv*, jobject);
jobject java_util_Map_entrySet(JNIEnv*, jobject);

#endif // ndef java_util_Map
1 change: 1 addition & 0 deletions src/main/c/Include/jep_util.h
Expand Up @@ -122,6 +122,7 @@ extern jclass JDOUBLE_ARRAY_TYPE;
F(JCLASS_TYPE, "java/lang/Class") \
F(JLIST_TYPE, "java/util/List") \
F(JMAP_TYPE, "java/util/Map") \
F(JENTRY_TYPE, "java/util/Map$Entry") \
F(JITERABLE_TYPE, "java/lang/Iterable") \
F(JITERATOR_TYPE, "java/util/Iterator") \
F(JCOLLECTION_TYPE, "java/util/Collection") \
Expand Down
4 changes: 2 additions & 2 deletions src/main/c/Include/pyembed.h
Expand Up @@ -61,8 +61,8 @@ void pyembed_thread_close(JNIEnv*, intptr_t);
void pyembed_close(void);
void pyembed_run(JNIEnv*, intptr_t, char*);
jobject pyembed_invoke_method(JNIEnv*, intptr_t, const char*, jobjectArray,
jintArray);
jobject pyembed_invoke(JNIEnv*, PyObject*, jobjectArray, jintArray);
jobject);
jobject pyembed_invoke(JNIEnv*, PyObject*, jobjectArray, jobject);
void pyembed_eval(JNIEnv*, intptr_t, char*);
int pyembed_compile_string(JNIEnv*, intptr_t, char*);
void pyembed_setloader(JNIEnv*, intptr_t, jobject);
Expand Down
2 changes: 1 addition & 1 deletion src/main/c/Jep/invocationhandler.c
Expand Up @@ -82,7 +82,7 @@ JNIEXPORT jobject JNICALL Java_jep_InvocationHandler_invoke
goto EXIT;
}

ret = pyembed_invoke(env, callable, args, types);
ret = pyembed_invoke(env, callable, args, NULL);

EXIT:
PyEval_ReleaseThread(jepThread->tstate);
Expand Down
58 changes: 58 additions & 0 deletions src/main/c/Jep/java_access/Entry.c
@@ -0,0 +1,58 @@
/*
jep - Java Embedded Python
Copyright (c) 2017 JEP AUTHORS.
This file is licensed under the the zlib/libpng License.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/

#include "Jep.h"

static jmethodID getKey = 0;
static jmethodID getValue = 0;


jobject java_util_Map_Entry_getKey(JNIEnv* env, jobject this)
{
jobject result = NULL;
Py_BEGIN_ALLOW_THREADS
if (JNI_METHOD(getKey, env, JENTRY_TYPE, "getKey",
"()Ljava/lang/Object;")) {
result = (*env)->CallObjectMethod(env, this, getKey);
}
Py_END_ALLOW_THREADS
return result;
}


jobject java_util_Map_Entry_getValue(JNIEnv* env, jobject this)
{
jobject result = NULL;
Py_BEGIN_ALLOW_THREADS
if (JNI_METHOD(getValue, env, JENTRY_TYPE, "getValue",
"()Ljava/lang/Object;")) {
result = (*env)->CallObjectMethod(env, this, getValue);
}
Py_END_ALLOW_THREADS
return result;
}

12 changes: 12 additions & 0 deletions src/main/c/Jep/java_access/Map.c
Expand Up @@ -33,6 +33,7 @@ static jmethodID keySet = 0;
static jmethodID put = 0;
static jmethodID map_remove = 0;
static jmethodID size = 0;
static jmethodID entrySet = 0;

jboolean java_util_Map_containsKey(JNIEnv* env, jobject this, jobject key)
{
Expand Down Expand Up @@ -103,3 +104,14 @@ jint java_util_Map_size(JNIEnv* env, jobject this)
Py_END_ALLOW_THREADS
return result;
}

jobject java_util_Map_entrySet(JNIEnv* env, jobject this)
{
jobject result = NULL;
Py_BEGIN_ALLOW_THREADS
if (JNI_METHOD(entrySet, env, JMAP_TYPE, "entrySet", "()Ljava/util/Set;")) {
result = (*env)->CallObjectMethod(env, this, entrySet);
}
Py_END_ALLOW_THREADS
return result;
}
2 changes: 1 addition & 1 deletion src/main/c/Jep/java_access/Modifier.c
Expand Up @@ -58,7 +58,7 @@ jboolean java_lang_reflect_Modifier_isAbstract(JNIEnv* env, jint mod)
jboolean result = JNI_FALSE;
if (isAbstract
|| (isAbstract = (*env)->GetStaticMethodID(env, JMODIFIER_TYPE, "isAbstract",
"(I)Z"))) {
"(I)Z"))) {
result = (*env)->CallStaticBooleanMethod(env, JMODIFIER_TYPE, isAbstract, mod);
}
return result;
Expand Down
6 changes: 3 additions & 3 deletions src/main/c/Jep/jep.c
Expand Up @@ -131,21 +131,21 @@ JNIEXPORT void JNICALL Java_jep_Jep_run
/*
* Class: jep_Jep
* Method: invoke
* Signature: (JLjava/lang/String;[Ljava/lang/Object;[II)Ljava/lang/Object;
* Signature: (JLjava/lang/String;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_jep_Jep_invoke
(JNIEnv *env,
jobject obj,
jlong tstate,
jstring name,
jobjectArray args,
jintArray types)
jobject kwargs)
{
const char *cname;
jobject ret;

cname = jstring2char(env, name);
ret = pyembed_invoke_method(env, (intptr_t) tstate, cname, args, types);
ret = pyembed_invoke_method(env, (intptr_t) tstate, cname, args, kwargs);
release_utf_char(env, name, cname);

return ret;
Expand Down

0 comments on commit 0d7d911

Please sign in to comment.