From f7a73f14faffba3045017fb2bb18d4e60ced54bd Mon Sep 17 00:00:00 2001 From: Fabien Fleutot Date: Thu, 25 Dec 2008 13:15:22 +0100 Subject: [PATCH] fixes on the compilation process separation through io.popen() --- src/compiler/Makefile | 10 ++++++---- src/compiler/bootstrap.lua | 9 +++++++-- src/compiler/metalua.mlua | 7 +++---- src/lib/metalua/package2.lua | 29 +++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/compiler/Makefile b/src/compiler/Makefile index 02f1b42..2d9d88c 100644 --- a/src/compiler/Makefile +++ b/src/compiler/Makefile @@ -50,14 +50,16 @@ mlp.luac: $(MLP_LUA) $(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 diff --git a/src/compiler/bootstrap.lua b/src/compiler/bootstrap.lua index 96c3d55..507cdb8 100644 --- a/src/compiler/bootstrap.lua +++ b/src/compiler/bootstrap.lua @@ -37,12 +37,17 @@ package.preload['metalua.mlc'] = function() 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) diff --git a/src/compiler/metalua.mlua b/src/compiler/metalua.mlua index 9e1fdc3..9342435 100644 --- a/src/compiler/metalua.mlua +++ b/src/compiler/metalua.mlua @@ -13,10 +13,9 @@ BYTECODE_SYNTHESE_ERROR_NUMBER = -100 -{ 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) diff --git a/src/lib/metalua/package2.lua b/src/lib/metalua/package2.lua index 7ab8abc..6940ef0 100644 --- a/src/lib/metalua/package2.lua +++ b/src/lib/metalua/package2.lua @@ -43,19 +43,26 @@ function package.findfile(name, path_string) 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 ---------------------------------------------------------------------- @@ -64,8 +71,14 @@ 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 ---------------------------------------------------------------------- -- 2.44.0