English (en) │
русский (ru) │
- 1 Pas2js : What is it ?
- 2 Where to get it
- 3 How to use pas2js
- 4 Supported syntax elements
- 5 Planned language features
- 6 Other not implemented features
- 7 Lazarus integration of pas2js
- 9 Debugging
- 10 Examples
Pas2js : What is it ?
- as a library
- as a command-line program
- as a webserver
It transpiles from actual Pascal source, it has no intermediate .ppu files. That means all sources must always be available.
through import units (comparable to the windows or unix units for the native compiler).
- For Node.js, basic support for the nodejs runtime environment is available.
- An import unit for jQuery is available (libjquery)
pas2js can automatically include this file in the generated output, like this:
pas2js -Jc -Jirtl.js -Tbrowser hello.pas
There is a basic Object Pascal RTL, several units from the FPC Packages are also available
- DB (yes, TDataset)
- fpcunit testsuite
- web (browser provided objects)
- libjquery (jquery is available too)
- nodejs (basic node runtime environment)
- browserconsole (support writeln)
Where to get it
The pas2js compiler is - naturally - open source and can be downloaded and used freely. The sources are currently the primary means to get it.
- The Pas2js compiler is part of Free Pascal. As such, the compiler is present in the utils/pas2js directory of the
- The pas2js RTL is still in flux. To enable faster releases, the RTL and demos are in a separate repository in SVN:
svn co https://svn.freepascal.org/svn/projects/pas2js/trunk pas2js
Till pas2js is integrated in the main FPC distribution, snapshots will be made available as time permits. They will contain binaries for Windows, Linux (64 bit) and Macos.
The snapshots are uploaded to
Every version will have a directory with the version number. A list of changes can be found Pas2js_Version_changes Here
How to use pas2js
The command-line arguments are kept mostly the same as the FPC command-line arguments. Error messages are also in the same format.
The compiler needs access to all sources, and so you need to specify the path to the sources of all used units.
As for the FPC compiler, a configuration file is supported, which has the same syntax as the FPC config file.
Basically, the command is the same as any FPC command line. The only thing that is different is the target: browser or nodeejs
Here is the complete list of command line arguments.
for the browser
Consider the classical:
program hello; begin Writeln('Hello, world!'); end.
Yes, writeln is supported. Here is how to compile it:
pas2js -Jc -Jirtl.js -Tbrowser hello.pas
When compiled succesfully, the code can be run in the browser by opening a html file in the browser with the following content:
The files that are needed are:
Whether hello.html is opened by double-clicking it in the explorer or put on a server and opened with an URL, is not relevant for the functioning.
The output is visible in the browser's web developer console. By including the browserconsole unit, it will be visible in the browser page:
program hello; uses browserconsole; begin Writeln('Hello, world!'); end.
pas2js -Tnodejs hello.pas
When compiled succesfully, the code can be run in node using the following command.
Note: on MacOS it is "node hello.js"
Supported syntax elements
Basically, Delphi 7 syntax minus interfaces is supported. This includes RTTI. A more detailed list can be found in the translation.html file in the sources.
- Delphi and ObjFPC mode
- Program, Units, namespaces
- unit initialization, but not finalization
- Var, Const, Type
- string (unicodestring), char (widechar), Boolean, Double, Byte, Shortint, Word, Smallint, longword, Longint, nativeint(int53), nativeuint(int52)
- Pointer (as a reference to a class or array, but no pointer arithmetic)
- Record (but no variant records)
- Functions, Procedures, nested
- function types, of object, reference to (closures)
- function arguments: default, const, var, out
- arrays static, dynamic, open, multi dimensionals
- class type, visibility, virtual, override, abstract, overload, properties, class properties, class var, class const, constructor, destructor
- Enumeration for..in..do
- compiler directives (e.g. $ifdef, $if, $define)
There are some constructs that are naturally not supported and will never be supported:
- Anything involving memory pointers and pointer arithmetic.
- Variant records
Planned language features
Basically, the idea is to get the pas2js transpiler up to the same level as FPC or Delphi. That means the following needs to be added:
- More runtime/compile time checks
- Runtime checks: Overflow -Co, $Q
- Runtime checks: Range -Cr, $R
- Runtime checks: Typecast -CR
- Type helpers
- Anonymous functions
- Pointer of record
- Nested classes
Needless to say, anything requiring direct memory access is not going to be supported.
Other not implemented features
- Advanced records
- Array of const
- Enums with custom values
- Global properties
- Helpers for types, classes, records
- Operator overloading
- Pointer arithmetic
- RTTI extended, $RTTI
- Type alias, e.g. type TTranslateString = type string;
- Variant records
Lazarus integration of pas2js
Lazarus understands the concept of external classes as used by pas2js, so code completion will work.
pas2js comes with several lazarus packages that 'import' the pas2js rtl units: by doing so, the IDE will know which units to use.
- pas2js_rtl : the basic RTL
- fcl_base_pas2js: The custapp and other units
- fpcunit_pas2js: the fpcunit units
- pas2js_fcldb: the database units
The integration is described here: lazarus pas2js integration. It is still under construction, but deep integration with lazarus is planned.
Here is a simple example:
TJSFunction = class external name 'Function'(TJSObject) private Flength: NativeInt external name 'length'; Fprototyp: TJSFunction external name 'prototyp'; public name: String; property prototyp: TJSFunction read Fprototyp; property length: NativeInt read Flength; function apply(thisArg: TJSObject; const ArgArray: TJSValueDynArray): JSValue; varargs; function bind(thisArg: TJSObject): JSValue; varargs; function call(thisArg: TJSObject): JSValue; varargs; end;
This declares the
- The "
(TJSObject)means it descends from
TJSObjectalso an external class. There does not need to be an ancestor type.
- Fields are declared just as in Pascal.
- To declare read-only fields, a trick can be used: declare the field using an external name "thename" modifier, and declare a read-only property with the same name.
(see the length declaration)
Varargscan be used to indicate that a function accepts any number of arguments.
JSValuecan be used to indicate an unknown type.
It is more or less equivalent to a Variant.
Moreover, the transpiler can generate a source map, which means that you will be able to see and debug the Pascal code in the browser. (not everything will work, but many things do. This depends on the browser too.)
A source map can be generated using the command-line parameter
The easiest is to include the Pascal sources in the source map
You can tell the compiler to store all file names relative to a local base directory:
And you can store an URL in the map, so the browser will use URL/above-relative-file-name to get the source: