-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a no-exceptions mode to JNIPP #39
base: master
Are you sure you want to change the base?
Conversation
ah, interesting! I was thinking that we'd add a noexcept overload for the various functions to populate an exception output parameter |
Just to make sure I understood you correctly, are you envisioning something like the following? // jnipp.cpp:693
method_t Class::getMethod(const char* name, const char* signature, Exception** ex) const // (1)
{
jmethodID id = env()->GetMethodID(getHandle(), name, signature);
if (id == nullptr)
*ex = new NameResolutionException(name); // (2)
return id;
} (changes from the original: (1) adds First problem that I see with this is that it'd make the callers' responsibility to free the exception object, otherwise we'll leak memory. The other problem is that I think if the caller would like to learn about the specific type of the exception being returned (note I think it may be OK to have an out-parameter that is an enum that carries a result code (or maybe a struct with error code and a message?). There is also // jnipp.cpp:655
Class::Class(const char* name) : Object(findClass(name), DeleteLocalInput) // findClass() can throw
{
} Exceptions allowed us to avoid creating "zombie objects" (the ctor would throw, the instance wouldn't be created, all is relatively fine) - the no-exceptions mode will not have that luxury... |
That does make sense. However, my concern is that in things like Chrome that might use this (because of using openxr), they really don't want to terminate just because there was a JNI exception trying to find openxr I might take a crack at my idea this week. I'll see if I can avoid having dual code paths, just have the thrower delegate to the no-throw. We already have nullable objects, so that's not that big of a deal. |
Sounds good, thanks for taking a look! I think it still makes sense to terminate in JNIPP, but maybe not in the OpenXR loader (depends on the specific situation). With the auto-generated wrappers built on top of JNIPP, I think the reasons why exceptions may be thrown are quite limited (e.g. NameResolutionExceptions should not be raised at all since they signify a programmer error like a typo in the class name / method name?). Maybe just changing the API surface for places that can throw InitializationExceptions is sufficient here? |
OK, I put up some of my partial work towards an alternate approach here: #43 I didn't get it nearly finished because it's been a little hectic, but hopefully it shows the idea of where I was going. Should be able to turn each throwing function into a non-throwing one and replace it with a call to the non-throwing one that then calls throwException on the exception data. |
Yeah, I think that approach would work, but as I've mentioned, most of the codebase will probably need to be rewritten. There's also the problem with zombie objects (i.e. objects that exist despite ctor wanting to throw an exception) that I think your draft PR does not show how they'd look like (e.g. |
Any update on either this or the alternative? |
This PR introduces a no-exceptions mode to JNIPP. Some of the downstream consumers of JNIPP are meant to be built with exceptions disabled, but that becomes a bit problematic when their direct or indirect dependency does not support such mode.
Changes:
JNIPP_USE_EXCEPTION_HANDLING
, ON by defaultJNIPP_USE_EXCEPTION
, injnipp.h
header (set to1
by default)JNIPP_USE_EXCEPTION
is set to0
, allthrow
exceptions injnipp.cpp
will be replaced by calls tostd::terminate()
; this is achieved via a cpp-only macro,JNIPP_THROW
-fno-exceptions
switchTested as follows: