1 local package = package
5 package.metalua_extension_prefix = 'metalua.extension.'
7 package.mpath = os.getenv 'LUA_MPATH' or
8 './?.mlua;/usr/local/share/lua/5.1/?.mlua;'..
9 '/usr/local/share/lua/5.1/?/init.mlua;'..
10 '/usr/local/lib/lua/5.1/?.mlua;'..
11 '/usr/local/lib/lua/5.1/?/init.mlua'
14 ----------------------------------------------------------------------
15 -- resc(k) returns "%"..k if it's a special regular expression char,
16 -- or just k if it's normal.
17 ----------------------------------------------------------------------
18 local regexp_magic = table.transpose{
19 "^", "$", "(", ")", "%", ".", "[", "]", "*", "+", "-", "?" }
20 local function resc(k)
21 return regexp_magic[k] and '%'..k or k
24 ----------------------------------------------------------------------
25 -- Take a Lua module name, return the open file and its name,
26 -- or <false> and an error message.
27 ----------------------------------------------------------------------
28 function package.findfile(name, path_string)
29 local config_regexp = ("([^\n])\n"):rep(5):sub(1, -2)
30 local dir_sep, path_sep, path_mark, execdir, igmark =
31 package.config:strmatch (config_regexp)
32 name = name:gsub ('%.', dir_sep)
34 local path_pattern = string.format('[^%s]+', resc(path_sep))
35 for path in path_string:gmatch (path_pattern) do
36 --printf('path = %s, rpath_mark=%s, name=%s', path, resc(path_mark), name)
37 local filename = path:gsub (resc (path_mark), name)
38 --printf('filename = %s', filename)
39 local file = io.open (filename, 'r')
40 if file then return file, filename end
41 table.insert(errors, string.format("\tno lua file %q", filename))
43 return false, table.concat(errors, "\n")..'\n'
47 ----------------------------------------------------------------------
48 -- Execute a metalua module sources compilation in a separate ring.
49 ----------------------------------------------------------------------
50 local function spring_load(filename)
51 local env_fast = os.getenv 'LUA_NOSPRINGS'
52 local try_springs = env_fast=='yes' or env_fast=='true'
53 local has_springs = try_springs and pcall(require, 'springs')
55 local r = springs.new()
56 r:dostring [[require 'metalua.compiler']]
57 local f = r:call('mlc.function_of_luafile', filename)
61 --print "LUA_NOSPRINGS mode"
62 return mlc.function_of_luafile(filename)
66 ----------------------------------------------------------------------
67 -- Load a metalua source file.
68 ----------------------------------------------------------------------
69 function package.metalua_loader (name)
70 local file, filename_or_msg = package.findfile (name, package.mpath)
71 if not file then return filename_or_msg end
73 return spring_load(filename_or_msg)
76 ----------------------------------------------------------------------
77 -- Placed after lua/luac loader, so precompiled files have
79 ----------------------------------------------------------------------
80 table.insert(package.loaders, package.metalua_loader)
82 ----------------------------------------------------------------------
84 ----------------------------------------------------------------------
85 function extension (name, noruntime)
86 local complete_name = package.metalua_extension_prefix..name
87 local x = require (complete_name)
88 if x==true then return
89 elseif type(x) ~= 'table' then
90 error ("extension returned %s instead of an AST", type(x))