libstub.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. {
  2. libstub - pas2js stub generator, library version
  3. Copyright (C) 2017 - 2020 by Michael Van Canneyt [email protected]
  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. library stub;
  11. {$mode objfpc}{$H+}
  12. uses
  13. SysUtils, Classes, stubcreator;
  14. Type
  15. PStubCreator = Pointer;
  16. Function GetStubCreator : PStubCreator; stdcall;
  17. begin
  18. Result:=TStubCreator.Create(Nil);
  19. end;
  20. Procedure FreeStubCreator(P : PStubCreator); stdcall;
  21. begin
  22. TStubCreator(P).Free;
  23. end;
  24. Function MaybeStr(P : PAnsiChar) : String;
  25. begin
  26. If Assigned(P) then
  27. Result:=P
  28. else
  29. Result:='';
  30. end;
  31. Procedure SetStubCreatorInputFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
  32. begin
  33. if Assigned(P) then
  34. With TStubCreator(P) do
  35. InputFileName:=AFileName;
  36. end;
  37. Procedure SetStubCreatorConfigFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
  38. begin
  39. if Assigned(P) then
  40. With TStubCreator(P) do
  41. ConfigFileName:=MaybeStr(AFileName);
  42. end;
  43. Procedure SetStubCreatorOutputFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
  44. begin
  45. if Assigned(P) then
  46. With TStubCreator(P) do
  47. OutputFileName:=MaybeStr(AFileName);
  48. end;
  49. Procedure SetStubCreatorHeaderFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
  50. begin
  51. if Assigned(P) then
  52. With TStubCreator(P) do
  53. HeaderFileName:=MaybeStr(AFileName);
  54. end;
  55. Procedure AddStubCreatorDefine(P : PStubCreator; ADefine : PAnsiChar); stdcall;
  56. begin
  57. if Assigned(P) then
  58. With TStubCreator(P) do
  59. TStubCreator(P).Defines.Add(MaybeStr(ADefine));
  60. end;
  61. Procedure AddStubCreatorForwardClass(P : PStubCreator; AForwardClass : PAnsiChar); stdcall;
  62. Var
  63. S : String;
  64. begin
  65. if Assigned(P) then
  66. With TStubCreator(P) do
  67. begin
  68. S:=MaybeStr(AForwardClass);
  69. if (S<>'') then
  70. begin
  71. if TStubCreator(P).ForwardClasses<>'' then
  72. S:=','+S;
  73. TStubCreator(P).ForwardClasses:=TStubCreator(P).ForwardClasses+S;
  74. end;
  75. end;
  76. end;
  77. Procedure SetStubCreatorHeaderContent(P : PStubCreator; AContent : PAnsiChar); stdcall;
  78. begin
  79. if Assigned(P) then
  80. With TStubCreator(P) do
  81. HeaderStream:=TStringStream.Create(MaybeStr(AContent));
  82. end;
  83. Procedure SetStubCreatorOuputCallBack(P : PStubCreator; AData : Pointer; ACallBack : TWriteCallBack); stdcall;
  84. begin
  85. if Assigned(P) then
  86. With TStubCreator(P) do
  87. begin
  88. CallbackData:=AData;
  89. OnWriteCallBack:=ACallBack;
  90. end;
  91. end;
  92. Function ExecuteStubCreator(P : PStubCreator) : Boolean; stdcall;
  93. begin
  94. Result:=TStubCreator(P).Execute;
  95. end;
  96. Procedure GetStubCreatorLastError(P : PStubCreator; AError : PAnsiChar;
  97. Var AErrorLength : Longint; AErrorClass : PAnsiChar; Var AErrorClassLength : Longint); stdcall;
  98. Var
  99. L : Integer;
  100. E,C : String;
  101. begin
  102. TStubCreator(P).GetLastError(E,C);
  103. L:=Length(E);
  104. if (L>AErrorLength) then
  105. L:=AErrorLength;
  106. if (L>0) then
  107. Move(E[1],AError^,L);
  108. L:=Length(C);
  109. if L>AErrorClassLength then
  110. L:=AErrorClassLength;
  111. if (L>0) then
  112. Move(C[1],AErrorClass^,L);
  113. end;
  114. Procedure SetStubCreatorUnitAliasCallBack(P : PStubCreator; ACallBack : TUnitAliasCallBack; CallBackData : Pointer); stdcall;
  115. begin
  116. TStubCreator(P).OnUnitAlias:=ACallBack;
  117. TStubCreator(P).OnUnitAliasData:=CallBackData;
  118. end;
  119. Procedure AddStubCreatorExtraUnit(P : PStubCreator; AUnitName : PAnsiChar); stdcall;
  120. begin
  121. TStubCreator(P).ExtraUnits:=AUnitName;
  122. end;
  123. exports
  124. // Stub creator
  125. GetStubCreator,
  126. FreeStubCreator,
  127. SetStubCreatorInputFileName,
  128. SetStubCreatorOutputFileName,
  129. SetStubCreatorHeaderFileName,
  130. SetStubCreatorConfigFileName,
  131. SetStubCreatorHeaderContent,
  132. SetStubCreatorOuputCallBack,
  133. GetStubCreatorLastError,
  134. AddStubCreatorDefine,
  135. AddStubCreatorForwardClass,
  136. AddStubCreatorExtraUnit,
  137. ExecuteStubCreator,
  138. SetStubCreatorUnitAliasCallBack;
  139. end.