webmcp
annotate 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.
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 | 
| rev | line source | 
|---|---|
| poelzi@31 | 1 --[[-- | 
| poelzi@31 | 2 status_code = -- executes and returns a lua file | 
| poelzi@31 | 3 execute.load_chunk{ | 
| poelzi@31 | 4 file_path = file_path, -- path to a lua source or byte-code file | 
| poelzi@31 | 5 app = app, -- app name to use or the current will be used | 
| poelzi@31 | 6 module = module, -- module where chunk is located | 
| poelzi@31 | 7 chunk = chunk -- filename of lua file to load | 
| poelzi@31 | 8 } | 
| poelzi@31 | 9 | 
| poelzi@31 | 10 This function loads and executes a lua file specified by a given path or constructs | 
| poelzi@31 | 11 a path to load from the module and chunk name. | 
| poelzi@31 | 12 | 
| poelzi@31 | 13 --]]-- | 
| poelzi@31 | 14 | 
| poelzi@31 | 15 function execute.load_chunk(args) | 
| poelzi@31 | 16 local file_path = args.file_path | 
| poelzi@31 | 17 local app = args.app | 
| poelzi@31 | 18 local module = args.module | 
| poelzi@31 | 19 local chunk = args.chunk | 
| poelzi@31 | 20 | 
| poelzi@31 | 21 app = app or request.get_app_name() | 
| poelzi@31 | 22 | 
| poelzi@31 | 23 file_path = file_path or encode.file_path(request.get_app_basepath(), | 
| poelzi@31 | 24 'app', app, module, chunk) | 
| poelzi@31 | 25 | 
| poelzi@31 | 26 local func, load_errmsg = loadfile(file_path) | 
| poelzi@31 | 27 if not func then | 
| poelzi@31 | 28 error('Could not load file "' .. file_path .. '": ' .. load_errmsg) | 
| poelzi@31 | 29 end | 
| poelzi@31 | 30 local result = func() | 
| poelzi@31 | 31 return result | 
| poelzi@31 | 32 end |