pastojs: fixed assigning class var of descended classes

git-svn-id: trunk@40168 -
This commit is contained in:
Mattias Gaertner 2018-11-01 22:40:57 +00:00
parent 3f50c51225
commit c39cc4475f
2 changed files with 62 additions and 41 deletions

View File

@ -6584,6 +6584,16 @@ begin
begin begin
Result:=ConvertTObjectFree_Bin(El,RightEl,AContext); Result:=ConvertTObjectFree_Bin(El,RightEl,AContext);
exit; exit;
end
else if (RightRef.Access in rraAllWrite)
and aResolver.IsClassField(RightRefDecl) then
begin
// e.g. "Something.aClassVar:=" -> "aClass.aClassVar:="
Left:=CreateReferencePathExpr(RightRefDecl.Parent,AContext);
Result:=TJSDotMemberExpression(CreateElement(TJSDotMemberExpression,El));
TJSDotMemberExpression(Result).MExpr:=Left;
TJSDotMemberExpression(Result).Name:=TJSString(TransformVariableName(RightRefDecl,AContext));
exit;
end; end;
end; end;
@ -6847,6 +6857,7 @@ var
Value: TResEvalValue; Value: TResEvalValue;
aResolver: TPas2JSResolver; aResolver: TPas2JSResolver;
BracketExpr: TJSBracketMemberExpression; BracketExpr: TJSBracketMemberExpression;
PathExpr: TJSElement;
begin begin
Result:=nil; Result:=nil;
if not (El.CustomData is TResolvedReference) then if not (El.CustomData is TResolvedReference) then
@ -6954,6 +6965,16 @@ begin
CallImplicit(Decl); CallImplicit(Decl);
exit; exit;
end end
else if (Ref.Access in rraAllWrite)
and aResolver.IsClassField(Decl) then
begin
// writing a class var -> aClass.VarName
PathExpr:=CreateReferencePathExpr(Decl.Parent,AContext);
Result:=TJSDotMemberExpression(CreateElement(TJSDotMemberExpression,El));
TJSDotMemberExpression(Result).MExpr:=PathExpr;
TJSDotMemberExpression(Result).Name:=TJSString(TransformVariableName(Decl,AContext));
exit;
end
else if Decl.ClassType=TPasConst then else if Decl.ClassType=TPasConst then
begin begin
if TPasConst(Decl).IsConst and (TPasConst(Decl).Expr<>nil) then if TPasConst(Decl).IsConst and (TPasConst(Decl).Expr<>nil) then
@ -6963,10 +6984,10 @@ begin
if Value<>nil then if Value<>nil then
try try
if Value.Kind in [revkNil,revkBool,revkInt,revkUInt,revkFloat,revkEnum] then if Value.Kind in [revkNil,revkBool,revkInt,revkUInt,revkFloat,revkEnum] then
begin begin
Result:=ConvertConstValue(Value,AContext,El); Result:=ConvertConstValue(Value,AContext,El);
exit; exit;
end; end;
finally finally
ReleaseEvalValue(Value); ReleaseEvalValue(Value);
end; end;
@ -9389,7 +9410,6 @@ begin
// left side is a variable // left side is a variable
if AssignContext.RightSide=nil then if AssignContext.RightSide=nil then
RaiseInconsistency(20180622211919,El); RaiseInconsistency(20180622211919,El);
end; end;
// convert inc(avar,b) to a+=b // convert inc(avar,b) to a+=b

View File

