@@ -275,13 +275,10 @@ public Object allocateInstance(KlassRef klass) {
275
275
276
276
@ Override
277
277
public void steppingInProgress (Thread t , boolean value ) {
278
- Object previous = null ;
279
- try {
280
- previous = controller .enterTruffleContext ();
278
+ performInContext (() -> {
281
279
context .getLanguage ().getThreadLocalStateFor (t ).setSteppingInProgress (value );
282
- } finally {
283
- controller .leaveTruffleContext (previous );
284
- }
280
+ return null ;
281
+ });
285
282
}
286
283
287
284
@ Override
@@ -313,14 +310,14 @@ public Object[] getAllGuestThreads() {
313
310
@ Override
314
311
public String getStringValue (Object object ) {
315
312
if (object instanceof StaticObject staticObject ) {
316
- return ( String ) InteropLibrary . getUncached () .toDisplayString (staticObject , false );
313
+ return performInContext (() -> ( String ) UNCACHED .toDisplayString (staticObject , false ) );
317
314
}
318
315
return object .toString ();
319
316
}
320
317
321
318
@ Override
322
319
public MethodVersionRef getMethodFromRootNode (RootNode root ) {
323
- if (root != null && root instanceof EspressoRootNode ) {
320
+ if (root instanceof EspressoRootNode ) {
324
321
return ((EspressoRootNode ) root ).getMethodVersion ();
325
322
}
326
323
return null ;
@@ -425,15 +422,17 @@ public int getArrayLength(Object array) {
425
422
StaticObject staticObject = (StaticObject ) array ;
426
423
EspressoLanguage language = context .getLanguage ();
427
424
if (staticObject .isForeignObject ()) {
428
- try {
429
- long arrayLength = UNCACHED .getArraySize (staticObject .rawForeignObject (language ));
430
- if (arrayLength > Integer .MAX_VALUE ) {
431
- return -1 ;
425
+ long arrayLength = performInContext (() -> {
426
+ try {
427
+ return UNCACHED .getArraySize (staticObject .rawForeignObject (language ));
428
+ } catch (UnsupportedMessageException e ) {
429
+ return (long ) -1 ;
432
430
}
433
- return ( int ) arrayLength ;
434
- } catch ( UnsupportedMessageException e ) {
431
+ }) ;
432
+ if ( arrayLength > Integer . MAX_VALUE ) {
435
433
return -1 ;
436
434
}
435
+ return (int ) arrayLength ;
437
436
}
438
437
return staticObject .length (language );
439
438
}
@@ -487,17 +486,19 @@ public Object getArrayValue(Object array, int index) {
487
486
Klass componentType = ((ArrayKlass ) arrayRef .getKlass ()).getComponentType ();
488
487
Meta meta = componentType .getMeta ();
489
488
if (arrayRef .isForeignObject ()) {
490
- Object value = null ;
491
- try {
492
- value = InteropLibrary .getUncached ().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
- }
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
+ });
501
502
} else if (componentType .isPrimitive ()) {
502
503
// primitive array type needs wrapping
503
504
Object boxedArray = getUnboxedArray (array );
@@ -513,19 +514,24 @@ public void setArrayValue(Object array, int index, Object value) {
513
514
Klass componentType = ((ArrayKlass ) arrayRef .getKlass ()).getComponentType ();
514
515
Meta meta = componentType .getMeta ();
515
516
if (arrayRef .isForeignObject ()) {
516
- try {
517
- Object unWrappedValue = value ;
518
- if (value instanceof StaticObject staticObject ) {
519
- unWrappedValue = staticObject .isForeignObject () ? staticObject .rawForeignObject (meta .getLanguage ()) : staticObject ;
520
- }
521
- InteropLibrary .getUncached ().writeArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index , unWrappedValue );
522
- } catch (UnsupportedMessageException e ) {
523
- throw EspressoError .shouldNotReachHere ("writeArrayElement on a non-array foreign object" , e );
524
- } catch (InvalidArrayIndexException e ) {
525
- throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
526
- } catch (UnsupportedTypeException e ) {
527
- throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
517
+ Object unWrappedValue ;
518
+ if (value instanceof StaticObject staticObject ) {
519
+ unWrappedValue = staticObject .isForeignObject () ? staticObject .rawForeignObject (meta .getLanguage ()) : staticObject ;
520
+ } else {
521
+ unWrappedValue = value ;
528
522
}
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
+ });
529
535
} else if (componentType .isPrimitive ()) {
530
536
// primitive array type needs wrapping
531
537
Object boxedArray = getUnboxedArray (array );
@@ -612,24 +618,18 @@ public boolean isInstanceOf(Object object, KlassRef klass) {
612
618
613
619
@ Override
614
620
public void stopThread (Object guestThread , Object guestThrowable ) {
615
- Object previous = null ;
616
- try {
617
- previous = controller .enterTruffleContext ();
621
+ performInContext (() -> {
618
622
context .getThreadAccess ().stop ((StaticObject ) guestThread , (StaticObject ) guestThrowable );
619
- } finally {
620
- controller .leaveTruffleContext (previous );
621
- }
623
+ return null ;
624
+ });
622
625
}
623
626
624
627
@ Override
625
628
public void interruptThread (Object thread ) {
626
- Object previous = null ;
627
- try {
628
- previous = controller .enterTruffleContext ();
629
+ performInContext (() -> {
629
630
context .interruptThread ((StaticObject ) thread );
630
- } finally {
631
- controller .leaveTruffleContext (previous );
632
- }
631
+ return null ;
632
+ });
633
633
}
634
634
635
635
@ Override
@@ -639,13 +639,10 @@ public boolean systemExitImplemented() {
639
639
640
640
@ Override
641
641
public void exit (int exitCode ) {
642
- Object previous = null ;
643
- try {
644
- previous = controller .enterTruffleContext ();
642
+ performInContext (() -> {
645
643
context .truffleExit (null , exitCode );
646
- } finally {
647
- controller .leaveTruffleContext (previous );
648
- }
644
+ return null ;
645
+ });
649
646
}
650
647
651
648
@ Override
@@ -845,4 +842,19 @@ public ModuleRef[] getAllModulesRefs() {
845
842
public synchronized int redefineClasses (List <RedefineInfo > redefineInfos ) {
846
843
return context .getClassRedefinition ().redefineClasses (redefineInfos , false , true );
847
844
}
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
+ }
848
860
}
0 commit comments