webmcp
view framework/env/execute/chunk.lua @ 205:48ee8826efbe
Correct handling of nil's in return tuples in execute.chunk{...}
| author | jbe | 
|---|---|
| date | Fri Jan 09 21:32:23 2015 +0100 (2015-01-09) | 
| parents | 3d43a5cf17c1 | 
| children | eb3e236d261d | 
 line source
     1 --[[--
     2 return_value =            -- return value of executed chunk
     3 execute.chunk{
     4   file_path = file_path,  -- path to a lua source or byte-code file
     5   app       = app,        -- app name to use or the current will be used
     6   module    = module,     -- module where chunk is located
     7   chunk     = chunk       -- name of chunk (filename without .lua extension)
     8   id        = id,         -- id to be returned by param.get_id(...) during execution
     9   params    = params      -- parameters to be returned by param.get(...) during execution
    10 }
    12 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. A chunk name should always begin with an underscore. All return values of the loaded and executed chunk are returned by this function as well.
    14 --]]--
    16 local function pack_return_values(...)
    17   local storage = {...}
    18   storage.n = select("#", ...)
    19 end
    21 local function unpack_return_values(storage)
    22   return table.unpack(storage, 1, storage.n)
    23 end
    25 function execute.chunk(args)
    26   local file_path = args.file_path
    27   local app       = args.app
    28   local module    = args.module
    29   local chunk     = args.chunk
    30   local id        = args.id
    31   local params    = args.params
    33   app = app or request.get_app_name()
    35   file_path = file_path or encode.file_path(
    36     request.get_app_basepath(),
    37     'app', app, module, chunk .. '.lua'
    38   )
    40   local func, load_errmsg = loadfile(file_path)
    41   if not func then
    42     error('Could not load file "' .. file_path .. '": ' .. load_errmsg)
    43   end
    45   if id or params then
    46     param.exchange(id, params)
    47   end
    49   local result = pack_return_values(func())
    51   if id or params then
    52     param.restore()
    53   end
    55   return unpack_return_values(result)
    56 end
