webmcp

changeset 309:4e69ce9a6365

Mechanism to load, compile, and cache Lua code from the filesystem
author jbe
date Sun Mar 22 22:25:37 2015 +0100 (2015-03-22)
parents 4dc2ec12643c
children 5f604bbbbc00
files framework/bin/mcp.lua framework/env/execute/chunk.lua framework/env/execute/file_path.lua framework/env/execute/load_chunk.lua framework/env/locale/_get_translation_table.lua
line diff
     1.1 --- a/framework/bin/mcp.lua	Sun Mar 22 20:39:13 2015 +0100
     1.2 +++ b/framework/bin/mcp.lua	Sun Mar 22 22:25:37 2015 +0100
     1.3 @@ -20,6 +20,36 @@
     1.4  _ENV = setmetatable({}, global_metatable)
     1.5  
     1.6  --[[--
     1.7 +lua_func =   -- compiled Lua function
     1.8 +loadcached(
     1.9 +  filename   -- path to a Lua source or byte-code file
    1.10 +)
    1.11 +
    1.12 +Loads, compiles and caches a Lua chunk. If the file does not exist, nil and an error string are returned. Otherwise the file is loaded, compiled, and cached. The cached value (i.e. the compiled function) is returned. An error is raised if the compilation was not successful.
    1.13 +
    1.14 +--]]--
    1.15 +do
    1.16 +  local cache = {}
    1.17 +  function loadcached(filename)
    1.18 +    local file, read_error = io.open(filename, "r")
    1.19 +    if file then
    1.20 +      local filedata = assert(file:read("*a"))
    1.21 +      assert(file:close())
    1.22 +      local func, compile_error = load(filedata, "=" .. filename, nil, _ENV)
    1.23 +      if func then
    1.24 +        cache[filename] = func
    1.25 +        return func
    1.26 +      else
    1.27 +        error(compile_error, 0)
    1.28 +      end
    1.29 +    else
    1.30 +      return nil, read_error
    1.31 +    end
    1.32 +  end
    1.33 +end
    1.34 +--//--
    1.35 +
    1.36 +--[[--
    1.37  WEBMCP_MODE
    1.38  
    1.39  A constant set to "listen" in case of a network request, or set to "interactive" in case of interactive mode.
    1.40 @@ -139,17 +169,10 @@
    1.41      setmetatable(self, autoloader_mt)
    1.42    end
    1.43    local function try_exec(filename)
    1.44 -    local file = io.open(filename, "r")
    1.45 -    if file then
    1.46 -      local filedata = file:read("*a")
    1.47 -      io.close(file)
    1.48 -      local func, errmsg = load(filedata, "=" .. filename, nil, _ENV)
    1.49 -      if func then
    1.50 -        func()
    1.51 -        return true
    1.52 -      else
    1.53 -        error(errmsg, 0)
    1.54 -      end
    1.55 +    local func = loadcached(filename)
    1.56 +    if func then
    1.57 +      func()
    1.58 +      return true
    1.59      else
    1.60        return false
    1.61      end
     2.1 --- a/framework/env/execute/chunk.lua	Sun Mar 22 20:39:13 2015 +0100
     2.2 +++ b/framework/env/execute/chunk.lua	Sun Mar 22 22:25:37 2015 +0100
     2.3 @@ -1,7 +1,7 @@
     2.4  --[[--
     2.5  return_value =            -- return value of executed chunk
     2.6  execute.chunk{
     2.7 -  file_path = file_path,  -- path to a lua source or byte-code file
     2.8 +  file_path = file_path,  -- path to a Lua source or byte-code file
     2.9    app       = app,        -- app name to use or the current will be used
    2.10    module    = module,     -- module where chunk is located
    2.11    chunk     = chunk       -- name of chunk (filename without .lua extension)
    2.12 @@ -35,7 +35,7 @@
    2.13      WEBMCP_BASE_PATH, 'app', app, module, chunk .. '.lua'
    2.14    )
    2.15  
    2.16 -  local func, load_errmsg = loadfile(file_path, nil, _ENV)
    2.17 +  local func, load_errmsg = loadcached(file_path)
    2.18    if not func then
    2.19      error('Could not load file "' .. file_path .. '": ' .. load_errmsg)
    2.20    end
     3.1 --- a/framework/env/execute/file_path.lua	Sun Mar 22 20:39:13 2015 +0100
     3.2 +++ b/framework/env/execute/file_path.lua	Sun Mar 22 22:25:37 2015 +0100
     3.3 @@ -1,7 +1,7 @@
     3.4  --[[--
     3.5  status_code =             -- status code returned by the executed lua file (a string)
     3.6  execute.file_path{
     3.7 -  file_path = file_path,  -- path to a lua source or byte-code file
     3.8 +  file_path = file_path,  -- path to a Lua source or byte-code file
     3.9    id        = id,         -- id to be returned by param.get_id(...) during execution
    3.10    params    = params      -- parameters to be returned by param.get(...) during execution
    3.11  }
    3.12 @@ -14,7 +14,7 @@
    3.13    local file_path = args.file_path
    3.14    local id        = args.id
    3.15    local params    = args.params
    3.16 -  local func, load_errmsg = loadfile(file_path, nil, _ENV)
    3.17 +  local func, load_errmsg = loadcached(file_path)
    3.18    if not func then
    3.19      error('Could not load file "' .. file_path .. '": ' .. load_errmsg)
    3.20    end
     4.1 --- a/framework/env/execute/load_chunk.lua	Sun Mar 22 20:39:13 2015 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,34 +0,0 @@
     4.4 ---[[--
     4.5 -return_value =            -- return value of executed chunk
     4.6 -execute.load_chunk{
     4.7 -  file_path = file_path,  -- path to a lua source or byte-code file
     4.8 -  app       = app,        -- app name to use or the current will be used
     4.9 -  module    = module,     -- module where chunk is located
    4.10 -  chunk     = chunk       -- filename of lua file to load (including filename extension)
    4.11 -  id        = id,         -- id to be returned by param.get_id(...) during execution
    4.12 -  params    = params      -- parameters to be returned by param.get(...) during execution
    4.13 -}
    4.14 -
    4.15 -NOTE: execute.load_chunk{...} is DEPRECATED and replaced by execute.chunk{...}. Both functions differ in interpretation of argument "chunk" regarding the filename extenstion '.lua'.
    4.16 -  
    4.17 -This function loads and executes a lua file specified by a given path or constructs a path to load from the module and chunk name.
    4.18 -
    4.19 ---]]--
    4.20 -
    4.21 -function execute.load_chunk(args)
    4.22 -  local chunk_name
    4.23 -  if args.chunk then
    4.24 -    chunk_name = string.match(args.chunk, "^(.*)%.lua$")
    4.25 -    if not chunk_name then
    4.26 -      error('"chunk_name" does not end with \'.lua\'')
    4.27 -    end
    4.28 -  end
    4.29 -  return execute.chunk{
    4.30 -    file_path = args.file_path,
    4.31 -    app       = args.app,
    4.32 -    module    = args.module,
    4.33 -    chunk     = chunk_name,
    4.34 -    id        = args.id,
    4.35 -    params    = args.params
    4.36 -  }
    4.37 -end
     5.1 --- a/framework/env/locale/_get_translation_table.lua	Sun Mar 22 20:39:13 2015 +0100
     5.2 +++ b/framework/env/locale/_get_translation_table.lua	Sun Mar 22 22:25:37 2015 +0100
     5.3 @@ -9,12 +9,9 @@
     5.4        return translation_table
     5.5      end
     5.6      local filename = encode.file_path(WEBMCP_BASE_PATH, "locale", "translations." .. language_code .. ".lua")
     5.7 -    local func
     5.8 -    if _ENV then
     5.9 -      func = assert(loadfile(filename), nil, {})
    5.10 -    else
    5.11 -      func = assert(loadfile(filename))
    5.12 -      setfenv(func, {})
    5.13 +    local func, load_errmsg = loadcached(filename)
    5.14 +    if not func then
    5.15 +      error('Could not load translation file "' .. filename .. '": ' .. load_errmsg)
    5.16      end
    5.17      translation_table = func()
    5.18      if type(translation_table) ~= "table" then

Impressum / About Us