[fpc-pascal] Adding Javascript to Pascal

Mattias Gaertner nc-gaertnma at netcologne.de
Mon Feb 24 13:23:58 CET 2025



On 2/21/25 12:04, Anthony Walter via fpc-pascal wrote:
>[...]
> 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

Here I see one big problem.
Pascal needs specific types at compile-time.
JS at compile time can only check some basic syntax. An IDE can use some 
heuristic to guess, what an identifier is referring to, but how should 
that work with a compiler?



>     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;

Mattias



More information about the fpc-pascal mailing list