Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite invoke() to support kwargs #94

Merged
merged 1 commit into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/c/Include/Jep.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading