$(LUA_COMPILE) -o $@ bootstrap $<
# Compiler/interpreter
-metalua: metalua.luac $(LIBRARIES)
+metalua: metalua.luac install-lib
$(LUA_RUN) metalua.luac --verbose --sharpbang '#!$(TARGET_BIN_PATH)/lua' --output metalua --file metalua.mlua
-install: metalua $(LIBRARIES)
+install-lib: $(LIBRARIES)
+ mkdir -p $(TARGET_LUA_PATH)/metalua
+ cp $(LIBRARIES) $(TARGET_LUA_PATH)/metalua/
+
+install: install-lib metalua
mkdir -p $(TARGET_BIN_PATH)
cp metalua $(TARGET_BIN_PATH)/
- mkdir -p $(TARGET_LUA_PATH)
- cp $(LIBRARIES) $(TARGET_LUA_PATH)/
.PHONY: all install
f:close()
return mlc.function_of_luastring (src, "@"..name)
end
+
+ -- don't let require() fork a separate process for *.mlua compilations.
+ package.metalua_nopopen = true
end
+package.path = '?.luac;?.lua;../lib/?.luac;../lib/?.lua'
+
require 'verbose_require'
require 'metalua.base'
-require 'metalua.bytecode'
-require 'metalua.mlp'
+require 'bytecode'
+require 'mlp'
require 'metalua.package2'
local function compile_file (src_filename)
-{ extension 'match' }
function spring_pcall (f, arg, name)
- -- FIXME: won't work if `arg' or `name' contain a backslash-escaped quot char.
- arg = arg :gsub ("'", "\\'")
- name = name :gsub ("'", "\\'")
- local pattern = [[lua -l metalua.mlc -l serialize -e "print(serialize(%s('%s', '%s')))"]]
+ local pattern =
+ [=[lua -l metalua.compiler -l serialize -e ]=]..
+ [=["print (serialize (%s ([[%s]], [[%s]])))"]=]
local cmd = string.format (pattern, f, arg, name)
--print ("Running the following process: " .. cmd)
local fd = io.popen (cmd)
return false, table.concat(errors, "\n")..'\n'
end
-
----------------------------------------------------------------------
-- Execute a metalua module sources compilation in a separate process
+-- Sending back the bytecode directly is difficult, as some shells
+-- (at least MS-Windows') interpret some characters. So rather than
+-- base64-encoding the bytecode, AST is returned from the child
+-- process, and converted to bytecode then function in the calling
+-- process.
----------------------------------------------------------------------
local function spring_load(filename)
+ -- FIXME: handle compilation errors
local pattern =
- [[lua -l metalua.mlc -l -e "print(mlc.luacstring_of_luafile('%s', '%s'))"]]
- local cmd = string.format (pattern, f, filename, filename)
- print ("running command: ``" .. cmd .. "''")
+ [=[lua -l metalua.compiler -l serialize -e ]=]..
+ [=["print(serialize(mlc.ast_of_luafile [[%s]]))"]=]
+ local cmd = string.format (pattern, filename)
+ --print ("running command: ``" .. cmd .. "''")
local fd = io.popen (cmd)
- local bytecode = fd:read '*a'
+ local ast_src = fd:read '*a'
fd:close()
- return string.undump (bytecode)
+ local ast = lua_loadstring (ast_src) () -- much faster than loadstring()
+ return mlc.function_of_ast (ast, filename)
end
----------------------------------------------------------------------
function package.metalua_loader (name)
local file, filename_or_msg = package.findfile (name, package.mpath)
if not file then return filename_or_msg end
- file:close()
- return spring_load(filename_or_msg)
+ if package.metalua_nopopen then
+ local luastring = file:read '*a'
+ file:close()
+ return mlc.function_of_luastring (luastring, name)
+ else
+ file:close()
+ return spring_load (filename_or_msg)
+ end
end
----------------------------------------------------------------------