]> git.lizzy.rs Git - metalua.git/blob - src/lib/package2.lua
738e32e21483583f774d12fcbd682a6cb4c6ed29
[metalua.git] / src / lib / package2.lua
1 local package = package
2
3 require 'mlc'
4
5 package.mpath = os.getenv 'LUA_MPATH' or 
6    './?.mlua;/usr/local/share/lua/5.1/?.mlua;'..
7    '/usr/local/share/lua/5.1/?/init.mlua;'..
8    '/usr/local/lib/lua/5.1/?.mlua;'..
9    '/usr/local/lib/lua/5.1/?/init.mlua'
10
11
12 ----------------------------------------------------------------------
13 -- resc(k) returns "%"..k if it's a special regular expression char,
14 -- or just k if it's normal.
15 ----------------------------------------------------------------------
16 local regexp_magic = table.transpose{
17    "^", "$", "(", ")", "%", ".", "[", "]", "*", "+", "-", "?" }
18 local function resc(k)
19    return regexp_magic[k] and '%'..k or k
20 end
21
22 ----------------------------------------------------------------------
23 -- Take a Lua module name, return the open file and its name, 
24 -- or <false> and an error message.
25 ----------------------------------------------------------------------
26 function package.findfile(name, path_string)
27    local config_regexp = ("([^\n])\n"):rep(5):sub(1, -2)
28    local dir_sep, path_sep, path_mark, execdir, igmark = 
29       package.config:strmatch (config_regexp)
30    name = name:gsub ('%.', dir_sep)
31    local errors = { }
32    local path_pattern = string.format('[^%s]+', resc(path_sep))
33    for path in path_string:gmatch (path_pattern) do
34       --printf('path = %s, rpath_mark=%s, name=%s', path, resc(path_mark), name)
35       local filename = path:gsub (resc (path_mark), name)
36       --printf('filename = %s', filename)
37       local file = io.open (filename, 'r')
38       if file then return file, filename end
39       table.insert(errors, string.format("\tno lua file %q", filename))
40    end
41    return false, table.concat(errors, "\n")..'\n'
42 end
43
44
45 ----------------------------------------------------------------------
46 -- Execute a metalua module sources compilation in a separate ring.
47 ----------------------------------------------------------------------
48 local function spring_load(filename)   
49    local env_fast = os.getenv 'LUA_MFAST'
50
51    if env_fast=='yes' or env_fast=='true' then 
52       -- degraded mode without spring:
53       -- print "Warning: loading metalua source file in the same compilation ring;"
54       -- print "metalevels 0 might interfere, condider unsetting environment variable LUA_MFAST"
55       return mlc.function_of_luafile(filename) 
56    else
57       -- run compilation in a separate spring universe:
58       require 'springs'
59       local r = springs.new()
60       r:dostring [[require 'metalua.compiler']]
61       local f = r:call('mlc.function_of_luafile', filename)
62       r:close()
63       return f
64    end
65 end
66
67 ----------------------------------------------------------------------
68 -- Load a metalua source file.
69 ----------------------------------------------------------------------
70 function package.metalua_loader (name)
71    local file, filename_or_msg = package.findfile (name, package.mpath)
72    if not file then return filename_or_msg end
73    file:close()
74    return spring_load(filename_or_msg)
75 end
76
77 ----------------------------------------------------------------------
78 -- Placed after lua/luac loader, so precompiled files have
79 -- higher precedence.
80 ----------------------------------------------------------------------
81 table.insert(package.loaders, package.metalua_loader)
82
83 ----------------------------------------------------------------------
84 -- Load an extension.
85 ----------------------------------------------------------------------
86 function extension (name, noruntime)
87    local complete_name = 'extension.'..name
88    local x = require (complete_name)
89    if x==true then return
90    elseif type(x) ~= 'table' then 
91       error ("extension returned %s instead of an AST", type(x))
92    else
93       return x
94    end
95 end
96
97 return package