Skip to content

Commit c58ab92

Browse files
authored
Add nullable to JSInterop (#22326)
1 parent e1fab30 commit c58ab92

18 files changed

+88
-86
lines changed

src/JSInterop/Microsoft.JSInterop/ref/Microsoft.JSInterop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.JSInterop.netstandard2.0.cs" />

src/JSInterop/Microsoft.JSInterop/ref/Microsoft.JSInterop.netcoreapp.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public JSException(string message, System.Exception innerException) { }
3030
public abstract partial class JSInProcessRuntime : Microsoft.JSInterop.JSRuntime, Microsoft.JSInterop.IJSInProcessRuntime, Microsoft.JSInterop.IJSRuntime
3131
{
3232
protected JSInProcessRuntime() { }
33-
protected abstract string InvokeJS(string identifier, string argsJson);
33+
protected abstract string? InvokeJS(string identifier, string? argsJson);
34+
[return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute]
3435
public TValue Invoke<TValue>(string identifier, params object[] args) { throw null; }
3536
}
3637
public static partial class JSInProcessRuntimeExtensions
@@ -42,15 +43,16 @@ public sealed partial class JSInvokableAttribute : System.Attribute
4243
{
4344
public JSInvokableAttribute() { }
4445
public JSInvokableAttribute(string identifier) { }
45-
public string Identifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
46+
public string? Identifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
4647
}
4748
public abstract partial class JSRuntime : Microsoft.JSInterop.IJSRuntime
4849
{
4950
protected JSRuntime() { }
5051
protected System.TimeSpan? DefaultAsyncTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
5152
protected internal System.Text.Json.JsonSerializerOptions JsonSerializerOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
52-
protected abstract void BeginInvokeJS(long taskId, string identifier, string argsJson);
53+
protected abstract void BeginInvokeJS(long taskId, string identifier, string? argsJson);
5354
protected internal abstract void EndInvokeDotNet(Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, in Microsoft.JSInterop.Infrastructure.DotNetInvocationResult invocationResult);
55+
[System.Diagnostics.DebuggerStepThroughAttribute]
5456
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) { throw null; }
5557
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args) { throw null; }
5658
}
@@ -74,7 +76,7 @@ public static partial class DotNetDispatcher
7476
{
7577
public static void BeginInvokeDotNet(Microsoft.JSInterop.JSRuntime jsRuntime, Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string argsJson) { }
7678
public static void EndInvokeJS(Microsoft.JSInterop.JSRuntime jsRuntime, string arguments) { }
77-
public static string Invoke(Microsoft.JSInterop.JSRuntime jsRuntime, in Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string argsJson) { throw null; }
79+
public static string? Invoke(Microsoft.JSInterop.JSRuntime jsRuntime, in Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string argsJson) { throw null; }
7880
}
7981
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
8082
public readonly partial struct DotNetInvocationInfo
@@ -92,11 +94,11 @@ public readonly partial struct DotNetInvocationResult
9294
{
9395
private readonly object _dummy;
9496
private readonly int _dummyPrimitive;
95-
public DotNetInvocationResult(System.Exception exception, string errorKind) { throw null; }
96-
public DotNetInvocationResult(object result) { throw null; }
97-
public string ErrorKind { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
98-
public System.Exception Exception { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
99-
public object Result { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
97+
public DotNetInvocationResult(System.Exception exception, string? errorKind) { throw null; }
98+
public DotNetInvocationResult(object? result) { throw null; }
99+
public string? ErrorKind { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
100+
public System.Exception? Exception { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
101+
public object? Result { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
100102
public bool Success { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
101103
}
102104
}

src/JSInterop/Microsoft.JSInterop/ref/Microsoft.JSInterop.netstandard2.0.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public JSException(string message, System.Exception innerException) { }
3030
public abstract partial class JSInProcessRuntime : Microsoft.JSInterop.JSRuntime, Microsoft.JSInterop.IJSInProcessRuntime, Microsoft.JSInterop.IJSRuntime
3131
{
3232
protected JSInProcessRuntime() { }
33-
protected abstract string InvokeJS(string identifier, string argsJson);
33+
protected abstract string? InvokeJS(string identifier, string? argsJson);
3434
public TValue Invoke<TValue>(string identifier, params object[] args) { throw null; }
3535
}
3636
public static partial class JSInProcessRuntimeExtensions
@@ -42,15 +42,16 @@ public sealed partial class JSInvokableAttribute : System.Attribute
4242
{
4343
public JSInvokableAttribute() { }
4444
public JSInvokableAttribute(string identifier) { }
45-
public string Identifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
45+
public string? Identifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
4646
}
4747
public abstract partial class JSRuntime : Microsoft.JSInterop.IJSRuntime
4848
{
4949
protected JSRuntime() { }
5050
protected System.TimeSpan? DefaultAsyncTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
5151
protected internal System.Text.Json.JsonSerializerOptions JsonSerializerOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
52-
protected abstract void BeginInvokeJS(long taskId, string identifier, string argsJson);
52+
protected abstract void BeginInvokeJS(long taskId, string identifier, string? argsJson);
5353
protected internal abstract void EndInvokeDotNet(Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, in Microsoft.JSInterop.Infrastructure.DotNetInvocationResult invocationResult);
54+
[System.Diagnostics.DebuggerStepThroughAttribute]
5455
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) { throw null; }
5556
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args) { throw null; }
5657
}
@@ -74,7 +75,7 @@ public static partial class DotNetDispatcher
7475
{
7576
public static void BeginInvokeDotNet(Microsoft.JSInterop.JSRuntime jsRuntime, Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string argsJson) { }
7677
public static void EndInvokeJS(Microsoft.JSInterop.JSRuntime jsRuntime, string arguments) { }
77-
public static string Invoke(Microsoft.JSInterop.JSRuntime jsRuntime, in Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string argsJson) { throw null; }
78+
public static string? Invoke(Microsoft.JSInterop.JSRuntime jsRuntime, in Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string argsJson) { throw null; }
7879
}
7980
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
8081
public readonly partial struct DotNetInvocationInfo
@@ -92,11 +93,11 @@ public readonly partial struct DotNetInvocationResult
9293
{
9394
private readonly object _dummy;
9495
private readonly int _dummyPrimitive;
95-
public DotNetInvocationResult(System.Exception exception, string errorKind) { throw null; }
96-
public DotNetInvocationResult(object result) { throw null; }
97-
public string ErrorKind { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
98-
public System.Exception Exception { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
99-
public object Result { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
96+
public DotNetInvocationResult(System.Exception exception, string? errorKind) { throw null; }
97+
public DotNetInvocationResult(object? result) { throw null; }
98+
public string? ErrorKind { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
99+
public System.Exception? Exception { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
100+
public object? Result { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
100101
public bool Success { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
101102
}
102103
}

src/JSInterop/Microsoft.JSInterop/src/DotNetObjectReferenceOfT.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class DotNetObjectReference<TValue> : IDotNetObjectReference, IDis
1818
{
1919
private readonly TValue _value;
2020
private long _objectId;
21-
private JSRuntime _jsRuntime;
21+
private JSRuntime? _jsRuntime;
2222

2323
/// <summary>
2424
/// Initializes a new instance of <see cref="DotNetObjectReference{TValue}" />.
@@ -57,7 +57,7 @@ internal long ObjectId
5757
}
5858
}
5959

60-
internal JSRuntime JSRuntime
60+
internal JSRuntime? JSRuntime
6161
{
6262
get
6363
{

src/JSInterop/Microsoft.JSInterop/src/Infrastructure/DotNetDispatcher.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public static class DotNetDispatcher
3434
/// <param name="invocationInfo">The <see cref="DotNetInvocationInfo"/>.</param>
3535
/// <param name="argsJson">A JSON representation of the parameters.</param>
3636
/// <returns>A JSON representation of the return value, or null.</returns>
37-
public static string Invoke(JSRuntime jsRuntime, in DotNetInvocationInfo invocationInfo, string argsJson)
37+
public static string? Invoke(JSRuntime jsRuntime, in DotNetInvocationInfo invocationInfo, string argsJson)
3838
{
3939
// This method doesn't need [JSInvokable] because the platform is responsible for having
4040
// some way to dispatch calls here. The logic inside here is the thing that checks whether
4141
// the targeted method has [JSInvokable]. It is not itself subject to that restriction,
4242
// because there would be nobody to police that. This method *is* the police.
4343

44-
IDotNetObjectReference targetInstance = default;
44+
IDotNetObjectReference? targetInstance = default;
4545
if (invocationInfo.DotNetObjectId != default)
4646
{
4747
targetInstance = jsRuntime.GetObjectReference(invocationInfo.DotNetObjectId);
@@ -75,9 +75,9 @@ public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo i
7575

7676
var callId = invocationInfo.CallId;
7777

78-
object syncResult = null;
79-
ExceptionDispatchInfo syncException = null;
80-
IDotNetObjectReference targetInstance = null;
78+
object? syncResult = null;
79+
ExceptionDispatchInfo? syncException = null;
80+
IDotNetObjectReference? targetInstance = null;
8181
try
8282
{
8383
if (invocationInfo.DotNetObjectId != default)
@@ -126,7 +126,7 @@ public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo i
126126
}
127127
}
128128

129-
private static object InvokeSynchronously(JSRuntime jsRuntime, in DotNetInvocationInfo callInfo, IDotNetObjectReference objectReference, string argsJson)
129+
private static object? InvokeSynchronously(JSRuntime jsRuntime, in DotNetInvocationInfo callInfo, IDotNetObjectReference? objectReference, string argsJson)
130130
{
131131
var assemblyName = callInfo.AssemblyName;
132132
var methodIdentifier = callInfo.MethodIdentifier;
@@ -168,14 +168,14 @@ private static object InvokeSynchronously(JSRuntime jsRuntime, in DotNetInvocati
168168
if (tie.InnerException != null)
169169
{
170170
ExceptionDispatchInfo.Capture(tie.InnerException).Throw();
171-
throw null; // unreached
171+
throw tie.InnerException; // Unreachable
172172
}
173173

174174
throw;
175175
}
176176
}
177177

178-
internal static object[] ParseArguments(JSRuntime jsRuntime, string methodIdentifier, string arguments, Type[] parameterTypes)
178+
internal static object?[] ParseArguments(JSRuntime jsRuntime, string methodIdentifier, string arguments, Type[] parameterTypes)
179179
{
180180
if (parameterTypes.Length == 0)
181181
{
@@ -189,7 +189,7 @@ internal static object[] ParseArguments(JSRuntime jsRuntime, string methodIdenti
189189
throw new JsonException("Invalid JSON");
190190
}
191191

192-
var suppliedArgs = new object[parameterTypes.Length];
192+
var suppliedArgs = new object?[parameterTypes.Length];
193193

194194
var index = 0;
195195
while (index < parameterTypes.Length && reader.Read() && reader.TokenType != JsonTokenType.EndArray)
@@ -331,7 +331,7 @@ private static (MethodInfo methodInfo, Type[] parameterTypes) GetCachedMethodInf
331331

332332
foreach (var method in invokableMethods)
333333
{
334-
var identifier = method.GetCustomAttribute<JSInvokableAttribute>(false).Identifier ?? method.Name;
334+
var identifier = method.GetCustomAttribute<JSInvokableAttribute>(false)!.Identifier ?? method.Name!;
335335
var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();
336336

337337
if (result.ContainsKey(identifier))
@@ -360,7 +360,7 @@ private static (MethodInfo methodInfo, Type[] parameterTypes) GetCachedMethodInf
360360
.Where(method => !method.ContainsGenericParameters && method.IsDefined(typeof(JSInvokableAttribute), inherit: false));
361361
foreach (var method in invokableMethods)
362362
{
363-
var identifier = method.GetCustomAttribute<JSInvokableAttribute>(false).Identifier ?? method.Name;
363+
var identifier = method.GetCustomAttribute<JSInvokableAttribute>(false)!.Identifier ?? method.Name;
364364
var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();
365365

366366
if (result.ContainsKey(identifier))
@@ -400,7 +400,7 @@ private static Assembly GetRequiredLoadedAssembly(AssemblyKey assemblyKey)
400400
public AssemblyKey(Assembly assembly)
401401
{
402402
Assembly = assembly;
403-
AssemblyName = assembly.GetName().Name;
403+
AssemblyName = assembly.GetName().Name!;
404404
}
405405

406406
public AssemblyKey(string assemblyName)
@@ -409,7 +409,7 @@ public AssemblyKey(string assemblyName)
409409
AssemblyName = assemblyName;
410410
}
411411

412-
public Assembly Assembly { get; }
412+
public Assembly? Assembly { get; }
413413

414414
public string AssemblyName { get; }
415415

src/JSInterop/Microsoft.JSInterop/src/Infrastructure/DotNetInvocationResult.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public readonly struct DotNetInvocationResult
1212
/// </summary>
1313
/// <param name="exception">The <see cref="System.Exception"/> that caused the failure.</param>
1414
/// <param name="errorKind">The error kind.</param>
15-
public DotNetInvocationResult(Exception exception, string errorKind)
15+
public DotNetInvocationResult(Exception exception, string? errorKind)
1616
{
1717
Result = default;
1818
Exception = exception ?? throw new ArgumentNullException(nameof(exception));
@@ -24,7 +24,7 @@ public DotNetInvocationResult(Exception exception, string errorKind)
2424
/// Constructor for a successful invocation.
2525
/// </summary>
2626
/// <param name="result">The result.</param>
27-
public DotNetInvocationResult(object result)
27+
public DotNetInvocationResult(object? result)
2828
{
2929
Result = result;
3030
Exception = default;
@@ -35,17 +35,17 @@ public DotNetInvocationResult(object result)
3535
/// <summary>
3636
/// Gets the <see cref="System.Exception"/> that caused the failure.
3737
/// </summary>
38-
public Exception Exception { get; }
38+
public Exception? Exception { get; }
3939

4040
/// <summary>
4141
/// Gets the error kind.
4242
/// </summary>
43-
public string ErrorKind { get; }
43+
public string? ErrorKind { get; }
4444

4545
/// <summary>
4646
/// Gets the result of a successful invocation.
4747
/// </summary>
48-
public object Result { get; }
48+
public object? Result { get; }
4949

5050
/// <summary>
5151
/// <see langword="true"/> if the invocation succeeded, otherwise <see langword="false"/>.

src/JSInterop/Microsoft.JSInterop/src/Infrastructure/DotNetObjectReferenceJsonConverterFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
2727
var instanceType = typeToConvert.GetGenericArguments()[0];
2828
var converterType = typeof(DotNetObjectReferenceJsonConverter<>).MakeGenericType(instanceType);
2929

30-
return (JsonConverter)Activator.CreateInstance(converterType, JSRuntime);
30+
return (JsonConverter)Activator.CreateInstance(converterType, JSRuntime)!;
3131
}
3232
}
3333
}

0 commit comments

Comments
 (0)