@@ -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
@@ -302,14 +299,14 @@ public Object[] getAllGuestThreads() {
302
299
@ Override
303
300
public String getStringValue (Object object ) {
304
301
if (object instanceof StaticObject staticObject ) {
305
- return ( String ) InteropLibrary . getUncached () .toDisplayString (staticObject , false );
302
+ return performInContext (() -> ( String ) UNCACHED .toDisplayString (staticObject , false ) );
306
303
}
307
304
return object .toString ();
308
305
}
309
306
310
307
@ Override
311
308
public MethodVersionRef getMethodFromRootNode (RootNode root ) {
312
- if (root != null && root instanceof EspressoRootNode ) {
309
+ if (root instanceof EspressoRootNode ) {
313
310
return ((EspressoRootNode ) root ).getMethodVersion ();
314
311
}
315
312
return null ;
@@ -414,15 +411,17 @@ public int getArrayLength(Object array) {
414
411
StaticObject staticObject = (StaticObject ) array ;
415
412
EspressoLanguage language = context .getLanguage ();
416
413
if (staticObject .isForeignObject ()) {
417
- try {
418
- long arrayLength = UNCACHED .getArraySize (staticObject .rawForeignObject (language ));
419
- if (arrayLength > Integer .MAX_VALUE ) {
420
- return -1 ;
414
+ long arrayLength = performInContext (() -> {
415
+ try {
416
+ return UNCACHED .getArraySize (staticObject .rawForeignObject (language ));
417
+ } catch (UnsupportedMessageException e ) {
418
+ return (long ) -1 ;
421
419
}
422
- return ( int ) arrayLength ;
423
- } catch ( UnsupportedMessageException e ) {
420
+ }) ;
421
+ if ( arrayLength > Integer . MAX_VALUE ) {
424
422
return -1 ;
425
423
}
424
+ return (int ) arrayLength ;
426
425
}
427
426
return staticObject .length (language );
428
427
}
@@ -476,17 +475,19 @@ public Object getArrayValue(Object array, int index) {
476
475
Klass componentType = ((ArrayKlass ) arrayRef .getKlass ()).getComponentType ();
477
476
Meta meta = componentType .getMeta ();
478
477
if (arrayRef .isForeignObject ()) {
479
- Object value = null ;
480
- try {
481
- value = InteropLibrary .getUncached ().readArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index );
482
- return ToEspressoNode .getUncachedToEspresso (componentType , meta ).execute (value );
483
- } catch (UnsupportedMessageException e ) {
484
- throw EspressoError .shouldNotReachHere ("readArrayElement on a non-array foreign object" , e );
485
- } catch (InvalidArrayIndexException e ) {
486
- throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
487
- } catch (UnsupportedTypeException e ) {
488
- throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
489
- }
478
+ return performInContext (() -> {
479
+ Object value = null ;
480
+ try {
481
+ value = UNCACHED .readArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index );
482
+ return ToEspressoNode .getUncachedToEspresso (componentType , meta ).execute (value );
483
+ } catch (UnsupportedMessageException e ) {
484
+ throw EspressoError .shouldNotReachHere ("readArrayElement on a non-array foreign object" , e );
485
+ } catch (InvalidArrayIndexException e ) {
486
+ throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
487
+ } catch (UnsupportedTypeException e ) {
488
+ throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
489
+ }
490
+ });
490
491
} else if (componentType .isPrimitive ()) {
491
492
// primitive array type needs wrapping
492
493
Object boxedArray = getUnboxedArray (array );
@@ -502,19 +503,24 @@ public void setArrayValue(Object array, int index, Object value) {
502
503
Klass componentType = ((ArrayKlass ) arrayRef .getKlass ()).getComponentType ();
503
504
Meta meta = componentType .getMeta ();
504
505
if (arrayRef .isForeignObject ()) {
505
- try {
506
- Object unWrappedValue = value ;
507
- if (value instanceof StaticObject staticObject ) {
508
- unWrappedValue = staticObject .isForeignObject () ? staticObject .rawForeignObject (meta .getLanguage ()) : staticObject ;
509
- }
510
- InteropLibrary .getUncached ().writeArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index , unWrappedValue );
511
- } catch (UnsupportedMessageException e ) {
512
- throw EspressoError .shouldNotReachHere ("writeArrayElement on a non-array foreign object" , e );
513
- } catch (InvalidArrayIndexException e ) {
514
- throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
515
- } catch (UnsupportedTypeException e ) {
516
- throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
506
+ Object unWrappedValue ;
507
+ if (value instanceof StaticObject staticObject ) {
508
+ unWrappedValue = staticObject .isForeignObject () ? staticObject .rawForeignObject (meta .getLanguage ()) : staticObject ;
509
+ } else {
510
+ unWrappedValue = value ;
517
511
}
512
+ performInContext (() -> {
513
+ try {
514
+ UNCACHED .writeArrayElement (arrayRef .rawForeignObject (arrayRef .getKlass ().getLanguage ()), index , unWrappedValue );
515
+ return null ;
516
+ } catch (UnsupportedMessageException e ) {
517
+ throw EspressoError .shouldNotReachHere ("writeArrayElement on a non-array foreign object" , e );
518
+ } catch (UnsupportedTypeException e ) {
519
+ throw meta .throwExceptionWithMessage (meta .java_lang_ClassCastException , "%s cannot be cast to %s" , value , componentType .getTypeAsString ());
520
+ } catch (InvalidArrayIndexException e ) {
521
+ throw meta .throwExceptionWithMessage (meta .java_lang_ArrayIndexOutOfBoundsException , e .getMessage ());
522
+ }
523
+ });
518
524
} else if (componentType .isPrimitive ()) {
519
525
// primitive array type needs wrapping
520
526
Object boxedArray = getUnboxedArray (array );
@@ -601,24 +607,18 @@ public boolean isInstanceOf(Object object, KlassRef klass) {
601
607
602
608
@ Override
603
609
public void stopThread (Object guestThread , Object guestThrowable ) {
604
- Object previous = null ;
605
- try {
606
- previous = controller .enterTruffleContext ();
610
+ performInContext (() -> {
607
611
context .getThreadAccess ().stop ((StaticObject ) guestThread , (StaticObject ) guestThrowable );
608
- } finally {
609
- controller .leaveTruffleContext (previous );
610
- }
612
+ return null ;
613
+ });
611
614
}
612
615
613
616
@ Override
614
617
public void interruptThread (Object thread ) {
615
- Object previous = null ;
616
- try {
617
- previous = controller .enterTruffleContext ();
618
+ performInContext (() -> {
618
619
context .interruptThread ((StaticObject ) thread );
619
- } finally {
620
- controller .leaveTruffleContext (previous );
621
- }
620
+ return null ;
621
+ });
622
622
}
623
623
624
624
@ Override
@@ -628,13 +628,10 @@ public boolean systemExitImplemented() {
628
628
629
629
@ Override
630
630
public void exit (int exitCode ) {
631
- Object previous = null ;
632
- try {
633
- previous = controller .enterTruffleContext ();
631
+ performInContext (() -> {
634
632
context .truffleExit (null , exitCode );
635
- } finally {
636
- controller .leaveTruffleContext (previous );
637
- }
633
+ return null ;
634
+ });
638
635
}
639
636
640
637
@ Override
@@ -834,4 +831,19 @@ public ModuleRef[] getAllModulesRefs() {
834
831
public synchronized int redefineClasses (List <RedefineInfo > redefineInfos ) {
835
832
return context .getClassRedefinition ().redefineClasses (redefineInfos , false , true );
836
833
}
834
+
835
+ private <R > R performInContext (InContextAction <R > action ) {
836
+ Object previous = null ;
837
+ try {
838
+ previous = controller .enterTruffleContext ();
839
+ return action .call ();
840
+ } finally {
841
+ controller .leaveTruffleContext (previous );
842
+ }
843
+ }
844
+
845
+ @ FunctionalInterface
846
+ private interface InContextAction <R > {
847
+ R call ();
848
+ }
837
849
}
0 commit comments