Skip to content

Commit e06abf2

Browse files
PurpleNoonzhangzemeng
andauthored
Mutual inheritance between pz class and ts class (#124)
* refactor: Refactoring the logic of mutual inheritance between pz classes and ts classes and related logic * test: Test case for adding logical refactoring that inherits from each other between the pz class and ts class --------- Co-authored-by: zhangzemeng <[email protected]>
1 parent 11fa1b4 commit e06abf2

File tree

13 files changed

+742
-350
lines changed

13 files changed

+742
-350
lines changed

lua/classcreate_template.lua

Lines changed: 0 additions & 9 deletions
This file was deleted.

lua/pipewrench_fixes.lua

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
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;
2313
end
14+
end
2415

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
2820

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
3625

37-
setmetatable(target.prototype, base.prototype);
26+
local function new(self, ...)
27+
return __TS__New(self, ...)
28+
end
3829

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
4236

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
4641

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)
5047
end
5148

5249
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,
5455
}

lua/reimport_template.lua

Lines changed: 0 additions & 9 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
"files": [
1919
"dist/index.js",
2020
"dist/config.js",
21-
"lua/reimport_template.lua",
22-
"lua/pipewrench_fixes.lua",
23-
"lua/classcreate_template.lua"
21+
"lua/pipewrench_fixes.lua"
2422
],
2523
"exports": "./dist/index.js",
2624
"types": "./dist/index.d.ts",

0 commit comments

Comments
 (0)