Pas2js Transpiler

Compiling pascal into javascript

Sun, 15 Sep 2019

As good as pascal has been over the years, it has had one major flaw. It simply cannot be used to write web applications that are as good looking and efficient as web applications written in JavaScript libraries like React etc. There has been some attempts to address this limitation over the years, one of them being the Pas2js Transpiler. It transpiles Pascal code directly into JavaScript that can be executed on browsers.

One of the Pas2js demo’s called fcldb reads data from json files and displays them in a grid. Below is some of the code used in the demo …

constructor TForm.create;
begin 
    Panel:=document.createElement('div');
    // attrs are default array property...
    Panel['class']:='panel panel-default';
    PanelContent:=document.createElement('div');
    PanelContent['class']:='panel-body';
    Button:=document.createElement('input');
    Button['id']:='Button1';
    Button['type']:='submit';
    Button.className:='btn btn-default';
    Button['value']:='Fetch countries';
    TJSHTMLElement(Button).onclick:=@ButtonClick;
    document.body.appendChild(panel);
    Panel.appendChild(PanelContent);
    PanelContent.appendChild(Button);

    Conn:=TRestConnection.Create(nil);
    Conn.BaseURL:='countries.json';
    Conn.OnGetURL:=@DoGetURL;

    DS:=TRestDataset.Create(Nil);
    DS.Connection:=Conn;
    DS.OnLoadFail:=@DoLoadFail;
    DS.AfterLoad:=@DoAfterLoad;
    DS.AfterOpen:=@DSOpen;  
end;

function TForm.CreateTable : TJSElement;
Var
    TH,R,H : TJSElement;
begin
    Result:=document.createElement('TABLE');
    Result.className:='table table-striped table-bordered table-hover table-condensed';
    TH:=document.createElement('THEAD');
    Result.Append(TH);
    R:=document.createElement('TR');
    TH.Append(R);
    H:=document.createElement('TH');
    R.Append(H);
    H.AppendChild(document.createTextNode('Name'));
    H:=document.createElement('TH');
    R.Append(H);
    H.AppendChild(document.createTextNode('Population'));
end;                            

When the pascal is compiled into javascript, the end result is a 443.7 Kb javascript file, that you can run from here. Its certainly seems fast enough but the size of the resulting javascript file is a worry.

Conclusion

Although the Pas2js Transpiler allows pascal programmers to write applications for the web. The concept seems flawed in my opinion. The web environment itself is perhaps more difficult to learn than the javascript programming language. Pas2js really doesn’t address that difficulty. If one must use pascal to write web applications then something like uniGUI Web Application Framework might be a better solution. It creates an atmosphere in which the web environment appears moreso like a desktop environment. That will be more familar to pascal developers.

Loading...