Skip to content

Commit eff34e2

Browse files
committed
fix runtime cglobal builtin function implementation
This had failed to be updated for the LazyLibrary changes to codegen.
1 parent b35c4f4 commit eff34e2

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/runtime_intrinsics.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -652,15 +652,9 @@ JL_DLLEXPORT jl_value_t *jl_cglobal(jl_value_t *v, jl_value_t *ty)
652652
return v;
653653
}
654654

655-
char *f_lib = NULL;
655+
jl_value_t *f_lib = NULL;
656656
if (jl_is_tuple(v) && jl_nfields(v) > 1) {
657-
jl_value_t *t1 = jl_fieldref(v, 1);
658-
if (jl_is_symbol(t1))
659-
f_lib = jl_symbol_name((jl_sym_t*)t1);
660-
else if (jl_is_string(t1))
661-
f_lib = jl_string_data(t1);
662-
else
663-
JL_TYPECHK(cglobal, symbol, t1)
657+
f_lib = jl_fieldref(v, 1);
664658
v = jl_fieldref(v, 0);
665659
}
666660

@@ -672,11 +666,15 @@ JL_DLLEXPORT jl_value_t *jl_cglobal(jl_value_t *v, jl_value_t *ty)
672666
else
673667
JL_TYPECHK(cglobal, symbol, v)
674668

675-
if (!f_lib)
676-
f_lib = (char*)jl_dlfind(f_name);
677-
678669
void *ptr;
679-
jl_dlsym(jl_get_library(f_lib), f_name, &ptr, 1);
670+
if (f_lib) {
671+
ptr = jl_lazy_load_and_lookup(f_lib, f_name);
672+
}
673+
else {
674+
void *handle = jl_get_library((char*)jl_dlfind(f_name));
675+
jl_dlsym(handle, f_name, &ptr, 1);
676+
}
677+
680678
jl_value_t *jv = jl_gc_alloc(jl_current_task->ptls, sizeof(void*), rt);
681679
*(void**)jl_data_ptr(jv) = ptr;
682680
JL_GC_POP();

stdlib/Libdl/test/runtests.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ mktempdir() do dir
266266
end
267267

268268
## Tests for LazyLibrary
269-
@testset "LazyLibrary" begin; mktempdir() do dir
269+
@testset "LazyLibrary" begin
270270
lclf_path = joinpath(private_libdir, "libccalllazyfoo.$(Libdl.dlext)")
271271
lclb_path = joinpath(private_libdir, "libccalllazybar.$(Libdl.dlext)")
272272

@@ -294,21 +294,26 @@ end
294294
@test !any(contains.(dllist(), lclb_path))
295295
end
296296

297-
global libccalllazyfoo = LazyLibrary(lclf_path; on_load_callback=() -> global lclf_loaded = true)
298-
global libccalllazybar = LazyLibrary(lclb_path; dependencies=[libccalllazyfoo], on_load_callback=() -> global lclb_loaded = true)
297+
let libccalllazyfoo = LazyLibrary(lclf_path; on_load_callback=() -> global lclf_loaded = true),
298+
libccalllazybar = LazyLibrary(lclb_path; dependencies=[libccalllazyfoo], on_load_callback=() -> global lclb_loaded = true)
299+
eval(:(const libccalllazyfoo = $libccalllazyfoo))
300+
eval(:(const libccalllazybar = $libccalllazybar))
301+
end
302+
Core.@latestworld
299303

300304
# Creating `LazyLibrary` doesn't actually load anything
301305
@test !lclf_loaded
302306
@test !lclb_loaded
303307

304308
# Explicitly calling `dlopen()` does:
305-
dlopen(libccalllazybar)
309+
h = dlopen(libccalllazybar)
306310
@test lclf_loaded
307311
@test lclb_loaded
308312
close_libs()
309313

310314
# Test that the library gets loaded when you use `ccall()`
311-
@test ccall((:bar, libccalllazybar), Cint, (Cint,), 2) == 6
315+
compiled_bar() = ccall((:bar, libccalllazybar), Cint, (Cint,), 2)
316+
@test ccall((:bar, libccalllazybar), Cint, (Cint,), 2) == compiled_bar() == 6
312317
@test lclf_loaded
313318
@test lclb_loaded
314319
close_libs()
@@ -324,11 +329,17 @@ end
324329
@test lclf_loaded
325330
close_libs()
326331

332+
# Test that `cglobal()` works, both compiled and runtime emulation
333+
compiled_cglobal() = cglobal((:bar, libccalllazybar))
334+
@test cglobal((:bar, libccalllazybar)) === compiled_cglobal() === dlsym(h, :bar)
335+
@test lclf_loaded
336+
close_libs()
337+
327338
# Test that we can use lazily-evaluated library names:
328339
libname = LazyLibraryPath(private_libdir, "libccalllazyfoo.$(Libdl.dlext)")
329340
lazy_name_lazy_lib = LazyLibrary(libname)
330341
@test dlpath(lazy_name_lazy_lib) == realpath(string(libname))
331-
end; end
342+
end
332343

333344
@testset "Docstrings" begin
334345
@test isempty(Docs.undocumented_names(Libdl))

0 commit comments

Comments
 (0)