Skip to content

Commit 3d84c85

Browse files
committed
Implement JNI upcall for ctx_(List/Dict)New
1 parent 6de8474 commit 3d84c85

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

graalpython/com.oracle.graal.python.jni/src/hpy_jni.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,15 @@ static JNIEnv* jniEnv;
8282
UPCALL(ListCheck, SIG_HPY, SIG_INT) \
8383
UPCALL(UnicodeFromWideChar, SIG_PTR SIG_SIZE_T, SIG_HPY) \
8484
UPCALL(UnicodeFromJCharArray, SIG_JCHARARRAY, SIG_HPY) \
85+
UPCALL(DictNew, , SIG_HPY) \
86+
UPCALL(ListNew, SIG_SIZE_T, SIG_HPY) \
8587

8688
#define UPCALL(name, jniSigArgs, jniSigRet) static jmethodID jniMethod_ ## name;
8789
ALL_UPCALLS
8890
#undef UPCALL
8991

9092
#define DO_UPCALL_HPY(jni_ctx, name, ...) ((HPy){(HPy_ssize_t)(*jniEnv)->CallLongMethod(jniEnv, (jni_ctx), jniMethod_ ## name, __VA_ARGS__)})
93+
#define DO_UPCALL_HPY_NOARGS(jni_ctx, name) ((HPy){(HPy_ssize_t)(*jniEnv)->CallLongMethod(jniEnv, (jni_ctx), jniMethod_ ## name)})
9194
#define DO_UPCALL_TRACKER(jni_ctx, name, ...) ((HPyTracker){(*jniEnv)->CallLongMethod(jniEnv, (jni_ctx), jniMethod_ ## name, __VA_ARGS__)})
9295
#define DO_UPCALL_PTR(jni_ctx, name, ...) (void*) (*jniEnv)->CallLongMethod(jniEnv, (jni_ctx), jniMethod_ ## name, __VA_ARGS__)
9396
#define DO_UPCALL_SIZE_T(jni_ctx, name, ...) (HPy_ssize_t) (*jniEnv)->CallLongMethod(jniEnv, (jni_ctx), jniMethod_ ## name, __VA_ARGS__)
@@ -170,6 +173,14 @@ static HPy ctx_Unicode_FromWideChar_jni(HPyContext ctx, const wchar_t *arr, HPy_
170173
return DO_UPCALL_HPY(CONTEXT_INSTANCE(ctx), UnicodeFromWideChar, (PTR_UP) arr, (SIZE_T_UP) idx);
171174
}
172175

176+
static HPy ctx_DictNew_jni(HPyContext ctx) {
177+
return DO_UPCALL_HPY_NOARGS(CONTEXT_INSTANCE(ctx), DictNew);
178+
}
179+
180+
static HPy ctx_ListNew_jni(HPyContext ctx, HPy_ssize_t len) {
181+
return DO_UPCALL_HPY(CONTEXT_INSTANCE(ctx), ListNew, (SIZE_T_UP) len);
182+
}
183+
173184
//*************************
174185
// BOXING
175186

@@ -466,6 +477,9 @@ JNIEXPORT jint JNICALL Java_com_oracle_graal_python_builtins_objects_cext_hpy_Gr
466477

467478
context->ctx_Unicode_FromWideChar = ctx_Unicode_FromWideChar_jni;
468479

480+
context->ctx_Dict_New = ctx_DictNew_jni;
481+
context->ctx_List_New = ctx_ListNew_jni;
482+
469483
context->ctx_Tracker_New = ctx_Tracker_New;
470484
context->ctx_Tracker_Add = ctx_Tracker_Add;
471485
context->ctx_Tracker_ForgetAll = ctx_Tracker_ForgetAll;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
import com.oracle.graal.python.nodes.expression.TernaryArithmetic;
186186
import com.oracle.graal.python.nodes.expression.UnaryArithmetic;
187187
import com.oracle.graal.python.nodes.object.GetClassNode;
188+
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
188189
import com.oracle.graal.python.runtime.AsyncHandler;
189190
import com.oracle.graal.python.runtime.PythonContext;
190191
import com.oracle.graal.python.runtime.PythonOptions;
@@ -1235,7 +1236,9 @@ public enum Counter {
12351236
UpcallFloatAsDouble,
12361237
UpcallFloatFromDouble,
12371238
UpcallUnicodeFromWideChar,
1238-
UpcallUnicodeFromJCharArray;
1239+
UpcallUnicodeFromJCharArray,
1240+
UpcallDictNew,
1241+
UpcallListNew;
12391242

12401243
long count;
12411244

@@ -1626,6 +1629,27 @@ public final long ctxUnicodeFromJCharArray(char[] arr) {
16261629
return createHandle(new String(arr, 0, arr.length)).getId(this, ConditionProfile.getUncached());
16271630
}
16281631

1632+
public final long ctxDictNew() {
1633+
Counter.UpcallDictNew.increment();
1634+
PDict dict = PythonObjectFactory.getUncached().createDict();
1635+
return createHandle(dict).getId(this, ConditionProfile.getUncached());
1636+
}
1637+
1638+
public final long ctxListNew(long llen) {
1639+
try {
1640+
Counter.UpcallListNew.increment();
1641+
int len = CastToJavaIntExactNode.getUncached().execute(llen);
1642+
Object[] data = new Object[len];
1643+
Arrays.fill(data, PNone.NONE);
1644+
PList list = PythonObjectFactory.getUncached().createList(data);
1645+
return createHandle(list).getId(this, ConditionProfile.getUncached());
1646+
} catch (PException e) {
1647+
HPyTransformExceptionToNativeNodeGen.getUncached().execute(this, e);
1648+
// NULL handle
1649+
return 0;
1650+
}
1651+
}
1652+
16291653
@ExportMessage
16301654
@SuppressWarnings("static-method")
16311655
final boolean hasMembers() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/resources/jni-config.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"methods":[
55
{"name":"ctxCast","parameterTypes":["long"] },
66
{"name":"ctxClose","parameterTypes":["long"] },
7+
{"name":"ctxDictNew","parameterTypes":[] },
78
{"name":"ctxDup","parameterTypes":["long"] },
89
{"name":"ctxFloatAsDouble","parameterTypes":["long"] },
910
{"name":"ctxFloatFromDouble","parameterTypes":["double"] },
@@ -12,6 +13,7 @@
1213
{"name":"ctxSetItemi","parameterTypes":["long","long","long"] },
1314
{"name":"ctxLength","parameterTypes":["long"] },
1415
{"name":"ctxListCheck","parameterTypes":["long"] },
16+
{"name":"ctxListNew","parameterTypes":["long"] },
1517
{"name":"ctxLongAsLong","parameterTypes":["long"] },
1618
{"name":"ctxLongFromLong","parameterTypes":["long"] },
1719
{"name":"ctxNew","parameterTypes":["long","long"] },

0 commit comments

Comments
 (0)