Skip to content

Commit 7967b0f

Browse files
committed
Switch to C test
1 parent 329f53f commit 7967b0f

File tree

7 files changed

+100
-33
lines changed

7 files changed

+100
-33
lines changed

test/gcext/Foreign/Manifest.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
julia_version = "1.9.0-DEV"
4+
manifest_format = "2.0"
5+
project_hash = "7b70172a2edbdc772ed789e79d4411d7528eae86"
6+
7+
[[deps.Libdl]]
8+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

test/gcext/Foreign/Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = "Foreign"
2+
uuid = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4"
3+
version = "0.1.0"
4+
5+
[deps]
6+
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
#include "julia.h"
4+
#include "julia_gcext.h"
5+
6+
int nmarks = 0;
7+
int nsweeps = 0;
8+
9+
uintptr_t mark(jl_ptls_t ptls, jl_value_t *p)
10+
{
11+
nmarks += 1;
12+
return 0;
13+
}
14+
15+
void sweep(jl_value_t *p)
16+
{
17+
nsweeps++;
18+
}
19+
20+
void init_dt_gc(jl_datatype_t *dt)
21+
{
22+
jl_reinit_foreign_type(dt, mark, sweep);
23+
nmarks = nsweeps = 0;
24+
}
25+
26+
int nmark_counter()
27+
{
28+
return nmarks;
29+
}
30+
31+
int nsweep_counter()
32+
{
33+
return nsweeps;
34+
}

test/gcext/Foreign/src/Foreign.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
module Foreign
4+
5+
using Libdl
6+
7+
const foreignlib = joinpath(dirname(joinpath(@__DIR__)), "deps", "foreignlib.so")
8+
9+
const FObj = ccall(:jl_new_foreign_type, Any, (Symbol, Module, Any, Any, Any, Cint, Cint),
10+
:FObj, Foreign, Any, C_NULL, C_NULL, 0, 0)
11+
12+
FObj() = ccall(:jl_new_struct_uninit, Any, (Any,), FObj)
13+
14+
get_nmark() = ccall((:nmark_counter, foreignlib), Cint, ())
15+
get_nsweep() = ccall((:nsweep_counter, foreignlib), Cint, ())
16+
17+
function __init__()
18+
ccall((:init_dt_gc, foreignlib), Cvoid, (Any,), FObj)
19+
end
20+
21+
end # module Foreign

test/gcext/Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,42 @@ SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
1919
# get the executable suffix, if any
2020
EXE := $(suffix $(abspath $(JULIA)))
2121

22+
DYLIB := .so
23+
2224
# get compiler and linker flags. (see: `contrib/julia-config.jl`)
2325
JULIA_CONFIG := $(JULIA) -e 'include(joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "julia-config.jl"))' --
2426
CPPFLAGS_ADD :=
2527
CFLAGS_ADD = $(shell $(JULIA_CONFIG) --cflags)
2628
LDFLAGS_ADD = -lm $(shell $(JULIA_CONFIG) --ldflags --ldlibs)
29+
DYLIBFLAGS := --shared -fPIC
2730

2831
DEBUGFLAGS += -g
2932

3033
#=============================================================================
3134

32-
release: $(BIN)/gcext$(EXE)
33-
debug: $(BIN)/gcext-debug$(EXE)
35+
release: $(BIN)/gcext$(EXE) $(BIN)/Foreign/deps/foreignlib$(DYLIB)
36+
debug: $(BIN)/gcext-debug$(EXE) $(BIN)/Foreign/deps/foreignlib-debug$(DYLIB)
3437

3538
$(BIN)/gcext$(EXE): $(SRCDIR)/gcext.c
3639
$(CC) $^ -o $@ $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
3740

3841
$(BIN)/gcext-debug$(EXE): $(SRCDIR)/gcext.c
3942
$(CC) $^ -o $@ $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS) $(DEBUGFLAGS)
4043

