Skip to content

Commit

Permalink
Address ILLink warning in InteropServices.JavaScript (#45727)
Browse files Browse the repository at this point in the history
* Address ILLink warning in InteropServices.JavaScript

In order to make the code trimming-compatible, a slight behavior change was made. The SetupJSContinuation method will only get the Result of Task<TResult> objects. If an object derives from the base Task class, and defines its own Result property, SetupJSContinuation will no longer respect that property.

This was done so the behavior remains consistent between trimmed and untrimmed applications.

Contributes to #45623
  • Loading branch information
eerhardt authored Dec 9, 2020
1 parent 8e29c00 commit 361d830
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Diagnostics;

namespace System.Runtime.InteropServices.JavaScript
{
Expand All @@ -19,6 +20,9 @@ public static class Runtime
// No need to lock as it is thread safe.
private static readonly ConditionalWeakTable<Delegate, JSObject> _weakDelegateTable = new ConditionalWeakTable<Delegate, JSObject>();

private const string TaskGetResultName = "get_Result";
private static readonly MethodInfo _taskGetResultMethodInfo = typeof(Task<>).GetMethod(TaskGetResultName)!;

// <summary>
// Execute the provided string in the JavaScript context
// </summary>
Expand Down Expand Up @@ -350,8 +354,9 @@ void Complete()
}
else
{
result = task_type.GetMethod("get_Result")?.Invoke(task, System.Array.Empty<object>());
result = GetTaskResultMethodInfo(task_type)?.Invoke(task, null);
}

continuationObj.Invoke("resolve", result);
}
else
Expand All @@ -371,6 +376,30 @@ void Complete()
}
}

/// <summary>
/// Gets the MethodInfo for the Task{T}.Result property getter.
/// </summary>
/// <remarks>
/// This ensures the returned MethodInfo is strictly for the Task{T} type, and not
/// a "Result" property on some other class that derives from Task or a "new Result"
/// property on a class that derives from Task{T}.
///
/// The reason for this restriction is to make this use of Reflection trim-compatible,
/// ensuring that trimming doesn't change the application's behavior.
/// </remarks>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Task<T>.Result is preserved by the ILLinker because _taskGetResultMethodInfo was initialized with it.")]
private static MethodInfo? GetTaskResultMethodInfo(Type taskType)
{
MethodInfo? result = taskType.GetMethod(TaskGetResultName);
if (result != null && result.HasSameMetadataDefinitionAs(_taskGetResultMethodInfo))
{
return result;
}

return null;
}

private static string ObjectToString(object o)
{
return o.ToString() ?? string.Empty;
Expand Down

0 comments on commit 361d830

Please sign in to comment.