readme.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. pas2jni - JNI bridge generator for Pascal.
  2. Copyright (c) 2013-2015 by Yury Sidorov.
  3. The pas2jni utility generates a JNI (Java Native Interface) bridge for a Pascal code. Then the Pascal code (including classes and other advanced features) can be easily used in Java programs.
  4. The pas2jni wiki page is available here: http://wiki.freepascal.org/pas2jni
  5. For example you can do the following in Java:
  6. import pas.classes.*;
  7. ...
  8. TStringList sl = TStringList.Create();
  9. sl.Add("Hello.");
  10. String s = sl.getStrings(0);
  11. sl.Free();
  12. ...
  13. The following Pascal features are supported by pas2jni:
  14. - function/procedure;
  15. - var/out parameters;
  16. - class;
  17. - record;
  18. - property;
  19. - constant;
  20. - enum;
  21. - set;
  22. - TGuid type;
  23. - pointer type;
  24. - string types;
  25. - all numeric types;
  26. - method pointer.
  27. USUPPORTED features:
  28. - array;
  29. - procedure pointer.
  30. Shared libraries, generated by pas2jni were tested with Java on Windows and Android. It should work on other systems as well.
  31. HOW TO USE
  32. pas2jni uses the ppudump utility included with Free Pascal Compiler to read unit interfaces. Therefore your Pascal code must be first compiled with FPC.
  33. When your units are compiled, you can run pas2jni. You need to specify a list of main units and units search path.
  34. When you specify a main unit, all its interface declarations will be available in Java. For linked units only used declarations will be available. You can fine tune included/excluded declaration using -I and -E command line options.
  35. The basic invocation of pas2jni:
  36. pas2jni myunit -U/path/to/my/units;/path/to/FPC/units/*
  37. Here you specify myunit as the main unit and provide path to your compiled units and FPC compiled units.
  38. After successful run of pas2jni you will get the following output files:
  39. - file "myunitjni.pas" - a generated library unit to be compiled to a shared library. It will contain all your Pascal code to be used from Java.
  40. - folder "pas" - generated Java package "pas" to be used in your Java program. Interface to each Pascal unit is placed to a separate Java public class.
  41. Note: You need to use ppudump of the same version as the FPC compiler. Use the -D switch to specify correct ppudump if it is not in PATH.
  42. CUSTOM HANDLERS
  43. It is possible to define the following custom handlers in your Pascal code.
  44. procedure JNI_OnException;
  45. - is called when an unhandled Pascal exception occurs. For example, you can log a stack back trace in this handler.
  46. Custom handlers must be public and defined in one of the main units specified when calling pas2jni.
  47. CODING TIPS
  48. * Setting handlers (method pointers) in a Java code.
  49. For example there is the following event handler in your Pascal code:
  50. TMyClass = class
  51. ...
  52. property OnChange: TNotifyEvent;
  53. ...
  54. end;
  55. In a Java code you get the following TMyClass instance:
  56. TMyClass myclass = TMyClass.Create();
  57. It is possible set a Java handler in 2 ways:
  58. 1) Place the handler inline.
  59. ...
  60. myclass.setOnChange(
  61. new TNotifyEvent() {
  62. protected void Execute(TObject Sender) {
  63. // The handler code
  64. }
  65. }
  66. );
  67. ...
  68. 2) Define the handler as a method in a class.
  69. public class MyJavaClass {
  70. private void DoOnChange(TObject Sender) {
  71. // The handler code
  72. }
  73. public void main() {
  74. ...
  75. // Set the handler to the method with the "DoOnChange" name in the current class (this).
  76. myclass.setOnChange( new TNotifyEvent(this, "DoOnChange") );
  77. ...
  78. }
  79. }
  80. COMMAND LINE OPTIONS
  81. Usage: pas2jni [options] <unit> [<unit2> <unit3> ...]
  82. Options:
  83. -U<path> - Unit search path, semicolon delimited. Wildcards are allowed.
  84. -L<name> - Set output library name.
  85. -P<name> - Set Java package name.
  86. -O<path> - Set output path for Pascal files.
  87. -J<path> - Set output path for Java files.
  88. -D<prog> - Set full path to the "ppudump" program.
  89. -I<list> - Include the list of specified objects in the output. The list is
  90. semicolon delimited. To read the list from a file use -I@<file>
  91. -E<list> - Exclude the list of specified objects from the output. The list is
  92. semicolon delimited. To read the list from a file use -E@<file>
  93. -? - Show this help information.