@@ -134,25 +134,29 @@ local function set(className, cls)
134134end
135135
136136local function multiKey (t , ...)
137- local k
138- for i = 1 , select (" #" , ... ) do
139- k = select (i , ... )
140- assert (k )
137+ local n , i , k = select (" #" , ... ), 1
138+ while true do
139+ k = assert (select (i , ... ))
140+ if i == n then
141+ break
142+ end
141143 local tk = t [k ]
142144 if tk == nil then
143145 tk = {}
144146 t [k ] = tk
145147 end
146148 t = tk
149+ i = i + 1
147150 end
148151 return t , k
149152end
150153
151154local function genericName (name , ...)
152- local t = { name , " [" }
153- local count = 3
155+ local n = select (" #" , ... )
156+ local t = { name , " `" , n , " [" }
157+ local count = 5
154158 local hascomma
155- for i = 1 , select ( " # " , ... ) do
159+ for i = 1 , n do
156160 local cls = select (i , ... )
157161 if hascomma then
158162 t [count ] = " ,"
@@ -280,27 +284,7 @@ local function setHasStaticCtor(cls, kind)
280284 setmetatable (cls , staticCtorMetatable )
281285end
282286
283- local function def (name , kind , cls , generic )
284- if type (cls ) == " function" then
285- if generic then
286- generic .__index = generic
287- generic .__call = new
288- end
289- local mt = {}
290- local fn = function (_ , ...)
291- local gt , gk = multiKey (mt , ... )
292- local t = gt [gk ]
293- if t == nil then
294- t = def (genericName (name , ... ), kind , cls (... ) or {}, true )
295- if generic then
296- setmetatable (t , generic )
297- end
298- gt [gk ] = t
299- end
300- return t
301- end
302- return set (name , setmetatable (generic or {}, { __call = fn , __index = Object }))
303- end
287+ local function defCore (name , kind , cls , generic )
304288 cls = cls or {}
305289 cls .__name__ = name
306290 if not generic then
@@ -328,22 +312,88 @@ local function def(name, kind, cls, generic)
328312 return cls
329313end
330314
331- local function defCls (name , cls , genericSuper )
332- return def (name , " C" , cls , genericSuper )
315+ local function def (name , kind , cls , generic )
316+ if type (cls ) == " function" then
317+ if generic then
318+ generic .__index = generic
319+ generic .__call = new
320+ end
321+ local mt = {}
322+ local fn = function (_ , ...)
323+ local gt , gk = multiKey (mt , ... )
324+ local t = gt [gk ]
325+ if t == nil then
326+ t = defCore (genericName (name , ... ), kind , cls (... ) or {}, true )
327+ if generic then
328+ setmetatable (t , generic )
329+ end
330+ gt [gk ] = t
331+ end
332+ return t
333+ end
334+ return set (name , setmetatable (generic or {}, { __call = fn , __index = Object }))
335+ else
336+ return defCore (name , kind , cls , generic )
337+ end
338+ end
339+
340+ local function defCls (name , cls , generic )
341+ return def (name , " C" , cls , generic )
333342end
334343
335344local function defInf (name , cls )
336345 return def (name , " I" , cls )
337346end
338347
339- local function defStc (name , cls , genericSuper )
340- return def (name , " S" , cls , genericSuper )
348+ local function defStc (name , cls , generic )
349+ return def (name , " S" , cls , generic )
341350end
342351
343352local function defEnum (name , cls )
344353 return def (name , " E" , cls )
345354end
346355
356+ local function defArray (name , cls , Array , MultiArray )
357+ Array .__index = Array
358+ MultiArray .__index = MultiArray
359+ setmetatable (MultiArray , Array )
360+
361+ local mt = {}
362+ local function create (Array , T )
363+ local ArrayT = mt [T ]
364+ if ArrayT == nil then
365+ ArrayT = defCore (T .__name__ .. " []" , " C" , cls (T ), true )
366+ setmetatable (ArrayT , Array )
367+ mt [T ] = ArrayT
368+ end
369+ return ArrayT
370+ end
371+
372+ local mtMulti = {}
373+ local function createMulti (MultiArray , T , dimension )
374+ local gt , gk = multiKey (mtMulti , T , dimension )
375+ local ArrayT = gt [gk ]
376+ if ArrayT == nil then
377+ local name = T .__name__ .. " [" .. (" ," ):rep (dimension - 1 ) .. " ]"
378+ ArrayT = defCore (name , " C" , cls (T ), true )
379+ setmetatable (ArrayT , MultiArray )
380+ gt [gk ] = ArrayT
381+ end
382+ return ArrayT
383+ end
384+
385+ return set (name , setmetatable (Array , {
386+ __index = Object ,
387+ __call = function (Array , T , dimension )
388+ if not dimension then
389+ return create (Array , T )
390+ else
391+ return createMulti (MultiArray , T , dimension )
392+ end
393+ end
394+ }))
395+ end
396+
347397local function trunc (num )
348398 return num > 0 and floor (num ) or ceil (num )
349399end
@@ -366,6 +416,7 @@ System = {
366416 defInf = defInf ,
367417 defStc = defStc ,
368418 defEnum = defEnum ,
419+ defArray = defArray ,
369420 trunc = trunc ,
370421 global = global ,
371422 yieldReturn = cyield ,
@@ -379,8 +430,14 @@ System.luaVersion = version
379430
380431if version < 5.3 then
381432 local bnot , band , bor , xor , sl , sr
382- local ok , bit = pcall (require , " bit" )
383- if ok then
433+ local bit = rawget (global , " bit" )
434+ if not bit then
435+ local ok , b = pcall (require , " bit" )
436+ if ok then
437+ bit = b
438+ end
439+ end
440+ if bit then
384441 bnot , band , bor , xor , sl , sr = bit .bnot , bit .band , bit .bor , bit .bxor , bit .lshift , bit .rshift
385442 else
386443 local function disable ()
0 commit comments