-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilationModeTests.cs
More file actions
233 lines (201 loc) · 8.63 KB
/
Copy pathCompilationModeTests.cs
File metadata and controls
233 lines (201 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using AuroraScript.Tests.Infrastructure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace AuroraScript.Tests;
public sealed class CompilationModeTests
{
[Theory]
[InlineData(CompilationMode.Dynamic)]
[InlineData(CompilationMode.OnlyRun)]
#if NET9_0_OR_GREATER
[InlineData(CompilationMode.Persistence)]
#endif
public async Task ReleaseCompilationModesProduceSameResult(CompilationMode mode)
{
using var workspace = new TestWorkspace();
var (engine, domain) = await workspace.CompileModuleAsync(
"@module(TEST); export func run(value) { return value * 2 + 2; }",
mode);
ScriptAssert.Equal(42, TestWorkspace.Execute(
domain,
"run",
arguments: AuroraScript.Runtime.ScriptDatum.FromNumber(20)));
if (mode == CompilationMode.Persistence)
{
var assemblyPath = Path.Combine(workspace.Root, "test-output.dll");
Assert.True(File.Exists(assemblyPath));
Assert.True(new FileInfo(assemblyPath).Length > 0);
ScriptAssert.Equal(42, TestWorkspace.Execute(engine.CreateDomain(), "run", arguments: AuroraScript.Runtime.ScriptDatum.FromNumber(20)));
}
}
#if NET9_0_OR_GREATER
[Fact]
public async Task PersistenceDebugBuildEmitsAsSequencePoints()
{
using var workspace = new TestWorkspace();
var assemblyPath = Path.Combine(workspace.Root, "debug-output.dll");
var sourcePath = workspace.WriteSource("main.as",
"""
@module(TEST);
var value = 40;
export func run() {
var local = value + 2;
return local;
}
""");
var options = EngineOptions.Default
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Persistence)
.WithOptimization(optimization => optimization.Level = OptimizeOptions.Debug)
.WithOutput(output => output.AssemblyFile = assemblyPath)
.WithRuntime(runtime => runtime.ConsoleStdOut = TextWriter.Null)
.WithRuntime(runtime => runtime.ConsoleErrorOut = TextWriter.Null);
var engine = new AuroraEngine(options);
await engine.BuildAsync(sourcePath);
Assert.True(File.Exists(assemblyPath));
using var stream = File.OpenRead(assemblyPath);
using var peReader = new PEReader(stream);
var embeddedPdb = Assert.Single(peReader.ReadDebugDirectory()
.Where(entry => entry.Type == DebugDirectoryEntryType.EmbeddedPortablePdb));
using var provider = peReader.ReadEmbeddedPortablePdbDebugDirectoryData(embeddedPdb);
var reader = provider.GetMetadataReader();
var document = Assert.Single(reader.Documents.Where(handle =>
string.Equals(
Path.GetFullPath(ReadDocumentName(reader, handle)),
Path.GetFullPath(sourcePath),
StringComparison.OrdinalIgnoreCase)));
var lines = GetVisibleSequencePointLines(reader, document);
Assert.Contains(2, lines);
Assert.Contains(4, lines);
Assert.Contains(5, lines);
}
[Fact]
public async Task PersistenceReleaseCanOmitStackTraceLocationWrites()
{
using var workspace = new TestWorkspace();
var assemblyPath = Path.Combine(workspace.Root, "trace-disabled.dll");
await BuildPersistenceStackTraceAssemblyAsync(workspace, assemblyPath, OptimizeOptions.Release, stackTrace: false);
Assert.False(ReferencesScriptContextLocation(assemblyPath));
}
[Fact]
public async Task PersistenceDebugKeepsStackTraceLocationWritesWhenDisabled()
{
using var workspace = new TestWorkspace();
var assemblyPath = Path.Combine(workspace.Root, "debug-trace-disabled.dll");
await BuildPersistenceStackTraceAssemblyAsync(workspace, assemblyPath, OptimizeOptions.Debug, stackTrace: false);
Assert.True(ReferencesScriptContextLocation(assemblyPath));
}
#endif
#if NET8_0
[Fact]
public async Task PersistenceModeRequiresNet9OrLater()
{
using var workspace = new TestWorkspace();
var error = await Assert.ThrowsAsync<PlatformNotSupportedException>(() => workspace.CompileModuleAsync(
"@module(TEST); export func run() { return 42; }",
CompilationMode.Persistence));
Assert.Contains(".NET 9.0", error.Message);
}
#endif
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ReleaseBuildWorksWithHotReloadDisabledOrEnabled(bool enableHotReload)
{
using var workspace = new TestWorkspace();
var (_, domain) = await workspace.CompileModuleAsync(
"@module(TEST); func local(value) { return value + 1; } export func run() { return local(41); }",
enableHotReload: enableHotReload);
ScriptAssert.Equal(42, TestWorkspace.Execute(domain, "run"));
}
[Fact]
public void StackTraceOptionDefaultsToEnabledAndCanBeDisabled()
{
Assert.True(EngineOptions.Default.Optimization.StackTrace);
var options = EngineOptions.Default.WithOptimization(optimization => optimization.StackTrace = false);
Assert.False(options.Optimization.StackTrace);
}
#if NET9_0_OR_GREATER
private static async Task BuildPersistenceStackTraceAssemblyAsync(
TestWorkspace workspace,
string assemblyPath,
OptimizeOptions level,
bool stackTrace)
{
var sourcePath = workspace.WriteSource("main.as",
"""
@module(TEST);
export func run(value) {
var local = value + 1;
if (local > 10) {
return local;
}
return local + 1;
}
""");
var options = EngineOptions.Default
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Persistence)
.WithOptimization(optimization => optimization.Level = level)
.WithOptimization(optimization => optimization.StackTrace = stackTrace)
.WithOutput(output => output.AssemblyFile = assemblyPath)
.WithRuntime(runtime => runtime.ConsoleStdOut = TextWriter.Null)
.WithRuntime(runtime => runtime.ConsoleErrorOut = TextWriter.Null);
var engine = new AuroraEngine(options);
await engine.BuildAsync(sourcePath);
}
private static bool ReferencesScriptContextLocation(string assemblyPath)
{
using var stream = File.OpenRead(assemblyPath);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
foreach (var handle in reader.MemberReferences)
{
var member = reader.GetMemberReference(handle);
if (!string.Equals(reader.GetString(member.Name), nameof(AuroraScript.Runtime.ScriptContext.Location), StringComparison.Ordinal))
{
continue;
}
if (member.Parent.Kind != HandleKind.TypeReference)
{
continue;
}
var type = reader.GetTypeReference((TypeReferenceHandle)member.Parent);
if (string.Equals(reader.GetString(type.Namespace), "AuroraScript.Runtime", StringComparison.Ordinal) &&
string.Equals(reader.GetString(type.Name), nameof(AuroraScript.Runtime.ScriptContext), StringComparison.Ordinal))
{
return true;
}
}
return false;
}
private static string ReadDocumentName(MetadataReader reader, DocumentHandle handle)
{
return reader.GetString(reader.GetDocument(handle).Name);
}
private static List<int> GetVisibleSequencePointLines(MetadataReader reader, DocumentHandle document)
{
var lines = new List<int>();
foreach (var methodHandle in reader.MethodDebugInformation)
{
var method = reader.GetMethodDebugInformation(methodHandle);
foreach (var point in method.GetSequencePoints())
{
var pointDocument = point.Document.IsNil ? method.Document : point.Document;
if (!point.IsHidden && pointDocument == document)
{
lines.Add(point.StartLine);
}
}
}
return lines;
}
#endif
}