class2pas.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2018 Michael Van Canneyt
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit class2pas;
  11. {$mode objfpc}{$H+}
  12. interface
  13. uses
  14. Classes, SysUtils, Types, JS;
  15. function ClassToPas(Const aName : string; Obj: TJSObject; aAncestor : string = ''; recurse : Boolean = False): string; overload;
  16. function ClassToPas(Const aJSName,aPascalName : string; Obj: TJSObject; aAncestor : string = ''; recurse : Boolean = False): string; overload;
  17. Procedure ClassToPas(Const aJSName,aPascalName,aAncestor : string; Obj: TJSObject; aDecl : TStrings; recurse : Boolean = False); overload;
  18. implementation
  19. function ClassToPas(Const aName : string; Obj: TJSObject; aAncestor : string = ''; recurse : Boolean = False): string;
  20. begin
  21. Result:=ClassToPas('TJS'+aName,aName,Obj,aAncestor,Recurse);
  22. end;
  23. function ClassToPas(Const aJSName,aPascalName : string; Obj: TJSObject; aAncestor : string = '';recurse : Boolean = False): string;
  24. Var
  25. S : TStringList;
  26. begin
  27. S:=TStringList.Create;
  28. try
  29. ClassToPas(aJSName,aPascalName,aAncestor,Obj,S,Recurse);
  30. Result:=S.Text;
  31. finally
  32. S.Free;
  33. end;
  34. end;
  35. Procedure ClassToPas(Const aJSName,aPascalName,aAncestor : string; Obj: TJSObject; aDecl : TStrings; recurse : Boolean = False); overload;
  36. var
  37. Names: TStringDynArray;
  38. i, j: Integer;
  39. ot,t: String;
  40. p: TJSArray;
  41. f: TJSFunction;
  42. Value: JSValue;
  43. begin
  44. T:=aPascalName+' = Class external name '''+aJSName+'''';
  45. if aAncestor<>'' then
  46. T:=T+' ('+aAncestor+')';
  47. aDecl.Add(T);
  48. aDecl.Add('Public');
  49. p:=TJSArray.new;
  50. while Obj<>nil do
  51. begin
  52. Names:=TJSObject.getOwnPropertyNames(Obj);
  53. for i:=0 to length(Names)-1 do
  54. begin
  55. try
  56. Value:=Obj[Names[i]];
  57. except
  58. aDecl.Add('// not readable property "'+Names[i]+'"'+sLineBreak);
  59. end;
  60. ot:=jsTypeOf(Value);
  61. if ot='function' then
  62. begin
  63. f:=TJSFunction(Value);
  64. t:=f.name;
  65. if t='' then
  66. T:=Names[i];
  67. t:='function '+T+'(';
  68. for j:=1 to NativeInt(f['length']) do
  69. begin
  70. if j>1 then t:=t+';';
  71. t:=t+'arg'+IntToStr(j)+': JSValue';
  72. end;
  73. t:=t+') : JSValue;';
  74. end
  75. else if ot='string' then
  76. t:=Names[i]+' : string;'
  77. else if ot='number' then
  78. t:=Names[i]+' : double;'
  79. else if ot='boolean' then
  80. t:=Names[i]+' : boolean;'
  81. else if ot='object' then
  82. t:=Names[i]+' : TJSObject;';
  83. if p.indexOf(t)<0 then
  84. begin
  85. p.push(t);
  86. aDecl.Add(' '+t);
  87. end;
  88. end;
  89. if Recurse then
  90. Obj:=TJSObject.getPrototypeOf(Obj)
  91. else
  92. Obj:=Nil;
  93. if Obj<>nil then
  94. aDecl.Add('// next getPrototypeOf ...');
  95. end;
  96. aDecl.Add('end;');
  97. end;
  98. end.