Skip to content

Commit

Permalink
Version 0.10.2
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
1Axen committed Mar 27, 2024
1 parent cef48d5 commit dcee734
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build/.darklua.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
"identifier": "VERSION",
"rule": "inject_global_value",
"value": "0.10.1"
"value": "0.10.2"
}
]
}
2 changes: 1 addition & 1 deletion plugin/.darklua.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
"identifier": "VERSION",
"rule": "inject_global_value",
"value": "0.10.1"
"value": "0.10.2"
}
]
}
21 changes: 12 additions & 9 deletions src/Generator/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()

Expand Down
11 changes: 11 additions & 0 deletions test/Sources/Test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions test/Test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)

0 comments on commit dcee734

Please sign in to comment.