Skip to content

Commit

Permalink
Merge pull request #62 from jaredpar/tuple
Browse files Browse the repository at this point in the history
Support for ValueTuple
  • Loading branch information
jaredpar authored Aug 28, 2024
2 parents 5bcaa26 + 269df57 commit 3865c99
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: dotnet pack -p:PackageOutputPath="${GITHUB_WORKSPACE}/packages" -p:IncludeSymbols=false -p:RepositoryCommit=${GITHUB_SHA} -p:PackageVersion="0.0.0.1"

- name: Test
run: dotnet test --no-build --verbosity normal
run: dotnet test --framework net8.0 --no-build --verbosity normal

- name: Upload Binary Log
uses: actions/upload-artifact@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.3" IncludeAssets="none" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" IncludeAssets="none" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" IncludeAssets="none" PrivateAssets="all" GeneratePathProperty="true" />
</ItemGroup>

<Import Project="Generated.targets" />
Expand Down
30 changes: 30 additions & 0 deletions Src/Basic.Reference.Assemblies.Net461/Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4476,6 +4476,11 @@ public static class ExtraReferenceInfos
/// The <see cref="ReferenceInfo"/> for System.Threading.Tasks.Extensions.dll
/// </summary>
public static ReferenceInfo SystemThreadingTasksExtensions => new ReferenceInfo("System.Threading.Tasks.Extensions.dll", Resources.SystemThreadingTasksExtensions, Net461.ExtraReferences.SystemThreadingTasksExtensions, global::System.Guid.Parse("bc890e4e-a34f-463c-8fd9-60f43c8beb88"));

/// <summary>
/// The <see cref="ReferenceInfo"/> for System.ValueTuple.dll
/// </summary>
public static ReferenceInfo SystemValueTuple => new ReferenceInfo("System.ValueTuple.dll", Resources.SystemValueTuple, Net461.ExtraReferences.SystemValueTuple, global::System.Guid.Parse("1aa5ee86-d143-43bd-94ec-c0749ff98222"));
private static ImmutableArray<ReferenceInfo> _all;
public static ImmutableArray<ReferenceInfo> All
{
Expand All @@ -4486,6 +4491,7 @@ public static ImmutableArray<ReferenceInfo> All
_all =
[
SystemThreadingTasksExtensions,
SystemValueTuple,
];
}
return _all;
Expand Down Expand Up @@ -4517,6 +4523,23 @@ public static PortableExecutableReference SystemThreadingTasksExtensions
}
}

private static PortableExecutableReference? _SystemValueTuple;

/// <summary>
/// The <see cref="PortableExecutableReference"/> for System.ValueTuple.dll
/// </summary>
public static PortableExecutableReference SystemValueTuple
{
get
{
if (_SystemValueTuple is null)
{
_SystemValueTuple = AssemblyMetadata.CreateFromImage(Resources.SystemValueTuple).GetReference(filePath: "System.ValueTuple.dll", display: "System.ValueTuple (net461)");
}
return _SystemValueTuple;
}
}

private static ImmutableArray<PortableExecutableReference> _all;
public static ImmutableArray<PortableExecutableReference> All
{
Expand All @@ -4527,6 +4550,7 @@ public static ImmutableArray<PortableExecutableReference> All
_all =
[
SystemThreadingTasksExtensions,
SystemValueTuple,
];
}
return _all;
Expand Down Expand Up @@ -5649,6 +5673,12 @@ public static class Resources
public static byte[] SystemThreadingTasksExtensions => ResourceLoader.GetOrCreateResource(ref _SystemThreadingTasksExtensions, "net461.System.Threading.Tasks.Extensions");
private static byte[]? _SystemThreadingTasksExtensions;

