poelzi@31: --[[-- jbe@46: return_value = -- return value of executed chunk jbe@46: execute.chunk{ poelzi@31: file_path = file_path, -- path to a lua source or byte-code file poelzi@31: app = app, -- app name to use or the current will be used poelzi@31: module = module, -- module where chunk is located jbe@46: chunk = chunk -- name of chunk (filename without .lua extension) poelzi@32: id = id, -- id to be returned by param.get_id(...) during execution poelzi@32: params = params -- parameters to be returned by param.get(...) during execution poelzi@31: } poelzi@31: jbe@205: 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. poelzi@31: poelzi@31: --]]-- poelzi@31: jbe@205: local function pack_return_values(...) jbe@205: local storage = {...} jbe@205: storage.n = select("#", ...) jbe@205: end jbe@205: jbe@205: local function unpack_return_values(storage) jbe@205: return table.unpack(storage, 1, storage.n) jbe@205: end jbe@205: jbe@46: function execute.chunk(args) poelzi@31: local file_path = args.file_path poelzi@31: local app = args.app poelzi@31: local module = args.module poelzi@31: local chunk = args.chunk poelzi@32: local id = args.id poelzi@32: local params = args.params poelzi@31: poelzi@31: app = app or request.get_app_name() poelzi@31: jbe@46: file_path = file_path or encode.file_path( jbe@46: request.get_app_basepath(), jbe@46: 'app', app, module, chunk .. '.lua' jbe@46: ) poelzi@32: poelzi@31: local func, load_errmsg = loadfile(file_path) poelzi@31: if not func then poelzi@31: error('Could not load file "' .. file_path .. '": ' .. load_errmsg) poelzi@31: end poelzi@32: poelzi@32: if id or params then poelzi@32: param.exchange(id, params) poelzi@32: end poelzi@32: jbe@205: local result = pack_return_values(func()) poelzi@32: poelzi@32: if id or params then poelzi@32: param.restore() poelzi@32: end poelzi@32: jbe@205: return unpack_return_values(result) poelzi@31: end