Skip to content

Commit 64d1fc8

Browse files
committed
[GR-65280] Make sure truffle context is entered.
PullRequest: graal/20891
2 parents 307aab1 + 323edb2 commit 64d1fc8

File tree

1 file changed

+67
-55
lines changed
  • espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/runtime

1 file changed

+67
-55
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/runtime/JDWPContextImpl.java

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,10 @@ public Object allocateInstance(KlassRef klass) {
275275

276276
@Override
277277
public void steppingInProgress(Thread t, boolean value) {
278-
Object previous = null;
279-
try {
280-
previous = controller.enterTruffleContext();
278+
performInContext(() -> {
281279
context.getLanguage().getThreadLocalStateFor(t).setSteppingInProgress(value);
282-
} finally {
283-
controller.leaveTruffleContext(previous);
284-
}
280+
return null;
281+
});
285282
}
286283

287284
@Override
@@ -313,14 +310,14 @@ public Object[] getAllGuestThreads() {
313310
@Override
314311
public String getStringValue(Object object) {
315312
if (object instanceof StaticObject staticObject) {
316-
return (String) InteropLibrary.getUncached().toDisplayString(staticObject, false);
313+
return performInContext(() -> (String) UNCACHED.toDisplayString(staticObject, false));
317314
}
318315
return object.toString();
319316
}
320317

321318
@Override
322319
public MethodVersionRef getMethodFromRootNode(RootNode root) {
323-
if (root != null && root instanceof EspressoRootNode) {
320+
if (root instanceof EspressoRootNode) {
324321
return ((EspressoRootNode) root).getMethodVersion();
325322
}
326323
return null;
@@ -425,15 +422,17 @@ public int getArrayLength(Object array) {
425422
StaticObject staticObject = (StaticObject) array;
426423
EspressoLanguage language = context.getLanguage();
427424
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;
432430
}
433-
return (int) arrayLength;
434-
} catch (UnsupportedMessageException e) {
431+
});
432+
if (arrayLength > Integer.MAX_VALUE) {
435433
return -1;
436434
}
435+
return (int) arrayLength;
437436
}
438437
return staticObject.length(language);
439438
}
@@ -487,17 +486,19 @@ public Object getArrayValue(Object array, int index) {
487486
Klass componentType = ((ArrayKlass) arrayRef.getKlass()).getComponentType();
488487
Meta meta = componentType.getMeta();
489488
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+
});
501502
} else if (componentType.isPrimitive()) {
502503
// primitive array type needs wrapping
503504
Object boxedArray = getUnboxedArray(array);
@@ -513,19 +514,24 @@ public void setArrayValue(Object array, int index, Object value) {
513514
Klass componentType = ((ArrayKlass) arrayRef.getKlass()).getComponentType();
514515
Meta meta = componentType.getMeta();
515516
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;
528522
}
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+
});
529535
} else if (componentType.isPrimitive()) {
530536
// primitive array type needs wrapping
531537
Object boxedArray = getUnboxedArray(array);
@@ -612,24 +618,18 @@ public boolean isInstanceOf(Object object, KlassRef klass) {
612618

613619
@Override
614620
public void stopThread(Object guestThread, Object guestThrowable) {
615-
Object previous = null;
616-
try {
617-
previous = controller.enterTruffleContext();
621+
performInContext(() -> {
618622
context.getThreadAccess().stop((StaticObject) guestThread, (StaticObject) guestThrowable);
619-
} finally {
620-
controller.leaveTruffleContext(previous);
621-
}
623+
return null;
624+
});
622625
}
623626

624627
@Override
625628
public void interruptThread(Object thread) {
626-
Object previous = null;
627-
try {
628-
previous = controller.enterTruffleContext();
629+
performInContext(() -> {
629630
context.interruptThread((StaticObject) thread);
630-
} finally {
631-
controller.leaveTruffleContext(previous);
632-
}
631+
return null;
632+
});
633633
}
634634

635635
@Override
@@ -639,13 +639,10 @@ public boolean systemExitImplemented() {
639639

640640
@Override
641641
public void exit(int exitCode) {
642-
Object previous = null;
643-
try {
644-
previous = controller.enterTruffleContext();
642+
performInContext(() -> {
645643
context.truffleExit(null, exitCode);
646-
} finally {
647-
controller.leaveTruffleContext(previous);
648-
}
644+
return null;
645+
});
649646
}
650647

651648
@Override
@@ -845,4 +842,19 @@ public ModuleRef[] getAllModulesRefs() {
845842
public synchronized int redefineClasses(List<RedefineInfo> redefineInfos) {
846843
return context.getClassRedefinition().redefineClasses(redefineInfos, false, true);
847844
}
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+
}
848860
}

0 commit comments

Comments
 (0)