writer.pas 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. {
  2. pas2jni - JNI bridge generator for Pascal.
  3. Copyright (c) 2013 by Yury Sidorov.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit writer;
  17. {$mode objfpc}{$H+}
  18. interface
  19. //{$define DEBUG}
  20. {$ifdef DEBUG}
  21. {$ASSERTIONS ON}
  22. {$endif}
  23. uses
  24. Classes, SysUtils, def, contnrs, PPUParser;
  25. const
  26. MaxMethodPointers = 10000;
  27. type
  28. { TTextOutStream }
  29. TTextOutStream = class(TFileStream)
  30. private
  31. FIndent: integer;
  32. FIndStr: string;
  33. procedure SetIndednt(const AValue: integer);
  34. public
  35. procedure Write(const s: ansistring); overload;
  36. procedure WriteLn(const s: ansistring = ''; ExtraIndent: integer = 0);
  37. procedure IncI;
  38. procedure DecI;
  39. property Indent: integer read FIndent write SetIndednt;
  40. property SIndent: string read FIndStr;
  41. end;
  42. { TClassInfo }
  43. TClassInfo = class
  44. public
  45. Def: TDef;
  46. Funcs: TObjectList;
  47. IsCommonClass: boolean;
  48. constructor Create;
  49. destructor Destroy; override;
  50. end;
  51. { TProcInfo }
  52. TProcInfo = class
  53. public
  54. Name: string;
  55. JniName: string;
  56. JniSignature: string;
  57. end;
  58. { TClassList }
  59. TClassList = class(TStringList)
  60. private
  61. function GetFullName(const AName: string; Def: TDef): string;
  62. public
  63. constructor Create;
  64. function Add(const AName: string; Def: TDef; Info: TClassInfo): integer;
  65. function IndexOf(const AName: string; Def: TDef): integer; reintroduce;
  66. function GetClassName(Index: integer): string;
  67. function GetClassInfo(Index: integer): TClassInfo;
  68. end;
  69. { TWriter }
  70. TWriter = class
  71. private
  72. Fjs, Fps: TTextOutStream;
  73. FClasses: TClassList;
  74. FPkgDir: string;
  75. FUniqueCnt: integer;
  76. FThisUnit: TUnitDef;
  77. FIntegerType: TDef;
  78. function DoCheckItem(const ItemName: string): TCheckItemResult;
  79. procedure WriteFileComment(st: TTextOutStream);
  80. procedure ProcessRules(d: TDef; const Prefix: string = '');
  81. function GetUniqueNum: integer;
  82. function DefToJniType(d: TDef; var err: boolean): string;
  83. function DefToJniSig(d: TDef): string;
  84. function DefToJavaType(d: TDef): string;
  85. function GetJavaClassPath(d: TDef; const AClassName: string = ''): string;
  86. function JniToPasType(d: TDef; const v: string; CheckNil: boolean): string;
  87. function PasToJniType(d: TDef; const v: string): string;
  88. function GetTypeInfoVar(ClassDef: TDef): string;
  89. function GetClassPrefix(ClassDef: TDef; const AClassName: string = ''): string;
  90. function IsJavaSimpleType(d: TDef): boolean;
  91. function IsJavaVarParam(ParamDef: TVarDef): boolean;
  92. function GetProcDeclaration(d: TProcDef; const ProcName: string = ''; FullTypeNames: boolean = False; InternalParaNames: boolean = False): string;
  93. function GetJavaProcDeclaration(d: TProcDef; const ProcName: string = ''): string;
  94. function GetJniFuncType(d: TDef): string;
  95. function GetJavaClassName(cls: TDef; it: TDef): string;
  96. procedure RegisterPseudoClass(d: TDef);
  97. function GetPasIntType(Size: integer): string;
  98. function GetPasType(d: TDef; FullName: boolean): string;
  99. // procedure AddCustomProc(ParentDef: TDef; const JniName, Name: string; RetType: TBasicType; const Params: array of TBasicType);
  100. function AddCustomProc(ParentDef: TDef; const JniName, Name: string; RetType: TBasicType; const Params: array of TBasicType): TProcDef;
  101. procedure AddNativeMethod(ParentDef: TDef; const JniName, Name, Signature: string);
  102. function GetProcSignature(d: TProcDef): string;
  103. procedure EHandlerStart;
  104. procedure EHandlerEnd(const EnvVarName: string; const ExtraCode: string = '');
  105. procedure WriteClassInfoVar(d: TDef);
  106. procedure WriteComment(d: TDef; const AType: string);
  107. procedure WriteClass(d: TClassDef; PreInfo: boolean);
  108. procedure WriteProc(d: TProcDef; Variable: TVarDef = nil; AParent: TDef = nil);
  109. procedure WriteVar(d: TVarDef; AParent: TDef = nil);
  110. procedure WriteConst(d: TConstDef);
  111. procedure WriteEnum(d: TDef);
  112. procedure WriteProcType(d: TProcDef; PreInfo: boolean);
  113. procedure WriteSet(d: TSetDef);
  114. procedure WritePointer(d: TPointerDef; PreInfo: boolean);
  115. procedure WriteUnit(u: TUnitDef);
  116. procedure WriteOnLoad;
  117. public
  118. SearchPath: string;
  119. LibName: string;
  120. JavaPackage: string;
  121. Units: TStringList;
  122. OutPath: string;
  123. JavaOutPath: string;
  124. IncludeList: TStringList;
  125. ExcludeList: TStringList;
  126. constructor Create;
  127. destructor Destroy; override;
  128. procedure ProcessUnits;
  129. end;
  130. implementation
  131. const
  132. JNIType: array[TBasicType] of string =
  133. ('', 'jshort', 'jbyte', 'jint', 'jshort', 'jlong', 'jint', 'jlong', 'jfloat', 'jdouble', 'jstring',
  134. 'jstring', 'jboolean', 'jchar', 'jchar', 'jint', 'jstring');
  135. JNITypeSig: array[TBasicType] of string =
  136. ('V', 'S', 'B', 'I', 'S', 'J', 'I', 'J', 'F', 'D', 'Ljava/lang/String;', 'Ljava/lang/String;',
  137. 'Z', 'C', 'C', 'I', 'Ljava/lang/String;');
  138. JavaType: array[TBasicType] of string =
  139. ('void', 'short', 'byte', 'int', 'short', 'long', 'int', 'long', 'float', 'double', 'String',
  140. 'String', 'boolean', 'char', 'char', 'int', 'String');
  141. TextIndent = 2;
  142. ExcludeStd: array[1..44] of string = (
  143. 'classes.TStream.ReadComponent', 'classes.TStream.ReadComponentRes', 'classes.TStream.WriteComponent', 'classes.TStream.WriteComponentRes',
  144. 'classes.TStream.WriteDescendent', 'classes.TStream.WriteDescendentRes', 'classes.TStream.WriteResourceHeader', 'classes.TStream.FixupResourceHeader',
  145. 'classes.TStream.ReadResHeader', 'classes.TComponent.WriteState', 'classes.TComponent.ExecuteAction', 'classes.TComponent.UpdateAction',
  146. 'classes.TComponent.GetEnumerator', 'classes.TComponent.VCLComObject', 'classes.TComponent.DesignInfo', 'classes.TComponent.Destroying',
  147. 'classes.TComponent.FreeNotification', 'classes.TComponent.RemoveFreeNotification', 'classes.TComponent.FreeOnRelease', 'classes.TComponent.SetSubComponent',
  148. 'system.TObject.newinstance', 'system.TObject.FreeInstance', 'system.TObject.SafeCallException', 'system.TObject.InitInstance',
  149. 'system.TObject.CleanupInstance', 'system.TObject.ClassInfo', 'system.TObject.AfterConstruction', 'system.TObject.BeforeDestruction',
  150. 'system.TObject.GetInterfaceEntry', 'system.TObject.GetInterfaceTable', 'system.TObject.MethodAddress', 'system.TObject.MethodName',
  151. 'system.TObject.FieldAddress', 'classes.TComponent.ComponentState', 'classes.TComponent.ComponentStyle', 'classes.TList.GetEnumerator',
  152. 'classes.TList.List', 'classes.TList.FPOAttachObserver', 'classes.TList.FPODetachObserver', 'classes.TList.FPONotifyObservers',
  153. 'classes.TPersistent.FPOAttachObserver', 'classes.TPersistent.FPODetachObserver', 'classes.TPersistent.FPONotifyObservers',
  154. 'system.fma'
  155. );
  156. ExcludeDelphi7: array[1..25] of string = (
  157. 'system.TObject.StringMessageTable', 'system.TObject.GetInterfaceEntryByStr', 'system.TObject.UnitName', 'system.TObject.Equals',
  158. 'system.TObject.GetHashCode', 'system.TObject.ToString','classes.TStream.ReadByte', 'classes.TStream.ReadWord',
  159. 'classes.TStream.ReadDWord', 'classes.TStream.ReadQWord', 'classes.TStream.ReadAnsiString', 'classes.TStream.WriteByte',
  160. 'classes.TStream.WriteWord', 'classes.TStream.WriteDWord', 'classes.TStream.WriteQWord', 'classes.TStream.WriteAnsiString',
  161. 'classes.TCollection.Exchange', 'classes.TStrings.Equals', 'classes.TStrings.GetNameValue', 'classes.TStrings.ExtractName',
  162. 'classes.TStrings.TextLineBreakStyle', 'classes.TStrings.StrictDelimiter', 'classes.TStrings.GetEnumerator', 'classes.TStringList.OwnsObjects',
  163. 'classes.TList.AddList'
  164. );
  165. SUnsupportedType = '<unsupported type>';
  166. function JniCaliing: string;
  167. begin
  168. Result:='{$ifdef windows} stdcall {$else} cdecl {$endif};';
  169. end;
  170. { TClassList }
  171. function TClassList.IndexOf(const AName: string; Def: TDef): integer;
  172. begin
  173. Result:=inherited IndexOf(GetFullName(AName, Def));
  174. end;
  175. function TClassList.GetClassName(Index: integer): string;
  176. var
  177. i: integer;
  178. begin
  179. Result:=Strings[Index];
  180. i:=Pos('.', Result);
  181. if i > 0 then
  182. System.Delete(Result, 1, i);
  183. end;
  184. function TClassList.GetClassInfo(Index: integer): TClassInfo;
  185. begin
  186. Result:=TClassInfo(Objects[Index]);
  187. end;
  188. function TClassList.GetFullName(const AName: string; Def: TDef): string;
  189. begin
  190. if (Def = nil) or (Def.DefType = dtUnit) then
  191. Result:=AName
  192. else begin
  193. while (Def.Parent <> nil) and (Def.DefType <> dtUnit) do
  194. Def:=Def.Parent;
  195. Result:=Def.Name + '.' + AName;
  196. end;
  197. end;
  198. constructor TClassList.Create;
  199. begin
  200. inherited Create;
  201. Sorted:=True;
  202. end;
  203. function TClassList.Add(const AName: string; Def: TDef; Info: TClassInfo): integer;
  204. begin
  205. Result:=AddObject(GetFullName(AName, Def), Info);
  206. end;
  207. { TTextOutStream }
  208. procedure TTextOutStream.SetIndednt(const AValue: integer);
  209. begin
  210. if FIndent = AValue then exit;
  211. FIndent:=AValue;
  212. SetLength(FIndStr, FIndent*TextIndent);
  213. if FIndent > 0 then
  214. FillChar(FIndStr[1], FIndent*TextIndent, ' ');
  215. end;
  216. procedure TTextOutStream.Write(const s: ansistring);
  217. begin
  218. WriteBuffer(PChar(s)^, Length(s));
  219. end;
  220. procedure TTextOutStream.WriteLn(const s: ansistring; ExtraIndent: integer);
  221. begin
  222. if s = '' then
  223. Write(LineEnding)
  224. else begin
  225. Indent:=Indent + ExtraIndent;
  226. try
  227. Write(FIndStr + s + LineEnding);
  228. finally
  229. Indent:=Indent - ExtraIndent;
  230. end;
  231. end;
  232. end;
  233. procedure TTextOutStream.IncI;
  234. begin
  235. Indent:=Indent + 1;
  236. end;
  237. procedure TTextOutStream.DecI;
  238. begin
  239. if Indent > 0 then
  240. Indent:=Indent - 1;
  241. end;
  242. { TClassInfo }
  243. constructor TClassInfo.Create;
  244. begin
  245. Funcs:=TObjectList.Create(True);
  246. end;
  247. destructor TClassInfo.Destroy;
  248. begin
  249. Funcs.Free;
  250. inherited Destroy;
  251. end;
  252. { TWriter }
  253. function TWriter.DefToJniType(d: TDef; var err: boolean): string;
  254. begin
  255. if d = nil then begin
  256. Result:=SUnsupportedType;
  257. err:=True;
  258. end
  259. else begin
  260. if not d.IsUsed then begin
  261. Result:='<excluded type> ' + d.Name;
  262. err:=True;
  263. end
  264. else
  265. case d.DefType of
  266. dtType:
  267. Result:=JNIType[TTypeDef(d).BasicType];
  268. dtClass, dtEnum:
  269. Result:='jobject';
  270. dtProcType:
  271. if poMethodPtr in TProcDef(d).ProcOpt then
  272. Result:='jobject'
  273. else begin
  274. Result:=SUnsupportedType + ' ' + d.Name;
  275. err:=True;
  276. end;
  277. dtSet:
  278. if TSetDef(d).Size <= 4 then
  279. Result:='jobject'
  280. else begin
  281. Result:=SUnsupportedType + ' ' + d.Name;
  282. err:=True;
  283. end;
  284. dtPointer:
  285. if TPointerDef(d).IsObjPtr then
  286. Result:='jobject'
  287. else
  288. Result:='jlong';
  289. dtJniObject:
  290. Result:='jobject';
  291. else begin
  292. Result:=SUnsupportedType + ' ' + d.Name;
  293. err:=True;
  294. d.SetNotUsed;
  295. end;
  296. end;
  297. end;
  298. end;
  299. function TWriter.DoCheckItem(const ItemName: string): TCheckItemResult;
  300. begin
  301. if IncludeList.IndexOf(ItemName) >= 0 then
  302. Result:=crInclude
  303. else
  304. if ExcludeList.IndexOf(ItemName) >= 0 then
  305. Result:=crExclude
  306. else
  307. Result:=crDefault;
  308. end;
  309. procedure TWriter.WriteFileComment(st: TTextOutStream);
  310. begin
  311. st.WriteLn('// This file was automatically generated by the pas2jni utility.');
  312. st.WriteLn('// Do not edit this file.');
  313. end;
  314. procedure TWriter.ProcessRules(d: TDef; const Prefix: string);
  315. var
  316. i: integer;
  317. s: string;
  318. begin
  319. if d.DefType = dtClass then
  320. with TClassDef(d) do
  321. if (AncestorClass = nil) and (CType in [ctClass, ctInterface]) and (CompareText(Parent.Name, 'system') <> 0) then begin
  322. SetNotUsed;
  323. exit;
  324. end;
  325. s:=Prefix + d.Name;
  326. i:=IncludeList.IndexOf(s);
  327. if i >= 0 then begin
  328. i:=ptruint(IncludeList.Objects[i]);
  329. if (i = 0) or (d.Count = i - 1) then
  330. d.IsUsed:=True;
  331. end
  332. else
  333. if ExcludeList.IndexOf(s) >= 0 then begin
  334. d.SetNotUsed;
  335. end;
  336. if not (d.DefType in [dtUnit, dtClass]) then
  337. exit;
  338. s:=s + '.';
  339. for i:=0 to d.Count - 1 do
  340. ProcessRules(d[i], s);
  341. end;
  342. function TWriter.GetUniqueNum: integer;
  343. begin
  344. Inc(FUniqueCnt);
  345. Result:=FUniqueCnt;
  346. end;
  347. function TWriter.DefToJniSig(d: TDef): string;
  348. begin
  349. if d = nil then
  350. Result:=SUnsupportedType
  351. else
  352. case d.DefType of
  353. dtType:
  354. Result:=JNITypeSig[TTypeDef(d).BasicType];
  355. dtClass, dtProcType, dtSet, dtEnum:
  356. Result:='L' + GetJavaClassPath(d) + ';';
  357. dtPointer:
  358. if TPointerDef(d).IsObjPtr then
  359. Result:='L' + GetJavaClassPath(d) + ';'
  360. else
  361. Result:='J';
  362. dtJniObject:
  363. Result:='Ljava/lang/Object;';
  364. else
  365. Result:=SUnsupportedType;
  366. end;
  367. end;
  368. function TWriter.DefToJavaType(d: TDef): string;
  369. begin
  370. if d = nil then
  371. Result:=SUnsupportedType
  372. else
  373. if not d.IsUsed and (d.DefType <> dtType) then
  374. Result:='<excluded type> ' + d.Name
  375. else
  376. case d.DefType of
  377. dtType:
  378. Result:=JavaType[TTypeDef(d).BasicType];
  379. dtClass, dtProcType, dtSet, dtEnum:
  380. Result:=d.Name;
  381. dtPointer:
  382. if TPointerDef(d).IsObjPtr then
  383. Result:=d.Name
  384. else
  385. Result:='long';
  386. dtJniObject:
  387. Result:='Object';
  388. else
  389. Result:=SUnsupportedType;
  390. end;
  391. end;
  392. function TWriter.GetJavaClassPath(d: TDef; const AClassName: string): string;
  393. var
  394. n: string;
  395. begin
  396. if AClassName = '' then
  397. n:=d.AliasName
  398. else
  399. n:=AClassName;
  400. Result:=StringReplace(JavaPackage, '.', '/', [rfReplaceAll]);
  401. if Result <> '' then
  402. Result:=Result + '/';
  403. if d.DefType = dtUnit then
  404. Result:=Result + n
  405. else
  406. Result:=Result + d.Parent.AliasName + '$' + n;
  407. end;
  408. procedure TWriter.WriteClass(d: TClassDef; PreInfo: boolean);
  409. var
  410. WrittenItems: TList;
  411. procedure _WriteConstructors(c: TClassDef; Written: TStringList);
  412. var
  413. i, j: integer;
  414. p: TProcDef;
  415. OldRet: TDef;
  416. s: string;
  417. begin
  418. if c = nil then
  419. exit;
  420. for i:=0 to c.Count - 1 do
  421. with c[i] do begin
  422. if (DefType = dtProc) and not c.IsPrivate and (TProcDef(c[i]).ProcType = ptConstructor) then begin
  423. p:=TProcDef(c[i]);
  424. j:=Written.IndexOf(p.Name);
  425. if (j < 0) or (Written.Objects[j] = c) then begin
  426. s:=p.Name + ':';
  427. for j:=0 to p.Count - 1 do
  428. if p[j].DefType = dtParam then
  429. s:=s + DefToJniSig(TVarDef(p[j]).VarType);
  430. if Written.IndexOf(s) < 0 then begin
  431. OldRet:=p.ReturnType;
  432. p.ReturnType:=d;
  433. p.Parent:=d;
  434. try
  435. WriteProc(p);
  436. finally
  437. p.ReturnType:=OldRet;
  438. p.Parent:=c;
  439. end;
  440. Written.Add(s);
  441. if not (poOverload in p.ProcOpt) then
  442. Written.AddObject(p.Name, c);
  443. end;
  444. end;
  445. end;
  446. end;
  447. _WriteConstructors(c.AncestorClass, Written);
  448. end;
  449. procedure WriteConstructors;
  450. var
  451. cc: TStringList;
  452. begin
  453. if not TClassDef(d).HasAbstractMethods then begin
  454. // Writing all constructors including parent's
  455. cc:=TStringList.Create;
  456. try
  457. cc.Sorted:=True;
  458. _WriteConstructors(TClassDef(d), cc);
  459. finally
  460. cc.Free;
  461. end;
  462. end;
  463. end;
  464. procedure _WriteReplacedItems(c: TClassDef);
  465. var
  466. i: integer;
  467. p: TReplDef;
  468. begin
  469. c:=c.AncestorClass;
  470. if c = nil then
  471. exit;
  472. if c.HasReplacedItems then begin
  473. for i:=0 to c.Count - 1 do
  474. with c[i] do begin
  475. p:=TReplDef(c[i]);
  476. if (DefType in ReplDefs) and ((p.IsReplaced) or p.IsReplImpl) then begin
  477. if p.ReplacedItem <> nil then
  478. WrittenItems.Add(p.ReplacedItem);
  479. if WrittenItems.IndexOf(p) >= 0 then
  480. continue;
  481. case p.DefType of
  482. dtProc:
  483. WriteProc(TProcDef(p), nil, d);
  484. dtProp, dtField:
  485. WriteVar(TVarDef(p), d);
  486. end;
  487. end;
  488. end;
  489. end;
  490. _WriteReplacedItems(c);
  491. end;
  492. procedure WriteReplacedItems;
  493. begin
  494. _WriteReplacedItems(TClassDef(d));
  495. end;
  496. procedure WriteItems(Regular, Replaced, ReplImpl: boolean);
  497. var
  498. i: integer;
  499. it: TReplDef;
  500. begin
  501. for i:=0 to d.Count - 1 do begin
  502. it:=TReplDef(d[i]);
  503. if not (it.DefType in ReplDefs) then
  504. continue;
  505. if not (it.IsReplImpl or it.IsReplaced) then begin
  506. if not Regular then
  507. continue;
  508. end
  509. else
  510. if (not Replaced and it.IsReplaced) or (not ReplImpl and it.IsReplImpl) then
  511. continue;
  512. if it.ReplacedItem <> nil then
  513. WrittenItems.Add(it.ReplacedItem);
  514. case it.DefType of
  515. dtProc:
  516. if TProcDef(it).ProcType <> ptConstructor then
  517. WriteProc(TProcDef(it));
  518. dtProp, dtField:
  519. WriteVar(TVarDef(it));
  520. end;
  521. end;
  522. end;
  523. procedure WriteTypeCast(const AName: string; SecondPass: boolean);
  524. var
  525. s, ss: string;
  526. begin
  527. with TClassDef(d) do begin
  528. if HasReplacedItems and not SecondPass then
  529. s:='protected'
  530. else
  531. s:='public';
  532. if (CType = ctInterface) and (AncestorClass = nil) then
  533. ss:=' __Init();'
  534. else
  535. ss:='';
  536. Fjs.WriteLn(Format('%s %s(PascalObject obj) { super(obj);%s }', [s, AName, ss]));
  537. Fjs.WriteLn(Format('%s %s(long objptr) { super(objptr);%s }', [s, AName, ss]));
  538. end;
  539. end;
  540. var
  541. s, ss, n: string;
  542. RegularClass: boolean;
  543. begin
  544. if PreInfo then begin
  545. WriteClassInfoVar(d);
  546. if d.CType in [ctObject, ctRecord] then begin
  547. s:=d.Parent.Name + '.' + d.Name;
  548. Fps.WriteLn;
  549. Fps.WriteLn(Format('function _%s_CreateObj(env: PJNIEnv; const r: %s): jobject;', [GetClassPrefix(d), s]));
  550. Fps.WriteLn(Format('var pr: ^%s;', [s]));
  551. Fps.WriteLn('begin');
  552. Fps.IncI;
  553. Fps.WriteLn('New(pr); pr^:=r;');
  554. Fps.WriteLn(Format('Result:=_CreateJavaObj(env, pr, %s);', [GetTypeInfoVar(d)]));
  555. Fps.DecI;
  556. Fps.WriteLn('end;');
  557. Fps.WriteLn;
  558. ss:=Format('_%s_Free', [GetClassPrefix(d)]);
  559. Fps.WriteLn(Format('procedure %s(env: PJNIEnv; _self: JObject; r: jlong);', [ss]) + JniCaliing);
  560. Fps.WriteLn(Format('var pr: ^%s;', [s]));
  561. Fps.WriteLn('begin');
  562. Fps.WriteLn('pr:=pointer(ptruint(r));', 1);
  563. Fps.WriteLn('Dispose(pr);', 1);
  564. Fps.WriteLn('end;');
  565. AddNativeMethod(d, ss, '__Destroy', '(J)V');
  566. end;
  567. exit;
  568. end;
  569. // Java
  570. case d.CType of
  571. ctInterface:
  572. s:='interface';
  573. ctObject:
  574. s:='interface';
  575. ctRecord:
  576. s:='record';
  577. else
  578. s:='class';
  579. end;
  580. WriteComment(d, s);
  581. n:=GetJavaClassName(d, nil);
  582. s:='public static class ' + n + ' extends ';
  583. with d do begin
  584. if AncestorClass <> nil then begin
  585. ss:=AncestorClass.Name;
  586. if ImplementsReplacedItems then
  587. ss:='__' + ss;
  588. s:=s + ss;
  589. end
  590. else
  591. if d.CType in [ctObject, ctRecord] then
  592. s:=s + Format('%s.system.Record', [JavaPackage])
  593. else
  594. if d.CType = ctInterface then
  595. s:=s + 'PascalObjectEx'
  596. else
  597. s:=s + 'PascalObject';
  598. end;
  599. Fjs.WriteLn(s + ' {');
  600. Fjs.IncI;
  601. case d.CType of
  602. ctObject, ctRecord:
  603. begin
  604. Fjs.WriteLn('private native void __Destroy(long pasobj);');
  605. Fjs.WriteLn(Format('protected %s(long objptr, boolean cleanup) { __Init(objptr, cleanup); }', [d.Name]));
  606. Fjs.WriteLn(Format('public %s() { __Init(0, true); }', [d.Name]));
  607. Fjs.WriteLn(Format('public void __Release() { __Destroy(_pasobj); _pasobj=0; }', [d.Name]));
  608. Fjs.WriteLn(Format('public int __Size() { return %d; }', [d.Size]));
  609. end;
  610. ctInterface:
  611. begin
  612. if d.AncestorClass = nil then begin
  613. Fjs.WriteLn('public void __Release() { if (_pasobj != 0) _Release(); _pasobj = 0; }');
  614. Fjs.WriteLn('public void __Init() { _cleanup=true; if (_pasobj != 0) _AddRef(); }');
  615. s:='_pasobj=objptr; __Init();';
  616. end
  617. else
  618. s:='super(objptr, cleanup);';
  619. Fjs.WriteLn(Format('protected %s(long objptr, boolean cleanup) { %s }', [d.Name, s]));
  620. end;
  621. end;
  622. WriteTypeCast(n, False);
  623. WrittenItems:=TList.Create;
  624. try
  625. RegularClass:=(d.DefType = dtClass) and not TClassDef(d).HasReplacedItems;
  626. if RegularClass then
  627. WriteConstructors;
  628. // Write regular items
  629. WriteItems(True, False, RegularClass);
  630. if RegularClass and TClassDef(d).ImplementsReplacedItems then
  631. // Write implementation wrappers for replaced mehods
  632. WriteReplacedItems;
  633. Fjs.DecI;
  634. Fjs.WriteLn('}');
  635. Fjs.WriteLn;
  636. if (d.DefType = dtClass) and (TClassDef(d).HasReplacedItems) then begin
  637. // Write replaced items
  638. Fjs.WriteLn(Format('public static class %s extends __%0:s {', [d.AliasName]));
  639. Fjs.IncI;
  640. WriteTypeCast(d.AliasName, True);
  641. WriteConstructors;
  642. WriteItems(False, True, True);
  643. if TClassDef(d).ImplementsReplacedItems then
  644. // Write implementation wrappers for replaced mehods
  645. WriteReplacedItems;
  646. Fjs.DecI;
  647. Fjs.WriteLn('}');
  648. Fjs.WriteLn;
  649. end;
  650. finally
  651. WrittenItems.Free;
  652. end;
  653. end;
  654. procedure TWriter.WriteProc(d: TProcDef; Variable: TVarDef; AParent: TDef);
  655. var
  656. i, j, ClassIdx: integer;
  657. s, ss, ps, TempRes, VarFin: string;
  658. err, tf: boolean;
  659. pi: TProcInfo;
  660. ci: TClassInfo;
  661. IsTObject: boolean;
  662. tempvars: TStringList;
  663. vd: TVarDef;
  664. UseTempObjVar, IsObj, IsProcVar: boolean;
  665. ItemDef: TDef;
  666. begin
  667. ASSERT(d.DefType = dtProc);
  668. if d.IsPrivate or not d.IsUsed then
  669. exit;
  670. IsTObject:=(d.Parent.DefType = dtClass) and (TClassDef(d.Parent).AncestorClass = nil);
  671. if (d.ProcType = ptDestructor) and not IsTObject then
  672. exit;
  673. if Variable <> nil then
  674. ItemDef:=Variable
  675. else
  676. ItemDef:=d;
  677. tempvars:=nil;
  678. pi:=TProcInfo.Create;
  679. with d do
  680. try
  681. pi.Name:=Name;
  682. s:=GetClassPrefix(d.Parent) + pi.Name;
  683. pi.JniName:=s;
  684. pi.JniSignature:=GetProcSignature(d);
  685. if AParent = nil then begin
  686. // Checking duplicate proc name and duplicate param types
  687. ClassIdx:=FClasses.IndexOf(GetJavaClassName(d.Parent, ItemDef), d.Parent);
  688. if ClassIdx >= 0 then begin
  689. ci:=FClasses.GetClassInfo(ClassIdx);
  690. j:=1;
  691. ss:=Copy(pi.JniSignature, 1, Pos(')', pi.JniSignature));
  692. repeat
  693. err:=False;
  694. for i:=0 to ci.Funcs.Count - 1 do
  695. with TProcInfo(ci.Funcs[i]) do
  696. if CompareText(JniName, pi.JniName) = 0 then begin
  697. Inc(j);
  698. pi.JniName:=Format('%s_%d', [s, j]);
  699. err:=True;
  700. break;
  701. end
  702. else
  703. if (CompareText(Name, pi.Name) = 0) and (ss = Copy(JniSignature, 1, Pos(')', JniSignature))) then
  704. // Duplicate params
  705. exit;
  706. until not err;
  707. end;
  708. err:=False;
  709. if ProcType in [ptFunction, ptConstructor] then
  710. s:='function'
  711. else
  712. s:='procedure';
  713. s:=s + ' ' + pi.JniName + '(_env: PJNIEnv; _jobj: jobject';
  714. IsObj:=(d.Parent.DefType = dtClass) and (TClassDef(d.Parent).CType = ctObject);
  715. if IsObj and (ProcType in [ptConstructor, ptDestructor]) then
  716. TempRes:='__tempres';
  717. IsProcVar:=(Variable <> nil) and (Variable.VarType <> nil) and (Variable.VarType.DefType = dtProcType);
  718. UseTempObjVar:=IsProcVar and (ProcType = ptProcedure) and (Variable.Parent.DefType <> dtUnit);
  719. for j:=0 to Count - 1 do begin
  720. vd:=TVarDef(Items[j]);
  721. if vd.DefType <> dtParam then
  722. continue;
  723. with vd do begin
  724. if (VarType <> nil) and (VarType.DefType = dtJniEnv) then
  725. continue;
  726. s:=s + '; ' + Name + ': ';
  727. if not IsJavaVarParam(vd) then
  728. s:=s + DefToJniType(VarType, err)
  729. else begin
  730. s:=s + 'jarray';
  731. if tempvars = nil then
  732. tempvars:=TStringList.Create;
  733. if VarType = nil then
  734. err:=True
  735. else
  736. Tag:=tempvars.AddObject('__tmp_' + Name, d.Items[j]) + 1;
  737. end;
  738. end;
  739. end;
  740. s:=s + ')';
  741. if ProcType in [ptFunction, ptConstructor] then
  742. s:=s + ': ' + DefToJniType(ReturnType, err);
  743. s:=s + '; ' + JniCaliing;
  744. if err then begin
  745. s:='// ' + s;
  746. Fjs.WriteLn('// NOT PROCESSED: ' + GetJavaProcDeclaration(d));
  747. d.SetNotUsed;
  748. end;
  749. Fps.WriteLn;
  750. Fps.WriteLn(s);
  751. if err then
  752. exit;
  753. if (tempvars <> nil) or UseTempObjVar or (TempRes <> '') then begin
  754. s:='';
  755. Fps.WriteLn('var');
  756. Fps.IncI;
  757. if tempvars <> nil then begin
  758. for i:=0 to tempvars.Count - 1 do begin
  759. vd:=TVarDef(tempvars.Objects[i]);
  760. Fps.WriteLn(Format('%s: %s;', [tempvars[i], GetPasType(vd.VarType, True)]));
  761. if IsJavaSimpleType(vd.VarType) then begin
  762. Fps.WriteLn(Format('%s_arr: P%s;', [tempvars[i], DefToJniType(vd.VarType, err)]));
  763. if s = '' then
  764. s:='__iscopy: JBoolean;';
  765. end;
  766. end;
  767. if s <> '' then
  768. Fps.WriteLn(s);
  769. end;
  770. if UseTempObjVar then
  771. Fps.WriteLn('__objvar: ' + d.Parent.Name + ';');
  772. if TempRes <> '' then begin
  773. s:=TempRes + ': ';
  774. if IsObj and (ProcType in [ptConstructor, ptDestructor]) then
  775. s:=s + '^' + GetPasType(d.Parent, True)
  776. else
  777. s:=s + GetPasType(d.ReturnType, True);
  778. Fps.WriteLn(s + ';');
  779. end;
  780. Fps.DecI;
  781. end;
  782. if IsProcVar and (ProcType = ptProcedure) then
  783. Fps.WriteLn('var __mvar: TMethod;');
  784. Fps.WriteLn('begin');
  785. Fps.IncI;
  786. EHandlerStart;
  787. tf:=False;
  788. // Assign var parameter values to local vars
  789. if tempvars <> nil then begin
  790. for i:=0 to tempvars.Count - 1 do begin
  791. vd:=TVarDef(tempvars.Objects[i]);
  792. Fps.WriteLn(Format('if _env^^.GetArrayLength(_env, %s) <> 1 then _RaiseVarParamException(''%s'');', [vd.Name, vd.Name]));
  793. if IsJavaSimpleType(vd.VarType) then begin
  794. Fps.WriteLn(Format('%s_arr:=_env^^.Get%sArrayElements(_env, %s, __iscopy);', [tempvars[i], GetJniFuncType(vd.VarType), vd.Name]));
  795. Fps.WriteLn(Format('if %s_arr = nil then _RaiseVarParamException(''%s'');', [tempvars[i], vd.Name]));
  796. s:=tempvars[i] + '_arr^';
  797. tf:=True;
  798. end
  799. else
  800. s:=Format('_env^^.GetObjectArrayElement(_env, %s, 0)', [vd.Name]);
  801. if voVar in vd.VarOpt then
  802. Fps.WriteLn(tempvars[i] + ':=' + JniToPasType(vd.VarType, s, False) + ';');
  803. end;
  804. end;
  805. if tf then begin
  806. Fps.WriteLn('try');
  807. Fps.IncI;
  808. end;
  809. s:='';
  810. if not (IsObj and (ProcType in [ptConstructor, ptDestructor])) then
  811. if Parent.DefType = dtUnit then
  812. s:=Parent.Name + '.'
  813. else
  814. if ProcType = ptConstructor then
  815. s:=Parent.Parent.Name + '.' + Parent.Name + '.'
  816. else
  817. s:=JniToPasType(d.Parent, '_jobj', True) + '.';
  818. if Variable = nil then begin
  819. // Regular proc
  820. s:=s + pi.Name;
  821. if Count > 0 then begin
  822. s:=s + '(';
  823. ps:='';
  824. for j:=0 to Count - 1 do begin
  825. vd:=TVarDef(Items[j]);
  826. if vd.DefType <> dtParam then
  827. continue;
  828. if vd.VarType.DefType = dtJniEnv then
  829. ss:='_env'
  830. else
  831. if vd.Tag <> 0 then
  832. ss:=tempvars[vd.Tag - 1]
  833. else begin
  834. ss:=Items[j].Name;
  835. ss:=JniToPasType(vd.VarType, ss, False);
  836. end;
  837. if ps <> '' then
  838. ps:=ps + ', ';
  839. ps:=ps + ss;
  840. end;
  841. s:=s + ps + ')';
  842. end;
  843. end
  844. else begin
  845. // Var access
  846. if UseTempObjVar then begin
  847. System.Delete(s, Length(s), 1);
  848. Fps.WriteLn('__objvar:=' + s + ';');
  849. s:='__objvar.';
  850. end;
  851. s:=s + Variable.Name;
  852. j:=Count;
  853. if ProcType = ptProcedure then
  854. Dec(j);
  855. if j > 0 then begin
  856. i:=j;
  857. ss:='';
  858. for j:=0 to j - 1 do begin
  859. if ss <> '' then
  860. ss:=ss + ', ';
  861. ss:=ss + JniToPasType(TVarDef(Items[j]).VarType, Items[j].Name, False);
  862. end;
  863. s:=Format('%s[%s]', [s, ss]);
  864. end
  865. else
  866. i:=0;
  867. if ProcType = ptProcedure then begin
  868. ASSERT(Count = i + 1);
  869. if Variable.VarType.DefType = dtProcType then begin
  870. Fps.WriteLn(Format('__mvar:=TMethod(%s);', [s]));
  871. VarFin:=Format('_RefMethodPtr(_env, TMethod(%s), True); _RefMethodPtr(_env, __mvar, False);', [s]);
  872. end;
  873. s:=s + ':=' + JniToPasType(TVarDef(Items[i]).VarType, Items[i].Name, False);
  874. end;
  875. end;
  876. if IsObj and (ProcType = ptConstructor) then begin
  877. s:=Format('system.New(%s, %s);', [TempRes, s]);
  878. Fps.WriteLn(s);
  879. s:=Format('Result:=_CreateJavaObj(_env, %s, %s, False);', [TempRes, GetTypeInfoVar(ReturnType)]);
  880. Fps.WriteLn(s);
  881. end
  882. else
  883. if IsObj and (ProcType = ptDestructor) then begin
  884. Fps.WriteLn(TempRes + ':=@' + JniToPasType(d.Parent, '_jobj', True) + ';');
  885. s:=Format('system.Dispose(%s, %s);', [TempRes, s]);
  886. Fps.WriteLn(s);
  887. end
  888. else begin
  889. if ProcType in [ptFunction, ptConstructor] then
  890. s:='Result:=' + PasToJniType(ReturnType, s);
  891. s:=s + ';';
  892. Fps.WriteLn(s);
  893. end;
  894. if VarFin <> '' then
  895. Fps.WriteLn(VarFin);
  896. // Return var/out parameters
  897. if tempvars <> nil then begin
  898. for i:=0 to tempvars.Count - 1 do begin
  899. vd:=TVarDef(tempvars.Objects[i]);
  900. if IsJavaSimpleType(vd.VarType) then
  901. Fps.WriteLn(Format('%s_arr^:=%s;', [tempvars[i], PasToJniType(vd.VarType, tempvars[i])]))
  902. else
  903. Fps.WriteLn(Format('_env^^.SetObjectArrayElement(_env, %s, 0, %s);', [vd.Name, PasToJniType(vd.VarType, tempvars[i])]));
  904. end;
  905. end;
  906. if IsTObject and ( (ProcType = ptDestructor) or (CompareText(Name, 'Free') = 0) ) then
  907. Fps.WriteLn(Format('_env^^.SetLongField(_env, _jobj, %s.ObjFieldId, 0);', [GetTypeInfoVar(d.Parent)]));
  908. if tf then begin
  909. Fps.WriteLn('finally', -1);
  910. if tempvars <> nil then begin
  911. for i:=0 to tempvars.Count - 1 do begin
  912. vd:=TVarDef(tempvars.Objects[i]);
  913. if IsJavaSimpleType(vd.VarType) then
  914. Fps.WriteLn(Format('_env^^.Release%sArrayElements(_env, %s, %s_arr, 0);', [GetJniFuncType(vd.VarType), vd.Name, tempvars[i]]));
  915. end;
  916. end;
  917. Fps.DecI;
  918. Fps.WriteLn('end;');
  919. end;
  920. s:='';
  921. if ProcType in [ptFunction, ptConstructor] then begin
  922. s:='0';
  923. if (ReturnType.DefType = dtType) and (TTypeDef(ReturnType).BasicType <= btDouble) then
  924. s:='0'
  925. else
  926. s:=Format('%s(0)', [DefToJniType(ReturnType, err)]);
  927. s:='Result:=' + s + ';';
  928. end;
  929. EHandlerEnd('_env', s);
  930. Fps.DecI;
  931. Fps.WriteLn('end;');
  932. AParent:=d.Parent;
  933. end
  934. else
  935. ClassIdx:=FClasses.IndexOf(GetJavaClassName(AParent, ItemDef), AParent);
  936. if ClassIdx < 0 then begin
  937. ci:=TClassInfo.Create;
  938. ci.Def:=AParent;
  939. s:=GetJavaClassName(AParent, ItemDef);
  940. ci.IsCommonClass:=s <> AParent.Name;
  941. ClassIdx:=FClasses.Add(s, AParent, ci);
  942. end;
  943. FClasses.GetClassInfo(ClassIdx).Funcs.Add(pi);
  944. pi:=nil;
  945. // Java part
  946. s:=GetJavaProcDeclaration(d) + ';';
  947. if (Parent.DefType = dtUnit) or (ProcType = ptConstructor) then
  948. s:='static ' + s;
  949. if Variable = nil then
  950. Fjs.WriteLn('// ' + GetProcDeclaration(d));
  951. if poPrivate in ProcOpt then
  952. ss:='private'
  953. else
  954. if poProtected in ProcOpt then
  955. ss:='protected'
  956. else
  957. ss:='public';
  958. Fjs.WriteLn(ss + ' native ' + s);
  959. finally
  960. pi.Free;
  961. tempvars.Free;
  962. end;
  963. end;
  964. procedure TWriter.WriteVar(d: TVarDef; AParent: TDef);
  965. function _WriteArrayIndex(pd: TProcDef): TDef;
  966. var
  967. ad: TArrayDef;
  968. i: integer;
  969. begin
  970. ad:=TArrayDef(d.VarType);
  971. i:=1;
  972. repeat
  973. with TVarDef.Create(pd, dtParam) do begin
  974. Name:='Index';
  975. if i > 1 then
  976. Name:=Name + IntToStr(i);
  977. VarType:=ad.RangeType;
  978. if (VarType.DefType = dtType) and (TTypeDef(VarType).BasicType in [btByte, btShortInt, btSmallInt]) then
  979. VarType:=FIntegerType;
  980. VarOpt:=[voRead];
  981. end;
  982. Result:=ad.ElType;
  983. ad:=TArrayDef(Result);
  984. Inc(i);
  985. until Result.DefType <> dtArray;
  986. end;
  987. var
  988. pd: TProcDef;
  989. vd: TVarDef;
  990. t: TTypeDef;
  991. vt: TDef;
  992. s, ss: string;
  993. i: integer;
  994. isarray, isdynarray: boolean;
  995. begin
  996. if not d.IsUsed then
  997. exit;
  998. isarray:=(d.VarType <> nil) and (d.VarType.DefType = dtArray);
  999. isdynarray:=isarray and (TArrayDef(d.VarType).RangeHigh < TArrayDef(d.VarType).RangeLow);
  1000. if isdynarray then
  1001. if not (voRead in d.VarOpt) then
  1002. exit
  1003. else
  1004. d.VarOpt:=d.VarOpt + [voWrite];
  1005. if d.VarType <> nil then begin
  1006. case d.DefType of
  1007. dtVar:
  1008. s:='var';
  1009. dtProp:
  1010. s:='property';
  1011. else
  1012. s:='';
  1013. end;
  1014. s:=Trim(s + ' ' + d.Name);
  1015. if d.Count > 0 then
  1016. s:=s + '[]';
  1017. ss:=d.VarType.Name;
  1018. if ss = '' then
  1019. if d.VarType.DefType = dtArray then
  1020. ss:='array';
  1021. Fjs.WriteLn(Format('// %s: %s', [s, ss]));
  1022. end;
  1023. if voRead in d.VarOpt then begin
  1024. pd:=TProcDef.Create(nil, dtProc);
  1025. try
  1026. pd.IsUsed:=True;
  1027. pd.Parent:=d.Parent;
  1028. pd.ProcType:=ptFunction;
  1029. pd.Name:='get' + d.Name;
  1030. if isarray then
  1031. // Array var
  1032. pd.ReturnType:=_WriteArrayIndex(pd)
  1033. else begin
  1034. pd.ReturnType:=d.VarType;
  1035. if d.DefType = dtProp then begin
  1036. for i:=0 to d.Count - 1 do begin
  1037. vd:=TVarDef(d.Items[i]);
  1038. with TVarDef.Create(pd, dtParam) do begin
  1039. Name:=vd.Name;
  1040. VarType:=vd.VarType;
  1041. VarOpt:=[voRead];
  1042. end;
  1043. end;
  1044. end;
  1045. end;
  1046. WriteProc(pd, d, AParent);
  1047. finally
  1048. pd.Free;
  1049. end;
  1050. end;
  1051. if voWrite in d.VarOpt then begin
  1052. pd:=TProcDef.Create(nil, dtProc);
  1053. try
  1054. pd.IsUsed:=True;
  1055. pd.Parent:=d.Parent;
  1056. pd.ProcType:=ptProcedure;
  1057. pd.Name:='set' + d.Name;
  1058. vt:=d.VarType;;
  1059. if isarray then begin
  1060. // Array var
  1061. if (d.DefType = dtProp) and not isdynarray then
  1062. exit;
  1063. vt:=_WriteArrayIndex(pd);
  1064. end
  1065. else
  1066. if d.DefType = dtProp then begin
  1067. for i:=0 to d.Count - 1 do begin
  1068. vd:=TVarDef(d.Items[i]);
  1069. with TVarDef.Create(pd, dtParam) do begin
  1070. Name:=vd.Name;
  1071. VarType:=vd.VarType;
  1072. VarOpt:=[voRead];
  1073. end;
  1074. end;
  1075. end;
  1076. s:='Value';
  1077. // Check if the name of value parameter is unique
  1078. i:=0;
  1079. while i < d.Count do begin
  1080. if AnsiCompareText(s, d.Items[i].AliasName) = 0 then begin
  1081. i:=0;
  1082. s:='_' + s;
  1083. continue;
  1084. end;
  1085. Inc(i);
  1086. end;
  1087. with TVarDef.Create(pd, dtParam) do begin
  1088. Name:=s;
  1089. VarType:=vt;
  1090. VarOpt:=[voRead];
  1091. end;
  1092. t:=TTypeDef.Create(nil, dtType);
  1093. try
  1094. t.BasicType:=btVoid;
  1095. pd.ReturnType:=t;
  1096. WriteProc(pd, d, AParent);
  1097. finally
  1098. t.Free;
  1099. end;
  1100. finally
  1101. pd.Free;
  1102. end;
  1103. end;
  1104. end;
  1105. procedure TWriter.WriteConst(d: TConstDef);
  1106. var
  1107. s, v: string;
  1108. begin
  1109. if not d.IsUsed then
  1110. exit;
  1111. v:=d.Value;
  1112. if d.VarType = nil then begin
  1113. if Copy(d.Value, 1, 1) = '"' then
  1114. s:='String'
  1115. else
  1116. s:='double';
  1117. end
  1118. else begin
  1119. s:='';
  1120. case d.VarType.DefType of
  1121. dtType:
  1122. case TTypeDef(d.VarType).BasicType of
  1123. btLongWord, btInt64:
  1124. v:=v + 'L';
  1125. btBoolean:
  1126. if v = '1' then
  1127. v:='true'
  1128. else
  1129. v:='false';
  1130. end;
  1131. dtArray:
  1132. with TArrayDef(d.VarType) do
  1133. if (ElType.DefType = dtType) and (TTypeDef(ElType).BasicType in [btChar, btWideChar]) then
  1134. s:='String';
  1135. end;
  1136. if s = '' then
  1137. s:=DefToJavaType(d.VarType);
  1138. end;
  1139. v:=Format('public static final %s %s = %s;', [s, d.Name, v]);
  1140. if s = SUnsupportedType then
  1141. v:='// ' + v;
  1142. Fjs.WriteLn(v);
  1143. end;
  1144. procedure TWriter.WriteEnum(d: TDef);
  1145. var
  1146. i: integer;
  1147. s: string;
  1148. begin
  1149. if not d.IsUsed then
  1150. exit;
  1151. RegisterPseudoClass(d);
  1152. WriteComment(d, 'enum');
  1153. Fjs.WriteLn(Format('public static class %s extends system.Enum {', [d.Name]));
  1154. Fjs.IncI;
  1155. for i:=0 to d.Count - 1 do begin
  1156. s:=Format('public final static int %s = %s;', [d[i].Name, TConstDef(d[i]).Value]);
  1157. Fjs.WriteLn(s);
  1158. end;
  1159. Fjs.WriteLn;
  1160. for i:=0 to d.Count - 1 do begin
  1161. s:=Format('public static %s %s() { return new %0:s(%1:s); }', [d.Name, d[i].Name]);
  1162. Fjs.WriteLn(s);
  1163. end;
  1164. Fjs.WriteLn;
  1165. Fjs.WriteLn(Format('public %s(int v) { Value = v; }', [d.Name]));
  1166. Fjs.WriteLn(Format('@Override public boolean equals(Object o) { return ((o instanceof %0:s) && Value == ((%0:s)o).Value) || super.equals(o); }', [d.Name]));
  1167. Fjs.DecI;
  1168. Fjs.WriteLn('}');
  1169. Fjs.WriteLn;
  1170. end;
  1171. procedure TWriter.WriteProcType(d: TProcDef; PreInfo: boolean);
  1172. procedure _AccessSimpleArray(vd: TVarDef; VarIndex: integer; DoSet: boolean);
  1173. begin
  1174. with vd do begin
  1175. Fps.WriteLn(Format('_tmp_%s:=_env^^.Get%sArrayElements(_env, _args[%d].L, PJBoolean(nil)^);', [Name, GetJniFuncType(VarType), VarIndex]));
  1176. Fps.WriteLn(Format('if _tmp_%s <> nil then', [Name]));
  1177. if DoSet then
  1178. Fps.WriteLn(Format('_tmp_%s^:=%s;', [Name, PasToJniType(VarType, Name)]), 1)
  1179. else
  1180. Fps.WriteLn(Format('%s:=%s;', [Name, JniToPasType(VarType, '_tmp_' + Name + '^', False)]), 1);
  1181. Fps.WriteLn(Format('_env^^.Release%sArrayElements(_env, _args[%d].L, _tmp_%s, 0);', [GetJniFuncType(VarType), VarIndex, Name]));
  1182. end;
  1183. end;
  1184. var
  1185. vd: TVarDef;
  1186. i: integer;
  1187. s, ss, hclass: string;
  1188. err: boolean;
  1189. begin
  1190. if not d.IsUsed or not (poMethodPtr in d.ProcOpt) then
  1191. exit;
  1192. if PreInfo then begin
  1193. WriteClassInfoVar(d);
  1194. // Handler proc
  1195. hclass:=GetClassPrefix(d) + 'Class';
  1196. Fps.WriteLn;
  1197. Fps.WriteLn(Format('type %s = class', [hclass]));
  1198. Fps.WriteLn(Format('private %s;', [ GetProcDeclaration(d, 'Handler', True, True)]), 1);
  1199. Fps.WriteLn('end;');
  1200. Fps.WriteLn;
  1201. Fps.WriteLn(GetProcDeclaration(d, Format('%s.Handler', [hclass]), True, True) + ';');
  1202. Fps.WriteLn('var');
  1203. Fps.IncI;
  1204. Fps.WriteLn('_env: PJNIEnv;');
  1205. Fps.WriteLn('_new_env: boolean;');
  1206. Fps.WriteLn('_mpi: _TMethodPtrInfo;');
  1207. if d.Count > 0 then begin
  1208. Fps.WriteLn(Format('_args: array[0..%d] of jvalue;', [d.Count - 1]));
  1209. for i:=0 to d.Count - 1 do begin
  1210. vd:=TVarDef(d[i]);
  1211. if vd.DefType <> dtParam then
  1212. continue;
  1213. with vd do
  1214. if IsJavaVarParam(vd) and IsJavaSimpleType(VarType) then
  1215. Fps.WriteLn(Format('_tmp_%s: P%s;', [Name, DefToJniType(VarType, err)]));
  1216. end;
  1217. end;
  1218. Fps.DecI;
  1219. Fps.WriteLn('begin');
  1220. Fps.IncI;
  1221. Fps.WriteLn('CurJavaVM^^.GetEnv(CurJavaVM, @_env, JNI_VERSION_1_6);');
  1222. Fps.WriteLn('_new_env:=_env = nil;');
  1223. Fps.WriteLn('if _new_env then CurJavaVM^^.AttachCurrentThread(CurJavaVM, @_env, nil);');
  1224. Fps.WriteLn('_env^^.PushLocalFrame(_env, 100);');
  1225. Fps.WriteLn('try');
  1226. Fps.IncI;
  1227. Fps.WriteLn('_MethodPointersCS.Enter;');
  1228. Fps.WriteLn('try');
  1229. Fps.WriteLn('_mpi:=_TMethodPtrInfo(_MethodPointers[-integer(ptruint(Self)) - 1]);', 1);
  1230. Fps.WriteLn('finally');
  1231. Fps.WriteLn('_MethodPointersCS.Leave;', 1);
  1232. Fps.WriteLn('end;');
  1233. for i:=0 to d.Count - 1 do begin
  1234. vd:=TVarDef(d[i]);
  1235. if vd.DefType <> dtParam then
  1236. continue;
  1237. with vd do begin
  1238. if not IsJavaVarParam(vd) then begin
  1239. s:='L';
  1240. if VarType.DefType = dtType then
  1241. s:=Copy(JNITypeSig[TTypeDef(VarType).BasicType], 1, 1);
  1242. ss:=PasToJniType(VarType, Name);
  1243. end
  1244. else begin
  1245. s:='L';
  1246. if IsJavaSimpleType(VarType) then
  1247. ss:=Format('_env^^.New%sArray(_env, 1)', [GetJniFuncType(VarType)])
  1248. else begin
  1249. if voVar in VarOpt then
  1250. ss:=PasToJniType(VarType, Name)
  1251. else
  1252. ss:='nil';
  1253. ss:=Format('_env^^.NewObjectArray(_env, 1, %s.ClassRef, %s)', [GetTypeInfoVar(VarType), ss]);
  1254. end;
  1255. end;
  1256. Fps.WriteLn(Format('_args[%d].%s:=%s;', [i, s, ss]));
  1257. if IsJavaVarParam(vd) and (voVar in VarOpt) and IsJavaSimpleType(VarType) then
  1258. _AccessSimpleArray(TVarDef(d[i]), i, True);
  1259. end;
  1260. end;
  1261. if d.Count > 0 then
  1262. s:='@_args'
  1263. else
  1264. s:='nil';
  1265. // Calling Java handler
  1266. s:=Format('_env^^.Call%sMethodA(_env, _mpi.Obj, _mpi.MethodId, %s)', [GetJniFuncType(d.ReturnType), s]);
  1267. if d.ProcType = ptFunction then
  1268. s:=Format('Result:=%s', [JniToPasType(d.ReturnType, s, False)]);
  1269. Fps.WriteLn(s + ';');
  1270. // Processing var/out parameters
  1271. for i:=0 to d.Count - 1 do begin
  1272. vd:=TVarDef(d[i]);
  1273. if vd.DefType <> dtParam then
  1274. continue;
  1275. with vd do
  1276. if IsJavaVarParam(vd) then
  1277. if IsJavaSimpleType(VarType) then
  1278. _AccessSimpleArray(TVarDef(d[i]), i, False)
  1279. else begin
  1280. s:=Format('_env^^.GetObjectArrayElement(_env, _args[%d].L, 0)', [i]);
  1281. Fps.WriteLn(Format('%s:=%s;', [Name, JniToPasType(VarType, s, False)]));
  1282. end;
  1283. end;
  1284. Fps.DecI;
  1285. Fps.WriteLn('finally');
  1286. Fps.WriteLn('_env^^.PopLocalFrame(_env, nil);', 1);
  1287. Fps.WriteLn('if _new_env then CurJavaVM^^.DetachCurrentThread(CurJavaVM);', 1);
  1288. Fps.WriteLn('end;');
  1289. Fps.DecI;
  1290. Fps.WriteLn('end;');
  1291. // Get handler proc
  1292. Fps.WriteLn;
  1293. Fps.WriteLn(Format('function %sGetHandler(env: PJNIEnv; jobj: jobject; const ci: _TJavaClassInfo): %s.%s;',
  1294. [GetClassPrefix(d), d.Parent.Name, d.Name]));
  1295. Fps.WriteLn('begin');
  1296. Fps.WriteLn(Format('TMethod(Result):=_GetMethodPtrHandler(env, jobj, @%s.Handler, %s);', [hclass, GetTypeInfoVar(d)]), 1);
  1297. Fps.WriteLn('end;');
  1298. exit;
  1299. end;
  1300. err:=False;
  1301. WriteComment(d, 'procedural type');
  1302. RegisterPseudoClass(d);
  1303. Fjs.WriteLn(Format('/* Pascal prototype: %s */', [GetProcDeclaration(d, 'Execute')]));
  1304. Fjs.WriteLn(Format('/* Java prototype: %s */', [GetJavaProcDeclaration(d, 'Execute')]));
  1305. Fjs.WriteLn(Format('public static class %s extends %s.system.MethodPtr {', [d.Name, JavaPackage]));
  1306. Fjs.IncI;
  1307. Fjs.WriteLn(Format('{ mSignature = "%s"; }', [GetProcSignature(d)]));
  1308. Fjs.WriteLn(Format('protected %s(long objptr, boolean cleanup) { _pasobj=objptr; }', [d.Name]));
  1309. Fjs.WriteLn(Format('@Deprecated public %s(Object Obj, String MethodName) { mObject=Obj; mName=MethodName; }', [d.Name]));
  1310. Fjs.WriteLn(Format('public %s() { mObject=this; mName="Execute"; }', [d.Name]));
  1311. Fjs.WriteLn(Format('protected %s throws NoSuchMethodException { throw new NoSuchMethodException(); }', [GetJavaProcDeclaration(d, 'Execute')]));
  1312. Fjs.DecI;
  1313. Fjs.WriteLn('}');
  1314. Fjs.WriteLn;
  1315. end;
  1316. procedure TWriter.WriteSet(d: TSetDef);
  1317. begin
  1318. if not d.IsUsed then
  1319. exit;
  1320. if d.ElType = nil then
  1321. raise Exception.Create('No element type.');
  1322. WriteComment(d, '');
  1323. Fjs.WriteLn(Format('/* set of %s */', [d.ElType.Name]));
  1324. if d.Size > 4 then begin
  1325. Fjs.WriteLn('/* Set size more than 32 bits is not supported */');
  1326. exit;
  1327. end;
  1328. RegisterPseudoClass(d);
  1329. Fjs.WriteLn(Format('public static class %s extends %s.system.Set<%s,%s> {', [d.Name, JavaPackage, d.Name, d.ElType.Name]));
  1330. Fjs.IncI;
  1331. Fjs.WriteLn(Format('protected byte Size() { return %d; }', [d.Size]));
  1332. Fjs.WriteLn(Format('protected int Base() { return %d; }', [d.Base]));
  1333. Fjs.WriteLn(Format('protected int ElMax() { return %d; }', [d.ElMax]));
  1334. Fjs.WriteLn(Format('protected int Ord(%s Element) { return Element.Ord(); }', [d.ElType.Name]));
  1335. Fjs.WriteLn(Format('public %s() { }', [d.Name]));
  1336. Fjs.WriteLn(Format('public %s(%s... Elements) { super(Elements); }', [d.Name, d.ElType.Name]));
  1337. Fjs.WriteLn(Format('public %0:s(%0:s... Elements) { super(Elements); }', [d.Name]));
  1338. Fjs.WriteLn(Format('public static %0:s Exclude(%0:s s1, %0:s s2) { %0:s r = new %0:s(s1); r.Exclude(s2); return r; }', [d.Name]));
  1339. Fjs.WriteLn(Format('public static %0:s Intersect(%0:s s1, %0:s s2) { %0:s r = new %0:s(s1); r.Intersect(s2); return r; }', [d.Name]));
  1340. Fjs.DecI;
  1341. Fjs.WriteLn('}');
  1342. Fjs.WriteLn;
  1343. end;
  1344. procedure TWriter.WritePointer(d: TPointerDef; PreInfo: boolean);
  1345. begin
  1346. if not d.IsUsed or not d.IsObjPtr then
  1347. exit;
  1348. if PreInfo then begin
  1349. WriteComment(d, 'pointer');
  1350. RegisterPseudoClass(d);
  1351. WriteClassInfoVar(d);
  1352. exit;
  1353. end;
  1354. Fjs.WriteLn(Format('public static class %s extends %s {', [d.Name, d.PtrType.Name]));
  1355. Fjs.IncI;
  1356. if TClassDef(d.PtrType).CType in [ctObject, ctRecord] then
  1357. Fjs.WriteLn(Format('protected %s(long objptr, boolean cleanup) { super(objptr, false); }', [d.Name]));
  1358. Fjs.WriteLn(Format('public %s(PascalObject obj) { super(obj); }', [d.Name]));
  1359. Fjs.WriteLn(Format('public %s(long objptr) { super(objptr); }', [d.Name]));
  1360. Fjs.DecI;
  1361. Fjs.WriteLn('}');
  1362. end;
  1363. procedure TWriter.WriteUnit(u: TUnitDef);
  1364. procedure _ProcessExcludedProcParams(d: TDef);
  1365. var
  1366. i: integer;
  1367. begin
  1368. if not d.IsUsed then
  1369. exit;
  1370. if d.DefType in [dtProc, dtProcType] then begin
  1371. for i:=0 to d.Count - 1 do
  1372. if d[i].DefType = dtParam then
  1373. with TVarDef(d[i]) do
  1374. if (VarType <> nil) and not VarType.IsUsed then begin
  1375. d.SetNotUsed;
  1376. break;
  1377. end;
  1378. end
  1379. else
  1380. for i:=0 to d.Count - 1 do
  1381. _ProcessExcludedProcParams(d[i]);
  1382. end;
  1383. var
  1384. d: TDef;
  1385. i: integer;
  1386. HasSystem: boolean;
  1387. begin
  1388. if u.Processed then
  1389. exit;
  1390. u.Processed:=True;
  1391. if not u.IsUsed then
  1392. exit;
  1393. _ProcessExcludedProcParams(u);
  1394. for i:=0 to High(u.UsedUnits) do
  1395. WriteUnit(u.UsedUnits[i]);
  1396. Fps.WriteLn;
  1397. Fps.WriteLn(Format('{ Unit %s }', [u.Name]));
  1398. u.Name:=LowerCase(u.Name);
  1399. Fjs:=TTextOutStream.Create(IncludeTrailingPathDelimiter(FPkgDir) + u.Name + '.java', fmCreate);
  1400. try
  1401. WriteFileComment(Fjs);
  1402. Fjs.WriteLn(Format('package %s;', [JavaPackage]));
  1403. HasSystem:=False;
  1404. if Length(u.UsedUnits) > 0 then begin
  1405. Fjs.WriteLn;
  1406. for i:=0 to High(u.UsedUnits) do
  1407. if u.UsedUnits[i].IsUsed then begin
  1408. Fjs.WriteLn(Format('import %s.%s.*;', [JavaPackage, LowerCase(u.UsedUnits[i].Name)]));
  1409. if AnsiCompareText(u.UsedUnits[i].Name, 'system') = 0 then
  1410. HasSystem:=True;
  1411. end;
  1412. if not HasSystem then
  1413. Fjs.WriteLn(Format('import %s.system.*;', [JavaPackage]));
  1414. end;
  1415. if u.Name = 'system' then begin
  1416. Fjs.WriteLn('import java.util.Date;');
  1417. Fjs.WriteLn('import java.util.TimeZone;');
  1418. end;
  1419. Fjs.WriteLn;
  1420. Fjs.WriteLn('public class ' + u.Name + ' {');
  1421. Fjs.IncI;
  1422. if u.Name = 'system' then begin
  1423. for i:=0 to u.Count - 1 do begin
  1424. d:=u[i];
  1425. if (d.DefType = dtType) and (TTypeDef(d).BasicType = btLongInt) then begin
  1426. FIntegerType:=d;
  1427. break;
  1428. end;
  1429. end;
  1430. Fjs.WriteLn('static private boolean _JniLibLoaded = false;');
  1431. Fjs.WriteLn('public static void InitJni() {');
  1432. Fjs.WriteLn('if (!_JniLibLoaded) {', 1);
  1433. Fjs.WriteLn('_JniLibLoaded=true;', 2);
  1434. Fjs.WriteLn(Format('System.loadLibrary("%s");', [LibName]), 2);
  1435. Fjs.WriteLn('}', 1);
  1436. Fjs.WriteLn('}');
  1437. // Support functions
  1438. Fjs.WriteLn('public native static long AllocMemory(int Size);');
  1439. AddNativeMethod(u, '_AllocMemory', 'AllocMemory', '(I)J');
  1440. // Base object
  1441. Fjs.WriteLn;
  1442. Fjs.WriteLn('public static class PascalObject {');
  1443. Fjs.IncI;
  1444. Fjs.WriteLn(Format('static { %s.system.InitJni(); }', [JavaPackage]));
  1445. Fjs.WriteLn('protected long _pasobj = 0;');
  1446. Fjs.WriteLn('protected PascalObject() { }');
  1447. Fjs.WriteLn('protected PascalObject(PascalObject obj) { if (obj == null) _pasobj=0; else _pasobj=obj._pasobj; }');
  1448. Fjs.WriteLn('protected PascalObject(long objptr) { _pasobj=objptr; }');
  1449. Fjs.WriteLn('@Override public boolean equals(Object o) { return ((o instanceof PascalObject) && _pasobj == ((PascalObject)o)._pasobj); }');
  1450. Fjs.WriteLn('@Override public int hashCode() { return (int)_pasobj; }');
  1451. Fjs.DecI;
  1452. Fjs.WriteLn('}');
  1453. Fjs.WriteLn;
  1454. Fjs.WriteLn('public static long Pointer(PascalObject obj) { return (obj == null) ? 0 : obj._pasobj; }');
  1455. // Object with finalization
  1456. Fjs.WriteLn;
  1457. Fjs.WriteLn('public static class PascalObjectEx extends PascalObject {');
  1458. Fjs.IncI;
  1459. Fjs.WriteLn('protected boolean _cleanup = false;');
  1460. Fjs.WriteLn('protected void finalize() { ');
  1461. {$ifdef DEBUG}
  1462. Fjs.WriteLn('String s = "finalize(): " + getClass().getName(); if (_cleanup) s=s+". Need __Release(). ptr="+_pasobj; System.out.println(s);', 1);
  1463. {$endif DEBUG}
  1464. Fjs.WriteLn('if (_cleanup) __Release();', 1);
  1465. Fjs.WriteLn('}');
  1466. Fjs.WriteLn('protected PascalObjectEx() { }');
  1467. Fjs.WriteLn('protected PascalObjectEx(PascalObject obj) { super(obj); }');
  1468. Fjs.WriteLn('protected PascalObjectEx(long objptr) { super(objptr); }');
  1469. Fjs.WriteLn('public void __Release() { _pasobj = 0; }');
  1470. Fjs.DecI;
  1471. Fjs.WriteLn('}');
  1472. // Record
  1473. Fjs.WriteLn;
  1474. Fjs.WriteLn('public static class Record extends PascalObjectEx {');
  1475. Fjs.IncI;
  1476. Fjs.WriteLn('protected PascalObject _objref;');
  1477. Fjs.WriteLn('protected void __Init(long objptr, boolean cleanup) { _pasobj=objptr; _cleanup=cleanup; if (_pasobj==0 && __Size() != 0) _pasobj=AllocMemory(__Size()); }');
  1478. Fjs.WriteLn('protected Record(PascalObject obj) { super(obj); _objref=obj; }');
  1479. Fjs.WriteLn('protected Record(long objptr) { super(objptr); }');
  1480. Fjs.WriteLn('public Record() { }');
  1481. Fjs.WriteLn('public int __Size() { return 0; }');
  1482. Fjs.DecI;
  1483. Fjs.WriteLn('}');
  1484. // Method pointer base class
  1485. d:=TClassDef.Create(FThisUnit, dtClass);
  1486. d.Name:='_TMethodPtrInfo';
  1487. d.AliasName:='MethodPtr';
  1488. WriteClassInfoVar(d);
  1489. // Method pointer support
  1490. Fps.WriteLn;
  1491. Fps.WriteLn('type');
  1492. Fps.IncI;
  1493. Fps.WriteLn('_TMethodPtrInfo = class');
  1494. Fps.IncI;
  1495. Fps.WriteLn('Obj: JObject;');
  1496. Fps.WriteLn('MethodId: JMethodID;');
  1497. Fps.WriteLn('Index, RefCnt: integer;');
  1498. Fps.WriteLn('RealMethod: TMethod;');
  1499. Fps.WriteLn('InlineHandler: boolean;');
  1500. Fps.WriteLn('constructor Create(env: PJNIEnv; JavaObj: JObject; const AMethodName, AMethodSig: ansistring);');
  1501. Fps.WriteLn('procedure Release(env: PJNIEnv);');
  1502. Fps.DecI;
  1503. Fps.WriteLn('end;');
  1504. Fps.DecI;
  1505. Fps.WriteLn;
  1506. Fps.WriteLn('var _MethodPointers: array of _TMethodPtrInfo;');
  1507. Fps.WriteLn('var _MethodPointersCS: TCriticalSection;');
  1508. Fps.WriteLn;
  1509. Fps.WriteLn('constructor _TMethodPtrInfo.Create(env: PJNIEnv; JavaObj: JObject; const AMethodName, AMethodSig: ansistring);');
  1510. Fps.WriteLn('var c: JClass;');
  1511. Fps.WriteLn('begin');
  1512. Fps.IncI;
  1513. Fps.WriteLn('if (JavaObj = nil) or (AMethodName = '''') then exit;');
  1514. Fps.WriteLn('c:=env^^.GetObjectClass(env, JavaObj);');
  1515. Fps.WriteLn('if c = nil then exit;');
  1516. Fps.WriteLn('MethodId:=env^^.GetMethodID(env, c, PAnsiChar(AMethodName), PAnsiChar(AMethodSig));');
  1517. Fps.WriteLn('if MethodId = nil then raise Exception.CreateFmt(''Method "%s" does not exist or has wrong parameters.'', [AMethodName]);');
  1518. Fps.WriteLn('Obj:=env^^.NewGlobalRef(env, JavaObj);');
  1519. Fps.WriteLn('_MethodPointersCS.Enter;');
  1520. Fps.WriteLn('try');
  1521. Fps.IncI;
  1522. Fps.WriteLn('Index:=Length(_MethodPointers) + 1;');
  1523. Fps.WriteLn(Format('if Index > %d then raise Exception.Create(''Too many method pointers.'');', [MaxMethodPointers]));
  1524. Fps.WriteLn('SetLength(_MethodPointers, Index);');
  1525. Fps.WriteLn('_MethodPointers[Index - 1]:=Self;');
  1526. Fps.WriteLn('finally', -1);
  1527. Fps.WriteLn('_MethodPointersCS.Leave;');
  1528. Fps.DecI;
  1529. Fps.WriteLn('end;');
  1530. Fps.DecI;
  1531. Fps.WriteLn('end;');
  1532. Fps.WriteLn;
  1533. Fps.WriteLn('procedure _TMethodPtrInfo.Release(env: PJNIEnv);');
  1534. Fps.WriteLn('var i: integer;');
  1535. Fps.WriteLn('begin');
  1536. Fps.IncI;
  1537. Fps.WriteLn('i:=InterlockedDecrement(RefCnt);');
  1538. {$ifdef DEBUG}
  1539. Fps.WriteLn('writeln(''_TMethodPtrInfo.Release(). RefCnt='',i,'' ptr='',ptruint(Self));');
  1540. {$endif DEBUG}
  1541. Fps.WriteLn('if i <> 0 then exit;');
  1542. Fps.WriteLn('if Index > 0 then begin');
  1543. Fps.IncI;
  1544. Fps.WriteLn('_MethodPointersCS.Enter;');
  1545. Fps.WriteLn('try');
  1546. Fps.IncI;
  1547. Fps.WriteLn('if InlineHandler then begin');
  1548. Fps.IncI;
  1549. {$ifdef DEBUG}
  1550. Fps.WriteLn('writeln(''Finalizing Java inline handler.'');');
  1551. {$endif DEBUG}
  1552. Fps.WriteLn(Format('env^^.SetLongField(env, Obj, %s.ObjFieldId, -1);', [GetTypeInfoVar(d)]));
  1553. Fps.DecI;
  1554. Fps.WriteLn('end;');
  1555. Fps.WriteLn('env^^.DeleteGlobalRef(env, Obj);');
  1556. Fps.WriteLn('_MethodPointers[Index-1]:=nil;');
  1557. Fps.WriteLn('Index:=High(_MethodPointers);');
  1558. Fps.WriteLn('while (Index >= 0) and (_MethodPointers[Index] = nil) do Dec(Index);');
  1559. Fps.WriteLn('SetLength(_MethodPointers, Index + 1);');
  1560. Fps.WriteLn('finally', -1);
  1561. Fps.WriteLn('_MethodPointersCS.Leave;');
  1562. Fps.DecI;
  1563. Fps.WriteLn('end;');
  1564. Fps.DecI;
  1565. Fps.WriteLn('end;');
  1566. Fps.WriteLn('Self.Destroy;');
  1567. {$ifdef DEBUG}
  1568. Fps.WriteLn('writeln(''_TMethodPtrInfo destroyed.'');');
  1569. {$endif DEBUG}
  1570. Fps.DecI;
  1571. Fps.WriteLn('end;');
  1572. Fps.WriteLn;
  1573. Fps.WriteLn('procedure _RefMethodPtr(env: PJNIEnv; const m: TMethod; AddRef: boolean);');
  1574. Fps.WriteLn('var i: integer;');
  1575. Fps.WriteLn('begin');
  1576. Fps.IncI;
  1577. Fps.WriteLn('i:=-integer(ptruint(m.Data));');
  1578. {$ifdef DEBUG}
  1579. Fps.WriteLn('writeln(''_RefMethodPtr. i='',i,'' AddRef='',AddRef);');
  1580. {$endif DEBUG}
  1581. Fps.WriteLn(Format('if (i < 1) or (i > %d) then exit;', [MaxMethodPointers]));
  1582. Fps.WriteLn('_MethodPointersCS.Enter;');
  1583. Fps.WriteLn('try');
  1584. Fps.IncI;
  1585. Fps.WriteLn('with _MethodPointers[i - 1] do');
  1586. Fps.WriteLn('if AddRef then InterlockedIncrement(RefCnt) else Release(env);', 1);
  1587. Fps.WriteLn('finally', -1);
  1588. Fps.WriteLn('_MethodPointersCS.Leave;');
  1589. Fps.DecI;
  1590. Fps.WriteLn('end;');
  1591. Fps.DecI;
  1592. Fps.WriteLn('end;');
  1593. Fps.WriteLn;
  1594. Fps.WriteLn('function _CreateMethodPtrObject(env: PJNIEnv; const m: TMethod; const ci: _TJavaClassInfo): jobject;');
  1595. Fps.WriteLn('var i: integer;');
  1596. Fps.WriteLn('var mpi: _TMethodPtrInfo;');
  1597. Fps.WriteLn('begin');
  1598. Fps.IncI;
  1599. Fps.WriteLn('_MethodPointersCS.Enter;');
  1600. Fps.WriteLn('try');
  1601. Fps.IncI;
  1602. Fps.WriteLn('i:=-integer(ptruint(m.Data));');
  1603. Fps.WriteLn(Format('if (i > 0) and (i <= %d) then begin', [MaxMethodPointers]));
  1604. Fps.WriteLn('mpi:=_MethodPointers[i - 1];', 1);
  1605. Fps.WriteLn('end');
  1606. Fps.WriteLn('else begin');
  1607. Fps.WriteLn('mpi:=_TMethodPtrInfo.Create(env, nil, '''', '''');', 1);
  1608. Fps.WriteLn('mpi.RealMethod:=m;', 1);
  1609. Fps.WriteLn('InterlockedIncrement(mpi.RefCnt);', 1);
  1610. Fps.WriteLn('end;');
  1611. Fps.WriteLn('finally', -1);
  1612. Fps.WriteLn('_MethodPointersCS.Leave;');
  1613. Fps.DecI;
  1614. Fps.WriteLn('end;');
  1615. Fps.WriteLn('Result:=_CreateJavaObj(env, pointer(mpi), ci);');
  1616. Fps.DecI;
  1617. Fps.WriteLn('end;');
  1618. Fps.WriteLn;
  1619. Fps.WriteLn('function _GetMethodPtrHandler(env: PJNIEnv; jobj: jobject; hptr: pointer; const ci: _TJavaClassInfo): TMethod;');
  1620. Fps.WriteLn('var mpi: _TMethodPtrInfo;');
  1621. Fps.WriteLn('begin');
  1622. Fps.IncI;
  1623. Fps.WriteLn( 'Result.Data:=nil; Result.Code:=nil;');
  1624. Fps.WriteLn( 'mpi:=_TMethodPtrInfo(_GetPasObj(env, jobj, ci, False));');
  1625. Fps.WriteLn( 'if mpi = nil then exit;');
  1626. Fps.WriteLn( 'if pointer(mpi) = pointer(ptruint(-1)) then begin');
  1627. Fps.WriteLn( 'env^^.CallVoidMethodA(env, jobj, env^^.GetMethodID(env, ci.ClassRef, ''Init'', ''()V''), nil);', 1);
  1628. Fps.WriteLn( 'Result:=_GetMethodPtrHandler(env, jobj, hptr, ci);', 1);
  1629. Fps.WriteLn( 'exit;', 1);
  1630. Fps.WriteLn( 'end;');
  1631. Fps.WriteLn( 'if mpi.Index = 0 then');
  1632. Fps.WriteLn( 'TMethod(Result):=mpi.RealMethod', 1);
  1633. Fps.WriteLn( 'else');
  1634. Fps.WriteLn( 'with TMethod(Result) do begin', 1);
  1635. Fps.WriteLn( 'Data:=pointer(ptruint(-integer(mpi.Index)));', 2);
  1636. Fps.WriteLn( 'Code:=hptr;', 2);
  1637. Fps.WriteLn( 'end;', 1);
  1638. Fps.DecI;
  1639. Fps.WriteLn('end;');
  1640. Fps.WriteLn;
  1641. Fps.WriteLn('procedure _TMethodPtrInfo_Init(env: PJNIEnv; _self, JavaObj: JObject; AMethodName, AMethodSig: jstring; IncRef: jboolean);' + JniCaliing);
  1642. Fps.WriteLn('var mpi: _TMethodPtrInfo;');
  1643. Fps.WriteLn('begin');
  1644. Fps.IncI;
  1645. EHandlerStart;
  1646. Fps.WriteLn('mpi:=_TMethodPtrInfo.Create(env, JavaObj, ansistring(_StringFromJString(env, AMethodName)), ansistring(_StringFromJString(env, AMethodSig)));');
  1647. Fps.WriteLn('if IncRef <> 0 then');
  1648. Fps.WriteLn('InterlockedIncrement(mpi.RefCnt)', 1);
  1649. Fps.WriteLn('else');
  1650. Fps.WriteLn('mpi.InlineHandler:=True;', 1);
  1651. {$ifdef DEBUG}
  1652. Fps.WriteLn('writeln(''_TMethodPtrInfo_Init. RefCnt='',mpi.RefCnt,'' ptr='',ptruint(mpi));');
  1653. {$endif DEBUG}
  1654. Fps.WriteLn(Format('env^^.SetLongField(env, _self, %s.ObjFieldId, Int64(ptruint(mpi)));', [GetTypeInfoVar(d)]));
  1655. EHandlerEnd('env');
  1656. Fps.DecI;
  1657. Fps.WriteLn('end;');
  1658. AddNativeMethod(d, '_TMethodPtrInfo_Init', '__Init', Format('(Ljava/lang/Object;%s%sZ)V', [JNITypeSig[btString], JNITypeSig[btString]]));
  1659. Fps.WriteLn;
  1660. Fps.WriteLn('procedure _TMethodPtrInfo_Release(env: PJNIEnv; _self: JObject);' + JniCaliing);
  1661. Fps.WriteLn('begin');
  1662. Fps.IncI;
  1663. EHandlerStart;
  1664. Fps.WriteLn(Format('_TMethodPtrInfo(_GetPasObj(env, _self, %s, True)).Release(env);', [GetTypeInfoVar(d)]));
  1665. EHandlerEnd('env');
  1666. Fps.DecI;
  1667. Fps.WriteLn('end;');
  1668. AddNativeMethod(d, '_TMethodPtrInfo_Release', '__Destroy', '()V');
  1669. Fjs.WriteLn;
  1670. Fjs.WriteLn('public static class MethodPtr extends PascalObjectEx {');
  1671. Fjs.IncI;
  1672. Fjs.WriteLn('private native void __Init(Object Obj, String MethodName, String MethodSignature, boolean IncRef);');
  1673. Fjs.WriteLn('private native void __Destroy();');
  1674. Fjs.WriteLn('protected Object mObject;');
  1675. Fjs.WriteLn('protected String mName;');
  1676. Fjs.WriteLn('protected String mSignature;');
  1677. Fjs.WriteLn('protected void Init() { __Init(mObject, mName, mSignature, this != mObject); }');
  1678. Fjs.WriteLn('protected MethodPtr() { _cleanup=true; _pasobj=-1; }');
  1679. Fjs.WriteLn('public void __Release() { if (_pasobj > 0) __Destroy(); }');
  1680. Fjs.DecI;
  1681. Fjs.WriteLn('}');
  1682. Fjs.WriteLn;
  1683. // Base class for Enum
  1684. Fjs.WriteLn('public static class Enum {');
  1685. Fjs.IncI;
  1686. Fjs.WriteLn('public int Value;');
  1687. Fjs.WriteLn('public int Ord() { return Value; }');
  1688. Fjs.WriteLn('@Override public boolean equals(Object o) { return (o instanceof Integer) && Value == (Integer)o; }');
  1689. Fjs.WriteLn('public boolean equals(int v) { return Value == v; }');
  1690. Fjs.WriteLn('@Override public int hashCode() { return Value; }');
  1691. Fjs.DecI;
  1692. Fjs.WriteLn('}');
  1693. Fjs.WriteLn;
  1694. // Base class for Set
  1695. Fjs.WriteLn('public static class Set<TS extends Set<?,?>,TE extends Enum> {');
  1696. Fjs.IncI;
  1697. Fjs.WriteLn('protected int Value = 0;');
  1698. Fjs.WriteLn('protected byte Size() { return 0; }');
  1699. Fjs.WriteLn('protected int Base() { return 0; }');
  1700. Fjs.WriteLn('protected int ElMax() { return 0; }');
  1701. Fjs.WriteLn('protected int Ord(TE Element) { return 0; }');
  1702. Fjs.WriteLn('protected int GetMask(TE Element) {');
  1703. Fjs.IncI;
  1704. Fjs.WriteLn('return 1 << (Ord(Element) - Base());');
  1705. Fjs.DecI;
  1706. Fjs.WriteLn('}');
  1707. Fjs.WriteLn('public Set() { }');
  1708. Fjs.WriteLn('public Set(TE... Elements) { Include(Elements); }');
  1709. Fjs.WriteLn('public Set(TS... Elements) { for (TS e : Elements) Include(e); }');
  1710. Fjs.WriteLn('public void Include(TE... Elements) { for (TE e: Elements) Value = Value | GetMask(e); }');
  1711. Fjs.WriteLn('public void Include(TS s) { Value=Value | s.Value; }');
  1712. Fjs.WriteLn('public void Exclude(TE... Elements) { for (TE e: Elements) Value = Value & ~GetMask(e); }');
  1713. Fjs.WriteLn('public void Exclude(TS s) { Value=Value & ~s.Value; }');
  1714. Fjs.WriteLn('public void Assign(TS s) { Value=s.Value; }');
  1715. Fjs.WriteLn('public void Intersect(TS s) { Value=Value & s.Value; }');
  1716. Fjs.WriteLn('public boolean Has(TE Element) { return (Value & GetMask(Element)) != 0; }');
  1717. Fjs.WriteLn('public boolean IsEmpty() { return Value == 0; }');
  1718. Fjs.WriteLn('public boolean equals(TS s) { return Value == s.Value; }');
  1719. Fjs.WriteLn('public boolean equals(TE Element) { return Value == Ord(Element); }');
  1720. Fjs.WriteLn('public boolean equals(int Element) { return Value == Element; }');
  1721. Fjs.DecI;
  1722. Fjs.WriteLn('}');
  1723. Fjs.WriteLn;
  1724. // TDateTime support
  1725. Fjs.WriteLn('public static class TDateTime {');
  1726. Fjs.IncI;
  1727. Fjs.WriteLn('public static Date toDateUTC(double d) {');
  1728. Fjs.WriteLn('return new Date(Math.round((d - 25569)*86400000.0));', 1);
  1729. Fjs.WriteLn('}');
  1730. Fjs.WriteLn('public static Date toDate(double d) {');
  1731. Fjs.WriteLn('long t = Math.round((d - 25569)*86400000.0); return new Date(t - TimeZone.getDefault().getOffset(t));', 1);
  1732. Fjs.WriteLn('}');
  1733. Fjs.WriteLn('public static double getUTC(Date d) {');
  1734. Fjs.WriteLn('return d.getTime()/86400000.0 + 25569;', 1);
  1735. Fjs.WriteLn('}');
  1736. Fjs.WriteLn('public static double get(Date d) {');
  1737. Fjs.WriteLn('return (d.getTime() + TimeZone.getDefault().getOffset(d.getTime()))/86400000.0 + 25569;', 1);
  1738. Fjs.WriteLn('}');
  1739. Fjs.DecI;
  1740. Fjs.WriteLn('}');
  1741. Fjs.WriteLn;
  1742. end;
  1743. Fjs.WriteLn(Format('static { %s.system.InitJni(); }', [JavaPackage]));
  1744. Fjs.WriteLn;
  1745. // First pass
  1746. for i:=0 to u.Count - 1 do begin
  1747. d:=u[i];
  1748. if not d.IsUsed then
  1749. continue;
  1750. case d.DefType of
  1751. dtSet, dtEnum:
  1752. WriteClassInfoVar(d);
  1753. dtClass:
  1754. WriteClass(TClassDef(d), True);
  1755. dtProcType:
  1756. WriteProcType(TProcDef(d), True);
  1757. dtPointer:
  1758. WritePointer(TPointerDef(d), True);
  1759. end;
  1760. end;
  1761. // Second pass
  1762. for i:=0 to u.Count - 1 do begin
  1763. d:=u[i];
  1764. if not d.IsUsed then
  1765. continue;
  1766. case d.DefType of
  1767. dtClass:
  1768. WriteClass(TClassDef(d), False);
  1769. dtProc:
  1770. WriteProc(TProcDef(d));
  1771. dtVar, dtProp:
  1772. WriteVar(TVarDef(d));
  1773. dtEnum:
  1774. WriteEnum(d);
  1775. dtProcType:
  1776. WriteProcType(TProcDef(d), False);
  1777. dtSet:
  1778. WriteSet(TSetDef(d));
  1779. dtConst:
  1780. WriteConst(TConstDef(d));
  1781. dtPointer:
  1782. WritePointer(TPointerDef(d), False);
  1783. end;
  1784. end;
  1785. Fjs.DecI;
  1786. Fjs.WriteLn('}');
  1787. finally
  1788. Fjs.Free;
  1789. end;
  1790. end;
  1791. procedure TWriter.WriteOnLoad;
  1792. var
  1793. i, j: integer;
  1794. ci: TClassInfo;
  1795. s, ss, fn: string;
  1796. d: TTypeDef;
  1797. begin
  1798. if FClasses.Count = 0 then
  1799. exit;
  1800. Fps.WriteLn;
  1801. Fps.WriteLn('function JNI_OnLoad(vm: PJavaVM; reserved: pointer): jint;' + JniCaliing);
  1802. Fps.WriteLn('const');
  1803. for i:=0 to FClasses.Count - 1 do begin
  1804. ci:=FClasses.GetClassInfo(i);
  1805. if ci.Funcs.Count = 0 then
  1806. continue;
  1807. Fps.WriteLn(Format(' _%sNativeMethods: array[0..%d] of JNINativeMethod = (', [GetClassPrefix(ci.Def, FClasses.GetClassName(i)), ci.Funcs.Count - 1]));
  1808. for j:=0 to ci.Funcs.Count - 1 do begin
  1809. with TProcInfo(ci.Funcs[j]) do
  1810. Fps.Write(Format(' (name: ''%s''; signature: ''%s''; fnPtr: @%s)', [Name, JniSignature, JniName]));
  1811. if j < ci.Funcs.Count - 1 then
  1812. Fps.Write(',');
  1813. Fps.WriteLn;
  1814. end;
  1815. Fps.WriteLn(' );');
  1816. end;
  1817. Fps.WriteLn;
  1818. Fps.WriteLn('var');
  1819. Fps.IncI;
  1820. Fps.WriteLn('env: PJNIEnv;');
  1821. Fps.WriteLn;
  1822. Fps.WriteLn('function _Reg(ClassName: PAnsiChar; Methods: PJNINativeMethod; Count: integer; ci: _PJavaClassInfo; const FieldName: ansistring = ''_pasobj''; const FieldSig: ansistring = ''J''): boolean;');
  1823. Fps.WriteLn('var');
  1824. Fps.WriteLn('c: jclass;', 1);
  1825. Fps.WriteLn('begin');
  1826. Fps.IncI;
  1827. Fps.WriteLn('Result:=False;');
  1828. Fps.WriteLn('c:=env^^.FindClass(env, ClassName);');
  1829. Fps.WriteLn('if c = nil then exit;');
  1830. Fps.WriteLn('Result:=(Count = 0) or (env^^.RegisterNatives(env, c, Methods, Count) = 0);');
  1831. Fps.WriteLn('if Result and (ci <> nil) then begin');
  1832. Fps.IncI;
  1833. Fps.WriteLn('ci^.ClassRef:=env^^.NewGlobalRef(env, c);');
  1834. Fps.WriteLn('Result:=ci^.ClassRef <> nil;');
  1835. Fps.WriteLn('if Result and (env^^.ExceptionCheck(env) = 0) then begin');
  1836. Fps.WriteLn('ci^.ConstrId:=env^^.GetMethodID(env, ci^.ClassRef, ''<init>'', ''(JZ)V'');', 1);
  1837. Fps.WriteLn('env^^.ExceptionClear(env);', 1);
  1838. Fps.WriteLn('end;');
  1839. Fps.WriteLn('if Result and (FieldName <> '''') then begin');
  1840. Fps.WriteLn('ci^.ObjFieldId:=env^^.GetFieldID(env, ci^.ClassRef, PAnsiChar(FieldName), PAnsiChar(FieldSig));', 1);
  1841. Fps.WriteLn('Result:=ci^.ObjFieldId <> nil;', 1);
  1842. Fps.WriteLn('end;');
  1843. Fps.DecI;
  1844. Fps.WriteLn('end;');
  1845. Fps.DecI;
  1846. Fps.WriteLn('end;');
  1847. Fps.WriteLn;
  1848. Fps.WriteLn('begin', -1);
  1849. Fps.WriteLn('Result:=JNI_ERR;');
  1850. Fps.WriteLn('if vm^^.GetEnv(vm, @env, JNI_VERSION_1_6) <> JNI_OK then exit;');
  1851. Fps.WriteLn('CurJavaVM:=vm;');
  1852. d:=TTypeDef.Create(nil, dtType);
  1853. try
  1854. d.BasicType:=btString;
  1855. s:=JNITypeSig[d.BasicType];
  1856. s:=Copy(s, 2, Length(s) - 2);
  1857. Fps.WriteLn(Format('if not _Reg(''%s'', nil, 0, @%s, '''', '''') then exit;',
  1858. [s, GetTypeInfoVar(d)]));
  1859. finally
  1860. d.Free;
  1861. end;
  1862. for i:=0 to FClasses.Count - 1 do begin
  1863. ci:=FClasses.GetClassInfo(i);
  1864. s:=GetTypeInfoVar(ci.Def);
  1865. if (s = '') or (ci.IsCommonClass) then
  1866. s:='nil'
  1867. else
  1868. s:='@' + s;
  1869. if ci.Funcs.Count = 0 then
  1870. ss:='nil'
  1871. else
  1872. ss:=Format('@_%sNativeMethods', [GetClassPrefix(ci.Def, FClasses.GetClassName(i))]);
  1873. fn:='';
  1874. if ci.Def <> nil then
  1875. if ci.Def.DefType in [dtSet, dtEnum] then
  1876. fn:=', ''Value'', ''I''';
  1877. Fps.WriteLn(Format('if not _Reg(''%s'', %s, %d, %s%s) then exit;',
  1878. [GetJavaClassPath(ci.Def, FClasses.GetClassName(i)), ss, ci.Funcs.Count, s, fn]));
  1879. end;
  1880. Fps.WriteLn('Result:=JNI_VERSION_1_6;');
  1881. Fps.DecI;
  1882. Fps.WriteLn('end;');
  1883. Fps.WriteLn;
  1884. Fps.WriteLn('exports JNI_OnLoad;');
  1885. end;
  1886. function TWriter.JniToPasType(d: TDef; const v: string; CheckNil: boolean): string;
  1887. var
  1888. n: string;
  1889. begin
  1890. Result:=v;
  1891. if d = nil then
  1892. exit;
  1893. case d.DefType of
  1894. dtType:
  1895. with TTypeDef(d) do
  1896. case BasicType of
  1897. btString, btWideString:
  1898. begin
  1899. Result:=Format('_StringFromJString(_env, %s)', [Result]);
  1900. if BasicType <> btWideString then
  1901. Result:=Format('%s(%s)', [d.Name, Result]);
  1902. end;
  1903. btBoolean:
  1904. Result:=Format('LongBool(%s)', [Result]);
  1905. btChar:
  1906. Result:=Format('char(widechar(%s))', [Result]);
  1907. btWideChar:
  1908. Result:=Format('widechar(%s)', [Result]);
  1909. btGuid:
  1910. Result:=Format('StringToGUID(ansistring(_StringFromJString(_env, %s)))', [Result]);
  1911. else
  1912. Result:=Format('%s.%s(%s)', [d.Parent.Name, d.Name, Result]);
  1913. end;
  1914. dtClass:
  1915. begin
  1916. if TClassDef(d).CType = ctRecord then
  1917. n:='True'
  1918. else
  1919. if CheckNil then
  1920. n:='True'
  1921. else
  1922. n:='False';
  1923. Result:=Format('_GetPasObj(_env, %s, %s, %s)', [Result, GetTypeInfoVar(d), n]);
  1924. if TClassDef(d).CType in [ctObject, ctRecord] then
  1925. Result:=Result + '^';
  1926. Result:=Format('%s.%s(%s)', [d.Parent.Name, d.Name, Result]);
  1927. end;
  1928. dtProcType:
  1929. Result:=Format('%sGetHandler(_env, %s, %s)', [GetClassPrefix(d), Result, GetTypeInfoVar(d)]);
  1930. dtEnum:
  1931. Result:=Format('%s.%s(_GetIntObjValue(_env, %s, %s))', [d.Parent.Name, d.Name, Result, GetTypeInfoVar(d)]);
  1932. dtSet:
  1933. Result:=Format('%s.%s(%s(_GetIntObjValue(_env, %s, %s)))', [d.Parent.Name, d.Name, GetPasIntType(TSetDef(d).Size), Result, GetTypeInfoVar(d)]);
  1934. dtPointer:
  1935. begin
  1936. if TPointerDef(d).IsObjPtr then
  1937. Result:=Format('%s.%s(_GetPasObj(_env, %s, %s, False))', [d.Parent.Name, d.Name, Result, GetTypeInfoVar(d)])
  1938. else
  1939. Result:=Format('pointer(ptruint(%s))', [Result]);
  1940. end;
  1941. end;
  1942. end;
  1943. function TWriter.PasToJniType(d: TDef; const v: string): string;
  1944. begin
  1945. Result:=v;
  1946. if d = nil then
  1947. exit;
  1948. case d.DefType of
  1949. dtType:
  1950. with TTypeDef(d) do
  1951. case BasicType of
  1952. btString, btWideString:
  1953. Result:=Format('_StringToJString(_env, _JNIString(%s))', [Result]);
  1954. btBoolean:
  1955. Result:=Format('jboolean(LongBool(%s))', [Result]);
  1956. btChar:
  1957. Result:=Format('jchar(widechar(%s))', [Result]);
  1958. btWideChar:
  1959. Result:=Format('jchar(%s)', [Result]);
  1960. btEnum:
  1961. Result:=Format('jint(%s)', [Result]);
  1962. btGuid:
  1963. Result:=Format('_StringToJString(_env, _JNIString(GUIDToString(%s)))', [Result]);
  1964. end;
  1965. dtClass:
  1966. case TClassDef(d).CType of
  1967. ctObject, ctRecord:
  1968. Result:=Format('_%s_CreateObj(_env, %s)', [GetClassPrefix(d), Result]);
  1969. ctInterface:
  1970. Result:=Format('_CreateJavaObj(_env, pointer(%s), %s)', [Result, GetTypeInfoVar(d)]);
  1971. else
  1972. Result:=Format('_CreateJavaObj(_env, %s, %s)', [Result, GetTypeInfoVar(d)]);
  1973. end;
  1974. dtProcType:
  1975. Result:=Format('_CreateMethodPtrObject(_env, TMethod(%s), %s)', [Result, GetTypeInfoVar(d)]);
  1976. dtEnum:
  1977. Result:=Format('_CreateIntObj(_env, longint(%s), %s)', [Result, GetTypeInfoVar(d)]);
  1978. dtSet:
  1979. Result:=Format('_CreateIntObj(_env, %s(%s), %s)', [GetPasIntType(TSetDef(d).Size), Result, GetTypeInfoVar(d)]);
  1980. dtPointer:
  1981. if TPointerDef(d).IsObjPtr then
  1982. Result:=Format('_CreateJavaObj(_env, %s, %s)', [Result, GetTypeInfoVar(d)])
  1983. else
  1984. Result:=Format('ptruint(pointer(%s))', [Result]);
  1985. end;
  1986. end;
  1987. function TWriter.GetTypeInfoVar(ClassDef: TDef): string;
  1988. begin
  1989. if ClassDef.DefType = dtUnit then
  1990. Result:=''
  1991. else
  1992. if ClassDef.DefType = dtType then
  1993. Result:='_Java_' + JavaType[TTypeDef(ClassDef).BasicType] + '_Info'
  1994. else
  1995. Result:='_JNI_' + ClassDef.Parent.Name + '_' + ClassDef.Name + '_Info';
  1996. end;
  1997. function TWriter.GetClassPrefix(ClassDef: TDef; const AClassName: string): string;
  1998. begin
  1999. if AClassName = '' then
  2000. Result:=ClassDef.Name
  2001. else
  2002. Result:=AClassName;
  2003. Result:=Result + '_';
  2004. if ClassDef.DefType <> dtUnit then
  2005. Result:=ClassDef.Parent.Name + '_' + Result;
  2006. Result:='JNI_' + Result;
  2007. end;
  2008. function TWriter.IsJavaSimpleType(d: TDef): boolean;
  2009. begin
  2010. Result:=d <> nil;
  2011. if Result then
  2012. case d.DefType of
  2013. dtType:
  2014. Result:=Length(JNITypeSig[TTypeDef(d).BasicType]) = 1;
  2015. dtPointer:
  2016. Result:=not TPointerDef(d).IsObjPtr;
  2017. else
  2018. Result:=False;
  2019. end;
  2020. end;
  2021. function TWriter.IsJavaVarParam(ParamDef: TVarDef): boolean;
  2022. begin
  2023. with ParamDef do
  2024. Result:=VarOpt * [voVar, voOut] <> [];
  2025. end;
  2026. function TWriter.GetProcDeclaration(d: TProcDef; const ProcName: string; FullTypeNames: boolean; InternalParaNames: boolean): string;
  2027. var
  2028. s, ss: string;
  2029. j: integer;
  2030. begin
  2031. with d do begin
  2032. s:='';
  2033. for j:=0 to Count - 1 do
  2034. with TVarDef(Items[j]) do begin
  2035. if DefType <> dtParam then
  2036. continue;
  2037. if s <> '' then
  2038. s:=s + '; ';
  2039. if voVar in VarOpt then
  2040. s:=s + 'var '
  2041. else
  2042. if voOut in VarOpt then
  2043. s:=s + 'out '
  2044. else
  2045. if voConst in VarOpt then
  2046. s:=s + 'const ';
  2047. if InternalParaNames then
  2048. s:=s + Name
  2049. else
  2050. s:=s + AliasName;
  2051. s:=s + ': ' + GetPasType(VarType, FullTypeNames);
  2052. end;
  2053. if s <> '' then
  2054. s:='(' + s + ')';
  2055. case ProcType of
  2056. ptConstructor:
  2057. ss:='constructor';
  2058. ptDestructor:
  2059. ss:='destructor';
  2060. ptProcedure:
  2061. ss:='procedure';
  2062. ptFunction:
  2063. ss:='function';
  2064. else
  2065. ss:='';
  2066. end;
  2067. if ProcType in [ptConstructor, ptFunction] then
  2068. s:=s + ': ' + GetPasType(ReturnType, FullTypeNames);
  2069. ss:=ss + ' ';
  2070. if ProcName <> '' then
  2071. ss:=ss + ProcName
  2072. else
  2073. ss:=ss + Name;
  2074. Result:=ss + s;
  2075. end;
  2076. end;
  2077. function TWriter.GetJavaProcDeclaration(d: TProcDef; const ProcName: string): string;
  2078. var
  2079. s, ss: string;
  2080. j: integer;
  2081. vd: TVarDef;
  2082. begin
  2083. with d do begin
  2084. if ProcName <> '' then
  2085. ss:=ProcName
  2086. else
  2087. ss:=AliasName;
  2088. ss:=DefToJavaType(ReturnType) + ' ' + ss + '(';
  2089. s:='';
  2090. for j:=0 to Count - 1 do begin
  2091. vd:=TVarDef(Items[j]);
  2092. if vd.DefType <> dtParam then
  2093. continue;
  2094. with vd do begin
  2095. if (VarType <> nil) and (VarType.DefType = dtJniEnv) then
  2096. continue;
  2097. if s <> '' then
  2098. s:=s + ', ';
  2099. s:=s + DefToJavaType(VarType);
  2100. if IsJavaVarParam(vd) then
  2101. s:=s + '[]';
  2102. s:=s + ' ' + AliasName;
  2103. end;
  2104. end;
  2105. ss:=ss + s + ')';
  2106. end;
  2107. Result:=ss;
  2108. end;
  2109. function TWriter.GetJniFuncType(d: TDef): string;
  2110. begin
  2111. if IsJavaSimpleType(d) then begin
  2112. if d.DefType = dtPointer then
  2113. Result:='Long'
  2114. else begin
  2115. Result:=JavaType[TTypeDef(d).BasicType];
  2116. Result[1]:=UpCase(Result[1]);
  2117. end;
  2118. end
  2119. else
  2120. Result:='Object';
  2121. end;
  2122. function TWriter.GetJavaClassName(cls: TDef; it: TDef): string;
  2123. begin
  2124. Result:=cls.AliasName;
  2125. if (cls.DefType <> dtClass) or ((it <> nil) and not (it.DefType in ReplDefs)) then
  2126. exit;
  2127. with TClassDef(cls) do begin
  2128. if not (HasReplacedItems or ImplementsReplacedItems) then
  2129. exit;
  2130. if ImplementsReplacedItems and not HasReplacedItems then
  2131. exit;
  2132. if it <> nil then
  2133. with TReplDef(it) do begin
  2134. if (it.DefType = dtProc) and (TProcDef(it).ProcType = ptConstructor) then
  2135. exit;
  2136. if IsReplaced or IsReplImpl then
  2137. exit;
  2138. end;
  2139. end;
  2140. Result:='__' + Result;
  2141. end;
  2142. procedure TWriter.RegisterPseudoClass(d: TDef);
  2143. var
  2144. ci: TClassInfo;
  2145. begin
  2146. if FClasses.IndexOf(d.Name, d) < 0 then begin
  2147. ci:=TClassInfo.Create;
  2148. ci.Def:=d;
  2149. FClasses.Add(d.Name, d, ci);
  2150. end;
  2151. end;
  2152. function TWriter.GetPasIntType(Size: integer): string;
  2153. begin
  2154. case Size of
  2155. 1: Result:='byte';
  2156. 2: Result:='word';
  2157. else
  2158. Result:='cardinal';
  2159. end;
  2160. end;
  2161. function TWriter.GetPasType(d: TDef; FullName: boolean): string;
  2162. begin
  2163. Result:=d.Name;
  2164. if FullName and (d.DefType <> dtType) then
  2165. Result:=d.Parent.Name + '.' + Result;
  2166. end;
  2167. function TWriter.AddCustomProc(ParentDef: TDef; const JniName, Name: string; RetType: TBasicType; const Params: array of TBasicType): TProcDef;
  2168. var
  2169. i: integer;
  2170. vd: TVarDef;
  2171. begin
  2172. Result:=TProcDef.Create(ParentDef, dtProc);
  2173. Result.Name:=JniName;
  2174. Result.AliasName:=Name;
  2175. if RetType = btVoid then
  2176. Result.ProcType:=ptProcedure
  2177. else
  2178. Result.ProcType:=ptFunction;
  2179. for i:=0 to High(Params) do begin
  2180. vd:=TVarDef.Create(Result, dtParam);
  2181. vd.Name:=Format('p%d', [i + 1]);
  2182. vd.VarType:=TTypeDef.Create(vd, dtType);
  2183. TTypeDef(vd.VarType).BasicType:=Params[i];
  2184. end;
  2185. Result.ReturnType:=TTypeDef.Create(ParentDef, dtType);
  2186. TTypeDef(Result.ReturnType).BasicType:=RetType;
  2187. end;
  2188. procedure TWriter.AddNativeMethod(ParentDef: TDef; const JniName, Name, Signature: string);
  2189. var
  2190. i: integer;
  2191. ci: TClassInfo;
  2192. pi: TProcInfo;
  2193. begin
  2194. pi:=TProcInfo.Create;
  2195. pi.Name:=Name;
  2196. pi.JniName:=JniName;
  2197. pi.JniSignature:=Signature;
  2198. i:=FClasses.IndexOf(ParentDef.AliasName, ParentDef);
  2199. if i < 0 then begin
  2200. ci:=TClassInfo.Create;
  2201. ci.Def:=ParentDef;
  2202. i:=FClasses.Add(ParentDef.AliasName, ParentDef, ci);
  2203. end;
  2204. FClasses.GetClassInfo(i).Funcs.Add(pi);
  2205. end;
  2206. function TWriter.GetProcSignature(d: TProcDef): string;
  2207. var
  2208. j: integer;
  2209. vd: TVarDef;
  2210. begin
  2211. Result:='(';
  2212. for j:=0 to d.Count - 1 do begin
  2213. vd:=TVarDef(d[j]);
  2214. if vd.DefType <> dtParam then
  2215. continue;
  2216. with vd do begin
  2217. if (VarType <> nil) and (VarType.DefType = dtJniEnv) then
  2218. continue;
  2219. if IsJavaVarParam(vd) then
  2220. Result:=Result + '[';
  2221. Result:=Result + DefToJniSig(VarType);
  2222. end;
  2223. end;
  2224. Result:=Result + ')' + DefToJniSig(d.ReturnType);
  2225. end;
  2226. procedure TWriter.EHandlerStart;
  2227. begin
  2228. Fps.WriteLn('try');
  2229. Fps.IncI;
  2230. end;
  2231. procedure TWriter.EHandlerEnd(const EnvVarName: string; const ExtraCode: string);
  2232. begin
  2233. Fps.WriteLn('except', -1);
  2234. Fps.WriteLn(Format('_HandleJNIException(%s);', [EnvVarName]));
  2235. if ExtraCode <> '' then
  2236. Fps.WriteLn(ExtraCode);
  2237. Fps.DecI;
  2238. Fps.WriteLn('end;');
  2239. end;
  2240. procedure TWriter.WriteClassInfoVar(d: TDef);
  2241. begin
  2242. Fps.WriteLn;
  2243. Fps.WriteLn(Format('var %s: _TJavaClassInfo;', [GetTypeInfoVar(d)]));
  2244. end;
  2245. procedure TWriter.WriteComment(d: TDef; const AType: string);
  2246. begin
  2247. Fps.WriteLn;
  2248. Fps.WriteLn(Format('{ %s }', [d.Name]));
  2249. Fjs.WriteLn(Format('/* %s */', [Trim(AType + ' ' + d.Name)]));
  2250. {$ifdef DEBUG}
  2251. Fjs.WriteLn(Format('/* Ref count: %d */', [d.RefCnt]));
  2252. {$endif}
  2253. end;
  2254. {
  2255. procedure TWriter.AddCustomProc(ParentDef: TDef; const JniName, Name: string; RetType: TBasicType; const Params: array of TBasicType);
  2256. var
  2257. i: integer;
  2258. ci: TClassInfo;
  2259. pi: TProcInfo;
  2260. begin
  2261. pi:=TProcInfo.Create;
  2262. pi.Name:=Name;
  2263. pi.JniName:=JniName;
  2264. pi.JniSignature:='(';
  2265. for i:=0 to High(Params) do
  2266. pi.JniSignature:=pi.JniSignature + JNITypeSig[Params[i]];
  2267. pi.JniSignature:=pi.JniSignature + ')';
  2268. pi.JniSignature:=pi.JniSignature + JNITypeSig[RetType];
  2269. i:=FClasses.IndexOf(ParentDef.Name);
  2270. if i < 0 then begin
  2271. ci:=TClassInfo.Create;
  2272. ci.Def:=ParentDef;
  2273. i:=FClasses.AddObject(ParentDef.Name, ci);
  2274. end;
  2275. TClassInfo(FClasses.Objects[i]).Funcs.Add(pi);
  2276. end;
  2277. }
  2278. constructor TWriter.Create;
  2279. var
  2280. i: integer;
  2281. begin
  2282. Units:=TStringList.Create;
  2283. FClasses:=TClassList.Create;
  2284. JavaPackage:='pas';
  2285. IncludeList:=TStringList.Create;
  2286. IncludeList.Duplicates:=dupIgnore;
  2287. ExcludeList:=TStringList.Create;
  2288. ExcludeList.Duplicates:=dupIgnore;
  2289. for i:=Low(ExcludeStd) to High(ExcludeStd) do
  2290. ExcludeList.Add(ExcludeStd[i]);
  2291. for i:=Low(ExcludeDelphi7) to High(ExcludeDelphi7) do
  2292. ExcludeList.Add(ExcludeDelphi7[i]);
  2293. FThisUnit:=TUnitDef.Create(nil, dtUnit);
  2294. end;
  2295. destructor TWriter.Destroy;
  2296. var
  2297. i: integer;
  2298. begin
  2299. for i:=0 to FClasses.Count - 1 do
  2300. FClasses.Objects[i].Free;
  2301. FClasses.Free;
  2302. Units.Free;
  2303. IncludeList.Free;
  2304. ExcludeList.Free;
  2305. FThisUnit.Free;
  2306. inherited Destroy;
  2307. end;
  2308. procedure TWriter.ProcessUnits;
  2309. procedure _ExcludeClasses(u: TDef; AAncestorClass: TClassDef);
  2310. var
  2311. i: integer;
  2312. d: TDef;
  2313. s: string;
  2314. excl: boolean;
  2315. begin
  2316. for i:=0 to u.Count - 1 do begin
  2317. d:=u[i];
  2318. if d.DefType = dtClass then begin
  2319. s:=u.Name + '.' + d.Name;
  2320. if AAncestorClass = nil then begin
  2321. excl:=DoCheckItem(s) = crExclude;
  2322. if not excl and (TClassDef(d).AncestorClass <> nil) then
  2323. with TClassDef(d).AncestorClass do
  2324. excl:=DoCheckItem(Parent.Name + '.' + Name) = crExclude;
  2325. end
  2326. else
  2327. excl:=TClassDef(d).AncestorClass = AAncestorClass;
  2328. if excl then begin
  2329. d.SetNotUsed;
  2330. ExcludeList.Add(s);
  2331. _ExcludeClasses(u, TClassDef(d));
  2332. end;
  2333. end;
  2334. end;
  2335. end;
  2336. var
  2337. p: TPPUParser;
  2338. i: integer;
  2339. s, ss: string;
  2340. d: TDef;
  2341. begin
  2342. if Units.Count = 0 then
  2343. raise Exception.Create('No unit name specified.');
  2344. if (OutPath <> '') and not DirectoryExists(OutPath) then
  2345. raise Exception.CreateFmt('Output path "%s" does not exist.', [OutPath]);
  2346. if (JavaOutPath <> '') and not DirectoryExists(JavaOutPath) then
  2347. raise Exception.CreateFmt('Output path "%s" does not exist.', [JavaOutPath]);
  2348. if LibName = '' then
  2349. LibName:=AnsiLowerCase(ChangeFileExt(Units[0], '')) + 'jni';
  2350. for i:=0 to IncludeList.Count - 1 do
  2351. IncludeList[i]:=Trim(IncludeList[i]);
  2352. IncludeList.Sorted:=True;
  2353. for i:=0 to ExcludeList.Count - 1 do
  2354. ExcludeList[i]:=Trim(ExcludeList[i]);
  2355. ExcludeList.Sorted:=True;
  2356. FThisUnit.Name:=LibName;
  2357. FThisUnit.AliasName:='system';
  2358. p:=TPPUParser.Create(SearchPath);
  2359. try
  2360. p.OnCheckItem:=@DoCheckItem;
  2361. for i:=0 to Units.Count - 1 do
  2362. IncludeList.Add(ChangeFileExt(ExtractFileName(Units[i]), ''));
  2363. for i:=0 to Units.Count - 1 do
  2364. p.Parse(ChangeFileExt(ExtractFileName(Units[i]), ''));
  2365. if OutPath <> '' then
  2366. OutPath:=IncludeTrailingPathDelimiter(OutPath);
  2367. if JavaOutPath <> '' then
  2368. JavaOutPath:=IncludeTrailingPathDelimiter(JavaOutPath);
  2369. FPkgDir:=JavaOutPath + StringReplace(JavaPackage, '.', DirectorySeparator, [rfReplaceAll]);
  2370. ForceDirectories(FPkgDir);
  2371. Fps:=TTextOutStream.Create(OutPath + LibName + '.pas', fmCreate);
  2372. WriteFileComment(Fps);
  2373. Fps.WriteLn('library '+ LibName + ';');
  2374. Fps.WriteLn('{$ifdef fpc} {$mode objfpc} {$H+} {$endif}');
  2375. Fps.WriteLn;
  2376. Fps.WriteLn('uses');
  2377. Fps.WriteLn('{$ifndef FPC} Windows, {$endif} {$ifdef unix} cthreads, {$endif} SysUtils, SyncObjs,', 1);
  2378. s:='';
  2379. for i:=0 to p.Units.Count - 1 do begin
  2380. ProcessRules(p.Units[i]);
  2381. ss:=LowerCase(p.Units[i].Name);
  2382. if (ss ='system') or (ss = 'objpas') or (ss = 'sysutils') or (ss = 'syncobjs') or (ss = 'jni') then
  2383. continue;
  2384. if s <> '' then
  2385. s:=s + ', ';
  2386. s:=s + p.Units[i].Name;
  2387. end;
  2388. Fps.WriteLn(s + ', jni;', 1);
  2389. // Types
  2390. Fps.WriteLn;
  2391. Fps.WriteLn('type');
  2392. Fps.IncI;
  2393. Fps.WriteLn('_JNIString = {$ifdef FPC} unicodestring {$else} widestring {$endif};');
  2394. Fps.WriteLn('{$ifndef FPC} ptruint = cardinal; {$endif}');
  2395. Fps.WriteLn;
  2396. Fps.WriteLn('_TJavaClassInfo = record');
  2397. Fps.WriteLn('ClassRef: JClass;', 1);
  2398. Fps.WriteLn('ConstrId: JMethodId;', 1);
  2399. Fps.WriteLn('ObjFieldId: JFieldId;', 1);
  2400. Fps.WriteLn('end;');
  2401. Fps.WriteLn('_PJavaClassInfo = ^_TJavaClassInfo;');
  2402. Fps.DecI;
  2403. Fps.WriteLn;
  2404. d:=TtypeDef.Create(nil, dtType);
  2405. TtypeDef(d).BasicType:=btString;
  2406. Fps.WriteLn(Format('var %s: _TJavaClassInfo;', [GetTypeInfoVar(d)]));
  2407. d.Free;
  2408. // Support functions
  2409. Fps.WriteLn;
  2410. Fps.WriteLn('function _StringFromJString(env: PJNIEnv; s: jstring): _JNIString;');
  2411. Fps.WriteLn('var');
  2412. Fps.WriteLn('p: PJChar;', 1);
  2413. Fps.WriteLn('c: JBoolean;', 1);
  2414. Fps.WriteLn('begin');
  2415. Fps.WriteLn('if s = nil then begin', 1);
  2416. Fps.WriteLn('Result:='''';', 2);
  2417. Fps.WriteLn('exit;', 2);
  2418. Fps.WriteLn('end;', 1);
  2419. Fps.WriteLn('p:=env^^.GetStringChars(env, s, c);', 1);
  2420. Fps.WriteLn('SetString(Result, PWideChar(p), env^^.GetStringLength(env, s));', 1);
  2421. Fps.WriteLn('env^^.ReleaseStringChars(env, s, p);', 1);
  2422. Fps.WriteLn('end;');
  2423. Fps.WriteLn;
  2424. Fps.WriteLn('function _StringToJString(env: PJNIEnv; const s: _JNIString): jstring;');
  2425. Fps.WriteLn('begin');
  2426. Fps.WriteLn('Result:=env^^.NewString(env, PJChar(PWideChar(s)), Length(s));', 1);
  2427. Fps.WriteLn('end;');
  2428. Fps.WriteLn;
  2429. Fps.WriteLn('function _CreateJavaObj(env: PJNIEnv; PasObj: pointer; const ci: _TJavaClassInfo; cleanup: boolean = True): jobject;');
  2430. Fps.WriteLn('var v: array [0..1] of jvalue;');
  2431. Fps.WriteLn('begin');
  2432. Fps.IncI;
  2433. Fps.WriteLn('Result:=nil;');
  2434. Fps.WriteLn('if PasObj = nil then exit;');
  2435. Fps.WriteLn('v[0].J:=Int64(ptruint(PasObj));');
  2436. Fps.WriteLn('if ci.ConstrId = nil then begin');
  2437. Fps.WriteLn('Result:=env^^.AllocObject(env, ci.ClassRef);', 1);
  2438. Fps.WriteLn('if Result = nil then exit;', 1);
  2439. Fps.WriteLn('env^^.SetLongField(env, Result, ci.ObjFieldId, v[0].J);', 1);
  2440. Fps.WriteLn('end else begin');
  2441. Fps.WriteLn('v[1].Z:=byte(cleanup) and 1;', 1);
  2442. Fps.WriteLn('Result:=env^^.NewObjectA(env, ci.ClassRef, ci.ConstrId, @v);', 1);
  2443. Fps.WriteLn('end;');
  2444. Fps.DecI;
  2445. Fps.WriteLn('end;');
  2446. Fps.WriteLn;
  2447. Fps.WriteLn('function _GetPasObj(env: PJNIEnv; jobj: jobject; const ci: _TJavaClassInfo; CheckNil: boolean): pointer;');
  2448. Fps.WriteLn('var pasobj: jlong;');
  2449. Fps.WriteLn('begin');
  2450. Fps.IncI;
  2451. Fps.WriteLn('if jobj <> nil then');
  2452. Fps.WriteLn('pasobj:=env^^.GetLongField(env, jobj, ci.ObjFieldId)', 1);
  2453. Fps.WriteLn('else');
  2454. Fps.WriteLn('pasobj:=0;', 1);
  2455. Fps.WriteLn('if CheckNil and (pasobj = 0) then');
  2456. Fps.WriteLn('raise Exception.Create(''Attempt to access a released Pascal object.'');', 1);
  2457. Fps.WriteLn('Result:=pointer(ptruint(pasobj));');
  2458. Fps.DecI;
  2459. Fps.WriteLn('end;');
  2460. Fps.WriteLn;
  2461. Fps.WriteLn('procedure _HandleJNIException(env: PJNIEnv);');
  2462. Fps.WriteLn('begin');
  2463. if p.OnExceptionProc <> nil then begin
  2464. Fps.WriteLn(Format('%s.%s;', [p.OnExceptionProc.Parent.Name, p.OnExceptionProc.Name]), 1);
  2465. p.OnExceptionProc.SetNotUsed;
  2466. end;
  2467. Fps.WriteLn('env^^.ThrowNew(env, env^^.FindClass(env, ''java/lang/Exception''), PAnsiChar(Utf8Encode(Exception(ExceptObject).Message)));', 1);
  2468. Fps.WriteLn('end;');
  2469. Fps.WriteLn;
  2470. Fps.WriteLn('procedure _RaiseVarParamException(const VarName: string);');
  2471. Fps.WriteLn('begin');
  2472. Fps.WriteLn('raise Exception.CreateFmt(''An array with only single element must be passed as parameter "%s".'', [VarName]);', 1);
  2473. Fps.WriteLn('end;');
  2474. Fps.WriteLn;
  2475. Fps.WriteLn('function _AllocMemory(env: PJNIEnv; jobj: jobject; size: jint): jlong;' + JniCaliing);
  2476. Fps.WriteLn('var p: pointer;');
  2477. Fps.WriteLn('begin');
  2478. Fps.WriteLn('GetMem(p, size);', 1);
  2479. Fps.WriteLn('FillChar(p^, size, 0);', 1);
  2480. Fps.WriteLn('Result:=ptruint(p);', 1);
  2481. Fps.WriteLn('end;');
  2482. // Set support
  2483. Fps.WriteLn;
  2484. Fps.WriteLn('function _GetIntObjValue(env: PJNIEnv; jobj: jobject; const ci: _TJavaClassInfo): longint;');
  2485. Fps.WriteLn('begin');
  2486. Fps.IncI;
  2487. Fps.WriteLn('if jobj = nil then raise Exception.Create(''Attempt to access a NULL set.'');');
  2488. Fps.WriteLn('Result:=env^^.GetIntField(env, jobj, ci.ObjFieldId);');
  2489. Fps.DecI;
  2490. Fps.WriteLn('end;');
  2491. Fps.WriteLn;
  2492. Fps.WriteLn('function _CreateIntObj(env: PJNIEnv; Value: longint; const ci: _TJavaClassInfo): jobject;');
  2493. Fps.WriteLn('begin');
  2494. Fps.IncI;
  2495. Fps.WriteLn('Result:=nil;');
  2496. Fps.WriteLn('Result:=env^^.AllocObject(env, ci.ClassRef);');
  2497. Fps.WriteLn('if Result = nil then exit;');
  2498. Fps.WriteLn('env^^.SetIntField(env, Result, ci.ObjFieldId, Value);');
  2499. Fps.DecI;
  2500. Fps.WriteLn('end;');
  2501. // Preprocess units
  2502. for i:=0 to p.Units.Count - 1 do begin
  2503. if AnsiCompareText(p.Units[i].Name, 'system') <> 0 then
  2504. _ExcludeClasses(p.Units[i], nil);
  2505. end;
  2506. // Write units
  2507. for i:=0 to p.Units.Count - 1 do
  2508. with TUnitDef(p.Units[i]) do begin
  2509. WriteUnit(TUnitDef(p.Units[i]));
  2510. end;
  2511. WriteOnLoad;
  2512. Fps.WriteLn;
  2513. Fps.WriteLn('procedure ___doexit;');
  2514. Fps.WriteLn('begin');
  2515. Fps.WriteLn('_MethodPointersCS.Free;', 1);
  2516. Fps.WriteLn('end;');
  2517. Fps.WriteLn;
  2518. Fps.WriteLn('begin');
  2519. Fps.WriteLn('ExitProc:=@___doexit;', 1);
  2520. Fps.WriteLn('IsMultiThread:=True;', 1);
  2521. Fps.WriteLn('_MethodPointersCS:=TCriticalSection.Create;', 1);
  2522. Fps.WriteLn('end.');
  2523. finally
  2524. Fps.Free;
  2525. p.Free;
  2526. end;
  2527. end;
  2528. end.