Browse Source

pas2js: transformmodulename: use local var

git-svn-id: trunk@45615 -
Mattias Gaertner 5 years ago
parent
commit
f642f6b609
2 changed files with 40 additions and 22 deletions
  1. 11 10
      packages/pastojs/src/fppas2js.pp
  2. 29 12
      packages/pastojs/tests/tcmodules.pas

+ 11 - 10
packages/pastojs/src/fppas2js.pp

@@ -9368,7 +9368,7 @@ begin
     end
   else if Decl.ClassType=TPasResString then
     begin
-    // read resourcestring -> rtl.getResStr($mod,"name")
+    // read resourcestring -> rtl.getResStr(pas.modulename,"name")
     Call:=CreateCallExpression(El);
     Result:=Call;
     Call.Expr:=CreateMemberExpression([GetBIName(pbivnRTL),GetBIName(pbifnGetResourcestring)]);
@@ -18136,9 +18136,7 @@ begin
       if CurEl is TPasSection then
         begin
         aModule:=CurEl.Parent as TPasModule;
-        aModName:=AContext.GetLocalName(aModule);
-        if aModName='' then
-          aModName:=TransformModuleName(aModule,true,AContext);
+        aModName:=TransformModuleName(aModule,true,AContext);
         Bracket:=TJSBracketMemberExpression(CreateElement(TJSBracketMemberExpression,El));
         Bracket.MExpr:=CreateMemberExpression([aModName,GetBIName(pbivnRTTI)]);
         Bracket.Name:=CreateLiteralString(El,aName);
@@ -18259,9 +18257,7 @@ begin
   aModule:=El.GetModule;
   if aModule=nil then
     RaiseInconsistency(20170418115552,El);
-  RttiPath:=AContext.GetLocalName(aModule);
-  if RttiPath='' then
-    RttiPath:=TransformModuleName(aContext.GetRootModule,true,AContext);
+  RttiPath:=TransformModuleName(aModule,true,AContext);
 
   Call:=CreateCallExpression(El);
   try
@@ -23130,10 +23126,10 @@ begin
         // parent is a class or record declaration
         if (ParentEl.ClassType=TPasClassType)
             and (TPasClassType(ParentEl).HelperForType<>nil)
-            and (El.Parent=ParentEl)
+            and (El=CurEl)
             and aResolver.IsHelperForMember(CurEl) then
           begin
-          // redirect to helper-for-type
+          // external helper proc/var -> redirect to helper-for-type
           ParentEl:=aResolver.ResolveAliasType(TPasClassType(ParentEl).HelperForType);
           IsClassRec:=(ParentEl.ClassType=TPasClassType)
                    or (ParentEl.ClassType=TPasRecordType);
@@ -23149,7 +23145,7 @@ begin
         else
           begin
           // Not in a Pascal dotscope and accessing a class member.
-          // Possible results: this.v, module.path.path.v, this.path.v
+          // Possible results: this.v, module.path.path.v, this.$class.v, $Self.v
           //    In nested proc 'this' can have another name, e.g. '$Self'
           SelfContext:=AContext.GetSelfContext;
           if ShortName<>'' then
@@ -24705,6 +24701,11 @@ var
   p, StartP: Integer;
   aName, Part: String;
 begin
+  if AddModulesPrefix then
+    begin
+    Result:=AContext.GetLocalName(El);
+    if Result<>'' then exit;
+    end;
   if El is TPasProgram then
     Result:='program'
   else

+ 29 - 12
packages/pastojs/tests/tcmodules.pas

@@ -30571,8 +30571,14 @@ end;
 
 procedure TTestModule.TestResourcestringProgram;
 begin
-  StartProgram(false);
+  AddModuleWithIntfImplSrc('unit2.pas',
+    LinesToStr([
+    'resourcestring Title = ''Nice'';',
+    '']),
+    '');
+  StartProgram(true);
   Add([
+  'uses unit2;',
   'const Bar = ''bar'';',
   'resourcestring',
   '  Red = ''red'';',
@@ -30582,6 +30588,7 @@ begin
   'begin',
   '  s:=red;',
   '  s:=test1.red;',
+  '  s:=Title;',
   '  c:=red[1];',
   '  c:=test1.red[2];',
   '  if red=foobar then ;',
@@ -30602,20 +30609,27 @@ begin
     '};',
     '']),
     LinesToStr([ // $mod.$main
-    '$mod.s = rtl.getResStr(pas.program, "Red");',
-    '$mod.s = rtl.getResStr(pas.program, "Red");',
-    '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
-    '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
-    'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
-    'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
+    '$mod.s = rtl.getResStr($mod, "Red");',
+    '$mod.s = rtl.getResStr($mod, "Red");',
+    '$mod.s = rtl.getResStr(pas.unit2, "Title");',
+    '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
+    '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
+    'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
+    'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
     '']));
 end;
 
 procedure TTestModule.TestResourcestringUnit;
 begin
-  StartUnit(false);
+  AddModuleWithIntfImplSrc('unit2.pas',
+    LinesToStr([
+    'resourcestring Title = ''Nice'';',
+    '']),
+    '');
+  StartUnit(true);
   Add([
   'interface',
+  'uses unit2;',
   'const Red = ''rEd'';',
   'resourcestring',
   '  Blue = ''blue'';',
@@ -30627,7 +30641,9 @@ begin
   'initialization',
   '  s:=blue+ImplGreen;',
   '  s:=test1.blue+test1.implgreen;',
-  '  s:=blue[1]+implgreen[2];']);
+  '  s:=blue[1]+implgreen[2];',
+  '  s:=Title;',
+  '']);
   ConvertUnit;
   CheckSource('TestResourcestringUnit',
     LinesToStr([ // statements
@@ -30646,9 +30662,10 @@ begin
     '};',
     '']),
     LinesToStr([ // $mod.$main
-    '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
-    '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
-    '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
+    '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
+    '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
+    '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
+    '$mod.s = rtl.getResStr(pas.unit2, "Title");',
     '']));
 end;