Skip to content

Commit 4c8b841

Browse files
committed
Source generator support
1 parent 4ad19bc commit 4c8b841

23 files changed

+5307
-804
lines changed

Directory.Packages.props

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,33 @@
66
<ItemGroup>
77
<PackageVersion Include="BenchmarkDotNet" Version="0.13.10" />
88
<PackageVersion Include="Esprima" Version="3.0.2" />
9+
<PackageVersion Include="Fluid.Core" Version="2.5.0" PrivateAssets="all" GeneratePathProperty="true" />
910
<PackageVersion Include="Flurl.Http.Signed" Version="3.2.4" />
1011
<PackageVersion Include="Jurassic" Version="3.2.7" />
11-
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.110" />
12+
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.112" />
13+
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
14+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
1215
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
16+
<PackageVersion Include="Microsoft.Extensions.FileProviders.Abstractions" Version="1.1.1" PrivateAssets="all" GeneratePathProperty="true" />
1317
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
1418
<PackageVersion Include="MongoDB.Bson.signed" Version="2.19.0" />
19+
<PackageVersion Include="NUnit" Version="3.14.0" />
20+
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
1521
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
1622
<PackageVersion Include="NiL.JS" Version="2.5.1674" />
1723
<PackageVersion Include="NodaTime" Version="3.1.9" />
18-
<PackageVersion Include="NUnit" Version="3.14.0" />
19-
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
24+
<PackageVersion Include="Parlot" Version="0.0.24" PrivateAssets="all" GeneratePathProperty="true" />
2025
<PackageVersion Include="SharpZipLib" Version="1.4.0" />
2126
<PackageVersion Include="Spectre.Console.Cli" Version="0.45.0" />
27+
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.3" />
28+
<PackageVersion Include="System.Text.Encodings.Web" Version="6.0.0" PrivateAssets="all" GeneratePathProperty="true" />
2229
<PackageVersion Include="System.Text.Json" Version="6.0.8" />
2330
<PackageVersion Include="Test262Harness" Version="0.0.22" />
31+
<PackageVersion Include="Verify.SourceGenerators" Version="2.2.0" />
32+
<PackageVersion Include="Verify.XUnit" Version="22.5.0" />
33+
<PackageVersion Include="YantraJS.Core" Version="1.2.206" />
2434
<PackageVersion Include="xunit" Version="2.6.2" />
2535
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" PrivateAssets="all" />
26-
<PackageVersion Include="YantraJS.Core" Version="1.2.206" />
2736
</ItemGroup>
2837
<ItemGroup>
2938
<GlobalPackageReference Include="GitHubActionsTestLogger" Version="2.3.3" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
4+
namespace Jint.SourceGenerators;
5+
6+
internal class FunctionDefinition : IComparable
7+
{
8+
public FunctionDefinition(IMethodSymbol method, AttributeData attribute)
9+
{
10+
var attributes = SourceGenerationHelper.GetAttributes((AttributeSyntax) attribute.ApplicationSyntaxReference!.GetSyntax());
11+
attributes.TryGetValue("Name", out var name);
12+
attributes.TryGetValue("Length", out var length);
13+
14+
if (string.IsNullOrWhiteSpace(name))
15+
{
16+
name = method.Name;
17+
if (char.IsUpper(name[0]))
18+
{
19+
name = char.ToLowerInvariant(name[0]) + name.Substring(1);
20+
}
21+
}
22+
23+
Name = name ?? throw new InvalidOperationException("Could not get name");
24+
ClrName = method.Name;
25+
26+
ProvideThis = method.Parameters.Any(x => x.Name.StartsWith("thisObj"));
27+
ProvideArguments = method.Parameters.Any(x => x.Name == "args" || x.Name.StartsWith("arguments"));
28+
29+
IsStatic = method.IsStatic;
30+
31+
if (string.IsNullOrWhiteSpace(length))
32+
{
33+
Length = method.Parameters.Length;
34+
if (ProvideThis)
35+
{
36+
Length--;
37+
}
38+
}
39+
else
40+
{
41+
Length = Convert.ToInt32(length);
42+
}
43+
44+
ParametersString = "";
45+
var needsComma = false;
46+
if (ProvideThis)
47+
{
48+
needsComma = true;
49+
ParametersString += "thisObject";
50+
}
51+
52+
var tmp = method.Parameters.Length;
53+
if (ProvideThis)
54+
{
55+
tmp--;
56+
}
57+
58+
if (ProvideArguments)
59+
{
60+
tmp--;
61+
}
62+
63+
for (var i = 0; i < tmp; ++i)
64+
{
65+
if (i > 0 || needsComma)
66+
{
67+
ParametersString += ", ";
68+
}
69+
70+
ParametersString += "arguments.At(" + i + ")";
71+
}
72+
73+
// arguments always last
74+
if (ProvideArguments)
75+
{
76+
if (needsComma)
77+
{
78+
ParametersString += ", ";
79+
}
80+
ParametersString += "arguments";
81+
}
82+
}
83+
84+
public string Name { get; }
85+
public string ClrName { get; }
86+
public bool IsStatic { get; }
87+
public int Length { get; }
88+
89+
public bool ProvideThis { get; }
90+
public bool ProvideArguments { get; }
91+
92+
public string ParametersString { get; }
93+
94+
public int CompareTo(object obj)
95+
{
96+
return string.Compare(Name, (obj as FunctionDefinition)?.Name, StringComparison.Ordinal);
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>latest</LangVersion>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all"/>
14+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all"/>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Fluid.Core" PrivateAssets="all" GeneratePathProperty="true"/>
19+
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" PrivateAssets="all" GeneratePathProperty="true"/>
20+
<PackageReference Include="Parlot" GeneratePathProperty="true"/>
21+
<PackageReference Include="System.Text.Encodings.Web" PrivateAssets="all" GeneratePathProperty="true"/>
22+
</ItemGroup>
23+
24+
<PropertyGroup>
25+
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
26+
</PropertyGroup>
27+
28+
<Target Name="GetDependencyTargetPaths">
29+
<ItemGroup>
30+
<TargetPathWithTargetPlatformMoniker Include="$(PkgFluid_Core)\lib\netstandard2.0\Fluid.dll" IncludeRuntimeDependency="false"/>
31+
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_Extensions_FileProviders_Abstractions)\lib\netstandard1.0\Microsoft.Extensions.FileProviders.Abstractions.dll" IncludeRuntimeDependency="false"/>
32+
<TargetPathWithTargetPlatformMoniker Include="$(PkgParlot)\lib\netstandard2.0\Parlot.dll" IncludeRuntimeDependency="false"/>
33+
<TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Text_Encodings_Web)\lib\netstandard2.0\System.Text.Encodings.Web.dll" IncludeRuntimeDependency="false"/>
34+
</ItemGroup>
35+
</Target>
36+
37+
<ItemGroup>
38+
<EmbeddedResource Include="Templates\*.liquid"/>
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<Compile Include="..\Jint\HighPerformance\*.cs" LinkBase="Referenced"/>
43+
</ItemGroup>
44+
45+
</Project>
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Microsoft.CodeAnalysis.CSharp.Syntax;
2+
3+
namespace Jint.SourceGenerators;
4+
5+
internal class ObjectDefinition
6+
{
7+
public ObjectDefinition(
8+
ClassDeclarationSyntax syntax,
9+
List<FunctionDefinition> functions,
10+
List<PropertyDefinition> properties)
11+
{
12+
Namespace = SourceGenerationHelper.GetNamespace(syntax);
13+
Name = syntax.Identifier.ToString();
14+
Syntax = syntax;
15+
Functions = functions;
16+
Properties = properties;
17+
18+
PropertyLookup = SourceGenerationHelper.GenerateLookups(false, this, "str", static (sb, indentStr, item) =>
19+
{
20+
sb.Append(indentStr);
21+
sb.AppendLine(" {");
22+
sb.Append(indentStr);
23+
sb.Append(" ").Append("match").Append(" = ");
24+
sb.Append(item.Accessor);
25+
sb.AppendLine(";");
26+
}, "_property", 12);
27+
28+
PropertySet = SourceGenerationHelper.GenerateLookups(false, this, "str", static (sb, indentStr, item) =>
29+
{
30+
sb.Append(indentStr);
31+
sb.AppendLine(" {");
32+
sb.Append(indentStr);
33+
sb.Append(" ").Append("match").Append(" = ");
34+
sb.Append(item.Accessor);
35+
sb.AppendLine(";");
36+
}, "_property", 12);
37+
38+
ValueLookup = SourceGenerationHelper.GenerateLookups(true, this, "str", static (sb, indentStr, item) =>
39+
{
40+
sb.Append(indentStr);
41+
sb.AppendLine(" {");
42+
sb.Append(indentStr);
43+
sb.Append(" ").Append(item.Accessor).Append("_backingField").Append(" = ");
44+
sb.Append("value");
45+
sb.AppendLine(";");
46+
}, "", 12);
47+
}
48+
49+
public string Namespace { get; }
50+
51+
public string Name { get; }
52+
public ClassDeclarationSyntax Syntax { get; }
53+
54+
public List<FunctionDefinition> Functions { get; }
55+
public List<PropertyDefinition> Properties { get; }
56+
public string PropertyLookup { get; }
57+
public string PropertySet { get; }
58+
public string ValueLookup { get; }
59+
60+
}

0 commit comments

Comments
 (0)