Anatomy of a pas2js project



This is an anatomy of a tiny pas2js application.
Notes:
  • The unit System is always loaded implicitely.
  • References to other units are translated to full path. For example TObject is translated to pas.system.TObject
  • References to dotted unitnames, aka units with namespaces are translated to pas["namespace.unitname"].
  • To create and initialize the units in topological order the pas2js compiler translates an Unit to the following JavaScript code.
Pas2JS pascal source code
{ filename: project1.lpr } program project1; {$mode objfpc} uses Classes, SysUtils, JS, Web, Unit1, Unit2, Unit3; begin // Your code here MyIntfProc1; MyIntfProc2; MyIntfProc3; end. //-------------------------- { filename: unit1.pas } unit Unit1; {$mode objfpc} interface uses Classes, SysUtils, JS, Web; procedure MyIntfProc1; var dIntf: double; sIntf: string = 'Unit1.pas'; implementation Const A = 'Unit1'; var dImpl: double; Procedure MyIntfProc1; begin dImpl:= dIntf; console.log(A); end; procedure MyImplProc; begin dImpl:= dIntf; end; // Note: The finalization section is not supported by pas2js. initialization console.log('Unit1 initialization'); end. //-------------------------- { filename: unit2.pas } unit Unit2; interface uses Classes, SysUtils, JS, Web; procedure MyIntfProc2; var dIntf: double; sIntf: string = 'Unit2.pas'; implementation Const B = 'Unit2'; var dImpl: double; Procedure MyIntfProc2; begin dImpl:= dIntf; console.log(B); end; procedure MyImplProc; begin dImpl:= dIntf; end; // Note: The finalization section is not supported by pas2js. initialization console.log('Unit2 initialization'); end. //-------------------------- { filename: unit3.pas } unit Unit3; interface uses Classes, SysUtils, JS, Web; procedure MyIntfProc3; var dIntf: double; sIntf: string = 'Unit3.pas'; implementation Const C = 'Unit3'; var dImpl: double; Procedure MyIntfProc3; begin dImpl:= dIntf; console.log(C); end; procedure MyImplProc; begin dImpl:= dIntf; end; // Note: The finalization section is not supported by pas2js. initialization console.log('Unit3 initialization'); end.