Skip to content

Commit 323edb2

Browse files
committed
Make sure truffle context is entered when performing operations that require so
1 parent 23125d9 commit 323edb2

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
@@ -302,14 +299,14 @@ public Object[] getAllGuestThreads() {
302299
@Override
303300
public String getStringValue(Object object) {
304301
if (object instanceof StaticObject staticObject) {
305-
return (String) InteropLibrary.getUncached().toDisplayString(staticObject, false);
302+
return performInContext(() -> (String) UNCACHED.toDisplayString(staticObject, false));
306303
}
307304
return object.toString();
308305
}
309306

310307
@Override
311308
public MethodVersionRef getMethodFromRootNode(RootNode root) {
312-
if (root != null && root instanceof EspressoRootNode) {
309+
if (root instanceof EspressoRootNode) {
313310
return ((EspressoRootNode) root).getMethodVersion();
314311
}
315312
return null;
@@ -414,15 +411,17 @@ public int getArrayLength(Object array) {
414411
StaticObject staticObject = (StaticObject) array;
415412
EspressoLanguage language = context.getLanguage();
416413
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;
421419
}
422-
return (int) arrayLength;
423-
} catch (UnsupportedMessageException e) {
420+
});
421+
if (arrayLength > Integer.MAX_VALUE) {
424422
return -1;
425423
}
424+
return (int) arrayLength;
426425
}
427426
return staticObject.length(language);
428427
}
@@ -476,17 +475,19 @@ public Object getArrayValue(Object array, int index) {
476475
Klass componentType = ((ArrayKlass) arrayRef.getKlass()).getComponentType();
477476
Meta meta = componentType.getMeta();
478477
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+
});
490491
} else if (componentType.isPrimitive()) {
491492
// primitive array type needs wrapping
492493
Object boxedArray = getUnboxedArray(array);
@@ -502,19 +503,24 @@ public void setArrayValue(Object array, int index, Object value) {
502503
Klass componentType = ((ArrayKlass) arrayRef.getKlass()).getComponentType();
503504
Meta meta = componentType.getMeta();
504505
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;
517511
}
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+
});
518524
} else if (componentType.isPrimitive()) {
519525
// primitive array type needs wrapping
520526
Object boxedArray = getUnboxedArray(array);
@@ -601,24 +607,18 @@ public boolean isInstanceOf(Object object, KlassRef klass) {
601607

602608
@Override
603609
public void stopThread(Object guestThread, Object guestThrowable) {
604-
Object previous = null;
605-
try {
606-
previous = controller.enterTruffleContext();
610+
performInContext(() -> {
607611
context.getThreadAccess().stop((StaticObject) guestThread, (StaticObject) guestThrowable);
608-
} finally {
609-
controller.leaveTruffleContext(previous);
610-
}
612+
return null;
613+
});
611614
}
612615

613616
@Override
614617
public void interruptThread(Object thread) {
615-
Object previous = null;
616-
try {
617-
previous = controller.enterTruffleContext();
618+
performInContext(() -> {
618619
context.interruptThread((StaticObject) thread);
619-
} finally {
620-
controller.leaveTruffleContext(previous);
621-
}
620+
return null;
621+
});
622622
}
623623

624624
@Override
@@ -628,13 +628,10 @@ public boolean systemExitImplemented() {
628628

629629
@Override
630630
public void exit(int exitCode) {
631-
Object previous = null;
632-
try {
633-
previous = controller.enterTruffleContext();
631+
performInContext(() -> {
634632
context.truffleExit(null, exitCode);
635-
} finally {
636-
controller.leaveTruffleContext(previous);
637-
}
633+
return null;
634+
});
638635
}
639636

640637
@Override
@@ -834,4 +831,19 @@ public ModuleRef[] getAllModulesRefs() {
834831
public synchronized int redefineClasses(List<RedefineInfo> redefineInfos) {
835832
return context.getClassRedefinition().redefineClasses(redefineInfos, false, true);
836833
}
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+
}
837849
}

0 commit comments

Comments
 (0)