About PAS2JS

pas2js is a compiler/transpiler to translate programs written in Pascal (subset of Delphi/ObjFPC syntax) to JavaScript.
The goal is to use strong typing, while still be able to use low level whenever you choose.
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:

  • file cache – loading, caching files, converting to UTF-8
  • file resolver – handling search paths, finding used units and include files
  • scanner – reading tokens, handling compiler directives like $IfDef and $Include
  • parser – reading the tokens, checking syntax, creating Pascal nodes
  • resolver – resolving references, type checking and checking duplicate identifiers
  • use analyzer – finding unused identifiers, emit hints and warning
  • converter – translating Pascal nodes into JavaScript nodes
  • compiler – handling config files, parameters, compiling recursively all used units, writes js
  • command line interface – a small wrapper to embed the compiler into a console program
  • library and interface – a small wrapper to embed the compiler into a library

Each part is tested separately and is used by other FPC tools as well. For example the scanner and parser are used by fpdoc too. Thus they are tested and extended by other programmers, reducing greatly the work for developing pas2js. Consistency is kept by several test suites, containing thousands of tests.

Note: The modular structure allows to compile any parts or the whole compiler into an IDE addon (not yet started).

Command line parameters

Most parameters work the same as their FPC equivalent. pas2js has some options of its own (see -J options).

Usage: pas2js <your.pas>
Put + after a boolean switch option to enable it, - to disable it
  @<x>    : Read compiler options from file <x> in addition to the default pas2js.cfg
  -B      : Rebuild all
  -d<x>   : Defines the symbol <x>. Optional: -d<x>:=<value>
  -i<x>   : Write information and halt. <x> is a combination of the following:
    -iD   : Write compiler date
    -iSO  : Write compiler OS
    -iSP  : Write compiler host processor
    -iTO  : Write target platform
    -iTP  : Write target processor
    -iV   : Write short compiler version
    -iW   : Write full compiler version
    -ic   : Write list of supported JS processors usable by -P<x>
    -io   : Write list of supported optimizations usable by -Oo<x>
    -it   : Write list of supported targets usable by -T<x>
    -iJ   : Write list of supported JavaScript identifiers -JoRTL-<x>
  -C<x>   : Code generation options. <x> is a combination of the following letters:
    o     : Overflow checking
    r     : Range checking
    R     : Object checks. Verify method calls and object type casts.
  -F...   Set file names and paths:
   -Fe<x> : Redirect output to file <x>. UTF-8 encoded.
   -FE<x> : Set main output path to <x>
   -Fi<x> : Add <x> to include paths
   -FN<x> : add <x> to namespaces. Namespaces with trailing - are removed.
                  Delphi calls this flag "unit scope names".
   -Fu<x> : Add <x> to unit paths
   -FU<x> : Set unit output path to <x>
  -I<x>   : Add <x> to include paths, same as -Fi
  -J...  Extra options of pas2js
   -Jc    : Write all JavaScript concatenated into the output file
   -Je<x> : Encode messages as <x>.
     -Jeconsole : Console codepage. Default.
     -Jesystem  : System codepage. On non Windows console and system are the same.
     -Jeutf-8   : Unicode UTF-8. Default when using -Fe.
     -JeJSON    : Output compiler messages as JSON. Logo etc are outputted as-is.
   -Ji<x> : Insert JS file <x> into main JS file. E.g. -Jirtl.js. Can be given multiple times.
   -Jl    : lower case identifiers
   -Jm    : generate source maps
     -Jmsourceroot=<x> : use x as "sourceRoot", prefix URL for source file names.
     -Jmbasedir=<x> : write source file names relative to directory x, default is map file folder.
     -Jminclude : include Pascal sources in source map.
     -Jmabsolute: store absolute filenames, not relative.
     -Jmxssiheader : start source map with XSSI protection )]}.
     -Jm- : disable generating source maps
   -Jo<x> : Enable or disable extra option. The x is case insensitive:
     -JoSearchLikeFPC : search source files like FPC, default: search case insensitive.
     -JoUseStrict : add "use strict" to modules, default.
     -JoCheckVersion-: do not add rtl version check, default. (since 1.1)
     -JoCheckVersion=main: insert rtl version check into main. (since 1.1)
     -JoCheckVersion=system: insert rtl version check into system unit init. (since 1.1)
     -JoCheckVersion=unit: insert rtl version check into every unit init. (since 1.1)
     -JoRTL-<x>=<y>: set RTL identifier x to value y. See -iJ. (since 1.1)
   -Jpcmd<command> : Run postprocessor. For each generated js execute
                  command passing the js as stdin and read the new js from stdout.
                  This option can be added multiple times to call several
                  postprocessors in succession.
   -Ju<x> : Add <x> to foreign unit paths. Foreign units are not compiled.
  -l      : Write logo
  -MDelphi: Delphi 7 compatibility mode
  -MObjFPC: FPC's Object Pascal compatibility mode (default)
  -NS<x>  : obsolete: add <x> to namespaces. Same as -FN<x>
  -n      : Do not read the default config files
  -o<x>   : Change main JavaScript file to <x>, "." means stdout
  -O<x>   : Optimizations:
    -O-   : Disable optimizations
    -O1   : Level 1 optimizations (quick and debugger friendly)
    -Oo<x> : Enable or disable optimization. The x is case insensitive:
      -OoEnumNumbers[-] : write enum values as number instead of name. Default in -O1.
      -OoRemoveNotUsedPrivates[-] : Default is enabled
      -OoRemoveNotUsedDeclarations[-] : Default enabled for programs with -Jc
      -OoRemoveNotUsedPublished[-] : Default is disabled
  -P<x>   : Set target processor. Case insensitive:
    -Pecmascript5  : default
  -S<x>   : Syntax options. <x> is a combination of the following letters:
    a     : Turn on assertions
    c     : Support operators like C (*=,+=,/= and -=)
    d     : Same as -Mdelphi
    m     : Enables macro replacements
    2     : Same as -Mobjfpc (default)
  -SI<x>   : Set interface style to <x>
    -SIcom   : COM, reference counted interface (default)
    -SIcorba : CORBA interface
  -T<x>   : Set target platform, case insensitive.
    -Tbrowser : default
    -Tnodejs  : add pas.run(), includes -Jc
  -u<x>   : Undefines the symbol <x>
  -v<x>   : Be verbose. <x> is a combination of the following letters:
    e     : Show errors (default)
    w     : Show warnings
    n     : Show notes
    h     : Show hints
    i     : Show info
    l     : Show line numbers, needs -vi
    a     : Show everything
    0     : Show nothing (except errors)
    b     : Show file names with full path
    c     : Show conditionals
    t     : Show tried/used files
    d     : Show debug notes and info, enables -vni
    q     : Show message numbers
    x     : Show used tools
    v     : Write pas2jsdebug.log with lots of debugging info
    z     : Write messages to stderr, -o. still uses stdout.
  -vm<x>,<y>: Do not show messages numbered <x> and <y>.
  -?      : Show this help
  -h      : Show this help