oil: add fallible build option

Summary: Add a second form for OIL AoT compilation which returns a `std::optional` instead of throwing. This is preferred if it's possible for the symbol to not be defined and you must not throw, instead preferring to handle the failure a different way. This still calls the throwing interface as that's what oilgen depends on, but makes sure the precondition is true first such that it won't throw.

Differential Revision: D50363926
This commit is contained in:
Jake Hillion 2023-10-17 05:54:43 -07:00 committed by Facebook Community Bot
parent f7bb1e75ad
commit 57c1a76228

View File

@ -20,6 +20,7 @@
#include <oi/types/dy.h>
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <vector>
@ -31,9 +32,6 @@ enum class Feature {
GenJitDebug,
};
template <typename T, Feature... Fs>
IntrospectionResult __attribute__((noinline)) introspect(const T& objectAddr);
#ifdef OIL_AOT_COMPILATION
template <class T, Feature... Fs>
@ -48,6 +46,16 @@ IntrospectionResult introspect(const T& objectAddr) {
return introspectImpl<T, Fs...>(objectAddr);
}
template <typename T, Feature... Fs>
std::optional<IntrospectionResult> tryIntrospect(const T& objectAddr) {
if (!introspectImpl<T, Fs...>)
return std::nullopt;
// This checks twice but is necessary for compile time as it currently
// depends on the presence of the strong symbol.
return introspect(objectAddr);
}
#endif
} // namespace oi