Browse Source

pastojs: with localvar do

git-svn-id: trunk@36242 -
Mattias Gaertner 8 years ago
parent
commit
a328f8df3d
2 changed files with 31 additions and 20 deletions
  1. 21 5
      packages/pastojs/src/fppas2js.pp
  2. 10 15
      packages/pastojs/tests/tcmodules.pas

+ 21 - 5
packages/pastojs/src/fppas2js.pp

@@ -10143,12 +10143,28 @@ begin
         PasExpr:=TPasElement(El.Expressions[i]);
         Expr:=ConvertElement(PasExpr,AContext);
 
-        // create unique local var name
         WithExprScope:=WithScope.ExpressionScopes[i] as TPas2JSWithExprScope;
-        WithExprScope.WithVarName:=FuncContext.CreateLocalIdentifier(FBuiltInNames[pbivnWith]);
-        // create local "var $with1 = expr;"
-        V:=CreateVarStatement(WithExprScope.WithVarName,Expr,PasExpr);
-        AddToStatementList(FirstSt,LastSt,V,PasExpr);
+        if (Expr is TJSPrimaryExpressionIdent)
+            and IsValidJSIdentifier(TJSPrimaryExpressionIdent(Expr).Name) then
+          begin
+          // 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;
       if Assigned(El.Body) then
         begin

+ 10 - 15
packages/pastojs/tests/tcmodules.pas

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