-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClass.ttslua
executable file
·82 lines (67 loc) · 2.35 KB
/
Class.ttslua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---@class ge_tts__Class
local Class = {}
---@param object table
---@return nil | table
function Class.getClass(object)
return (--[[---@type { __class: nil | table } ]] object).__class
end
---@param sourceClass table
---@param targetClass table
---@return boolean
local function isDescendentClass(sourceClass, targetClass)
if sourceClass == targetClass then
return true
end
local parentClass = (--[[---@type { __index: nil | table } ]] getmetatable(sourceClass)).__index
return parentClass ~= nil and isDescendentClass(--[[---@not nil]] parentClass, targetClass)
end
---@type table<table, nil | table<table, nil | boolean>>
local descendentCache = {}
---@param sourceClass table
---@param targetClass table
---@return boolean
function Class.isDescendentClass(sourceClass, targetClass)
if sourceClass == targetClass then
return true
end
local sourceCache = descendentCache[sourceClass]
if sourceCache then
local isDescendent = (--[[---@not nil]] sourceCache)[targetClass]
if isDescendent ~= nil then
return --[[---@not nil]] isDescendent
end
else
sourceCache = {}
descendentCache[sourceClass] = sourceCache
end
local parentClass = (--[[---@type { __index: nil | table } ]] getmetatable(sourceClass)).__index
local isDescendent = parentClass ~= nil and isDescendentClass(--[[---@not nil]] parentClass, targetClass)
;(--[[---@not nil]] sourceCache)[targetClass] = isDescendent
return isDescendent
end
---@param object table
---@return boolean
function Class.isInstance(object, class)
local objectClass = Class.getClass(object)
return objectClass ~= nil and Class.isDescendentClass(--[[---@not nil]] Class.getClass(object), class)
end
---@generic AncestorClass
---@generic Class : AncestorClass
---@param descendent Class
---@param parent nil | AncestorClass
---@return AncestorClass
function Class.parentConstructor(descendent, parent)
local metatable = --[[---@type { __call: function }]] getmetatable(parent)
local call = metatable.__call
local wrappedStaticParent = function(...)
return call(descendent, ...)
end
return --[[---@type AncestorClass]] wrappedStaticParent
end
---@generic Class
---@param class Class
---@return { __class: Class }
function Class.rootConstructor(class)
return { __class = class }
end
return Class