/// <summary>
/// The image bytes for System.ValueTuple.dll
/// </summary>
public static byte[] SystemValueTuple => ResourceLoader.GetOrCreateResource(ref _SystemValueTuple, "net461.System.ValueTuple");
private static byte[]? _SystemValueTuple;


}
}
Expand Down
4 changes: 4 additions & 0 deletions Src/Basic.Reference.Assemblies.Net461/Generated.targets
Original file line number Diff line number Diff line change
Expand Up @@ -740,5 +740,9 @@
<LogicalName>net461.System.Threading.Tasks.Extensions</LogicalName>
<Link>Resources\net461\System.Threading.Tasks.Extensions.dll</Link>
</EmbeddedResource>
<EmbeddedResource Include="$(NuGetPackageRoot)\system.valuetuple\4.5.0\lib\net461\System.ValueTuple.dll" WithCulture="false">
<LogicalName>net461.System.ValueTuple</LogicalName>
<Link>Resources\net461\System.ValueTuple.dll</Link>
</EmbeddedResource>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net472</TargetFrameworks>
<IsPackable>false</IsPackable>
<RollForward>LatestMajor</RollForward>
</PropertyGroup>
Expand Down
77 changes: 77 additions & 0 deletions Src/Basic.Reference.Assemblies.UnitTests/CompilationUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Operations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Basic.Reference.Assemblies.UnitTests;
internal static class CompilationUtil
{
public static MemoryStream CompileToLibrary(string code, string assemblyName, IEnumerable<MetadataReference> references)
{
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create(
assemblyName,
[CSharpSyntaxTree.ParseText(code)],
references,
options);

var peStream = new MemoryStream();
var emitResult = compilation.Emit(peStream);
if (!emitResult.Success)
{
throw new Exception(GetMessage(emitResult.Diagnostics));
}

peStream.Position = 0;
return peStream;

static string GetMessage(IEnumerable<Diagnostic> diagnostics)
{
var builder = new StringBuilder();
builder.AppendLine("Compilation failed with the following errors:");
foreach (var d in diagnostics)
{
builder.AppendLine(d.ToString());
}
return builder.ToString();
}
}

public static Assembly CompileToLibraryAndLoad(string code, string assemblyName, IEnumerable<MetadataReference> references)
{
var stream = CompileToLibrary(code, assemblyName, references);
return Load(stream, assemblyName);
}

/// <summary>
/// Compile and run the code expecting to find a static Lib.Go method
/// </summary>
public static string? CompileAndRun(string code, string assemblyName, IEnumerable<MetadataReference> references)
{
var assembly = CompileToLibraryAndLoad(code, assemblyName, references);
var libType = assembly
.GetTypes()
.Where(x => x.Name == "Lib")
.Single();
var method = libType.GetMethod("Go", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var obj = method!.Invoke(null, null);
return (string?)obj;
}

public static Assembly Load(MemoryStream stream, string assemblyName)
{
stream.Position = 0;
#if NET
var alc = new System.Runtime.Loader.AssemblyLoadContext(assemblyName);
return alc.LoadFromStream(stream);
#else
return Assembly.Load(stream.ToArray());
#endif
}
}
3 changes: 3 additions & 0 deletions Src/Basic.Reference.Assemblies.UnitTests/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

Expand Down
27 changes: 27 additions & 0 deletions Src/Basic.Reference.Assemblies.UnitTests/SanityUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace Basic.Reference.Assemblies.UnitTests;
public class SanityUnitTests(ITestOutputHelper outputHelper)
{
public ITestOutputHelper TestOutputHelper { get; } = outputHelper;
public bool IsCoreClr =>
#if NET
true;
#else
false;
#endif

[Theory]
[MemberData(nameof(TestData.ApplicationReferences), MemberType = typeof(TestData))]
Expand Down Expand Up @@ -176,4 +182,25 @@ static void Main()
Assert.True(emitResult.Success);
Assert.Empty(emitResult.Diagnostics);
}

[Fact]
public void RunTuple()
{
var source = """
static class Lib
{
public static string Go()
{
var tuple = (1, 2);
return tuple.ToString();
}
}
""";

var references = IsCoreClr
? Net80.References.All
: [.. Net461.References.All, .. Net461.ExtraReferences.All];
var actual = CompilationUtil.CompileAndRun(source, nameof(RunTuple), references);
Assert.Equal("(1, 2)", actual);
}
}
5 changes: 4 additions & 1 deletion Src/Generate/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ void Net461()
var content = GetGeneratedContent(
"Net461",
[@"microsoft.netframework.referenceassemblies.net461\1.0.3\build\.NETFramework\v4.6.1"],
[@"system.threading.tasks.extensions\4.5.4\lib\net461"]);
[
@"system.threading.tasks.extensions\4.5.4\lib\net461",
@"system.valuetuple\4.5.0\lib\net461"
]);
var targetDir = Path.Combine(srcPath, "Basic.Reference.Assemblies.Net461");
File.WriteAllText(Path.Combine(targetDir, "Generated.cs"), content.CodeContent, encoding);
File.WriteAllText(Path.Combine(targetDir, "Generated.targets"), content.TargetsContent, encoding);
Expand Down

0 comments on commit 3865c99

Please sign in to comment.