44+
$(BIN)/Foreign/deps/foreignlib$(DYLIB): $(SRCDIR)/Foreign/deps/foreignlib.c
45+
$(CC) $^ -o $@ $(DYLIBFLAGS) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
46+
47+
$(BIN)/Foreign/deps/foreignlib-debug$(DYLIB): $(SRCDIR)/Foreign/deps/foreignlib.c
48+
$(CC) $^ -o $@ $(DYLIBFLAGS) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS) $(DEBUGFLAGS)
49+
4150
ifneq ($(abspath $(BIN)),$(abspath $(SRCDIR)))
4251
# for demonstration purposes, our demo code is also installed
4352
# in $BIN, although this would likely not be typical
4453
$(BIN)/LocalModule.jl: $(SRCDIR)/LocalModule.jl
4554
cp $< $@
4655
endif
4756

48-
check: $(BIN)/gcext$(EXE) $(BIN)/LocalTest.jl
57+
check: $(BIN)/gcext$(EXE) $(BIN)/LocalTest.jl $(BIN)/Foreign/deps/foreignlib$(DYLIB)
4958
$(JULIA) --depwarn=error $(SRCDIR)/gcext-test.jl $<
5059
@echo SUCCESS
5160

test/gcext/gcext-test.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# tests the output of the embedding example is correct
44
using Test
5+
using Pkg
56

67
if Sys.iswindows()
78
# libjulia needs to be in the same directory as the embedding executable or in path
@@ -40,3 +41,21 @@ end
4041
@test checknum(lines[6], r"([0-9]+) corrupted auxiliary roots",
4142
n -> n == 0)
4243
end
44+
45+
@testset "Package with foreign type" begin
46+
load_path = joinpath(@__DIR__, "Foreign")
47+
push!(LOAD_PATH, load_path)
48+
try
49+
(@eval (using Foreign))
50+
@test Base.invokelatest(Foreign.get_nmark) == 0
51+
@test Base.invokelatest(Foreign.get_nsweep) == 0
52+
x = [Base.invokelatest(Foreign.FObj) for _ in 1:10]
53+
GC.gc(true)
54+
x = nothing
55+
GC.gc(true)
56+
@test Base.invokelatest(Foreign.get_nmark) > 0
57+
@test Base.invokelatest(Foreign.get_nsweep) > 0
58+
finally
59+
filter!(()(load_path), LOAD_PATH)
60+
end
61+
end

test/precompile.jl

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,35 +1554,5 @@ precompile_test_harness("issue #46296") do load_path
15541554
(@eval (using CodeInstancePrecompile))
15551555
end
15561556

1557-
precompile_test_harness("foreign types") do path
1558-
write(joinpath(load_path, "Foreign.jl"),
1559-
"""
1560-
module Foreign
1561-
1562-
export FObj
1563-
1564-
const FObj = ccall(:jl_new_foreign_type, Any, (Symbol, Module, Any, Any, Any, Cint, Cint),
1565-
:FObj, Foreign, Any, C_NULL, C_NULL, 0, 0)
1566-
1567-
FObj() = ccall(:jl_new_struct_uninit, Any, (Any,), FObj)
1568-
1569-
const nmark = Ref(0)
1570-
const nsweep = Ref(0)
1571-
inc_nmark() = (nmark[] += 1; return nothing)
1572-
inc_nsweep() = (nsweep[] += 1; return nothing)
1573-
1574-
function __init__()
1575-
ccall(:jl_reinit_foreign_type, Cint, (Any, Any, Any),
1576-
FObj, @cfunction(inc_nmark, Cvoid, ()), @cfunction(inc_nsweep, Cvoid, ()))
1577-
end
1578-
1579-
end # module Foreign
1580-
""")
1581-
(@eval (using Foreign))
1582-
x = [FObj() for _ in 1:1000]
1583-
GC.gc(true)
1584-
@test_broken Foreign.nmark[] > 0 || Foreign.nsweep[] > 0
1585-
end
1586-
15871557
empty!(Base.DEPOT_PATH)
15881558
append!(Base.DEPOT_PATH, original_depot_path)

0 commit comments

Comments
 (0)