pastojs: with localvar do

git-svn-id: trunk@36242 -
This commit is contained in:
Mattias Gaertner 2017-05-18 09:34:54 +00:00
parent 0c66d8b362
commit a328f8df3d
2 changed files with 31 additions and 20 deletions

View File

@ -10143,12 +10143,28 @@ begin
PasExpr:=TPasElement(El.Expressions[i]); PasExpr:=TPasElement(El.Expressions[i]);
Expr:=ConvertElement(PasExpr,AContext); Expr:=ConvertElement(PasExpr,AContext);
// create unique local var name
WithExprScope:=WithScope.ExpressionScopes[i] as TPas2JSWithExprScope; WithExprScope:=WithScope.ExpressionScopes[i] as TPas2JSWithExprScope;
WithExprScope.WithVarName:=FuncContext.CreateLocalIdentifier(FBuiltInNames[pbivnWith]); if (Expr is TJSPrimaryExpressionIdent)
// create local "var $with1 = expr;" and IsValidJSIdentifier(TJSPrimaryExpressionIdent(Expr).Name) then
V:=CreateVarStatement(WithExprScope.WithVarName,Expr,PasExpr); begin
AddToStatementList(FirstSt,LastSt,V,PasExpr); // expression is already a local variable
WithExprScope.WithVarName:=String(TJSPrimaryExpressionIdent(Expr).Name);
Expr.Free;
end
else if Expr is TJSPrimaryExpressionThis then
begin
// expression is 'this'
WithExprScope.WithVarName:='this';
Expr.Free;
end
else
begin
// create unique local var name
WithExprScope.WithVarName:=FuncContext.CreateLocalIdentifier(FBuiltInNames[pbivnWith]);
// create local "var $with1 = expr;"
V:=CreateVarStatement(WithExprScope.WithVarName,Expr,PasExpr);
AddToStatementList(FirstSt,LastSt,V,PasExpr);
end;
end; end;
if Assigned(El.Body) then if Assigned(El.Body) then
begin begin

View File

@ -7912,18 +7912,16 @@ begin
' if (5 == this.cI) ;', ' if (5 == this.cI) ;',
' if (this.cI == 6) ;', ' if (this.cI == 6) ;',
' if (7 == this.cI) ;', ' if (7 == this.cI) ;',
' var $with1 = this;', ' if (this.cI == 11) ;',
' if ($with1.cI == 11) ;', ' if (12 == this.cI) ;',
' if (12 == $with1.cI) ;',
' };', ' };',
' this.DoMore = function () {', ' this.DoMore = function () {',
' if (this.cI == 8) ;', ' if (this.cI == 8) ;',
' if (this.cI == 9) ;', ' if (this.cI == 9) ;',
' if (10 == this.cI) ;', ' if (10 == this.cI) ;',
' if (11 == this.cI) ;', ' if (11 == this.cI) ;',
' var $with1 = this;', ' if (this.cI == 13) ;',
' if ($with1.cI == 13) ;', ' if (14 == this.cI) ;',
' if (14 == $with1.cI) ;',
' };', ' };',
'});', '});',
'this.Obj = null;', 'this.Obj = null;',
@ -8229,10 +8227,8 @@ begin
' l = rtl.freeLoc(l);', ' l = rtl.freeLoc(l);',
' rtl.free(o, "Obj");', ' rtl.free(o, "Obj");',
' rtl.free(o, "Obj");', ' rtl.free(o, "Obj");',
' var $with1 = o;', ' rtl.free(o, "Obj");',
' rtl.free($with1, "Obj");', ' rtl.free(o, "Obj");',
' var $with2 = o;',
' rtl.free($with2, "Obj");',
' Result = rtl.freeLoc(Result);', ' Result = rtl.freeLoc(Result);',
' Result = rtl.freeLoc(Result);', ' Result = rtl.freeLoc(Result);',
' return Result;', ' return Result;',
@ -9360,7 +9356,7 @@ begin
Add(' a:=test1.texta.new();'); Add(' a:=test1.texta.new();');
Add(' a:=test1.texta.new(3);'); Add(' a:=test1.texta.new(3);');
ConvertProgram; ConvertProgram;
CheckSource('TestExternalClass_ObjectCreate', CheckSource('TestExternalClass_New',
LinesToStr([ // statements LinesToStr([ // statements
'this.A = null;', 'this.A = null;',
'']), '']),
@ -9368,10 +9364,9 @@ begin
'$mod.A = new ExtA();', '$mod.A = new ExtA();',
'$mod.A = new ExtA();', '$mod.A = new ExtA();',
'$mod.A = new ExtA(1,2);', '$mod.A = new ExtA(1,2);',
'var $with1 = ExtA;', '$mod.A = new ExtA();',
'$mod.A = new $with1();', '$mod.A = new ExtA();',
'$mod.A = new $with1();', '$mod.A = new ExtA(2,2);',
'$mod.A = new $with1(2,2);',
'$mod.A = new ExtA();', '$mod.A = new ExtA();',
'$mod.A = new ExtA();', '$mod.A = new ExtA();',
'$mod.A = new ExtA(3,2);', '$mod.A = new ExtA(3,2);',