Skip to content

Commit

Permalink
Address ILLink warning in InteropServices.JavaScript
Browse files Browse the repository at this point in the history
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 dotnet#45623
  • Loading branch information
eerhardt committed Dec 7, 2020
1 parent 301056d commit d0d5ba6
Show file tree
Hide file tree
Showing 2 changed files with 26 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 Down Expand Up @@ -336,6 +337,9 @@ private static void SetupJSContinuation(Task task, JSObject continuationObj)
else
task.GetAwaiter().OnCompleted(Complete);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Justification = "The DynamicDependency ensures Task<T>.Result is always preserved.")]
[DynamicDependency("get_Result", typeof(Task<>))]
void Complete()
{
try
Expand All @@ -348,10 +352,15 @@ void Complete()
{
result = System.Array.Empty<object>();
}
else if (IsAssignableToGenericTaskType(task_type))
{
result = task_type.GetMethod("get_Result")?.Invoke(task, null);
}
else
{
result = task_type.GetMethod("get_Result")?.Invoke(task, System.Array.Empty<object>());
result = null;
}

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

private static bool IsAssignableToGenericTaskType(Type? t)
{
while (t != null)
{
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Task<>))
{
return true;
}

t = t.BaseType;
}

return false;
}

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

0 comments on commit d0d5ba6

Please sign in to comment.