About the Pas2JS



Overview

Pas2js is a Pascal to JavaScript transpiler.


Transpiler is a specific term for taking source code written in one language and transforming into another language that has a similar level of abstraction. Pas2JS is a type of compiler that translates/parses Object Pascal and emits JavaScript.

The language used by Pas2JS compiler is an Object Pascal dialect (subset of Delphi/FPC syntax) that retains as much compatibility as possible with the Object Pascal language used by the Delphi. As you know, Object Pascal is a highly-structured, strongly-typed, object-oriented language that is also simple to use due to its English-like keywords and statements. The Pas2JS compiler can be used in the Lazarus IDE, it takes advantage of the strongly-typed nature of the Object Pascal language, that combine both the design and coding aspects of applications into one unified, highly-productive tool - you have the code completion feature all the time! Any functions, procedures, constants, variables, types, classes, member variables, methods, or properties that aren't referenced in the source code for an application are not emitted. This keeps the size of the resultant application as small as possible.

The Pas2JS compiler includes a very small runtime overhead of around 25KB for an empty, uncompressed, non-visual application, it is a small javascript file called rtl.js. Exactly, for the generated code to work (project.js) it's required the rtl.js. This core file just defines an object RTL. This object will start the Object Pascal code if you include a call to rtl.run() in the html page. By default, pas2js automatically include this file in the generated output.

Tiny Pas2JS application

The following pas2js example illustrates the use of modules, 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).

Example
program project1; {$mode objfpc} uses JS, Classes, SysUtils, Web, Unit1; var app: TApplication; begin // Your code here app:= TApplication.Create; app.RunApp; end. //----------------------------------------------------- unit Unit1; {$mode objfpc} interface uses JS, Web, Types, Math, Classes, SysUtils; type TApplication = class procedure RunApp; end; implementation { TApplication } procedure TApplication.RunApp; begin WriteLn('starting app'); end; initialization end.
Please take a moment to check out the emitted javascript of the above example.

Compiler features

Some of the features of the pas2js compiler are:

  • The compiler can display automatic warnings and hints for source code issues such as uninitialized variable references or variables that are declared but never referenced;
  • Fast compiler that compiles the source code of any project into JavaScript in seconds or even in milliseconds;
  • Any functions, procedures, constants, variables, types, classes, member variables, methods, or properties that aren't referenced in the source code for an application are not emitted. This keeps the size of the resultant application as small as possible;
  • The JavaScript emitted by the pas2js compiler is currently of level ECMAScript 5 and should run in any modern browser or in Node.js (target "nodejs"). It is available in 3 forms:
    1. as a library;
    2. as a command-line program; and
    3. as a webserver.
  • The compiled Pascal functions can be used in DOM events or called by JavaScript. pas2js is written completely in FPC, runs on many platforms like Windows, Mac and Linux and more;
  • It is built modular consisting of the following parts:
    1. file cache: loading, caching files, converting to UTF-8.
    2. file resolver: handling search paths, finding used units and include files.
    3. scanner: reading tokens, handling compiler directives like $IfDef and $Include.
    4. parser : reading the tokens, checking syntax, creating Pascal nodes.
    5. resolver: resolving references, type checking and checking duplicate identifiers.
    6. use analyzer: finding unused identifiers, emit hints and warning.
    7. converter: translating Pascal nodes into JavaScript nodes.
    8. compiler: handling config files, parameters, compiling recursively all used units, writes js.
    9. command line interface: a small wrapper to embed the compiler into a console program.
    10. library and interface: a small wrapper to embed the compiler into a library.
  • It transpiles from actual Pascal source, it has no intermediate .ppu files, that means all sources must always be available;
  • You can use third party tools to compress and obfuscate the emitted JavaScript, which makes it very difficult to read with the naked eye or reverse-engineer into any meaningful source code;
  • Through external class definitions, the compiler can use JavaScript classes:
    1. All classes available in the JavaScript runtime, and in the browser are available through import units;
    2. For Node.js, basic support for the nodejs runtime environment is available;
    3. An import unit for jQuery is available (libjquery).