mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: add a new dl.loader module, to simplify dynamic library loading, when the DLLs may be in multiple customisable locations (#17161)
This commit is contained in:

committed by
GitHub

parent
2d51379f00
commit
40ec2a292e
83
vlib/dl/loader/loader_test.v
Normal file
83
vlib/dl/loader/loader_test.v
Normal file
@ -0,0 +1,83 @@
|
||||
import dl.loader
|
||||
import dl
|
||||
|
||||
fn test_dl_loader() ! {
|
||||
$if linux {
|
||||
run_test_invalid_lib_linux()!
|
||||
return
|
||||
}
|
||||
$if windows {
|
||||
run_test_invalid_lib_windows()!
|
||||
run_test_valid_lib_windows()!
|
||||
run_test_invalid_sym_windows()!
|
||||
run_test_valid_sym_windows()!
|
||||
return
|
||||
} $else {
|
||||
eprint('currently not implemented on this platform')
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_loader(name string, paths []string) !&loader.DynamicLibLoader {
|
||||
return loader.get_or_create_dynamic_lib_loader(
|
||||
key: name
|
||||
paths: paths
|
||||
flags: dl.rtld_now
|
||||
)
|
||||
}
|
||||
|
||||
fn run_test_invalid_lib_linux() ! {
|
||||
// ensure a not-existing dl won't be loaded
|
||||
mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
|
||||
'not-existing-dynamic-link-library',
|
||||
])!
|
||||
defer {
|
||||
dl_loader.unregister()
|
||||
}
|
||||
h := dl_loader.open() or { unsafe { nil } }
|
||||
assert isnil(h)
|
||||
}
|
||||
|
||||
fn run_test_invalid_lib_windows() ! {
|
||||
// ensure a not-existing dl won't be loaded
|
||||
mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
|
||||
'not-existing-dynamic-link-library',
|
||||
])!
|
||||
defer {
|
||||
dl_loader.unregister()
|
||||
}
|
||||
h := dl_loader.open() or { unsafe { nil } }
|
||||
assert isnil(h)
|
||||
}
|
||||
|
||||
fn run_test_valid_lib_windows() ! {
|
||||
mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
|
||||
'not-existing-dynamic-link-library',
|
||||
'shell32',
|
||||
])!
|
||||
defer {
|
||||
dl_loader.unregister()
|
||||
}
|
||||
h := dl_loader.open() or { unsafe { nil } }
|
||||
assert !isnil(h)
|
||||
}
|
||||
|
||||
fn run_test_invalid_sym_windows() ! {
|
||||
mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', ['shell32'])!
|
||||
defer {
|
||||
dl_loader.unregister()
|
||||
}
|
||||
proc := dl_loader.get_sym('CommandLineToArgvW2') or { unsafe { nil } }
|
||||
assert isnil(proc)
|
||||
}
|
||||
|
||||
fn run_test_valid_sym_windows() ! {
|
||||
mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
|
||||
'not-existing-dynamic-link-library',
|
||||
'shell32',
|
||||
])!
|
||||
defer {
|
||||
dl_loader.unregister()
|
||||
}
|
||||
proc := dl_loader.get_sym('CommandLineToArgvW') or { unsafe { nil } }
|
||||
assert !isnil(proc)
|
||||
}
|
Reference in New Issue
Block a user