Sitemap
Breitsch

Breitsch

pas2js

5 min readMar 19, 2023
Press enter or click to view image in full size

Pas2js is an open source Pascal to JavaScript transpiler. It parses Object Pascal or maXbox files and emits JavaScript. It takes Delphi/Lazarus projects and modules (.DPR, .LPR, .PAS, .PP) and converts them to JavaScript (.JS). The JavaScript is currently of level ECMAScript 5 and should run in any browser or in Node.js (target “nodejs”). It is available in 5 forms:

  • as a library
  • as a command-line program
  • as a webserver
  • as a node.js program
  • as a program running in the browser.

It trans-piles from actual Pascal source, it has no intermediate .ppu files. That means all sources must always be available.

So my first example is a radarchart from ChartJS which I build in Lazarus:

program demoradar; {$MODE OBJFPC} {$MODESWITCH EXTERNALCLASS} uses ChartJS; var config: TChartConfiguration; dataset1, dataset2: TChartRadarDataset; begin config := TChartConfiguration.new; config.type_ := 'radar'; config.data := TChartData.new; config.data.labels := ['Eating', 'Drinking', 'Sleeping', 'Designing', 'Coding', 'Cycling', 'Running']; dataset1 := TChartRadarDataset.new; dataset1.label_ := 'My First Dataset'; dataset1.data := [65, 59, 90, 81, 56, 55, 40]; dataset1.fill := True; dataset1.backgroundColor := 'rgba(255, 99, 132, 0.2)'; dataset1.borderColor := 'rgb(255, 99, 132)'; dataset1.pointBackgroundColor := 'rgb(255, 99, 132)'; dataset1.pointBorderColor := '#fff'; dataset1.pointHoverBackgroundColor := '#fff'; dataset1.pointHoverBorderColor := 'rgb(255, 99, 132)';

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)
  • a converter from maXbox to lpr project files

https://wiki.freepascal.org/pas2js

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. Then I pass the file above to the transpiler:

pas2js can automatically include this file (rtl.js) in the generated output, like this:

pas2js -Jc -Jirtl.js -Tbrowser demoradar.lpr >>> C:\Program Files\Streaming\maxbox4\examples\pas2js-windows-2.2.0\pas2js-windows-2.2.0\bin\i386-win32>pas2js -Jc -Jirtl.js -Tbrowser demoradar.lpr Pas2JS Compiler version 2.2.0 [2022/02/22] for Win32 i386 Copyright (c) 2021 Free Pascal team. Info: 8699 lines in 6 files compiled, 0.2 secs

The pas2js compiler and RTL are — naturally — open source and can be downloaded and used freely. And I got my output as a javasscript file demoradar.js

var pas = { $libimports: {}}; var rtl = { version: 20200, quiet: false, debug_load_units: false, debug_rtti: false, $res : {}, debug: function(){ if (rtl.quiet || !console || !console.log) return; console.log(arguments); }, error: function(s){ rtl.debug('Error: ',s); throw s; }, warn: function(s){ rtl.debug('Warn: ',s); }, checkVersion: function(v){ if (rtl.version != v) throw "expected rtl version "+v+", but found "+rtl.version; }, hiInt: Math.pow(2,53), hasString: function(s){ return rtl.isString(s) && (s.length>0); }, isArray: function(a) { return Array.isArray(a); }, isFunction: function(f){ return typeof(f)==="function"; }, and much more...............................................................

This is then integrated in a html file:

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content="Example showing how to use TChartJS"> <meta name="author" content="silvioprog"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js" integrity="sha256-oSgtFCCmHWRPQ/JmR4OoZ3Xke1Pw4v50uh6pLcu+fIc=" crossorigin="anonymous"></script> <script src="demoradar.js"></script> <title>TChartJS example</title> <style> .title { margin: 20px 0 20px 0 } </style> </head> <body> <div class="container"> <h3 class="title"> TChartJS <small class="text-muted">Create radar charts using Pas2JS & ChartJS (<a href="demoradar.lpr">view source</a>)</small> </h3> <canvas id="myChart" height="100"></canvas> </div> <script> rtl.run(); </script> </body> </html>

The crossorigin=”anonymous”> anonymous means:

Get Breitsch’s stories in your inbox

Join Medium for free to get updates from this writer.

Request uses CORS headers and credentials flag is set to 'same-origin'. There is no exchange of user credentials via cookies, client-side SSL certificates or HTTP authentication, unless destination is the same origin.

which you can see and call at:

http://softwareschule.ch/examples/demoradar.html

Press enter or click to view image in full size

Chart.js provides a set of frequently used chart types, plugins, and customization options. In addition to a reasonable set of built-in chart types, you can use additional community-maintained chart types . On top of that, it’s possible to combine several chart types into a mixed chart (essentially, blending multiple chart types into one on the same canvas).

Releases of pas2js

The releases contain binaries for Windows (32 and 64bit), Linux (64 bit) and macOS.

Installation procedure:

1. Download pas2js from:

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

2. Unpack it in folder of your choice. The example below uses C:\lazarus\pas2js\. The release contains three folders:

  • bin — contains the compiler as executable (pas2js or pas2js.exe), a pas2js.cfg, a library and some utilities.
  • demo — lots of examples
  • packages — the Pascal units of the RTL and other packages.
  • tools — html2form — HTML to pascal code converter program
  • utils — A script to create a pas2js.cfg: createconfig.pp
Press enter or click to view image in full size

https://wiki.freepascal.org/lazarus_pas2js_integration

\maxbox4\examples\pas2js-windows-2.2.0\pas2js-windows-2.2.0\bin\i386-win32>pas2js -Jc -Jirtl.js -Tbrowser ..\..\demo\pacman\pacman.lpr Pas2JS Compiler version 2.2.0 [2022/02/22] for Win32 i386 Copyright © 2021 Free Pascal team. \pacman.lpr(6,15)Hint:Unit”JS”notused in pacman \pacman.lpr(6,38)Hint:Unit”Web”notused in pacman \packages\fcl-base\browserapp.pas(32,9) Hint: Unit “Rtl.BrowserLoadHelper” not used in browserapp \maxbox4\examples\pas2js-windows-2.2.0\pas2js-windows-2.2.0\packages\rtl\Rtl.BrowserLoadHelper.pas(46,32) Hint: Parameter”response”not used \pacman\upacman.pp(6,34)Hint:Unit”JS”notused in upacman \upacman.pp(1403,23) Hint:Function result does not seem to be set \upacman.pp(1414,23) Hint:Function result does not seem to be set \upacman.pp(90,30)Hint:Parameter”Event”not used \upacman.pp(91,35)Hint:Parameter”Sender”not used \upacman.pp(92,33)Hint:Parameter”aEvent”not used \upacman.pp(144,28) Hint:Private method “TPacman.DoRestartClick”is never used \upacman.pp(255,11) Hint:Local constant”WallSet”notused \maxbox4\examples\pas2js-windows-2.2.0\pas2js-windows-2.2.0\demo\pacman\upacman.pp(309,3)Hint:Localvariable”n”not used Info: 30569 lines in 14 files compiled, 0.3 secs

C:\Program Files\Streaming\maxbox4\examples\pas2js-windows-2.2.0\pas2js-windows-2.2.0\bin\i386-win32>

PacMan transpiled

Press enter or click to view image in full size

Originally published at http://maxbox4.wordpress.com on March 19, 2023.

--

--