|
1 | | -local function __PW__ClassExtends(target, base) |
2 | | - |
3 | | - target.____super = base; |
4 | | - |
5 | | - local staticMetatable = setmetatable({ __index = base }, base); |
6 | | - setmetatable(target, staticMetatable); |
7 | | - |
8 | | - local baseMetatable = getmetatable(base); |
9 | | - if baseMetatable then |
10 | | - if type(baseMetatable.__index) == "function" then |
11 | | - staticMetatable.__index = baseMetatable.__index; |
12 | | - end |
13 | | - if type(baseMetatable.__newindex) == "function" then |
14 | | - staticMetatable.__newindex = baseMetatable.__newindex; |
15 | | - end |
16 | | - end |
17 | | - |
18 | | - ------------------------------------ |
19 | | - |
20 | | - -- PZ Class-Structure reroute. |
21 | | - if base.prototype == nil then |
22 | | - base.prototype = base; |
| 1 | +local ____lualib = require('lualib_bundle') |
| 2 | +local __TS__Class = ____lualib.__TS__Class |
| 3 | +local __TS__New = ____lualib.__TS__New |
| 4 | +local __TS__ClassExtends = ____lualib.__TS__ClassExtends |
| 5 | + |
| 6 | +-- insert before ts class extends |
| 7 | +local function __PW__ClassExtendsPatch(target, base) |
| 8 | + -- PZ fix |
| 9 | + -- if base is pz class, then add ts class prop |
| 10 | + if base.prototype == nil and base.Type then |
| 11 | + base.prototype = base |
| 12 | + base.prototype.constructor = base; |
23 | 13 | end |
| 14 | +end |
24 | 15 |
|
25 | | - -- PZ 'Type' field reroute. |
26 | | - target.Type = target.prototype.name; |
27 | | - target.prototype.Type = target.Type; |
| 16 | +local function derive(self, type) |
| 17 | + local __Cls = __TS__Class() |
| 18 | + __Cls.name = type |
| 19 | + __Cls.Type = type |
28 | 20 |
|
29 | | - -- PZ Constructor reroute. |
30 | | - if base.prototype.new then |
31 | | - base.prototype.____constructor = base.prototype.new; |
32 | | - target.prototype.____constructor = base.prototype.new; |
33 | | - end |
34 | | - |
35 | | - ------------------------------------ |
| 21 | + __PW__ClassExtendsPatch(__Cls, self) |
| 22 | + __TS__ClassExtends(__Cls, self) |
| 23 | + return __Cls |
| 24 | +end |
36 | 25 |
|
37 | | - setmetatable(target.prototype, base.prototype); |
| 26 | +local function new(self, ...) |
| 27 | + return __TS__New(self, ...) |
| 28 | +end |
38 | 29 |
|
39 | | - if type(base.prototype.__index) == "function" then |
40 | | - target.prototype.__index = base.prototype.__index; |
41 | | - end |
| 30 | +-- top level base class, like Object class in js or ISBaseObject class in pz |
| 31 | +local __PW__BaseClass = __TS__Class() |
| 32 | +__PW__BaseClass.name = '__PW__BaseClass' |
| 33 | +__PW__BaseClass.Type = __PW__BaseClass.name |
| 34 | +__PW__BaseClass.prototype.derive = derive |
| 35 | +__PW__BaseClass.prototype.new = new |
42 | 36 |
|
43 | | - if type(base.prototype.__newindex) == "function" then |
44 | | - target.prototype.__newindex = base.prototype.__newindex; |
45 | | - end |
| 37 | +-- insert after ts class name set |
| 38 | +local function __PW__ClassPatch(cls) |
| 39 | + cls.Type = cls.name |
| 40 | +end |
46 | 41 |
|
47 | | - if type(base.prototype.__tostring) == "function" then |
48 | | - target.prototype.__tostring = base.prototype.__tostring; |
49 | | - end |
| 42 | +-- add |
| 43 | +local function __PW__BaseClassExtends(cls) |
| 44 | + -- if cls not extend other class, then extend base class |
| 45 | + -- to get the implementation of derive and new |
| 46 | + __TS__ClassExtends(cls, __PW__BaseClass) |
50 | 47 | end |
51 | 48 |
|
52 | 49 | return { |
53 | | - __PW__ClassExtends = __PW__ClassExtends |
| 50 | + __PW__ClassExtendsPatch = __PW__ClassExtendsPatch, |
| 51 | + __PW__ClassPatch = __PW__ClassPatch, |
| 52 | + -- expose base class for third-party modification |
| 53 | + __PW__BaseClass = __PW__BaseClass, |
| 54 | + __PW__BaseClassExtends = __PW__BaseClassExtends, |
54 | 55 | } |
0 commit comments