@@ -275,20 +275,18 @@ public Object allocateInstance(KlassRef klass) {
275
275
276
276
@ Override
277
277
public void steppingInProgress (Thread t , boolean value ) {
278
- performInContext (() -> {
279
- context .getLanguage ().getThreadLocalStateFor (t ).setSteppingInProgress (value );
280
- return null ;
281
- });
278
+ context .getLanguage ().getThreadLocalStateFor (t ).setSteppingInProgress (value );
282
279
}
283
280
284
281
@ Override
285
282
public boolean isSteppingInProgress (Thread t ) {
286
- Object previous = null ;
287
- try {
288
- previous = controller .enterTruffleContext ();
289
- return context .getLanguage ().getThreadLocalStateFor (t ).isSteppingInProgress ();
290
- } finally {
291
- controller .leaveTruffleContext (previous );
283
+ EspressoThreadLocalState state = context .getLanguage ().getThreadLocalStateFor (t );
284
+ // Here, the thread local state can be null for threads having been unregistered already.
285
+ // This is OK, and we can safely return false in such cases.
286
+ if (state != null ) {
287
+ return state .isSteppingInProgress ();
288
+ } else {
289
+ return false ;
292
290
}
293
291
}
294
292
@@ -304,13 +302,13 @@ public Object[] getAllGuestThreads() {
304
302
}
305
303
result .add (activeThread );
306
304
}
307
- return result .toArray (new StaticObject [ result . size ()] );
305
+ return result .toArray (StaticObject . EMPTY_ARRAY );
308
306
}
309
307
310
308
@ Override
311
309
public String getStringValue (Object object ) {
312
310
if (object instanceof StaticObject staticObject ) {
313
- return performInContext (() -> ( String ) UNCACHED .toDisplayString (staticObject , false ) );
311
+ return ( String ) UNCACHED .toDisplayString (staticObject , false );
314
312
}
315
313
return object .toString ();
316
314
}
@@ -422,13 +420,12 @@ public int getArrayLength(Object array) {
422
420
StaticObject staticObject = (StaticObject ) array ;
423
421
EspressoLanguage language = context .getLanguage ();
424
422
if (staticObject .isForeignObject ()) {
425
- long arrayLength = performInContext (() -> {
426
- try {
427
- return UNCACHED .getArraySize (staticObject .rawForeignObject (language ));
428
- } catch (UnsupportedMessageException e ) {
429
- return (long ) -1 ;
430
- }
431
- });
423
+ long arrayLength ;
424
+ try {
425
+ arrayLength = UNCACHED .getArraySize (staticObject .rawForeignObject (language ));
426
+ } catch (UnsupportedMessageException e ) {
427
+ return -1 ;
428
+ }
432
429
if (arrayLength > Integer .MAX_VALUE ) {
433
430
return -1 ;
434
431
}
@@ -486,19 +483,17 @@ public Object getArrayValue(Object array, int index) {
486
483
Klass componentType = ((ArrayKlass ) arrayRef .getKlass ()).getComponentType ();
487
484
Meta meta = componentType .getMeta ();
488
485
if (arrayRef .isForeignObject ()) {
489
- return performInContext (() -> {
490
- Object value = null ;
491
- try {
492
- value = UNCACHED .readArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index );
493
- return ToEspressoNode .getUncachedToEspresso (componentType , meta ).execute (value );
494
- } catch (UnsupportedMessageException e ) {
495
- throw EspressoError .shouldNotReachHere ("readArrayElement on a non-array foreign object" , e );
496
- } catch (InvalidArrayIndexException e ) {
497
- throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
498
- } catch (UnsupportedTypeException e ) {
499
- throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
500
- }
501
- });
486
+ Object value = null ;
487
+ try {
488
+ value = UNCACHED .readArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index );
489
+ return ToEspressoNode .getUncachedToEspresso (componentType , meta ).execute (value );
490
+ } catch (UnsupportedMessageException e ) {
491
+ throw EspressoError .shouldNotReachHere ("readArrayElement on a non-array foreign object" , e );
492
+ } catch (InvalidArrayIndexException e ) {
493
+ throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
494
+ } catch (UnsupportedTypeException e ) {
495
+ throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
496
+ }
502
497
} else if (componentType .isPrimitive ()) {
503
498
// primitive array type needs wrapping
504
499
Object boxedArray = getUnboxedArray (array );
@@ -520,18 +515,15 @@ public void setArrayValue(Object array, int index, Object value) {
520
515
} else {
521
516
unWrappedValue = value ;
522
517
}
523
- performInContext (() -> {
524
- try {
525
- UNCACHED .writeArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index , unWrappedValue );
526
- return null ;
527
- } catch (UnsupportedMessageException e ) {
528
- throw EspressoError .shouldNotReachHere ("writeArrayElement on a non-array foreign object" , e );
529
- } catch (UnsupportedTypeException e ) {
530
- throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
531
- } catch (InvalidArrayIndexException e ) {
532
- throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
533
- }
534
- });
518
+ try {
519
+ UNCACHED .writeArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index , unWrappedValue );
520
+ } catch (UnsupportedMessageException e ) {
521
+ throw EspressoError .shouldNotReachHere ("writeArrayElement on a non-array foreign object" , e );
522
+ } catch (UnsupportedTypeException e ) {
523
+ throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
524
+ } catch (InvalidArrayIndexException e ) {
525
+ throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
526
+ }
535
527
} else if (componentType .isPrimitive ()) {
536
528
// primitive array type needs wrapping
537
529
Object boxedArray = getUnboxedArray (array );
@@ -618,18 +610,12 @@ public boolean isInstanceOf(Object object, KlassRef klass) {
618
610
619
611
@ Override
620
612
public void stopThread (Object guestThread , Object guestThrowable ) {
621
- performInContext (() -> {
622
- context .getThreadAccess ().stop ((StaticObject ) guestThread , (StaticObject ) guestThrowable );
623
- return null ;
624
- });
613
+ context .getThreadAccess ().stop ((StaticObject ) guestThread , (StaticObject ) guestThrowable );
625
614
}
626
615
627
616
@ Override
628
617
public void interruptThread (Object thread ) {
629
- performInContext (() -> {
630
- context .interruptThread ((StaticObject ) thread );
631
- return null ;
632
- });
618
+ context .interruptThread ((StaticObject ) thread );
633
619
}
634
620
635
621
@ Override
@@ -639,10 +625,7 @@ public boolean systemExitImplemented() {
639
625
640
626
@ Override
641
627
public void exit (int exitCode ) {
642
- performInContext (() -> {
643
- context .truffleExit (null , exitCode );
644
- return null ;
645
- });
628
+ context .truffleExit (null , exitCode );
646
629
}
647
630
648
631
@ Override
@@ -842,19 +825,4 @@ public ModuleRef[] getAllModulesRefs() {
842
825
public synchronized int redefineClasses (List <RedefineInfo > redefineInfos ) {
843
826
return context .getClassRedefinition ().redefineClasses (redefineInfos , false , true );
844
827
}
845
-
846
- private <R > R performInContext (InContextAction <R > action ) {
847
- Object previous = null ;
848
- try {
849
- previous = controller .enterTruffleContext ();
850
- return action .call ();
851
- } finally {
852
- controller .leaveTruffleContext (previous );
853
- }
854
- }
855
-
856
- @ FunctionalInterface
857
- private interface InContextAction <R > {
858
- R call ();
859
- }
860
828
}
0 commit comments