Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/bflat.Tests/BflatCompilationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public void Run(string expectedOutput = null)
}

string stdOut = p.StandardOutput.ReadToEnd();
string stdErr = p.StandardError.ReadToEnd();

Assert.True(p.ExitCode == 0, $"Non-zero exit code: {p.ExitCode}\nStdout:\n{stdOut}\nStderr:\n{stdErr}");

if (expectedOutput != null)
{
Assert.Equal(expectedOutput, stdOut);
}

Assert.Equal(0, p.ExitCode);
}

}
Expand Down
24 changes: 23 additions & 1 deletion src/bflat.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,26 @@ public void Test1()
{
new BflatCompilation().Build("System.Console.WriteLine(\"Hello\");").Run("Hello" + Environment.NewLine);
}
}

[Fact]
public void ConsoleTitleAndEnvironmentExitWorkWithReflectionDisabled()
{
new BflatCompilation().Build("""
using System;

public static class Program
{
public static void Main() => FailFast("Done", 0);

private static void FailFast(string msg, int code)
{
Console.WriteLine(msg);
_ = Console.Title;
Environment.Exit(code);
}
}
""", "--no-reflection --no-stacktrace-data --no-globalization --no-exception-messages --optimize-space --no-debug-info")
.Run("Done" + Environment.NewLine);
}

}
2 changes: 1 addition & 1 deletion src/bflat/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public override int Handle(ParseResult result)
}
else
{
mdBlockingPolicy = new FullyBlockedMetadataBlockingPolicy();
mdBlockingPolicy = new SpanMetadataBlockingPolicy();
resBlockingPolicy = new FullyBlockedManifestResourceBlockingPolicy();
}
DynamicInvokeThunkGenerationPolicy invokeThunkGenerationPolicy = new DefaultDynamicInvokeThunkGenerationPolicy();
Expand Down
17 changes: 17 additions & 0 deletions src/bflat/SpanMetadataBlockingPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ILCompiler;
using Internal.TypeSystem;

// .NET 10 Console.Title formats its result through Span<char>.ToString().
// Keep only the metadata needed for that virtual call when reflection is disabled.
internal sealed class SpanMetadataBlockingPolicy : FullyBlockedMetadataBlockingPolicy
{
public override bool IsBlocked(MetadataType type) => !IsSpanType(type) && base.IsBlocked(type);

public override bool IsBlocked(MethodDesc method) => !(IsSpanType(method.OwningType) && method.Name == "ToString") && base.IsBlocked(method);

public override bool IsBlocked(FieldDesc field) => base.IsBlocked(field);

private static bool IsSpanType(TypeDesc type) => type is MetadataType metadataType
&& metadataType.Namespace == "System"
&& metadataType.Name == "Span`1";
}