libs-imp-foreign.lmt /size: 1874 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['libs-imp-foreign'] = {
2    version   = 1.001,
3    comment   = "companion to luat-imp-foreign.mkxl",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- See libs-imp-foreign.mkxl for some comments.
10
11local report = logs.reporter("foreign")
12
13local libname  = "foreign"
14----- libfile  = "libffi-7"
15local libfile  = "libffi*"
16----- libfile  = "d:/inkscape/bin/libffi-7.dll" -- libffi*
17
18local libforeign = resolvers.libraries.validoptional(libname)
19
20if package.loaded[libname] then
21    return package.loaded[libname]
22end
23
24local function okay()
25    -- Don't worry, when one overloads this flag the engine will abort with an
26    -- error message anyway, but it's less nice.
27    if not status.permit_loadlib then
28        report()
29        report("opening foreign libraries is not enabled")
30        report()
31        os.exit()
32    elseif libforeign and resolvers.libraries.optionalloaded(libname,libfile) then
33        okay = function() return true end
34    else
35        okay = function() return false end
36    end
37    return okay()
38end
39
40local foreignload = libforeign.load
41
42local foreign = {
43    types     = libforeign.types,
44    abivalues = libforeign.abivalues,
45    totable   = libforeign.totable,
46    newbuffer = libforeign.newbuffer,
47    getbuffer = libforeign.getbuffer,
48    load      = function(name)
49       if okay() then
50           local fullname = resolvers.findlib(name)
51           if fullname and fullname ~= "" then
52               return foreignload(fullname)
53           else
54               -- report an error
55           end
56       end
57    end,
58}
59
60-- In due time I'll add the struct and array methods using Lua 5.4 features.
61
62package .loaded[libname] = foreign
63optional.loaded[libname] = foreign
64
65return foreign
66