Changeset 39
- Timestamp:
- 05/01/2011 11:13:36 PM (2 years ago)
- File:
-
- 1 edited
-
trunk/lua/chdku.lua (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lua/chdku.lua
r38 r39 39 39 end 40 40 41 --[[ 41 -- TODO this should be split out into it's own module(s) 42 --[[ 43 simple library system for building remote commands 42 44 chunks of source code to be used remotely 43 45 can be used with chdku.exec 44 46 TODO some of these are duplicated with local code, but we don't yet have an easy way of sharing them 45 47 TODO would be good to minify 46 TODO handle order and dependencies47 48 TODO passing compiled chunks might be better but our lua configurations are different 48 49 ]] 49 chdku.rlib={ 50 local rlibs = { 51 libs={}, 52 } 53 54 --[[ 55 register{ 56 name='libname' 57 depend={'lib1','lib2'...}, -- already registered rlibs this one requires (cyclic deps not allowed) 58 code='', -- main lib code. 59 } 60 ]] 61 function rlibs:register(t) 62 if type(t.depend) == 'nil' then 63 t.depend = {} 64 elseif type(t.depend) ~= 'table' then 65 error('expected dependency table') 66 end 67 if type(t.code) ~= 'string' then 68 error('expected code string') 69 end 70 if type(t.name) ~= 'string' then 71 error('expected name string') 72 end 73 for i,v in ipairs(t.depend) do 74 if not self.libs[v] then 75 errf('%s missing dep %s\n',t.name,v) 76 end 77 end 78 self.libs[t.name] = t 79 end 80 81 --[[ 82 register an array of libs 83 ]] 84 function rlibs:register_array(t) 85 for i,v in ipairs(t) do 86 self:register(v) 87 end 88 end 89 90 --[[ 91 add deps for a single lib 92 ]] 93 function rlibs:build_single(build,name) 94 local lib = self.libs[name] 95 if not lib then 96 errf('unknown lib %s\n',tostring(name)) 97 end 98 -- already added 99 if build.map[name] then 100 return 101 end 102 for i,dname in ipairs(lib.depend) do 103 self:build_single(build,dname) 104 end 105 build.map[name]=lib 106 table.insert(build.list,lib) 107 end 108 --[[ 109 return a list of rlibs in dependency order 110 ]] 111 function rlibs:build_list(libnames) 112 local build={ 113 list={}, 114 map={}, 115 } 116 for i,name in ipairs(libnames) do 117 self:build_single(build,name) 118 end 119 return build.list 120 end 121 --[[ 122 return a string containing all the required rlib code 123 ]] 124 function rlibs:build(names) 125 local liblist = self:build_list(names) 126 -- TODO would be good to keep a map of line numbers here somehow 127 -- or possibly exec should keep the code around ? 128 local code="" 129 for i,lib in ipairs(liblist) do 130 code = code .. lib.code .. '\n' 131 end 132 return code 133 end 134 135 rlibs:register_array{ 50 136 --[[ 51 137 mostly duplicated from util.serialize 52 138 global defaults can be changed from code 53 139 ]] 54 serialize=[[ 140 { 141 name='serialize', 142 code=[[ 55 143 serialize_r = function(v,opts,seen,depth) 56 144 local vt = type(v) … … 119 207 return serialize_r(v,opts) 120 208 end 121 122 209 ]], 210 }, 123 211 -- override default table serialization for messages 124 serialize_msgs=[[ 212 { 213 name='serialize_msgs', 214 depend={'serialize'}, 215 code=[[ 125 216 usb_msg_table_to_string=serialize 126 217 ]], 218 }, 127 219 --[[ 128 220 status[,err]=dir_iter(path,func,opts) … … 132 224 func is called with a nil filename after listing is complete 133 225 ]] 134 dir_iter=[[ 226 { 227 name='dir_iter', 228 code=[[ 135 229 function dir_iter(path,func,opts) 136 230 if not opts then … … 151 245 end 152 246 ]], 247 }, 153 248 --[[ 154 249 function to batch stuff in groups of messages … … 161 256 b:flush() sends any remaining items 162 257 ]] 163 msg_batch=[[ 258 { 259 name='msg_batcher', 260 depend={'serialize_msgs'}, 261 code=[[ 164 262 function msg_batcher(opts_in) 165 263 local t = { … … 195 293 end 196 294 ]], 295 }, 197 296 --[[ 198 297 retrieve a directory listing of names, batched in messages 199 298 ]] 200 ls_simple=[[ 299 { 300 name='ls_simple', 301 depend={'msg_batcher'}, 302 code=[[ 201 303 function ls_simple(path) 202 304 local b=msg_batcher() … … 213 315 end 214 316 ]], 317 }, 215 318 --[[ 216 319 TODO rework this to a general iterate over directory function … … 242 345 TODO handle case if 'path' is a file 243 346 ]] 244 ls=[[ 347 { 348 name='ls', 349 depend={'serialize_msgs'}, 350 code=[[ 245 351 function ls(path,opts_in) 246 352 local opts={ … … 293 399 end 294 400 ]], 295 } 296 401 }, 402 } 403 404 chdku.rlibs = rlibs 297 405 --[[ 298 406 opts may be a table, or a string containing lua code for a table … … 310 418 { 311 419 wait=true, 312 libs={' serialize','serialize_msgs','ls'},420 libs={'ls'}, 313 421 msgs=function(msg) 314 422 if msg.subtype ~= 'table' then … … 368 476 local opts = extend_table({},opts_in) 369 477 if opts.libs then 370 local libcode='' 371 for k,v in ipairs(opts.libs) do 372 if chdku.rlib[v] then 373 libcode = libcode .. chdku.rlib[v]; 374 else 375 return false,'unknown rlib'..v 376 end 377 end 378 code = libcode .. code 478 code = chdku.rlibs:build(opts.libs) .. code 379 479 end 380 480 local status,err=chdk.execlua(code)
Note: See TracChangeset
for help on using the changeset viewer.