[fpc-pascal] Adding Javascript to Pascal

Anthony Walter sysrpl at gmail.com
Fri Feb 21 12:04:38 CET 2025


Greetings all,

I am working on a project that needs scripting and I've decided to make use
of Fabrice Bellard's awesome QuickJS library. I am considering making it
nice to use for the rest of the Free Pascal user community by implementing
an interesting way of integrating Javascript that would make its usage as
easy as possible.

Before I do so however, I wanted to get some feedback on how this would
work to gauge if it would be of interest to fellow Free Pascal users. Here
is how it would work. All feedback would be appreciated.

Javascript:

let data = { name: "James", age: 25 };

function update(name) {
  data.name = name;
}

function verify(note) {
  console.log("received note " + note);
  console.log("data name is " + data.name);
  hello(data, " message from javascript");
  console.log("table size is " + table.size[0] + " by " + table.size[1]);
}

Pascal:

function JavascriptCallback(Script: IScript; Args: TScriptValues):
TScriptValue;
begin
  WriteLn(Args[0].age, Args[1]);
  // writes 25 message from javascript
  Result := Nothing;
end;

var
  TestScript: IJavascript;

procedure LoadScript;
begin
   Script := NewScript(MyScriptText);
   Script.Global.table.size := [15, 6];
   // { table: { size: [15, 6] } } is now available to javascript
   Script.Global.hello := Script.DefineCallback(JavascriptCallback);
end;

procedure CallScript;
begin
  // Call the javascript verify method passing it a note
  Script.Global.verify("a note from pascal");
  // verify will call back out JavascriptCallback function
  Script.Global.update("Ralph");
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20250221/c1c6913c/attachment.htm>


More information about the fpc-pascal mailing list