nickysn e83f3d2978 * updated all the remaining makefiles that 'fpcmake -r' missed 10 年之前
..
Makefile e83f3d2978 * updated all the remaining makefiles that 'fpcmake -r' missed 10 年之前
Makefile.fpc ac87da7c42 * Filter the Makefile's unit searchpaths from the fpmake command line. 10 年之前
Makefile.fpc.fpcmake 94e7cd5a51 * Switched utils to fpmake building 12 年之前
def.pas 130eba51ee * pas2jni: Fixed code generation in case of duplicate method names in a class hierarchy. 10 年之前
fpmake.pp d407b668d1 * version updated to 3.1.1 10 年之前
pas2jni.pas 9019c2cf17 * Removed unneeded {$apptype console}. 12 年之前
ppuparser.pas de770592da * pas2jni: - Fixed method pointer handling for ARM platforms. 10 年之前
readme.txt bb7aee5e8e * Added link to the pas2jni wiki page. 10 年之前
writer.pas de770592da * pas2jni: - Fixed method pointer handling for ARM platforms. 10 年之前

readme.txt

pas2jni - JNI bridge generator for Pascal.

Copyright (c) 2013-2015 by Yury Sidorov.

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.

The pas2jni wiki page is available here: http://wiki.freepascal.org/pas2jni

For example you can do the following in Java:

import pas.classes.*;

...

TStringList sl = TStringList.Create();
sl.Add("Hello.");
String s = sl.getStrings(0);
sl.Free();

...

The following Pascal features are supported by pas2jni:

- function/procedure;
- var/out parameters;
- class;
- record;
- property;
- constant;
- enum;
- set;
- TGuid type;
- pointer type;
- string types;
- all numeric types;
- method pointer.

USUPPORTED features:
- array;
- procedure pointer.

Shared libraries, generated by pas2jni were tested with Java on Windows and Android. It should work on other systems as well.

HOW TO USE

pas2jni uses the ppudump utility included with Free Pascal Compiler to read unit interfaces. Therefore your Pascal code must be first compiled with FPC.
When your units are compiled, you can run pas2jni. You need to specify a list of main units and units search path.
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.

The basic invocation of pas2jni:

pas2jni myunit -U/path/to/my/units;/path/to/FPC/units/*

Here you specify myunit as the main unit and provide path to your compiled units and FPC compiled units.

After successful run of pas2jni you will get the following output files:
- 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.
- 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.

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.

CUSTOM HANDLERS

It is possible to define the following custom handlers in your Pascal code.

procedure JNI_OnException;
- is called when an unhandled Pascal exception occurs. For example, you can log a stack back trace in this handler.

Custom handlers must be public and defined in one of the main units specified when calling pas2jni.

CODING TIPS

* Setting handlers (method pointers) in a Java code.

For example there is the following event handler in your Pascal code:

TMyClass = class
...
property OnChange: TNotifyEvent;
...
end;

In a Java code you get the following TMyClass instance:

TMyClass myclass = TMyClass.Create();

It is possible set a Java handler in 2 ways:

1) Place the handler inline.

...
myclass.setOnChange(
new TNotifyEvent() {
protected void Execute(TObject Sender) {
// The handler code
}
}
);
...

2) Define the handler as a method in a class.

public class MyJavaClass {
private void DoOnChange(TObject Sender) {
// The handler code
}

public void main() {
...
// Set the handler to the method with the "DoOnChange" name in the current class (this).
myclass.setOnChange( new TNotifyEvent(this, "DoOnChange") );
...
}
}

COMMAND LINE OPTIONS

Usage: pas2jni [options] [ ...]

Options:
-U - Unit search path, semicolon delimited. Wildcards are allowed.
-L - Set output library name.
-P - Set Java package name.
-O - Set output path for Pascal files.
-J - Set output path for Java files.
-D - Set full path to the "ppudump" program.
-I - Include the list of specified objects in the output. The list is
semicolon delimited. To read the list from a file use -I@
-E - Exclude the list of specified objects from the output. The list is
semicolon delimited. To read the list from a file use -E@
-? - Show this help information.