@@ -74,12 +74,14 @@ static JNIEnv* jniEnv;
74
74
UPCALL(FloatFromDouble, SIG_DOUBLE, SIG_HPY) \
75
75
UPCALL(FloatAsDouble, SIG_HPY, SIG_DOUBLE) \
76
76
UPCALL(LongAsLong, SIG_HPY, SIG_LONG) \
77
+ UPCALL(LongAsDouble, SIG_HPY, SIG_DOUBLE) \
77
78
UPCALL(LongFromLong, SIG_LONG, SIG_HPY) \
78
79
UPCALL(Dup, SIG_HPY, SIG_HPY) \
79
80
UPCALL(GetItemi, SIG_HPY SIG_SIZE_T, SIG_HPY) \
80
81
UPCALL(SetItemi, SIG_HPY SIG_SIZE_T SIG_HPY, SIG_INT) \
81
82
UPCALL(SetItem, SIG_HPY SIG_HPY SIG_HPY, SIG_INT) \
82
83
UPCALL(NumberCheck, SIG_HPY, SIG_INT) \
84
+ UPCALL(TypeCheck, SIG_HPY SIG_HPY, SIG_INT) \
83
85
UPCALL(Length, SIG_HPY, SIG_SIZE_T) \
84
86
UPCALL(ListCheck, SIG_HPY, SIG_INT) \
85
87
UPCALL(UnicodeFromWideChar, SIG_PTR SIG_SIZE_T, SIG_HPY) \
@@ -127,6 +129,10 @@ static long ctx_LongAsLong_jni(HPyContext *ctx, HPy h) {
127
129
return DO_UPCALL_LONG (CONTEXT_INSTANCE (ctx ), LongAsLong , HPY_UP (h ));
128
130
}
129
131
132
+ static double ctx_LongAsDouble_jni (HPyContext * ctx , HPy h ) {
133
+ return DO_UPCALL_DOUBLE (CONTEXT_INSTANCE (ctx ), LongAsDouble , HPY_UP (h ));
134
+ }
135
+
130
136
static HPy ctx_LongFromLong_jni (HPyContext * ctx , long l ) {
131
137
return DO_UPCALL_HPY (CONTEXT_INSTANCE (ctx ), LongFromLong , l );
132
138
}
@@ -159,6 +165,10 @@ static int ctx_NumberCheck_jni(HPyContext *ctx, HPy obj) {
159
165
return DO_UPCALL_INT (CONTEXT_INSTANCE (ctx ), NumberCheck , HPY_UP (obj ));
160
166
}
161
167
168
+ static int ctx_TypeCheck_jni (HPyContext * ctx , HPy obj , HPy type ) {
169
+ return DO_UPCALL_INT (CONTEXT_INSTANCE (ctx ), TypeCheck , HPY_UP (obj ), HPY_UP (type ));
170
+ }
171
+
162
172
static int ctx_ListCheck_jni (HPyContext * ctx , HPy obj ) {
163
173
return DO_UPCALL_INT (CONTEXT_INSTANCE (ctx ), ListCheck , HPY_UP (obj ));
164
174
}
@@ -191,6 +201,7 @@ static HPy ctx_ListNew_jni(HPyContext *ctx, HPy_ssize_t len) {
191
201
#define NAN_BOXING_INT (0x0001000000000000llu)
192
202
#define NAN_BOXING_INT_MASK (0x00000000FFFFFFFFllu)
193
203
#define NAN_BOXING_MAX_HANDLE (0x000000007FFFFFFFllu)
204
+ #define IMMUTABLE_HANDLES (0x0000000000000100llu)
194
205
195
206
static bool isBoxedDouble (uint64_t value ) {
196
207
return value >= NAN_BOXING_BASE ;
@@ -249,9 +260,11 @@ static HPy (*original_Dup)(HPyContext *ctx, HPy h);
249
260
static HPy (* original_FloatFromDouble )(HPyContext * ctx , double v );
250
261
static double (* original_FloatAsDouble )(HPyContext * ctx , HPy h );
251
262
static long (* original_LongAsLong )(HPyContext * ctx , HPy h );
263
+ static double (* original_LongAsDouble )(HPyContext * ctx , HPy h );
252
264
static HPy (* original_LongFromLong )(HPyContext * ctx , long l );
253
265
static int (* original_ListCheck )(HPyContext * ctx , HPy h );
254
266
static int (* original_NumberCheck )(HPyContext * ctx , HPy h );
267
+ static int (* original_TypeCheck )(HPyContext * ctx , HPy h , HPy type );
255
268
static void (* original_Close )(HPyContext * ctx , HPy h );
256
269
static HPy (* original_UnicodeFromWideChar )(HPyContext * ctx , const wchar_t * arr , HPy_ssize_t size );
257
270
@@ -289,6 +302,15 @@ static long augment_LongAsLong(HPyContext *ctx, HPy h) {
289
302
}
290
303
}
291
304
305
+ static double augment_LongAsDouble (HPyContext * ctx , HPy h ) {
306
+ uint64_t bits = toBits (h );
307
+ if (isBoxedInt (bits )) {
308
+ return unboxInt (bits );
309
+ } else {
310
+ return original_LongAsDouble (ctx , h );
311
+ }
312
+ }
313
+
292
314
static HPy augment_LongFromLong (HPyContext * ctx , long l ) {
293
315
int32_t i = (int32_t ) l ;
294
316
if (l == i ) {
@@ -308,6 +330,9 @@ static void augment_Close(HPyContext *ctx, HPy h) {
308
330
static HPy augment_Dup (HPyContext * ctx , HPy h ) {
309
331
uint64_t bits = toBits (h );
310
332
if (isBoxedHandle (bits )) {
333
+ if (bits < IMMUTABLE_HANDLES ) {
334
+ return h ;
335
+ }
311
336
return original_Dup (ctx , h );
312
337
} else {
313
338
return h ;
@@ -323,6 +348,26 @@ static int augment_NumberCheck(HPyContext *ctx, HPy obj) {
323
348
}
324
349
}
325
350
351
+ static int augment_TypeCheck (HPyContext * ctx , HPy obj , HPy type ) {
352
+ uint64_t bits = toBits (obj );
353
+ if (isBoxedInt (bits )) {
354
+ if (toBits (type ) == toBits (ctx -> h_LongType )) {
355
+ return true;
356
+ }
357
+ if (toBits (type ) == toBits (ctx -> h_FloatType )) {
358
+ return false;
359
+ }
360
+ } else if (isBoxedDouble (bits )) {
361
+ if (toBits (type ) == toBits (ctx -> h_FloatType )) {
362
+ return true;
363
+ }
364
+ if (toBits (type ) == toBits (ctx -> h_LongType )) {
365
+ return false;
366
+ }
367
+ }
368
+ return original_TypeCheck (ctx , obj , type );
369
+ }
370
+
326
371
static int augment_ListCheck (HPyContext * ctx , HPy obj ) {
327
372
uint64_t bits = toBits (obj );
328
373
if (isBoxedHandle (bits )) {
@@ -390,9 +435,12 @@ void initDirectFastPaths(HPyContext *context) {
390
435
391
436
original_FloatAsDouble = context -> ctx_Float_AsDouble ;
392
437
context -> ctx_Float_AsDouble = augment_FloatAsDouble ;
393
-
438
+
394
439
original_LongAsLong = context -> ctx_Long_AsLong ;
395
440
context -> ctx_Long_AsLong = augment_LongAsLong ;
441
+
442
+ original_LongAsDouble = context -> ctx_Long_AsDouble ;
443
+ context -> ctx_Long_AsDouble = augment_LongAsDouble ;
396
444
397
445
original_LongFromLong = context -> ctx_Long_FromLong ;
398
446
context -> ctx_Long_FromLong = augment_LongFromLong ;
@@ -409,6 +457,9 @@ void initDirectFastPaths(HPyContext *context) {
409
457
original_NumberCheck = context -> ctx_Number_Check ;
410
458
context -> ctx_Number_Check = augment_NumberCheck ;
411
459
460
+ original_TypeCheck = context -> ctx_TypeCheck ;
461
+ context -> ctx_TypeCheck = augment_TypeCheck ;
462
+
412
463
original_ListCheck = context -> ctx_List_Check ;
413
464
context -> ctx_List_Check = augment_ListCheck ;
414
465
@@ -437,11 +488,13 @@ JNIEXPORT jint JNICALL Java_com_oracle_graal_python_builtins_objects_cext_hpy_Gr
437
488
context -> ctx_Float_FromDouble = ctx_FloatFromDouble_jni ;
438
489
context -> ctx_Float_AsDouble = ctx_FloatAsDouble_jni ;
439
490
context -> ctx_Long_AsLong = ctx_LongAsLong_jni ;
491
+ context -> ctx_Long_AsDouble = ctx_LongAsDouble_jni ;
440
492
context -> ctx_AsStruct = ctx_AsStruct_jni ;
441
493
context -> ctx_Close = ctx_Close_jni ;
442
494
443
495
context -> ctx_Dup = ctx_Dup_jni ;
444
496
context -> ctx_Number_Check = ctx_NumberCheck_jni ;
497
+ context -> ctx_TypeCheck = ctx_TypeCheck_jni ;
445
498
context -> ctx_List_Check = ctx_ListCheck_jni ;
446
499
447
500
context -> ctx_Length = ctx_Length_jni ;
0 commit comments