pas2js makestub

From Free Pascal wiki

The pas2js stub generator

makestub utility


The makestub utility converts a Pascal import unit for importing Pascal classes to a unit that is compileable by Delphi


command-line makestub usage is quite simple:

makestub -i jsunit.pas -o delphiunit.pas

you can add defines into the mix

makestub -d DELPHI -d DEBUG -i jsunit.pas -o delphiunit.pas

You can also include a header in the generated unit:

makestub -h copyright.pas -i jsunit.pas -o delphiunit.pas

It does not put comments around the header, the header file is inserted as-is, at the start of the generated file.

makestub currently has some problems with forward-defined classes. There is an option to give a list of classes that must be defined forward:

makestub -f TClassA,TClassB -i jsunit.pas -o delphiunit.pas

The special word 'all' will define all classes as forward:

makestub -f all -i jsunit.pas -o delphiunit.pas

There is the possibility of using a config file (.ini) to set some options:

makestub -c myconfig.ini -i jsunit.pas -o delphiunit.pas

Options on the command-line override options set in the config file.

Config file

The makestub program supports a config file. This is a .INI file which has 1 section called config, which can contain the following keys :

  • input: The name of an input file (corresponds to -i).
  • output: The name of an output file (corresponds to -o)
  • header: the name of a header file to insert (corresponds to -h)
  • defines: a list of defines (corresponds to -d)
  • extra: a list of units to insert at the start of the uses clause. Note that the jsdelphisystem unit is always inserted, and that you can use all to indicate that all classes must be eclared forward.
  • includepaths : include paths where to search for include files.
  • forwardclasses: a comma-separated list of classes that must be declared forward.
  • indentsize: number of spaces to use for indent (default 2)
  • addlinenumber: Boolean (0/1) each line is prefixed with a comment containing the line number.
  • addsourcelinenumber: Boolean (0/1) each line is prefixed with a comment containing the line number of the source file that was used to create the current line.
  • linenumberwidth: number of digits to use for line numbers (default 4)


Here is an example config file:


libstub library


The libstub library is the library equivalent of the makestub command-line tool.

It can be used in Delphi to automate conversion of pas2js units. The stub creator is written in Lazarus/Free Pascal and therefor cannot be compiled in Delphi, since it uses a lot of units not available in pas2js.

Exported functions

The following functions are exported by the library creator. Given the above explanations about config files and command-line options, their usage should be clear.

  PStubCreator = Pointer;  

Function GetStubCreator : PStubCreator; stdcall;
Procedure FreeStubCreator(P : PStubCreator); stdcall;
Procedure SetStubCreatorInputFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
Procedure SetStubCreatorConfigFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
Procedure SetStubCreatorOutputFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
Procedure SetStubCreatorHeaderFileName(P : PStubCreator; AFileName : PAnsiChar); stdcall;
Procedure AddStubCreatorDefine(P : PStubCreator; ADefine : PAnsiChar); stdcall;
Procedure AddStubCreatorForwardClass(P : PStubCreator; AForwardClass : PAnsiChar); stdcall;
Procedure SetStubCreatorHeaderContent(P : PStubCreator; AContent : PAnsiChar); stdcall;
Procedure SetStubCreatorOuputCallBack(P : PStubCreator; AData : Pointer; ACallBack : TWriteCallBack); stdcall;
function ExecuteStubCreator(P : PStubCreator) : Boolean; stdcall;
Procedure GetStubCreatorLastError(P : PStubCreator; AError : PAnsiChar;
  Var AErrorLength : Longint; AErrorClass : PAnsiChar; Var AErrorClassLength : Longint); stdcall;