|
| 1 | +using System.Reflection; |
| 2 | +using Foundation; |
| 3 | +using ObjCRuntime; |
| 4 | + |
| 5 | +namespace FirebaseFoundationE2E; |
| 6 | + |
| 7 | +static partial class FirebaseRuntimeDriftCases |
| 8 | +{ |
| 9 | + sealed class ObjCExceptionProbe : IDisposable |
| 10 | + { |
| 11 | + bool disposed; |
| 12 | + |
| 13 | + ObjCExceptionProbe() |
| 14 | + { |
| 15 | + Runtime.MarshalObjectiveCException += OnMarshalObjectiveCException; |
| 16 | + } |
| 17 | + |
| 18 | + public static ObjCExceptionProbe Attach() |
| 19 | + { |
| 20 | + return new ObjCExceptionProbe(); |
| 21 | + } |
| 22 | + |
| 23 | + public NSException? Exception { get; private set; } |
| 24 | + |
| 25 | + public MarshalObjectiveCExceptionMode? ExceptionMode { get; private set; } |
| 26 | + |
| 27 | + public bool HasException => Exception is not null; |
| 28 | + |
| 29 | + public string Name => FormatDetail(Exception?.Name?.ToString()); |
| 30 | + |
| 31 | + public string Reason => FormatDetail(Exception?.Reason); |
| 32 | + |
| 33 | + public string Mode => FormatDetail(ExceptionMode?.ToString()); |
| 34 | + |
| 35 | + public void Dispose() |
| 36 | + { |
| 37 | + if (disposed) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + Runtime.MarshalObjectiveCException -= OnMarshalObjectiveCException; |
| 43 | + disposed = true; |
| 44 | + } |
| 45 | + |
| 46 | + void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEventArgs args) |
| 47 | + { |
| 48 | + Exception ??= args.Exception; |
| 49 | + ExceptionMode ??= args.ExceptionMode; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static string FormatNSError(NSError error) |
| 54 | + { |
| 55 | + return $"{error.Domain} ({error.Code}): {error.LocalizedDescription}"; |
| 56 | + } |
| 57 | + |
| 58 | + static void RequireConstructor(Type type, Type[] parameterTypes, string selector) |
| 59 | + { |
| 60 | + var constructor = type.GetConstructor(parameterTypes); |
| 61 | + if (constructor is null) |
| 62 | + { |
| 63 | + throw new InvalidOperationException( |
| 64 | + $"Expected managed constructor '{type.FullName}({string.Join(", ", parameterTypes.Select(parameterType => parameterType.FullName))})' " + |
| 65 | + $"was not found for selector '{selector}'."); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + static void RequireVoidMethod(Type type, string methodName, string selector) |
| 70 | + { |
| 71 | + RequireVoidMethod(type, methodName, Type.EmptyTypes, selector); |
| 72 | + } |
| 73 | + |
| 74 | + static void RequireVoidMethod(Type type, string methodName, Type[] parameterTypes, string selector) |
| 75 | + { |
| 76 | + var method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public, binder: null, types: parameterTypes, modifiers: null); |
| 77 | + if (method?.ReturnType != typeof(void)) |
| 78 | + { |
| 79 | + throw new InvalidOperationException( |
| 80 | + $"Expected managed API '{type.FullName}.{methodName}({string.Join(", ", parameterTypes.Select(parameterType => parameterType.FullName))})' " + |
| 81 | + $"to return void for selector '{selector}', observed '{method?.ReturnType.FullName ?? "<missing>"}'."); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + static string FormatDetail(string? value) |
| 86 | + { |
| 87 | + return string.IsNullOrWhiteSpace(value) ? "<empty>" : value; |
| 88 | + } |
| 89 | +} |
0 commit comments