Skip to content

Commit 2a6c0bb

Browse files
committed
ObjectFile: update the API to use file paths, add tests
1 parent 8c9b7ec commit 2a6c0bb

20 files changed

+279
-226
lines changed

Cesium.CodeGen.Tests/CodeGenPInvokeTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
using Cesium.TestFramework;
6+
using TruePath;
67

78
namespace Cesium.CodeGen.Tests;
89

@@ -12,7 +13,7 @@ public class CodeGenPInvokeTests : CodeGenTestBase
1213

1314
private static async Task DoTest(string source)
1415
{
15-
var processed = await PreprocessorUtil.DoPreprocess(_mainMockedFilePath, source);
16+
var processed = await PreprocessorUtil.DoPreprocess(new AbsolutePath(_mainMockedFilePath), source);
1617
var assembly = GenerateAssembly(default, processed);
1718

1819
var moduleType = assembly.Modules.Single().GetType("<Module>");

Cesium.CodeGen.Tests/CodeGenTestBase.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using JetBrains.Annotations;
1313
using Mono.Cecil;
1414
using Mono.Cecil.Cil;
15+
using TruePath;
1516
using Yoakke.Streams;
1617
using Yoakke.SynKit.C.Syntax;
1718

@@ -49,7 +50,12 @@ protected static (AssemblyDefinition, byte[]) GenerateAssembly(
4950
string globalTypeFqn = "",
5051
string[]? referencePaths = null)
5152
{
52-
var context = CreateAssembly(runtime, arch, @namespace: @namespace, globalTypeFqn: globalTypeFqn, referencePaths);
53+
var context = CreateAssembly(
54+
runtime,
55+
arch,
56+
@namespace: @namespace,
57+
globalTypeFqn: globalTypeFqn,
58+
referencePaths?.Select(x => new LocalPath(x)).ToArray());
5359
GenerateCode(context, sources);
5460
return EmitAssembly(context);
5561
}
@@ -82,22 +88,22 @@ private static AssemblyContext CreateAssembly(
8288
TargetArchitectureSet targetArchitectureSet = TargetArchitectureSet.Dynamic,
8389
string @namespace = "",
8490
string globalTypeFqn = "",
85-
string[]? referencePaths = null)
91+
LocalPath[]? referencePaths = null)
8692
{
87-
var allReferences = (referencePaths ?? Array.Empty<string>()).ToList();
88-
allReferences.Insert(0, typeof(Console).Assembly.Location);
93+
var allReferences = (referencePaths ?? []).ToList();
94+
allReferences.Insert(0, new LocalPath(typeof(Console).Assembly.Location));
8995

9096
CompilationOptions compilationOptions = new(
9197
targetRuntime ?? CSharpCompilationUtil.DefaultRuntime,
9298
targetArchitectureSet,
9399
ModuleKind.Console,
94-
typeof(Math).Assembly.Location,
95-
typeof(RuntimeHelpers).Assembly.Location,
100+
new LocalPath(typeof(Math).Assembly.Location),
101+
new LocalPath(typeof(RuntimeHelpers).Assembly.Location),
96102
allReferences,
97103
@namespace,
98104
globalTypeFqn,
99-
Array.Empty<string>(),
100-
Array.Empty<string>(),
105+
[],
106+
[],
101107
ProducePreprocessedFile: false,
102108
ProduceAstFile: false);
103109
return AssemblyContext.Create(

Cesium.CodeGen/Cesium.CodeGen.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SPDX-License-Identifier: MIT
1616
<PackageReference Include="Mono.Cecil" />
1717
<PackageReference Include="QuikGraph" />
1818
<PackageReference Include="QuikGraph.Graphviz" />
19+
<PackageReference Include="TruePath" />
1920
</ItemGroup>
2021

2122
<ItemGroup>

Cesium.CodeGen/CompilationOptions.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
// SPDX-License-Identifier: MIT
44

55
using Mono.Cecil;
6+
using TruePath;
67

78
namespace Cesium.CodeGen;
89

910
public record CompilationOptions(
1011
TargetRuntimeDescriptor TargetRuntime,
1112
TargetArchitectureSet TargetArchitectureSet,
1213
ModuleKind ModuleKind,
13-
string CorelibAssembly,
14-
string CesiumRuntime,
15-
IList<string> ImportAssemblies,
14+
LocalPath CorelibAssembly,
15+
LocalPath CesiumRuntime,
16+
IList<LocalPath> ImportAssemblies,
1617
string Namespace,
1718
string GlobalClassFqn,
1819
IList<string> DefineConstants,
19-
IList<string> AdditionalIncludeDirectories,
20+
IList<LocalPath> AdditionalIncludeDirectories,
2021
bool ProducePreprocessedFile,
2122
bool ProduceAstFile);

Cesium.CodeGen/Contexts/AssemblyContext.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ private AssemblyContext(
121121
Module = module;
122122
CompilationOptions = compilationOptions;
123123

124-
MscorlibAssembly = AssemblyDefinition.ReadAssembly(compilationOptions.CorelibAssembly);
125-
CesiumRuntimeAssembly = AssemblyDefinition.ReadAssembly(compilationOptions.CesiumRuntime);
126-
ImportAssemblies = compilationOptions.ImportAssemblies.Select(AssemblyDefinition.ReadAssembly).Union(new[] { MscorlibAssembly, CesiumRuntimeAssembly }).Distinct().ToArray();
124+
MscorlibAssembly = AssemblyDefinition.ReadAssembly(compilationOptions.CorelibAssembly.Value);
125+
CesiumRuntimeAssembly = AssemblyDefinition.ReadAssembly(compilationOptions.CesiumRuntime.Value);
126+
ImportAssemblies = compilationOptions.ImportAssemblies
127+
.Select(x => AssemblyDefinition.ReadAssembly(x.Value))
128+
.Union([MscorlibAssembly, CesiumRuntimeAssembly])
129+
.Distinct().ToArray();
127130
_constantPool = new(
128131
() =>
129132
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using TruePath;
2+
3+
namespace Cesium.Compiler.Tests;
4+
5+
public class JsonObjectFileTests
6+
{
7+
[Theory]
8+
[InlineData("file.json", false)]
9+
[InlineData("file.obj", true)]
10+
public void CorrectExtensions(string fileName, bool result) =>
11+
Assert.Equal(result, JsonObjectFile.IsCorrectExtension(new LocalPath(fileName)));
12+
13+
[Fact]
14+
public void ObjectFileGetsDumpedCorrectly() => Assert.Fail();
15+
16+
[Fact]
17+
public void ObjectFileGetsReadCorrectly() => Assert.Fail();
18+
19+
[Fact]
20+
public void CompilationSucceedsForObjectFile() => Assert.Fail("Migrate me to the integration tests");
21+
}

Cesium.Compiler/Arguments.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class Arguments
2323
[Value(0)]
2424
public IList<string> InputFilePaths { get; init; } = null!;
2525

26-
[Option('c', HelpText = "Compile only into Cessium Object Format (r) (RFC-8259)")]
27-
public bool CompileOnly { get; init; } = false;
26+
[Option('c', HelpText = "Produce a JSON-based object file imitation.")]
27+
public bool ProduceObjectFileImitation { get; init; } = false;
2828

2929
[Option('o', "out")]
3030
public string OutputFilePath { get; init; } = null!;

Cesium.Compiler/Cesium.Compiler.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SPDX-License-Identifier: MIT
1818

1919
<ItemGroup>
2020
<PackageReference Include="CommandLineParser" />
21+
<PackageReference Include="TruePath" />
2122
<PackageReference Include="Yoakke.SynKit.C.Syntax" />
2223
</ItemGroup>
2324

0 commit comments

Comments
 (0)