pas2js

From Free Pascal wiki
Jump to: navigation, search

English (en)



Pas2js : What is it ?

Compiler

Pas2js is a Pascal to JavaScript transpiler. It parses Object Pascal and emits JavaScript. The JavaScript is currently of level ECMAScript 5 and should run in any browser or in Node.js (target "nodejs"). It is available in 3 forms:

  • 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 external class definitions, the compiler can use JavaScript classes:

  • All classes available in the JavaScript runtime, and in the browser are 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)

RTL

For the generated code to work, a small JavaScript file is needed: rtl.js. It defines an object rtl. This object will start the Object Pascal code if you include a call to rtl.run() in the html page.

<script type="application/javascript">
  rtl.run()
</script>

pas2js can automatically include this file in the generated output, like this:

pas2js -Jc -Jirtl.js -Tbrowser hello.pas

For nodejs, the compiler will insert the call to rtl.run() automatically at the end of the generated Javascript file.

There is a basic Object Pascal RTL, several units from the FPC Packages are also available

  • system
  • sysutils
  • Math
  • strutils
  • rtlconst
  • classes
  • contnrs
  • DB (yes, TDataset)
  • fpcunit testsuite
  • custapp
  • restconnection
  • js (javascript system objects)
  • web (browser provided objects)
  • libjquery (jquery is available too)
  • nodejs (basic node runtime environment)
  • typinfo
  • objpas
  • browserconsole (support writeln)
  • dateutils
  • browserapp
  • nodejsapp

Where to get it

The pas2js compiler and RTL are - naturally - open source and can be downloaded and used freely.

Snapshots

The snapshots contain binaries for Windows (32 and 64bit), Linux (64 bit) and Macos.

The snapshots are uploaded to

Every version has a directory with the version number. A list of changes can be found on the changelog page Pas2JS Version Changes

SVN

svn co https://svn.freepascal.org/svn/projects/pas2js/trunk pas2js

You need fpc 3.0.4 or better to compile it.

Change to the directory and build it with:

make clean all

This creates compiler/utils/pas2js/pas2js (Windows: compiler\utils\pas2js\pas2js.exe)

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. Note that the snapshots and svn version already contains a default pas2js.cfg with unit search paths (-Fu) for the rtl and fcl.

Basically, the command is the same as any FPC command line. The only thing that is different is the target: -Tbrowser or -Tnodeejs

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:

<html>
  <head>
    <meta charset="utf-8"/>
    <script type="application/javascript" src="hello.js"></script>
  </head>
  <body>
    <script type="application/javascript">
     rtl.run();
    </script>
  </body>
</html>

The files that are needed are:

  • hello.html
  • hello.js

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.

for NodeJS

pas2js -Tnodejs hello.pas

When compiled succesfully, the code can be run in node using the following command.

nodejs hello.js

Note: on MacOS it is "node hello.js"

Supported syntax elements

Basically, Delphi 7 syntax 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), currency
  • resourcestrings
  • Pointer (as a reference to a class, array, record, pointer of record, interface, 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
  • If-then-else
  • For-do
  • Repeat-until
  • While-do
  • With-do
  • try-finally
  • try-except
  • enums
  • sets
  • arrays static, dynamic, open, multi dimensionals
  • class type, visibility, virtual, override, abstract, overload, properties, class properties, class var, class const, constructor, destructor
  • class-of
  • nested classes
  • interfaces: CORBA, COM, delegations, method resolution, reference counting, TVirtualInterface
  • Enumeration for..in..do
  • Type alias, e.g. type TTranslateString = type string;
  • RTTI
  • String like array operations: a:=[1,2,3]+[1,1];
  • compiler directives (e.g. $ifdef, $if, $define, $modeswitch, $R+)
  • compile time and run time range checking

There are some constructs that are naturally not supported and will never be supported:

  • Anything involving memory pointers and pointer arithmetic.
  • Variant records

Details about supported elements and the conversion from Pascal to JavaScript: translation.

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:

  • Anonymous functions
  • Advanced records
  • Runtime checks: Overflow -Co, $Q
  • Generics
  • Type helpers

Needless to say, anything requiring direct memory access is not going to be supported.

Other not implemented features

  • Array of const
  • Attributes
  • Enums with custom values
  • Global properties
  • Futures
  • Helpers for types, classes, records
  • Inline
  • Library
  • Objects
  • Operator overloading
  • Pointer arithmetic
  • Resources
  • RTTI extended, $RTTI
  • Variant records
  • Variants

Lazarus integration of pas2js

Lazarus understands the concept of external classes as used by pas2js, so code completion will work.

Since Lazarus 1.9 the IDE can use pas2js.exe as a normal compiler.

The integration is described here: lazarus pas2js integration. It is still under construction, but deep integration with lazarus is planned.

Importing Javascript classes

To import a javascript class, one writes a normal class definition that mimics the Javascript class. It is possible to use properties. Many examples can be found in the JS, web, nodejs and libjquery units.

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 TJSFunction object : in Javascript, functions are objects.

  • The "external name 'Function'" means that you declare a Javascript class where the Javascript name of the class is 'Function'.
  • The (TJSObject) means it descends from TJSObject also 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:</b> declare the field using an external name "thename"</code> modifier, and declare a read-only property with the same name.
    (see the length declaration)
  • Varargs can be used to indicate that a function accepts any number of arguments.
  • JSValue can be used to indicate an unknown type.
    It is more or less equivalent to a Variant.

Create simple JS objects with the new function

Some JS-framework functions expect an JS object as parameter. Here is how to do that in Pascal using the new function from unit JS:

// JavaScript:
DoIt({name:"Fred", id:3, size:4.3});
// Pascal;
DoIt(new(['name','Fred', 'id',3, 'size',4.3]));

You can nest it to create sub objects:

// JavaScript:
DoIt({name:"Fred", size:{width:3,height:2}});
// Pascal;
DoIt(new(['name','Fred', 'size',new(['width',3, 'height',2])]));

You can use TJSArray._of to create JS arrays on the fly:

// JavaScript:
DoIt({numbers:[1,2,3]});
// Pascal;
DoIt(new(['numbers',TJSArray._of(1,2,3)]));

Debugging

The generated Javascript source code is of course visible and debuggable in the browser.

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

-Jm

The easiest is to include the Pascal sources in the source map

-Jminclude

You can tell the compiler to store all file names relative to a local base directory:

-Jmbasedir=DirName

And you can store an URL in the map, so the browser will use URL/above-relative-file-name to get the source:

-Jmsourceroot=URL

Bugs

Please report bugs in the FPC bugtracker with category pas2js: http://bugs.freepascal.org

Examples

FAQ

Why is a simple hello world program so big?

This is mainly due to the used rtl.js. The rtl.js contains code for Pascal modules, classes, RTTI, sets, range checks, etc and is written with big WebApps in mind, not for scripts with a few lines of code.

  1. You can use a Javascript minifier to reduce the created Javascript
  2. You can create your own minified rtl.js by removing all functions you don't need. Eventually this will be done automatically by pas2js.