Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@

namespace BabylonReactNative
{
bool TryGetXrContext(facebook::jsi::Runtime& jsiRuntime, const facebook::jsi::Value& xrSession, IXrContextOpenXR*& xrContext)
bool TryGetXrContext(facebook::jsi::Runtime& jsiRuntime, IXrContextOpenXR*& xrContext)
{
xrContext = nullptr;
if (!xrSession.isObject() ||
!xrSession.asObject(jsiRuntime).hasProperty(jsiRuntime, "nativeXrContext") ||
!xrSession.asObject(jsiRuntime).hasProperty(jsiRuntime, "nativeXrContextType") ||
xrSession.asObject(jsiRuntime).getProperty(jsiRuntime, "nativeXrContextType").asString(jsiRuntime).utf8(jsiRuntime) != "OpenXR")
if (!jsiRuntime.global().hasProperty(jsiRuntime, "navigator"))
{
return false;
}

auto nativeExtensionPtr = static_cast<uintptr_t>(xrSession.asObject(jsiRuntime).getProperty(jsiRuntime, "nativeXrContext").asNumber());
auto navigator{ jsiRuntime.global().getProperty(jsiRuntime, "navigator").asObject(jsiRuntime) };
if (!navigator.hasProperty(jsiRuntime, "xr"))
{
return false;
}

auto nativeXr{ navigator.getProperty(jsiRuntime, "xr").asObject(jsiRuntime) };
if (!nativeXr.hasProperty(jsiRuntime, "nativeXrContext") ||
!nativeXr.hasProperty(jsiRuntime, "nativeXrContextType") ||
nativeXr.getProperty(jsiRuntime, "nativeXrContextType").asString(jsiRuntime).utf8(jsiRuntime) != "OpenXR")
{
return false;
}

auto nativeExtensionPtr = static_cast<uintptr_t>(nativeXr.getProperty(jsiRuntime, "nativeXrContext").asNumber());
xrContext = reinterpret_cast<IXrContextOpenXR*>(nativeExtensionPtr);
return true;
}
Expand Down