Skip to content

Commit 2ddf52d

Browse files
LibEspresso: be more robust to context enter/leave/close errors
1 parent 0687444 commit 2ddf52d

File tree

1 file changed

+24
-4
lines changed
  • espresso/src/com.oracle.truffle.espresso.libjavavm/src/com/oracle/truffle/espresso/libjavavm

1 file changed

+24
-4
lines changed

espresso/src/com.oracle.truffle.espresso.libjavavm/src/com/oracle/truffle/espresso/libjavavm/LibEspresso.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ static int createJavaVM(@SuppressWarnings("unused") IsolateThread thread, JNIJav
111111
context.close(true);
112112
}
113113
return JNIErrors.JNI_ERR();
114+
} catch (IllegalArgumentException e) {
115+
// This can happen during option processing (build call above)
116+
// OptionType converters can throw IllegalArgumentException
117+
STDERR.println(e.getMessage());
118+
return JNIErrors.JNI_ERR();
114119
}
115120
Value java = bindings.getMember("<JavaVM>");
116121
if (!java.isNativePointer()) {
@@ -161,7 +166,12 @@ static int enterContext(@SuppressWarnings("unused") IsolateThread thread, JNIJav
161166
STDERR.println("Cannot enter context: no context found");
162167
return JNIErrors.JNI_ERR();
163168
}
164-
context.enter();
169+
try {
170+
context.enter();
171+
} catch (PolyglotException | IllegalStateException e) {
172+
STDERR.println("Cannot enter context: " + e.getMessage());
173+
return JNIErrors.JNI_ERR();
174+
}
165175
return JNIErrors.JNI_OK();
166176
}
167177

@@ -173,7 +183,12 @@ static int leaveContext(@SuppressWarnings("unused") IsolateThread thread, JNIJav
173183
STDERR.println("Cannot leave context: no context found");
174184
return JNIErrors.JNI_ERR();
175185
}
176-
context.leave();
186+
try {
187+
context.leave();
188+
} catch (IllegalStateException e) {
189+
STDERR.println("Cannot leave context: " + e.getMessage());
190+
return JNIErrors.JNI_ERR();
191+
}
177192
return JNIErrors.JNI_OK();
178193
}
179194

@@ -189,8 +204,13 @@ static int closeContext(@SuppressWarnings("unused") IsolateThread thread, JNIJav
189204
ObjectHandle contextHandle = javaVM.getFunctions().getContext();
190205
Context context = ObjectHandles.getGlobal().get(contextHandle);
191206
ObjectHandles.getGlobal().destroy(contextHandle);
192-
context.leave();
193-
context.close();
207+
try {
208+
context.leave();
209+
context.close();
210+
} catch (PolyglotException | IllegalStateException e) {
211+
STDERR.println("Cannot close context: " + e.getMessage());
212+
return JNIErrors.JNI_ERR();
213+
}
194214
return JNIErrors.JNI_OK();
195215
}
196216

0 commit comments

Comments
 (0)