From dcee734c855800bf215cd680acebe2e904e14377 Mon Sep 17 00:00:00 2001 From: DoomIsEternal <65947351+DoomIsEternal@users.noreply.github.com> Date: Thu, 28 Mar 2024 01:35:26 +0200 Subject: [PATCH] Version 0.10.2 * Standalone write not returns its own buffer instead of taking a user provided buffer * Fix tagged enum writing index at the wrong offset * Fix standalone setting read cursor to the end of the buffer --- build/.darklua.json | 2 +- plugin/.darklua.json | 2 +- src/Generator/init.luau | 21 ++++++++++++--------- test/Sources/Test.txt | 11 +++++++++++ test/Test.luau | 29 +++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/build/.darklua.json b/build/.darklua.json index 3f38c75..33cf420 100644 --- a/build/.darklua.json +++ b/build/.darklua.json @@ -26,7 +26,7 @@ { "identifier": "VERSION", "rule": "inject_global_value", - "value": "0.10.1" + "value": "0.10.2" } ] } \ No newline at end of file diff --git a/plugin/.darklua.json b/plugin/.darklua.json index 3f38c75..33cf420 100644 --- a/plugin/.darklua.json +++ b/plugin/.darklua.json @@ -26,7 +26,7 @@ { "identifier": "VERSION", "rule": "inject_global_value", - "value": "0.10.1" + "value": "0.10.2" } ] } \ No newline at end of file diff --git a/src/Generator/init.luau b/src/Generator/init.luau index aea263e..7e14ca9 100644 --- a/src/Generator/init.luau +++ b/src/Generator/init.luau @@ -225,6 +225,7 @@ function Declarations.TagEnum(Declaration: Parser.TagEnumDeclaration, Read: Bloc local TagIndex = `{Variable}.{Tag}` Types.u8.Read(`local Tag`, Read) + local Allocation = Write:Allocate(1) Read = Read:Compare("Tag", "0", "Equals") Write = Write:Compare(TagIndex, Header, "Equals") @@ -238,7 +239,10 @@ function Declarations.TagEnum(Declaration: Parser.TagEnumDeclaration, Read: Bloc Write = Write:Branch("Conditional", TagIndex, Name, "Equals") end - Types.u8.Write(tostring(Index - 1), Write) + + Write:_lineFront(`buffer.writeu8(SendBuffer, {Allocation}, {Index - 1})`) + Write:Advance(1) + Generators.UserType(Variant, Read, Write, Variable) Read:Line(`{TagIndex} = {Name}`) end @@ -877,9 +881,8 @@ function Generators.Export(Declaration: Type) local Identifier = Value.Identifier local _, Values, Export, Returns = Generators.LuauType(Declaration) - local Arguments = "Buffer: buffer" - local Read = Blocks.Function("Read", Arguments, `({Export})`, true) - local Write = Blocks.Function("Write", `{Arguments}, {Values}`, `()`, true) + local Read = Blocks.Function("Read", "Buffer: buffer", `({Export})`, true) + local Write = Blocks.Function("Write", `{Values}`, `(buffer)`, true) local Body = Builder.new() local Indent = GetScopeIndent() @@ -893,16 +896,13 @@ function Generators.Export(Declaration: Type) "local PreviousCursor = RecieveCursor\n", "local PreviousBuffer = RecieveBuffer\n", "local PreviousInstanceCursor = RecieveInstanceCursor\n", - "RecieveCursor = buffer.len(Buffer)\n", + "RecieveCursor = 0\n", "RecieveBuffer = Buffer\n", }, 1) Write:Lines({ "local Previous = Save()\n", - "SendSize = buffer.len(Buffer)\n", - "SendCursor = SendSize\n", - "SendOffset = SendSize\n", - "SendBuffer = Buffer\n", + "Load()\n", }, 1) Read:Advance(#Read.Content - 1) @@ -925,10 +925,13 @@ function Generators.Export(Declaration: Type) "RecieveInstanceCursor = PreviousInstanceCursor\n", }, 1) + Write:Line("local Result = SendBuffer") Write:Line(`Load(Previous)`) --> End export Read:Return(Returns) + Write:Return("Result") + Read:End():Wrap("", ",") Write:End() diff --git a/test/Sources/Test.txt b/test/Sources/Test.txt index 5d20de3..d3312af 100644 --- a/test/Sources/Test.txt +++ b/test/Sources/Test.txt @@ -49,6 +49,17 @@ enum Event = "Type" { } } +export struct Standalone { + One: u8, + Two: u8, + Three: u8, + Event: Event, + Nested: struct { + Four: string, + Five: string + } +} + type Number = u8 struct Example { Field: u8, diff --git a/test/Test.luau b/test/Test.luau index 2bc23f6..aa6c101 100644 --- a/test/Test.luau +++ b/test/Test.luau @@ -371,6 +371,17 @@ local function InvokeAndExpectTuple(Name: string, ClientFunction: any, ServerFun OkPrint(`Test "{Name}" passed!`) end +local function RunClosure(Name: string, Closure: () -> ()) + PrettyPrint("cyan", `Testing: {Name}...`, true) + + local Success, Error = pcall(Closure) + if not Success then + ErrorPrint(Error) + end + + OkPrint(`Test "{Name}" passed!`) +end + --> Tests local Instance = setmetatable({ __typeof = "Instance", @@ -488,6 +499,24 @@ FireAndExpect("TagEnum Join", Server.PlayerEvent, Client.PlayerEvent, {Type = "J FireAndExpect("TagEnum Chat", Server.PlayerEvent, Client.PlayerEvent, {Type = "Chat", UserId = 5, Message = "Hello"}) FireAndExpect("TagEnum Leave", Server.PlayerEvent, Client.PlayerEvent, {Type = "Leave", UserId = 5}) +RunClosure("Standalone", function() + local Original = { + One = 1, + Two = 2, + Three = 3, + Event = {Type = "Join", Name = "JohnDoe", UserId = 1}, + Nested = { + Four = "ABC", + Five = "DEF" + } + } + + local Serialized = Server.Standalone.Write(Original) + local Deserialized = Server.Standalone.Read(Serialized) + + assert(CompareTables(Original, Deserialized), "Deserialized doesn't match original") +end) + PrettyPrint("green", `All tests passed successfully!`) process.exit(0) \ No newline at end of file