luatex-swiglib.lua /size: 2361 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['luatex-swiglib'] = {
2    version   = 1.001,
3    comment   = "companion to luatex-swiglib.tex",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local savedrequire = require
10
11local libsuffix = os.type == "windows" and ".dll"    or ".so"
12local pathsplit = "([^" .. io.pathseparator .. "]+)"
13
14function requireswiglib(required,version)
15    local library = package.loaded[required]
16    if library then
17        return library
18    else
19        local full = string.gsub(required,"%.","/")
20        local path = file.pathpart(full)
21        local name = file.nameonly(full) .. libsuffix
22        local list = kpse.show_path("clua")
23        for root in string.gmatch(list,pathsplit) do
24            local full = false
25            if type(version) == "string" and version ~= "" then
26                full = root .. "/" .. path .. "/" .. version .. "/" .. name
27                full = lfs.isfile(full) and full
28            end
29            if not full then
30                full = root .. "/" .. path .. "/" .. name
31                full = lfs.isfile(full) and full
32            end
33            if full then
34                local path, base = string.match(full,"^(.-)([^\\/]+)" .. libsuffix .."$")
35                local savedlibrary = package.loaded[base]
36                package.loaded[base] = nil
37                local savedpath = lfs.currentdir()
38                lfs.chdir(path)
39                library = package.loadlib(full,"luaopen_" .. base)
40                if type(library) == "function" then
41                    library = library()
42                    texio.write("<swiglib: '",required,"' is loaded>")
43                end
44                lfs.chdir(savedpath)
45                package.loaded[base] = savedlibrary
46                package.loaded[required] = library
47                return library
48            end
49        end
50        texio.write("<swiglib: '",name,"'is not found on '",list,"'")
51    end
52    texio.write("<swiglib: '",required,"' is not found>")
53end
54
55function require(name)
56    if string.find(name,"^swiglib%.") then
57        return requireswiglib(name)
58    else
59        return savedrequire(name)
60    end
61end
62
63function swiglib(name,version)
64    return requireswiglib("swiglib." .. name,version)
65end
66