View Issue Details

IDProjectCategoryView StatusLast Update
0038859pas2jstranspilerpublic2021-05-05 21:09
Reporterhenrique Assigned ToMattias Gaertner  
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
PlatformPas2JsOSWindows 
Summary0038859: Anonymous constructor
DescriptionI have a new suggestion, to allow anonymous constructors. Allow you to place an empty string in the external name when it is a constructor.

Attached the change I made to make this work.
TagsNo tags attached.
Fixed in Revision
Attached Files

Activities

henrique

2021-05-05 14:45

reporter  

0001-Construtor-anonimo.patch (3,303 bytes)   
From 43f52194ae28f22e3da7c9dbb4baa85f6cb72e3d Mon Sep 17 00:00:00 2001
From: Henrique Gottardi Werlang <henriquewerlang@hotmail.com>
Date: Wed, 5 May 2021 11:42:22 -0300
Subject: [PATCH] Construtor anonimo.

---
 packages/pastojs/src/fppas2js.pp | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/packages/pastojs/src/fppas2js.pp b/packages/pastojs/src/fppas2js.pp
index 26983a2843..c46e74963f 100644
--- a/packages/pastojs/src/fppas2js.pp
+++ b/packages/pastojs/src/fppas2js.pp
@@ -4596,6 +4596,7 @@ var
   ptm: TProcTypeModifier;
   TypeEl, ElTypeEl, HelperForType: TPasType;
   FuncType: TPasFunctionType;
+  IsExternalConstructor: Boolean;
 begin
   inherited FinishProcedureType(El);
 
@@ -4618,6 +4619,7 @@ begin
 
   if El.Parent is TPasProcedure then
     begin
+    IsExternalConstructor:=False;
     Proc:=TPasProcedure(El.Parent);
 
     // calling convention
@@ -4696,7 +4698,8 @@ begin
               // constructor of external class can't be overriden -> forbid virtual
               RaiseMsg(20170323100447,nInvalidXModifierY,sInvalidXModifierY,
                 [Proc.ElementTypeName,'virtual,external'],Proc);
-            ComputeConstString(Proc.LibrarySymbolName,true,true);
+            IsExternalConstructor:=pmExternal in Proc.Modifiers;
+            ComputeConstString(Proc.LibrarySymbolName,true,not IsExternalConstructor);
             end
           else
             RaiseMsg(20170322163210,nPasElementNotSupported,sPasElementNotSupported,
@@ -4783,7 +4786,7 @@ begin
             [Proc.ElementTypeName,ModifierNames[pm]],Proc);
 
       // compute external name
-      ExtName:=ComputeConstString(Proc.LibrarySymbolName,true,true);
+      ExtName:=ComputeConstString(Proc.LibrarySymbolName,true,not IsExternalConstructor);
 
       // a virtual must have the external name, so that override works
       if Proc.IsVirtual and (Proc.Name<>ExtName) then
@@ -12339,7 +12342,7 @@ begin
       // external constructor ProcName
       ExtName:='';
       if aResolver<>nil then
-        ExtName:=aResolver.ComputeConstString(Proc.LibrarySymbolName,true,true);
+        ExtName:=aResolver.ComputeConstString(Proc.LibrarySymbolName,true,not (Proc is TPasConstructor));
       if ExtName='{}' then
         begin
         // external constructor {} -> "{}"
@@ -25211,10 +25214,11 @@ begin
     ShortName:=TransformElToJSName(El,AContext);
     if Result='' then
       Result:=ShortName
-    else if (ShortName<>'') and (ShortName[1] in ['[','(']) then
-      Result:=Result+ShortName
-    else
-      Result:=Result+'.'+ShortName;
+    else if ShortName<>'' then
+      if ShortName[1] in ['[','('] then
+        Result:=Result+ShortName
+      else
+        Result:=Result+'.'+ShortName;
     end;
   end;
 end;
@@ -26898,7 +26902,7 @@ var
   aType: TPasType;
 begin
   if (El is TPasProcedure) and (TPasProcedure(El).LibrarySymbolName<>nil) then
-    Result:=ComputeConstString(TPasProcedure(El).LibrarySymbolName,AContext,true)
+    Result:=ComputeConstString(TPasProcedure(El).LibrarySymbolName,AContext,not (El is TPasConstructor))
   else if (El is TPasVariable) and (TPasVariable(El).ExportName<>nil) then
     Result:=ComputeConstString(TPasVariable(El).ExportName,AContext,true)
   else if (El is TPasType) then
-- 
2.31.1.windows.1

0001-Construtor-anonimo.patch (3,303 bytes)   

Michael Van Canneyt

2021-05-05 17:28

administrator   ~0130767

Can you give an example of a use - case ?

henrique

2021-05-05 18:15

reporter   ~0130769

Last edited: 2021-05-05 18:16

View 2 revisions

Sure!
In the Maps Google API, it has the latitude and longitude class, named LatLng, and its constructor is the class's own name.
The construction path in JS is google.maps.LatLng(number, number).
With the anonymous constructor, I can declare the classes this way:
TGoogle = class external name 'google'
public type
  TMaps = class external name 'maps'
  public type
    TLatLng = class external name 'LatLng'
    public
      constructor Create(Lat, Lng: Double); external name '';
    end;
  end;
end;

This is a small example, but it has several classes, which I am declaring, that follow the same rule.

Issue History

Date Modified Username Field Change
2021-05-05 14:45 henrique New Issue
2021-05-05 14:45 henrique File Added: 0001-Construtor-anonimo.patch
2021-05-05 17:28 Michael Van Canneyt Note Added: 0130767
2021-05-05 18:15 henrique Note Added: 0130769
2021-05-05 18:16 henrique Note Edited: 0130769 View Revisions
2021-05-05 21:09 Mattias Gaertner Assigned To => Mattias Gaertner
2021-05-05 21:09 Mattias Gaertner Status new => assigned