Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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