]> git.lizzy.rs Git - metalua.git/blob - metalua/compiler/globals.lua
Merge branch 'master' of ssh://git.eclipse.org/gitroot/koneki/org.eclipse.koneki...
[metalua.git] / metalua / compiler / globals.lua
1 --------------------------------------------------------------------------------
2 -- Copyright (c) 2006-2013 Fabien Fleutot and others.
3 --
4 -- All rights reserved.
5 --
6 -- This program and the accompanying materials are made available
7 -- under the terms of the Eclipse Public License v1.0 which
8 -- accompanies this distribution, and is available at
9 -- http://www.eclipse.org/legal/epl-v10.html
10 --
11 -- This program and the accompanying materials are also made available
12 -- under the terms of the MIT public license which accompanies this
13 -- distribution, and is available at http://www.lua.org/license.html
14 --
15 -- Contributors:
16 --     Fabien Fleutot - API and implementation
17 --
18 --------------------------------------------------------------------------------
19
20 --*-lua-*-----------------------------------------------------------------------
21 -- Override Lua's default compilation functions, so that they support Metalua
22 -- rather than only plain Lua
23 --------------------------------------------------------------------------------
24
25 local mlc = require 'metalua.compiler'
26
27 local M = { }
28
29 -- Original versions
30 local original_lua_versions = {
31     load       = load,
32     loadfile   = loadfile,
33     loadstring = loadstring,
34     dofile     = dofile }
35
36 local lua_loadstring = loadstring
37 local lua_loadfile = loadfile
38
39 function M.loadstring(str, name)
40    if type(str) ~= 'string' then error 'string expected' end
41    if str:match '^\027LuaQ' then return lua_loadstring(str) end
42    local n = str:match '^#![^\n]*\n()'
43    if n then str=str:sub(n, -1) end
44    -- FIXME: handle erroneous returns (return nil + error msg)
45    return mlc.new():src_to_function(str, name)
46 end
47
48 function M.loadfile(filename)
49    local f, err_msg = io.open(filename, 'rb')
50    if not f then return nil, err_msg end
51    local success, src = pcall( f.read, f, '*a')
52    pcall(f.close, f)
53    if success then return M.loadstring (src, '@'..filename)
54    else return nil, src end
55 end
56
57 function M.load(f, name)
58    local acc = { }
59    while true do
60       local x = f()
61       if not x then break end
62       assert(type(x)=='string', "function passed to load() must return strings")
63       table.insert(acc, x)
64    end
65    return M.loadstring(table.concat(acc))
66 end
67
68 function M.dostring(src)
69    local f, msg = M.loadstring(src)
70    if not f then error(msg) end
71    return f()
72 end
73
74 function M.dofile(name)
75    local f, msg = M.loadfile(name)
76    if not f then error(msg) end
77    return f()
78 end
79
80 -- Export replacement functions as globals
81 for name, f in pairs(M) do _G[name] = f end
82
83 -- To be done *after* exportation
84 M.lua = original_lua_versions
85
86 return M