]> git.lizzy.rs Git - metalua.git/blob - src/lib/metalua/mlc_xcall.lua
pushed in a hurry, alimentation issue on laptop
[metalua.git] / src / lib / metalua / mlc_xcall.lua
1 -- lua -l mlc_xcall -e 'luafile_to_astfile ("/tmp/tmp12345.lua", "/tmp/tmp54321.ast")'
2 -- lua -l mlc_xcall -e 'lua_to_astfile ("/tmp/tmp54321.ast")'
3
4 mlc_xcall = { }
5
6
7 -- This is the back-end function, called in a separate lua process
8 -- by `mlc_xcall.client_*()' through `os.execute()'.
9 --  * inputs:
10 --     * the name of a lua source file to compile in a separate process
11 --     * the name of a writable file where the resulting ast is dumped
12 --       with `serialize()'.
13 --  * results:
14 --     * an exit status of 0 or -1, depending on whethet compilation
15 --       succeeded;
16 --     * the ast file filled will either the serialized ast, or the
17 --       error message.
18 function mlc_xcall.server (luafilename, astfilename)
19
20    -- We don't want these to be loaded when people only do client-side business
21    require 'metalua.compiler'
22    require 'serialize'
23
24    -- compile the content of luafile name in an AST, serialized in astfilename
25    --local status, ast = pcall (mlc.luafile_to_ast, luafilename)
26    local status, ast
27    local function compile() return mlc.luafile_to_ast (luafilename) end
28    if mlc.metabugs or true then 
29       print 'mlc_xcall.server/metabugs'
30       status, ast = xpcall (compile, debug.traceback)
31    else status, ast = pcall (compile) end
32    local out = io.open (astfilename, 'w')
33    if status then -- success
34       out:write (serialize (ast))
35       out:close ()
36       os.exit (0)
37    else -- failure, `ast' is actually the error message
38       out:write (ast)
39       out:close ()
40       os.exit (-1)
41    end      
42 end
43
44 -- Compile the file whose name is passed as argument, in a separate process,
45 -- communicating through a temporary file.
46 -- returns:
47 --  * true or false, indicating whether the compilation succeeded
48 --  * the ast, or the error message.
49 function mlc_xcall.client_file (luafile)
50
51    -- printf("\n\nmlc_xcall.client_file(%q)\n\n", luafile)
52
53    local tmpfilename = os.tmpname()
54    local cmd = string.format (
55       [=[lua -l metalua.mlc_xcall -e "mlc_xcall.server([[%s]], [[%s]])"]=], 
56       luafile, tmpfilename)
57
58    -- printf("os.execute [[%s]]\n\n", cmd)
59
60    local status = (0 == os.execute (cmd))
61    local result -- ast or error msg
62    if status then 
63       result = (lua_loadfile or loadfile) (tmpfilename) ()
64    else
65       local f = io.open (tmpfilename)
66       result = f :read '*a'
67       f :close()
68    end
69    os.remove(tmpfilename)
70    return status, result
71 end
72
73 -- Compile a source string into an ast, by dumping it in a tmp
74 -- file then calling `mlc_xcall.client_file()'.
75 -- returns: the same as `mlc_xcall.client_file()'.
76 function mlc_xcall.client_literal (luasrc)
77    local srcfilename = os.tmpname()
78    local srcfile, msg = io.open (srcfilename, 'w')
79    if not srcfile then print(msg) end
80    srcfile :write (luasrc)
81    srcfile :close ()
82    local status, ast = mlc_xcall.client_file (srcfilename)
83    os.remove(srcfilename)
84    return status, ast
85 end
86
87 return mlc_xcall