webmcp

diff framework/env/execute/load_chunk.lua @ 31:1cd9e69b85cb

add execute.load_chunk(args)

load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
author Daniel Poelzleithner <poelzi@poelzi.org>
date Tue Oct 05 02:34:04 2010 +0200 (2010-10-05)
parents
children e31af860e97c
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/execute/load_chunk.lua	Tue Oct 05 02:34:04 2010 +0200
     1.3 @@ -0,0 +1,32 @@
     1.4 +--[[--
     1.5 +status_code =             -- executes and returns a lua file
     1.6 +execute.load_chunk{
     1.7 +  file_path = file_path,  -- path to a lua source or byte-code file
     1.8 +  app       = app,        -- app name to use or the current will be used
     1.9 +  module    = module,     -- module where chunk is located
    1.10 +  chunk     = chunk       -- filename of lua file to load 
    1.11 +}
    1.12 +
    1.13 +This function loads and executes a lua file specified by a given path or constructs 
    1.14 +a path to load from the module and chunk name.
    1.15 +
    1.16 +--]]--
    1.17 +
    1.18 +function execute.load_chunk(args)
    1.19 +  local file_path = args.file_path
    1.20 +  local app       = args.app
    1.21 +  local module    = args.module
    1.22 +  local chunk     = args.chunk
    1.23 +
    1.24 +  app = app or request.get_app_name()
    1.25 +
    1.26 +  file_path = file_path or encode.file_path(request.get_app_basepath(),
    1.27 +                                 'app', app, module, chunk)
    1.28 +
    1.29 +  local func, load_errmsg = loadfile(file_path)
    1.30 +  if not func then
    1.31 +    error('Could not load file "' .. file_path .. '": ' .. load_errmsg)
    1.32 +  end
    1.33 +  local result = func()
    1.34 +  return result
    1.35 +end

Impressum / About Us