Browse Source

* Some fixes in code generation

Michaël Van Canneyt 1 year ago
parent
commit
859fa9fe8f
1 changed files with 34 additions and 12 deletions
  1. 34 12
      packages/fcl-web/src/jsonrpc/fprpccodegen.pp

+ 34 - 12
packages/fcl-web/src/jsonrpc/fprpccodegen.pp

@@ -62,6 +62,7 @@ type
     FPasName: String;
     FPasName: String;
     FPasReturnType: String;
     FPasReturnType: String;
     FReturnType: TJSONtype;
     FReturnType: TJSONtype;
+    function GetPasReturnType: String;
     procedure SetParams(AValue: TAPIMethodParams);
     procedure SetParams(AValue: TAPIMethodParams);
   Public
   Public
     Constructor Create(aCollection : TCollection) ; override;
     Constructor Create(aCollection : TCollection) ; override;
@@ -70,7 +71,7 @@ type
     Property Name : String Read FName Write FName;
     Property Name : String Read FName Write FName;
     Property PasName : String Read FPasName Write FPasName;
     Property PasName : String Read FPasName Write FPasName;
     Property ReturnType : TJSONtype Read FReturnType Write FReturnType;
     Property ReturnType : TJSONtype Read FReturnType Write FReturnType;
-    Property PasReturnType : String Read FPasReturnType Write FPasReturnType;
+    Property PasReturnType : String Read GetPasReturnType Write FPasReturnType;
     Property Params : TAPIMethodParams Read FParams Write SetParams;
     Property Params : TAPIMethodParams Read FParams Write SetParams;
   end;
   end;
 
 
@@ -120,7 +121,7 @@ type
   protected
   protected
     // Overrides
     // Overrides
     Function BaseUnits : String; override;
     Function BaseUnits : String; override;
-    function StringToJSType(const S: String): TJSONtype;
+    function StringToJSType(S: String): TJSONtype;
     // High-level decl
     // High-level decl
     procedure GenerateServiceClassDeclarations(aServices: TAPIServices); virtual;
     procedure GenerateServiceClassDeclarations(aServices: TAPIServices); virtual;
     procedure GenerateServiceDeclaration(aService: TAPIService); virtual;
     procedure GenerateServiceDeclaration(aService: TAPIService); virtual;
@@ -198,6 +199,21 @@ begin
   FParams.Assign(AValue);
   FParams.Assign(AValue);
 end;
 end;
 
 
+function TAPIServiceMethod.GetPasReturnType: String;
+begin
+  Result:=FPasReturnType;
+  if Result='' then
+    Case ReturnType of
+      jtNull,
+      jtUnknown : Result:='JSValue';
+      jtNumber : Result:='Double';
+      jtString : Result:='String';
+      jtBoolean : Result:='Boolean';
+      jtArray : Result:='TJSArray';
+      jtObject : Result:='TJSObject';
+    end;
+end;
+
 constructor TAPIServiceMethod.Create(aCollection: TCollection);
 constructor TAPIServiceMethod.Create(aCollection: TCollection);
 begin
 begin
   inherited Create(aCollection);
   inherited Create(aCollection);
@@ -472,14 +488,11 @@ begin
     end;
     end;
 end;
 end;
 
 
-function TAPIClientCodeGen.StringToJSType(const S : String) : TJSONtype;
-
-var
-  LS : String;
+function TAPIClientCodeGen.StringToJSType(S : String) : TJSONtype;
 
 
 begin
 begin
-  LS:=LowerCase(S);
-  Case LS of
+  S:=LowerCase(S);
+  Case S of
     'jtunknown' : Result:=jtUnknown;
     'jtunknown' : Result:=jtUnknown;
     'jtnumber'  : Result:=jtNumber;
     'jtnumber'  : Result:=jtNumber;
     'jtstring'  : Result:=jtString;
     'jtstring'  : Result:=jtString;
@@ -508,6 +521,8 @@ begin
     aMeth.Name:=aJSON.Get('name','');
     aMeth.Name:=aJSON.Get('name','');
     aMeth.PasName:=GetServiceMethodName(aSvc.Name,aMeth.Name);
     aMeth.PasName:=GetServiceMethodName(aSvc.Name,aMeth.Name);
     aMeth.ReturnType:=StringToJSType(aJSON.Get('resulttype',''));
     aMeth.ReturnType:=StringToJSType(aJSON.Get('resulttype',''));
+    if (aMeth.ReturnType=jtNumber) and  (ccoPreferNativeInt in Options) then
+      aMeth.PasReturnType:='NativeInt';
     aParams:=aJSON.Get('paramdefs',TJSONarray(Nil));
     aParams:=aJSON.Get('paramdefs',TJSONarray(Nil));
     if (aJSON.Get('len',0)>0) and Assigned(aParams) then
     if (aJSON.Get('len',0)>0) and Assigned(aParams) then
       FillAPIMethod(aSvc,aMeth,aParams);
       FillAPIMethod(aSvc,aMeth,aParams);
@@ -604,7 +619,14 @@ begin
   indent;
   indent;
     Addln('If Assigned(aOnSuccess) then');
     Addln('If Assigned(aOnSuccess) then');
     Indent;
     Indent;
-      Addln('aOnSuccess(%s(aResult))',[aMeth.PasReturnType]);
+      if (ccoForceJSValueResult in options) then
+        Addln('aOnSuccess(aResult)')
+      else
+        begin
+        if (aMeth.PasReturnType='') then
+          Addln('// Return type is unknown, this will likely result in a compiler error. Set return type in API definition');
+        Addln('aOnSuccess(%s(aResult))',[aMeth.PasReturnType]);
+        end;
     undent;
     undent;
   undent;
   undent;
   Addln('end;');
   Addln('end;');
@@ -637,7 +659,7 @@ Var
   I : integer;
   I : integer;
 
 
 begin
 begin
-  ClassComment(aService.PasName);
+  ClassHeader(aService.PasName);
   AddLn('%s = Class(TRPCCustomService)',[aService.PasName]);
   AddLn('%s = Class(TRPCCustomService)',[aService.PasName]);
   Addln('Protected');
   Addln('Protected');
   Indent;
   Indent;
@@ -671,7 +693,7 @@ Var
   I : integer;
   I : integer;
 
 
 begin
 begin
-  ClassComment(aService.PasName);
+  ClassHeader(aService.PasName);
   Addln('');
   Addln('');
   GenerateRPCClassNameImplementation(aService);
   GenerateRPCClassNameImplementation(aService);
   For I:=0 to aService.Methods.Count-1 do
   For I:=0 to aService.Methods.Count-1 do
@@ -683,7 +705,7 @@ end;
 function TAPIClientCodeGen.BaseUnits: String;
 function TAPIClientCodeGen.BaseUnits: String;
 
 
 begin
 begin
-  Result:='fprpcclient';
+  Result:='js, fprpcclient';
 end;
 end;