Skip to content

Commit 73ed7ff

Browse files
committed
fixup! Add support for automatically calling unsafe_load() in getproperty()
1 parent 89be1a6 commit 73ed7ff

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

test/generators.jl

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,53 @@ end
251251
end
252252

253253
@testset "Struct getproperty()/setproperty!()" begin
254+
options = Dict("general" => Dict{String, Any}("auto_mutability" => true,
255+
"auto_mutability_with_new" => false,
256+
"auto_mutability_includelist" => ["WithFields"]),
257+
"codegen" => Dict{String, Any}("field_access_method_list" => ["WithFields", "Other"]))
258+
259+
# Test the default getproperty()/setproperty!() behaviour
260+
mktemp() do path, io
261+
options["general"]["output_file_path"] = path
262+
ctx = create_context([joinpath(@__DIR__, "include/struct-properties.h")], get_default_args(), options)
263+
build!(ctx)
264+
265+
println(read(path, String))
266+
267+
m = Module()
268+
Base.include(m, path)
269+
270+
# We now have to run in the latest world to use the new definitions
271+
Base.invokelatest() do
272+
obj = m.WithFields(1, C_NULL, m.Other(42), C_NULL, m.TypedefStruct(1), (1, 1))
273+
274+
GC.@preserve obj begin
275+
obj_ptr = Ptr{m.WithFields}(pointer_from_objref(obj))
276+
277+
# The default getproperty() should basically always return a
278+
# pointer to the field (except for bitfields, which are tested
279+
# elsewhere).
280+
@test obj_ptr.int_value isa Ptr{Cint}
281+
@test obj_ptr.int_ptr isa Ptr{Ptr{Cint}}
282+
@test obj_ptr.struct_value isa Ptr{m.Other}
283+
@test obj_ptr.typedef_struct_value isa Ptr{m.TypedefStruct}
284+
@test obj_ptr.array isa Ptr{NTuple{2, Cint}}
285+
286+
# Sanity test
287+
int_value = unsafe_load(obj_ptr.int_value)
288+
@test int_value == obj.int_value
289+
290+
# Test setproperty!()
291+
obj_ptr.int_value = int_value + 1
292+
@test unsafe_load(obj_ptr.int_value) == int_value + 1
293+
end
294+
end
295+
end
296+
254297
# Test the auto_field_dereference option
255298
mktemp() do path, io
256-
options = Dict("general" => Dict{String, Any}("output_file_path" => path,
257-
"auto_mutability" => true,
258-
"auto_mutability_with_new" => false,
259-
"auto_mutability_includelist" => ["WithFields"]),
260-
"codegen" => Dict{String, Any}("field_access_method_list" => ["WithFields", "Other"],
261-
"auto_field_dereference" => true))
299+
options["general"]["output_file_path"] = path
300+
options["codegen"]["auto_field_dereference"] = true
262301
ctx = create_context([joinpath(@__DIR__, "include/struct-properties.h")], get_default_args(), options)
263302
build!(ctx)
264303

0 commit comments

Comments
 (0)