Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2025-02

Adding Javascript to Pascal

11 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anthony Walter

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;

Reply by email

Alexey T.
to Anthony Walter

Scripting is good thing. But I hate JS. No mandatory quotes around dict keys. Arrow functions which are sugar and not needed. etc etc. I suggest to use Python.
Python-for-Lazarus is in the github.
I like Python!
Alexey

Reply by email

DougC
to Alexey T.

I am also allergic to javascript!

Consider Lua instead.

Doug C.

---- On Fri, 21 Feb 2025 07:35:39 -0500 Alexey T. via fpc-pascal wrote ---

Scripting is good thing. But I hate JS. No mandatory quotes around dict keys. Arrow functions which are sugar and not needed. etc etc. I suggest to use Python.
Python-for-Lazarus is in the github.
I like Python!
Alexey

Reply by email

Unknown author
to DougC

I'm not a fan of javascript myself, but I see nothing wrong with having it as an option.  It's never bad to have another tool in the toolbox.  I can see situations where it would be useful.  I'd not be likely to use it, because javascript isn't my thing, but I certainly can easily point to projects that could benefit from having a scripting language, and if js was available, I'd wager many more projects would use it.

Again, not my choice, but folks do tend to like it for some reason, so why not?

DougC wrote 28 lines
I am also allergic to javascript! Consider Lua instead. Doug C. ---- On Fri, 21 Feb 2025 07:35:39 -0500 *Alexey T. via fpc-pascal * wrote --- Scripting is good thing. But I hate JS. No mandatory quotes around dict keys. Arrow functions which are sugar and not needed. etc etc. I suggest to use Python. Python-for-Lazarus is in the github. I like Python! Alexey _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

DougC
to Unknown author

Why not? Because it helps a distinctly inferior language design to perpetuate.

It used to be said that any programmer that first learned to program using BASIC was ruined for life. I would say the same thing about Javascript. 

Doug C.

---- On Fri, 21 Feb 2025 17:46:49 -0500 wrote ---

Again, not my choice, but folks do tend to like it for some reason, so why not?

Reply by email

Karoly Balogh
to DougC

Hi,

DougC wrote 6 lines
Why not? Because it helps a distinctly inferior language design to perpetuate. It used to be said that any programmer that first learned to program using BASIC was ruined for life. I would say the same thing about Javascript. 

We're in the offtopic again, but I really hate this kind of elitism. As a kid I started with BASIC - on various Commodore home computers then also on early IBM PCs - and I hope to think I grew up to be a semi-decent programmer. At least I can fix your Pascal compiler for ya...

In fact, in I think nowadays one of the big problems is, we do not really have anything similar to BASIC to teach really young kids. Python, which they usually try to use for this purpose is way too strict in its syntax, and complicated. Just like most modern languages (Pascal/Delphi included).

Not to mention, Pascal - originally conceived as a teaching language! - had its own share of abuse from this kind of elitist thinking, it's enough to think about that infamous "Real Programmers don't use Pascal" letter/post from the early eighties.

So, yeah. I do not like Javascript, to put it mildly. But also, lets be real for a moment - it's here to stay. So nothing wrong can come out to gain at least some understanding and knowledge of it.

Charlie

Reply by email

Arne Hanssen
to Karoly Balogh

Den 24.02.2025 09:27, skreiv Karoly Balogh via fpc-pascal:

Show quoted text 18 lines
Hi, On Sat, 22 Feb 2025, DougC via fpc-pascal wrote: > Why not? Because it helps a distinctly inferior language design to > perpetuate. > > It used to be said that any programmer that first learned to program > using BASIC was ruined for life. I would say the same thing about > Javascript. [...] In fact, in I think nowadays one of the big problems is, we do not really have anything similar to BASIC to teach really young kids. Python, which they usually try to use for this purpose is way too strict in its syntax, and complicated. Just like most modern languages (Pascal/Delphi included). [...]

As a former OS/2 user I find REXX (Regina Rexx nowadays) very versatile and useful - and easy to learn.  But you're right, this is off-topic, sorry.

Best regards Arne Hanssen

Reply by email

Michalis Kamburelis
to Karoly Balogh: We're in the offtopic again, but I really hate this kind of elitism. As a kid I started…
Karoly Balogh wrote
We're in the offtopic again, but I really hate this kind of elitism. As a kid I started with BASIC - on various Commodore home computers then also on early IBM PCs - and I hope to think I grew up to be a semi-decent programmer. At least I can fix your Pascal compiler for ya...

It's good to hear some positive voices in this thread :)

1. Let's focus on constructive answers to questions like "how do I integrate Pascal with language X", for any X, *before* we jump into discussion with statements "I hate X". People want such integration also because they like Pascal, not only X. We work in an ecosystem filled with lots of technologies, with devs knowing other languages than Pascal, with www browsers using JS, whether we like it or not. The choice of integrating with JS (or really any other popular language X) may be just reasonable for a given project.

2. I don't have a constructive answer myself, though I know people working on QuickJS integration with Castle Game Engine, see #quickjs channel on Castle Game Engine Discord (
https://castle-engine.io/talk.php ) and
https://github.com/castle-engine/quickjs-demo . You can touch with people on that channel, in particular Coldzer0, if you're looking for people familiar with Pascal + QuickJS.

3. Discussions whether language X is good or bad should be an independent discussion, in an independent thread(s), IMHO. Because such talk can quickly go off-topic -- which this thread clearly shows. The poster didn't get any sort of an answer he counted on (feedback about QuickJS + Pascal), instead the thread went into JS and do we like it, and Python, and Lua, and Basic...

We can all talk about advantages and disadvantages of various languages, and to be clear I think it can be a constructive discussion, but it should be a separate thread(s). Because we know such discussions can get both lengthy and heated :)

4. Personally, from the "integration with other languages", I'm looking in the future for Python + Castle Game Engine integration, which I described for
https://castle-engine.io/roadmap#_python_integration . I documented there the reasons "why" (you can scroll to the "why" section there).

Regards,
Michalis

Reply by email

Mattias Gaertner
to Anthony Walter: I am working on a project that needs scripting and I've decided to make use of Fabrice…
Anthony Walter wrote 22 lines
[...] 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]);
Show quoted text 12 lines
// 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 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?

Show quoted text 10 lines
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

Reply by email

Tomas Hajny
to Arne Hanssen: Den 24.02.2025 09:27, skreiv Karoly Balogh via fpc-pascal: As a former OS/2 user I find…
Arne Hanssen wrote
Den 24.02.2025 09:27, skreiv Karoly Balogh via fpc-pascal: > On Sat, 22 Feb 2025, DougC via fpc-pascal wrote:

Hi,

> [...] As a former OS/2 user I find REXX (Regina Rexx nowadays) very versatile and useful - and easy to learn.  But you're right, this is off-topic, sorry.

FWIW, integration to REXX has already been provided with FPC since more than 20 years ago (for all of OS/2 REXX, Regina and Amiga/AROS REXX implementations).

Tomas

Reply by email