webmcp
view framework/env/execute/file_path.lua @ 299:47c5f33e4a6b
Trace real time and CPU time
| author | jbe | 
|---|---|
| date | Sun Mar 22 19:25:38 2015 +0100 (2015-03-22) | 
| parents | 82cc171e8510 | 
| children | 4e69ce9a6365 | 
 line source
     1 --[[--
     2 status_code =             -- status code returned by the executed lua file (a string)
     3 execute.file_path{
     4   file_path = file_path,  -- path to a lua source or byte-code file
     5   id        = id,         -- id to be returned by param.get_id(...) during execution
     6   params    = params      -- parameters to be returned by param.get(...) during execution
     7 }
     9 This function loads and executes a lua file specified by a given path. If an "id" or "params" are provided, the param.get_id(...) and/or param.get(...) functions will return the provided values during execution. The lua routine must return true, false, nil or a string. In case of true or nil, this function returns the string "ok", in case of false, this function returns "error", otherwise the string returned by the lua routine will be returned by this function as well.
    11 --]]--
    13 function execute.file_path(args)
    14   local file_path = args.file_path
    15   local id        = args.id
    16   local params    = args.params
    17   local func, load_errmsg = loadfile(file_path, nil, _ENV)
    18   if not func then
    19     error('Could not load file "' .. file_path .. '": ' .. load_errmsg)
    20   end
    21   if id or params then
    22     param.exchange(id, params)
    23   end
    24   local result = func()
    25   if result == nil or result == true then
    26     result = 'ok'
    27   elseif result == false then
    28     result = 'error'
    29   elseif type(result) ~= "string" then
    30     error("Unexpected type of result: " .. type(result))
    31   end
    32   if id or params then
    33     param.restore()
    34   end
    35   return result
    36 end
