webmcp
changeset 46:209a686464a1
Deprecated execute.load_chunk{...} and introduced execute.chunk{...}
author | jbe |
---|---|
date | Sat Oct 16 18:45:35 2010 +0200 (2010-10-16) |
parents | 3c12e7dd93e0 |
children | 09f749330d13 |
files | framework/env/execute/chunk.lua framework/env/execute/load_chunk.lua |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/execute/chunk.lua Sat Oct 16 18:45:35 2010 +0200 1.3 @@ -0,0 +1,48 @@ 1.4 +--[[-- 1.5 +return_value = -- return value of executed chunk 1.6 +execute.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 -- name of chunk (filename without .lua extension) 1.11 + id = id, -- id to be returned by param.get_id(...) during execution 1.12 + params = params -- parameters to be returned by param.get(...) during execution 1.13 +} 1.14 + 1.15 +This function loads and executes a lua file specified by a given path or constructs 1.16 +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. 1.17 + 1.18 +--]]-- 1.19 + 1.20 +function execute.chunk(args) 1.21 + local file_path = args.file_path 1.22 + local app = args.app 1.23 + local module = args.module 1.24 + local chunk = args.chunk 1.25 + local id = args.id 1.26 + local params = args.params 1.27 + 1.28 + app = app or request.get_app_name() 1.29 + 1.30 + file_path = file_path or encode.file_path( 1.31 + request.get_app_basepath(), 1.32 + 'app', app, module, chunk .. '.lua' 1.33 + ) 1.34 + 1.35 + local func, load_errmsg = loadfile(file_path) 1.36 + if not func then 1.37 + error('Could not load file "' .. file_path .. '": ' .. load_errmsg) 1.38 + end 1.39 + 1.40 + if id or params then 1.41 + param.exchange(id, params) 1.42 + end 1.43 + 1.44 + local result = {func()} 1.45 + 1.46 + if id or params then 1.47 + param.restore() 1.48 + end 1.49 + 1.50 + return unpack(result) 1.51 +end
2.1 --- a/framework/env/execute/load_chunk.lua Sat Oct 16 18:04:36 2010 +0200 2.2 +++ b/framework/env/execute/load_chunk.lua Sat Oct 16 18:45:35 2010 +0200 2.3 @@ -1,47 +1,35 @@ 2.4 --[[-- 2.5 -status_code = -- executes and returns a lua file 2.6 +return_value = -- return value of executed chunk 2.7 execute.load_chunk{ 2.8 file_path = file_path, -- path to a lua source or byte-code file 2.9 app = app, -- app name to use or the current will be used 2.10 module = module, -- module where chunk is located 2.11 - chunk = chunk -- filename of lua file to load 2.12 + chunk = chunk -- filename of lua file to load (including filename extension) 2.13 id = id, -- id to be returned by param.get_id(...) during execution 2.14 params = params -- parameters to be returned by param.get(...) during execution 2.15 } 2.16 2.17 +NOTE: execute.load_chunk{...} is DEPRECATED and replaced by execute.chunk{...}. Both functions differ in interpretation of argument "chunk" regarding the filename extenstion '.lua'. 2.18 + 2.19 This function loads and executes a lua file specified by a given path or constructs 2.20 a path to load from the module and chunk name. 2.21 2.22 --]]-- 2.23 2.24 function execute.load_chunk(args) 2.25 - local file_path = args.file_path 2.26 - local app = args.app 2.27 - local module = args.module 2.28 - local chunk = args.chunk 2.29 - local id = args.id 2.30 - local params = args.params 2.31 - 2.32 - app = app or request.get_app_name() 2.33 - 2.34 - file_path = file_path or encode.file_path(request.get_app_basepath(), 2.35 - 'app', app, module, chunk) 2.36 - 2.37 - 2.38 - local func, load_errmsg = loadfile(file_path) 2.39 - if not func then 2.40 - error('Could not load file "' .. file_path .. '": ' .. load_errmsg) 2.41 + local chunk_name 2.42 + if args.chunk then 2.43 + chunk_name = string.match(args.chunk, "^(.*)%.lua$") 2.44 + if not chunk_name then 2.45 + error('"chunk_name" does not end with \'.lua\'') 2.46 + end 2.47 end 2.48 - 2.49 - if id or params then 2.50 - param.exchange(id, params) 2.51 - end 2.52 - 2.53 - local result = func() 2.54 - 2.55 - if id or params then 2.56 - param.restore() 2.57 - end 2.58 - 2.59 - return result 2.60 + return execute.chunk{ 2.61 + file_path = args.file_path, 2.62 + app = args.app, 2.63 + module = args.module, 2.64 + chunk = chunk_name, 2.65 + id = args.id, 2.66 + params = args.params 2.67 + } 2.68 end