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
19
examples/dynamic_library_loader/modules/library/library.v
Normal file
19
examples/dynamic_library_loader/modules/library/library.v
Normal file
@ -0,0 +1,19 @@
|
||||
module library
|
||||
|
||||
// add_1 is exported with the C name `add_1`.
|
||||
// It can be called by external programs, when the module is compiled
|
||||
// as a shared library.
|
||||
// It is exported, because the function is declared as public with `pub`.
|
||||
// The exported C name is `add_1`, because of the export: tag.
|
||||
// (Normally, the exported name is a V mangled version based on the module
|
||||
// name followed by __, followed by the fn name, i.e. it would have been
|
||||
// `library__add_1`, if not for the export: tag).
|
||||
[export: 'add_1']
|
||||
pub fn add_1(x int, y int) int {
|
||||
return my_private_function(x + y)
|
||||
}
|
||||
|
||||
// this function is not exported and will not be visible to external programs.
|
||||
fn my_private_function(x int) int {
|
||||
return 1 + x
|
||||
}
|
35
examples/dynamic_library_loader/use.v
Normal file
35
examples/dynamic_library_loader/use.v
Normal file
@ -0,0 +1,35 @@
|
||||
module main
|
||||
|
||||
// Note: This program, requires that the shared library was already compiled.
|
||||
// To do so, run `v -d no_backtrace -o library -shared modules/library/library.v`
|
||||
// before running this program.
|
||||
import os
|
||||
import dl
|
||||
import dl.loader
|
||||
|
||||
type FNAdder = fn (int, int) int
|
||||
|
||||
const (
|
||||
cfolder = os.dir(@FILE)
|
||||
default_paths = [
|
||||
os.join_path(cfolder, 'library${dl.dl_ext}'),
|
||||
os.join_path(cfolder, 'location1/library${dl.dl_ext}'),
|
||||
os.join_path(cfolder, 'location2/library${dl.dl_ext}'),
|
||||
os.join_path(cfolder, 'modules/library/library${dl.dl_ext}'),
|
||||
]
|
||||
)
|
||||
|
||||
fn main() {
|
||||
mut dl_loader := loader.get_or_create_dynamic_lib_loader(
|
||||
key: cfolder + '/library'
|
||||
paths: default_paths
|
||||
)!
|
||||
defer {
|
||||
dl_loader.unregister()
|
||||
}
|
||||
sym := dl_loader.get_sym('add_1')!
|
||||
f := FNAdder(sym)
|
||||
eprintln('f: ${ptr_str(f)}')
|
||||
res := f(1, 2)
|
||||
eprintln('res: ${res}')
|
||||
}
|
68
examples/dynamic_library_loader/use_test.v
Normal file
68
examples/dynamic_library_loader/use_test.v
Normal file
@ -0,0 +1,68 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
import dl
|
||||
|
||||
const (
|
||||
vexe = os.real_path(os.getenv('VEXE'))
|
||||
so_ext = dl.dl_ext
|
||||
)
|
||||
|
||||
fn test_vexe() {
|
||||
// dump(vexe)
|
||||
assert vexe != ''
|
||||
// dump(os.executable())
|
||||
// dump(@FILE)
|
||||
// dump(cfolder)
|
||||
// dump(so_ext)
|
||||
}
|
||||
|
||||
fn test_can_compile_library() {
|
||||
os.chdir(cfolder) or {}
|
||||
library_file_path := os.join_path(cfolder, dl.get_libname('library'))
|
||||
os.rm(library_file_path) or {}
|
||||
v_compile('-d no_backtrace -o library -shared modules/library/library.v')
|
||||
assert os.is_file(library_file_path)
|
||||
}
|
||||
|
||||
fn test_can_compile_main_program() {
|
||||
os.chdir(cfolder) or {}
|
||||
library_file_path := os.join_path(cfolder, dl.get_libname('library'))
|
||||
assert os.is_file(library_file_path)
|
||||
result := v_compile('run use.v')
|
||||
// dump(result)
|
||||
assert result.output.contains('res: 4')
|
||||
os.rm(library_file_path) or {}
|
||||
}
|
||||
|
||||
fn test_can_compile_and_use_library_with_skip_unused_home_dir() {
|
||||
os.chdir(cfolder) or {}
|
||||
library_file_path := os.join_path(cfolder, dl.get_libname('library'))
|
||||
os.rm(library_file_path) or {}
|
||||
v_compile('-skip-unused -d no_backtrace -o library -shared modules/library/library.v')
|
||||
assert os.is_file(library_file_path)
|
||||
result := v_compile('run use.v')
|
||||
assert result.output.contains('res: 4')
|
||||
os.rm(library_file_path) or {}
|
||||
}
|
||||
|
||||
fn test_can_compile_and_use_library_with_skip_unused_location1_dir() {
|
||||
os.chdir(cfolder) or {}
|
||||
library_file_path := os.join_path(cfolder, 'location1', dl.get_libname('library'))
|
||||
os.rm(library_file_path) or {}
|
||||
os.mkdir('location1') or {}
|
||||
v_compile('-skip-unused -d no_backtrace -o location1/library -shared modules/library/library.v')
|
||||
assert os.is_file(library_file_path)
|
||||
result := v_compile('run use.v')
|
||||
assert result.output.contains('res: 4')
|
||||
os.rm(library_file_path) or {}
|
||||
}
|
||||
|
||||
fn v_compile(vopts string) os.Result {
|
||||
cmd := '${os.quoted_path(vexe)} -showcc ${vopts}'
|
||||
// dump(cmd)
|
||||
res := os.execute_or_exit(cmd)
|
||||
// dump(res)
|
||||
assert res.exit_code == 0
|
||||
return res
|
||||
}
|
Reference in New Issue
Block a user