@ -448,7 +448,8 @@ type
Procedure TestClass_CallInherited_NoParams; Procedure TestClass_CallInherited_NoParams;
Procedure TestClass_CallInherited_WithParams; Procedure TestClass_CallInherited_WithParams;
Procedure TestClasS_CallInheritedConstructor; Procedure TestClasS_CallInheritedConstructor;
Procedure TestClass_ClassVar; Procedure TestClass_ClassVar_Assign;
//ToDo Procedure TestClass_ClassVar_Arg;
Procedure TestClass_CallClassMethod; Procedure TestClass_CallClassMethod;
Procedure TestClass_Property; Procedure TestClass_Property;
Procedure TestClass_Property_ClassMethod; Procedure TestClass_Property_ClassMethod;
@ -9957,7 +9958,7 @@ begin
])); ]));
end; end;
procedure TTestModule.TestClass_ClassVar; procedure TTestModule.TestClass_ClassVar_Assign;
begin begin
StartProgram(false); StartProgram(false);
Add([ Add([
@ -9991,7 +9992,7 @@ begin
' obj.sub:=nil;', ' obj.sub:=nil;',
' obj.sub.sub:=nil;']); ' obj.sub.sub:=nil;']);
ConvertProgram; ConvertProgram;
CheckSource('TestClass_ClassVar', CheckSource('TestClass_ClassVar_Assign',
LinesToStr([ // statements LinesToStr([ // statements
'rtl.createClass($mod,"TObject",null,function(){', 'rtl.createClass($mod,"TObject",null,function(){',
' this.vI = 0;', ' this.vI = 0;',
@ -10001,15 +10002,15 @@ begin
' this.$final = function () {', ' this.$final = function () {',
' };', ' };',
' this.Create = function(){', ' this.Create = function(){',
' this.$class.vI = this.vI+1;', ' $mod.TObject.vI = this.vI+1;',
' this.$class.vI = this.vI+1;', ' $mod.TObject.vI = this.vI+1;',
' this.$class.vI += 1;', ' $mod.TObject.vI += 1;',
' };', ' };',
' this.GetIt = function(Par){', ' this.GetIt = function(Par){',
' var Result = null;', ' var Result = null;',
' this.vI = this.vI + Par;', ' $mod.TObject.vI = this.vI + Par;',
' this.vI = this.vI + Par;', ' $mod.TObject.vI = this.vI + Par;',
' this.vI += 1;', ' $mod.TObject.vI += 1;',
' Result = this.Sub;', ' Result = this.Sub;',
' return Result;', ' return Result;',
' };', ' };',
@ -10021,8 +10022,8 @@ begin
'$mod.TObject.vI = 3;', '$mod.TObject.vI = 3;',
'if ($mod.TObject.vI === 4);', 'if ($mod.TObject.vI === 4);',
'$mod.TObject.Sub=null;', '$mod.TObject.Sub=null;',
'$mod.Obj.$class.Sub=null;', '$mod.TObject.Sub=null;',
'$mod.Obj.Sub.$class.Sub=null;', '$mod.TObject.Sub=null;',
''])); '']));
end; end;
@ -10079,25 +10080,25 @@ begin
' this.$final = function () {', ' this.$final = function () {',
' };', ' };',
' this.Create = function(){', ' this.Create = function(){',
' this.$class.Sub = this.$class.GetIt(3);', ' $mod.TObject.Sub = this.$class.GetIt(3);',
' this.$class.vI = this.GetMore(4);', ' $mod.TObject.vI = this.GetMore(4);',
' this.$class.Sub = this.$class.GetIt(5);', ' $mod.TObject.Sub = this.$class.GetIt(5);',
' this.$class.vI = this.GetMore(6);', ' $mod.TObject.vI = this.GetMore(6);',
' };', ' };',
' this.GetMore = function(Par){', ' this.GetMore = function(Par){',
' var Result = 0;', ' var Result = 0;',
' this.$class.Sub = this.$class.GetIt(11);', ' $mod.TObject.Sub = this.$class.GetIt(11);',
' this.$class.vI = this.GetMore(12);', ' $mod.TObject.vI = this.GetMore(12);',
' this.$class.Sub = this.$class.GetIt(13);', ' $mod.TObject.Sub = this.$class.GetIt(13);',
' this.$class.vI = this.GetMore(14);', ' $mod.TObject.vI = this.GetMore(14);',
' return Result;', ' return Result;',
' };', ' };',
' this.GetIt = function(Par){', ' this.GetIt = function(Par){',
' var Result = null;', ' var Result = null;',
' this.Sub = this.GetIt(21);', ' $mod.TObject.Sub = this.GetIt(21);',
' this.vI = this.Sub.GetMore(22);', ' $mod.TObject.vI = this.Sub.GetMore(22);',
' this.Sub = this.GetIt(23);', ' $mod.TObject.Sub = this.GetIt(23);',
' this.vI = this.Sub.GetMore(24);', ' $mod.TObject.vI = this.Sub.GetMore(24);',
' return Result;', ' return Result;',
' };', ' };',
'});', '});',
@ -10108,7 +10109,7 @@ begin
'$mod.TObject.GetIt(5);', '$mod.TObject.GetIt(5);',
'$mod.Obj.$class.GetIt(6);', '$mod.Obj.$class.GetIt(6);',
'$mod.Obj.Sub.$class.GetIt(7);', '$mod.Obj.Sub.$class.GetIt(7);',
'$mod.Obj.Sub.$class.GetIt(8).$class.Sub=null;', '$mod.TObject.Sub=null;',
'$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);', '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
'$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);', '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
''])); '']));
@ -11768,8 +11769,8 @@ begin
' function Sub() {', ' function Sub() {',
' Self.Key = Self.Key + 2;', ' Self.Key = Self.Key + 2;',
' Self.Key = Self.Key + 3;', ' Self.Key = Self.Key + 3;',
' Self.$class.State = Self.State + 4;', ' $mod.TObject.State = Self.State + 4;',
' Self.$class.State = Self.State + 5;', ' $mod.TObject.State = Self.State + 5;',
' $mod.TObject.State = $mod.TObject.State + 6;', ' $mod.TObject.State = $mod.TObject.State + 6;',
' Self.SetSize(Self.GetSize() + 7);', ' Self.SetSize(Self.GetSize() + 7);',
' Self.SetSize(Self.GetSize() + 8);', ' Self.SetSize(Self.GetSize() + 8);',
@ -11777,8 +11778,8 @@ begin
' Sub();', ' Sub();',
' Self.Key = Self.Key + 12;', ' Self.Key = Self.Key + 12;',
' Self.Key = Self.Key + 13;', ' Self.Key = Self.Key + 13;',
' Self.$class.State = Self.State + 14;', ' $mod.TObject.State = Self.State + 14;',
' Self.$class.State = Self.State + 15;', ' $mod.TObject.State = Self.State + 15;',
' $mod.TObject.State = $mod.TObject.State + 16;', ' $mod.TObject.State = $mod.TObject.State + 16;',
' Self.SetSize(Self.GetSize() + 17);', ' Self.SetSize(Self.GetSize() + 17);',
' Self.SetSize(Self.GetSize() + 18);', ' Self.SetSize(Self.GetSize() + 18);',
@ -11844,8 +11845,8 @@ begin
' function Sub() {', ' function Sub() {',
' Self.Key = Self.Key + 2;', ' Self.Key = Self.Key + 2;',
' Self.Key = Self.Key + 3;', ' Self.Key = Self.Key + 3;',
' Self.$class.State = Self.State + 4;', ' $mod.TObject.State = Self.State + 4;',
' Self.$class.State = Self.State + 5;', ' $mod.TObject.State = Self.State + 5;',
' $mod.TObject.State = $mod.TObject.State + 6;', ' $mod.TObject.State = $mod.TObject.State + 6;',
' Self.SetSize(Self.GetSize() + 7);', ' Self.SetSize(Self.GetSize() + 7);',
' Self.SetSize(Self.GetSize() + 8);', ' Self.SetSize(Self.GetSize() + 8);',
@ -11853,8 +11854,8 @@ begin
' Sub();', ' Sub();',
' Self.Key = Self.Key + 12;', ' Self.Key = Self.Key + 12;',
' Self.Key = Self.Key + 13;', ' Self.Key = Self.Key + 13;',
' Self.$class.State = Self.State + 14;', ' $mod.TObject.State = Self.State + 14;',
' Self.$class.State = Self.State + 15;', ' $mod.TObject.State = Self.State + 15;',
' $mod.TObject.State = $mod.TObject.State + 16;', ' $mod.TObject.State = $mod.TObject.State + 16;',
' Self.SetSize(Self.GetSize() + 17);', ' Self.SetSize(Self.GetSize() + 17);',
' Self.SetSize(Self.GetSize() + 18);', ' Self.SetSize(Self.GetSize() + 18);',
@ -11910,16 +11911,16 @@ begin
' this.DoIt = function () {', ' this.DoIt = function () {',
' var Self = this;', ' var Self = this;',
' function Sub() {', ' function Sub() {',
' Self.State = Self.State + 2;', ' $mod.TObject.State = Self.State + 2;',
' Self.State = Self.State + 3;', ' $mod.TObject.State = Self.State + 3;',
' $mod.TObject.State = $mod.TObject.State + 4;', ' $mod.TObject.State = $mod.TObject.State + 4;',
' Self.SetSize(Self.GetSize() + 5);', ' Self.SetSize(Self.GetSize() + 5);',
' Self.SetSize(Self.GetSize() + 6);', ' Self.SetSize(Self.GetSize() + 6);',
' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);', ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
' };', ' };',
' Sub();', ' Sub();',
' Self.State = Self.State + 12;', ' $mod.TObject.State = Self.State + 12;',
' Self.State = Self.State + 13;', ' $mod.TObject.State = Self.State + 13;',
' $mod.TObject.State = $mod.TObject.State + 14;', ' $mod.TObject.State = $mod.TObject.State + 14;',
' Self.SetSize(Self.GetSize() + 15);', ' Self.SetSize(Self.GetSize() + 15);',
' Self.SetSize(Self.GetSize() + 16);', ' Self.SetSize(Self.GetSize() + 16);',
@ -12478,7 +12479,7 @@ begin
'this.C = null;' 'this.C = null;'
]), ]),
LinesToStr([ // $mod.$main LinesToStr([ // $mod.$main
'$mod.C.id = $mod.C.id;', '$mod.TObject.id = $mod.C.id;',
''])); '']));
end; end;
@ -12628,7 +12629,7 @@ begin
' b = this === null;', ' b = this === null;',
' b = this.GlobalId === 3;', ' b = this.GlobalId === 3;',
' b = 4 === this.GlobalId;', ' b = 4 === this.GlobalId;',
' this.GlobalId = 5;', ' $mod.TObject.GlobalId = 5;',
' this.ProcA();', ' this.ProcA();',
' };', ' };',
'});' '});'