diff --git a/ExtendedJavaScriptSubset.sln b/ExtendedJavaScriptSubset.sln index d134919..523c309 100644 --- a/ExtendedJavaScriptSubset.sln +++ b/ExtendedJavaScriptSubset.sln @@ -62,6 +62,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{04AB samples\this.js = samples\this.js samples\typeresolving.js = samples\typeresolving.js samples\vec2d.js = samples\vec2d.js + samples\cycled.js = samples\cycled.js EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{FB8F6EE1-1942-46D6-954E-9A1647BBDF10}" diff --git a/samples/cycled.js b/samples/cycled.js new file mode 100644 index 0000000..18ca8e8 --- /dev/null +++ b/samples/cycled.js @@ -0,0 +1,8 @@ +type CycledType = { + x: CycledType; +} +let obj: CycledType = { + x: null; +} +obj.x = obj +print(obj as string) \ No newline at end of file diff --git a/src/Domain/HydraScript.Domain.BackEnd/HydraScript.Domain.BackEnd.csproj b/src/Domain/HydraScript.Domain.BackEnd/HydraScript.Domain.BackEnd.csproj index 7faaaf6..00c662b 100644 --- a/src/Domain/HydraScript.Domain.BackEnd/HydraScript.Domain.BackEnd.csproj +++ b/src/Domain/HydraScript.Domain.BackEnd/HydraScript.Domain.BackEnd.csproj @@ -7,4 +7,8 @@ true + + false + + diff --git a/src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/AsString.cs b/src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/AsString.cs index 83e7429..85bf6d0 100644 --- a/src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/AsString.cs +++ b/src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/AsString.cs @@ -3,25 +3,34 @@ namespace HydraScript.Domain.BackEnd.Impl.Instructions.WithAssignment; -public class AsString(IValue value) : Simple(value) +public partial class AsString(IValue value) : Simple(value) { + private static readonly AsStringSerializationContext AsStringJsonContext = new(new JsonSerializerOptions + { + WriteIndented = true, + ReferenceHandler = ReferenceHandler.IgnoreCycles, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals + }); + public override IAddress Execute(IExecuteParams executeParams) { var frame = executeParams.Frames.Peek(); frame[Left!] = JsonSerializer.Serialize( - Right.right!.Get(frame), - new JsonSerializerOptions - { - WriteIndented = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - ReferenceHandler = ReferenceHandler.IgnoreCycles, - NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals - } - ); + value: Right.right!.Get(frame)!, + AsStringJsonContext.Object); return Address.Next; } protected override string ToStringInternal() => $"{Left} = {Right.right} as string"; + + [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(List))] + [JsonSerializable(typeof(Dictionary))] + [JsonSerializable(typeof(bool))] + [JsonSerializable(typeof(double))] + [JsonSerializable(typeof(string))] + private partial class AsStringSerializationContext : JsonSerializerContext; } \ No newline at end of file diff --git a/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/HydraScript.Infrastructure.LexerRegexGenerator.csproj b/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/HydraScript.Infrastructure.LexerRegexGenerator.csproj index 172aa6d..57b3ef2 100644 --- a/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/HydraScript.Infrastructure.LexerRegexGenerator.csproj +++ b/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/HydraScript.Infrastructure.LexerRegexGenerator.csproj @@ -22,4 +22,8 @@ + + false + + diff --git a/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.TokenTypes.cs b/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.TokenTypes.cs index e166d3e..c9fbd3c 100644 --- a/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.TokenTypes.cs +++ b/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.TokenTypes.cs @@ -1,16 +1,9 @@ -using System.Diagnostics.CodeAnalysis; -using System.Text.Json; using System.Text.Json.Serialization; namespace HydraScript.Infrastructure.LexerRegexGenerator; public partial class PatternGenerator { - private static readonly JsonSerializerOptions JsonSerializerOptions = new() - { - Converters = { new TokenTypesReadConverter() } - }; - private record TokenType( string Tag, string Pattern, @@ -19,31 +12,7 @@ private record TokenType( public string GetNamedRegex() => $"(?<{Tag}>{Pattern})"; } - [ExcludeFromCodeCoverage] - private class TokenTypesReadConverter : JsonConverter> - { - public override IEnumerable Read( - ref Utf8JsonReader reader, - Type typeToConvert, - JsonSerializerOptions options) - { - var root = JsonElement.ParseValue(ref reader); - var tokenTypes = root.EnumerateArray() - .Select(element => - { - var tag = element.GetProperty("tag").GetString()!; - var pattern = element.GetProperty("pattern").GetString()!; - var priority = element.GetProperty("priority").GetInt32(); - - return new TokenType(tag, pattern, priority); - }) - .OrderBy(x => x.Priority); - return tokenTypes; - } - - public override void Write( - Utf8JsonWriter writer, - IEnumerable value, - JsonSerializerOptions options) => throw new NotSupportedException(); - } + [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] + [JsonSerializable(typeof(IEnumerable))] + private partial class PatternGeneratorContext : JsonSerializerContext; } \ No newline at end of file diff --git a/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.cs b/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.cs index 62f9202..afab806 100644 --- a/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.cs +++ b/src/Infrastructure/HydraScript.Infrastructure.LexerRegexGenerator/PatternGenerator.cs @@ -64,9 +64,10 @@ private static void GenerateCode( { foreach (var info in containerInfos) { - var tokenTypes = JsonSerializer.Deserialize>( + var tokenTypes = JsonSerializer.Deserialize( info.Json, - JsonSerializerOptions)! + PatternGeneratorContext.Default.IEnumerableTokenType)! + .OrderBy(x => x.Priority) .Concat([new TokenType("ERROR", @"\S+", int.MaxValue)]); var pattern = string.Join('|', tokenTypes.Select(t => t.GetNamedRegex())); diff --git a/src/Infrastructure/HydraScript.Infrastructure/HydraScript.Infrastructure.csproj b/src/Infrastructure/HydraScript.Infrastructure/HydraScript.Infrastructure.csproj index 75844c0..7056f88 100644 --- a/src/Infrastructure/HydraScript.Infrastructure/HydraScript.Infrastructure.csproj +++ b/src/Infrastructure/HydraScript.Infrastructure/HydraScript.Infrastructure.csproj @@ -28,4 +28,8 @@ + + false + + diff --git a/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs b/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs index f6d61f0..44bd978 100644 --- a/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs +++ b/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs @@ -6,17 +6,12 @@ namespace HydraScript.Infrastructure; -internal class TokenTypesProvider : ITokenTypesProvider +internal partial class TokenTypesProvider : ITokenTypesProvider { - private static readonly JsonSerializerOptions JsonSerializerOptions = new() - { - Converters = { new TokenTypesReadConverter() } - }; - public IEnumerable GetTokenTypes() => - JsonSerializer.Deserialize>( + JsonSerializer.Deserialize( TokenTypesJson.String, - JsonSerializerOptions)!; + TokenTypesProviderContext.Default.IEnumerableTokenType)!; [ExcludeFromCodeCoverage] private class TokenTypesReadConverter : JsonConverter> @@ -52,4 +47,8 @@ public override void Write( private record PrioritizedTokenType(TokenType TokenType, int Priority); } + + [JsonSourceGenerationOptions(Converters = [typeof(TokenTypesReadConverter)])] + [JsonSerializable(typeof(IEnumerable))] + private partial class TokenTypesProviderContext : JsonSerializerContext; } \ No newline at end of file diff --git a/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj b/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj index beb70b1..4136bb7 100644 --- a/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj +++ b/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj @@ -40,6 +40,10 @@ Samples\ceil.js Always + + Samples\cycled.js + Always + Samples\defaultarray.js Always diff --git a/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs b/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs index bce5ee2..52f2f27 100644 --- a/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs +++ b/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs @@ -24,6 +24,7 @@ public void Invoke_NoError_ReturnCodeIsZero(string fileName) "arraddremove.js", "arreditread.js", "ceil.js", + "cycled.js", "defaultarray.js", "equals.js", "exprtest.js",