Skip to content

Commit

Permalink
Fix issues with manipulating elementary types
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSlamecka committed Feb 15, 2024
1 parent 53a4c96 commit f69602c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/runtime_lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ function write_bool(ptr, from_bits, value)

segment = ptr.traverser.segments[ptr.segment]
# println("Writing value ", value, " at segment ", ptr.segment, ", byte ", ptr.offset * 8 + from ÷ 8)
byte = unsafe_load!(Ptr{type}(pointer(segment) + 8 * ptr.offset + byte_position))
byte = unsafe_load(Ptr{UInt8}(pointer(segment) + 8 * ptr.offset + byte_position))
# make the desired position zero and then place `value` to it
byte = byte & ~(UInt8(1) << in_byte_position) | (value << in_byte_position)
unsafe_store!(Ptr{type}(pointer(segment) + 8 * ptr.offset + byte_position), byte)
unsafe_store!(Ptr{UInt8}(pointer(segment) + 8 * ptr.offset + byte_position), byte)
end

# "Bits" types except for bool.
Expand Down
16 changes: 8 additions & 8 deletions src/schema.capnp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1445,35 +1445,35 @@ if !@isdefined(capnp); eval(:(module capnp end)); end
Capnp.write_bits(ptr, 0, UInt16, 1) # union discriminant
end
function Value_getInt8(ptr)
value = Capnp.read_bits(ptr, 2, UInt8)
value = Capnp.read_bits(ptr, 2, Int8)
value
end
function Value_setInt8(ptr, value)
Capnp.write_bits(ptr, 2, UInt8, value)
Capnp.write_bits(ptr, 2, Int8, value)
Capnp.write_bits(ptr, 0, UInt16, 2) # union discriminant
end
function Value_getInt16(ptr)
value = Capnp.read_bits(ptr, 2, UInt16)
value = Capnp.read_bits(ptr, 2, Int16)
value
end
function Value_setInt16(ptr, value)
Capnp.write_bits(ptr, 2, UInt16, value)
Capnp.write_bits(ptr, 2, Int16, value)
Capnp.write_bits(ptr, 0, UInt16, 3) # union discriminant
end
function Value_getInt32(ptr)
value = Capnp.read_bits(ptr, 4, UInt32)
value = Capnp.read_bits(ptr, 4, Int32)
value
end
function Value_setInt32(ptr, value)
Capnp.write_bits(ptr, 4, UInt32, value)
Capnp.write_bits(ptr, 4, Int32, value)
Capnp.write_bits(ptr, 0, UInt16, 4) # union discriminant
end
function Value_getInt64(ptr)
value = Capnp.read_bits(ptr, 8, UInt64)
value = Capnp.read_bits(ptr, 8, Int64)
value
end
function Value_setInt64(ptr, value)
Capnp.write_bits(ptr, 8, UInt64, value)
Capnp.write_bits(ptr, 8, Int64, value)
Capnp.write_bits(ptr, 0, UInt16, 5) # union discriminant
end
function Value_getUint8(ptr)
Expand Down
8 changes: 4 additions & 4 deletions src/schema_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ capnp_sizeof(::SchemaFloat32) = 4
capnp_sizeof(::SchemaFloat64) = 8

# capnp_type_to_bits_type(::SchemaTypeBool) = Bool see capnp_sizeof
capnp_type_to_bits_type(::SchemaInt8) = UInt8
capnp_type_to_bits_type(::SchemaInt16) = UInt16
capnp_type_to_bits_type(::SchemaInt32) = UInt32
capnp_type_to_bits_type(::SchemaInt64) = UInt64
capnp_type_to_bits_type(::SchemaInt8) = Int8
capnp_type_to_bits_type(::SchemaInt16) = Int16
capnp_type_to_bits_type(::SchemaInt32) = Int32
capnp_type_to_bits_type(::SchemaInt64) = Int64
capnp_type_to_bits_type(::SchemaUInt8) = UInt8
capnp_type_to_bits_type(::SchemaUInt16) = UInt16
capnp_type_to_bits_type(::SchemaUInt32) = UInt32
Expand Down
7 changes: 7 additions & 0 deletions test/elementary.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@0x930cadbc14628588;

struct Test {
booleanFalse @0 :Bool;
booleanTrue @1 :Bool;
signed64 @2 :Int64;
}
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ end
@test result == expected
end

@testset "Elementary types" begin
run(`capnpc -o./capnpc-jl test/elementary.capnp`)
include("elementary.capnp.jl")

# writing part
message = Capnp.AllocMessageBuilder()
test = initRoot_Test(message)
Test_setBooleanFalse(test, false)
Test_setBooleanTrue(test, true)
Test_setSigned64(test, -1)

# finish writing and flush into buffer for reading
buffer = IOBuffer()
writeMessageToStream(message, buffer)
seek(buffer, 0)

# reading part
message = Capnp.MessageReader(buffer)
test = root_Test(message)

booleanFalse = Test_getBooleanFalse(test)
@test booleanFalse == false

booleanTrue = Test_getBooleanTrue(test)
@test booleanTrue == true

signed64 = Test_getSigned64(test)
@test signed64 == -1
end

@testset "Lists" begin
run(`capnpc -o./capnpc-jl test/lists.capnp`)
include("lists.capnp.jl")
Expand Down

0 comments on commit f69602c

Please sign in to comment.