Lazarus

Programming => General => Topic started by: domasz on May 17, 2025, 07:49:33 pm

Title: 64 bits and PascalScript
Post by: domasz on May 17, 2025, 07:49:33 pm
Is there a problem with Pascal Script in 64 bits? 32 bit Lazarus runs the same code using Pascal Script just fine. 64 bit Lazarus crashes in weird places.
Title: Re: 64 bits and PascalScript
Post by: Martin_fr on May 17, 2025, 08:27:09 pm
I can't recall any from the top of my head.

But it may well depend on what data types you use.

Also, it may depend on which (and how) access to your compiled code you setup.
- callbacks use some asm to emulate calls. that code is different for each cpu / bitness
- events between script and compiled app only work for some cpu
Title: Re: 64 bits and PascalScript
Post by: LV on May 17, 2025, 10:38:55 pm
There is certainly an issue:

https://forum.lazarus.freepascal.org/index.php/topic,70668

https://forum.lazarus.freepascal.org/index.php/topic,70535

https://forum.lazarus.freepascal.org/index.php/topic,68442
Title: Re: 64 bits and PascalScript
Post by: domasz on May 18, 2025, 12:40:02 pm
Thanks, @LV!
Title: Re: 64 bits and PascalScript
Post by: maurog on May 18, 2025, 04:59:49 pm
I use PascalScript in LazCAD and have successfully compiled and run it on both Linux and Windows (64-bit). The application runs smoothly without any crashes. However, in form (GUI) scripts, the event handler routines – such as Button1.OnClick – are not executed.
Title: Re: 64 bits and PascalScript
Post by: jamie on May 18, 2025, 10:10:03 pm
I guess the question is, is it actually executing the events and are they executing 32-bit versions of the address?

I think using 32-bit addresses wouldn't play well in a 64-bit system.

Title: Re: 64 bits and PascalScript
Post by: maurog on May 21, 2025, 08:25:29 am
@jamie
I’ve just compiled and run my application in 32-bit mode on Windows 7 64-bit, and now the events are being triggered correctly.

So it works!

This leads me to believe that PascalScript indeed uses 32-bit method pointers internally, which causes problems when running in 64-bit mode — especially for event handlers (like OnClick).

Thanks a lot for the helpful hints and confirmations!

Best regards,
Maurog
Title: Re: 64 bits and PascalScript
Post by: Thaddy on May 21, 2025, 09:14:00 am
That does not take away that really there should be, at one point, an effort to de-32bit the code. I don't use Pascal script anymore, so count me out.
Title: Re: 64 bits and PascalScript
Post by: jamie on May 21, 2025, 11:27:42 pm
@jamie
I’ve just compiled and run my application in 32-bit mode on Windows 7 64-bit, and now the events are being triggered correctly.

So it works!

This leads me to believe that PascalScript indeed uses 32-bit method pointers internally, which causes problems when running in 64-bit mode — especially for event handlers (like OnClick).

Thanks a lot for the helpful hints and confirmations!

Best regards,
Maurog

 The point I was trying to make is that in 64-bit mode it still maybe constructing the event address to call the method however, it could be cutting off the upper 32-bit part and there for could be calling some even code in la la land.

 This obviously would be cause for alarm.

Title: Re: 64 bits and PascalScript
Post by: maurog on June 06, 2025, 02:06:28 pm
I'm using Pascal Script as a runtime scripting engine in LazCAD, and I originally planned to develop a form designer for creating script-based interfaces. However, due to the issue mentioned above – namely, that events are not being triggered correctly – I’ve put the project on hold for now.

Now I’m faced with a decision: Should I take a closer look at Pascal Script and try to make it 64-bit compatible, even though I have limited experience in that area? Or would it be more reasonable to switch to a different scripting language? (In that case, I would also need to adapt the CAD scripting interface.)

Which of the two options would involve less effort overall? And if switching turns out to be the better path – which alternative scripting language would you recommend?

Thanks a lot!
Maurog-
Title: Re: 64 bits and PascalScript
Post by: Martin_fr on June 06, 2025, 02:44:36 pm
I have a distant memory that event were simple not implemented for all bitness/cpu combinations.
But a recent look at the IFDEF blocks says they are (or well at least the code seems to be "not empty").

In any case you will most likely find that the issue is in "simulating the call" to the procedure.

Any call (event or otherwise) needs to pass the parameters. Some need to be in CPU registers, others need to be on the stack, possible with specific alignments. (depends also on calling convention)

Newer versions of Delphi have some API to make calls using RTTI... I don't know what is avail in FPC 3.3.1 (and I heard somewhere it may need some so/dll - but I may have mistaken something else).

Anyway, afaik, the script engine does not make use of this in FPC.

Instead the script engine "builds" the callstack itself. That requires assembler code, as it needs to set the correct cpu registers.

There is an inc file for each CPU, if you want to look at it...



I don't recall exactly, but I may have had some issues with that even in normal procedure/methods calls... Not sure, could be the got fixed.
Anyway, the more architectures were to be supported, the more risky this approach is. Because you need to test a ton of different calls (with different mix of parameters by type and order) for each of them.

PascalScript offers a 2nd way:
    RegisterMethodName
    RegisterPropertyNameHelper
(the version in Lazarus has more of this than the original)

This register a function that doesn't get called with the parameters directly as given by the caller.

E.g. if the caller does
    Foo(1,'abc')

then the registered method does not get an integer and a string.
It gets a "TPSStack" object. And that will have 2 entries that you can get as int/string (or as TPSVariant... which you will need e.g. for enum).

The advantage: It doesn't need the per-CPU asm stuff. The method signature is known at compile time, and it is a normal method call generated by the compiler.

But afaik that doesn't exist for events ....

Title: Re: 64 bits and PascalScript
Post by: LV on June 07, 2025, 11:59:17 pm
If you develop a graphical interface on the host program side, it is quite simple and works reliably in both 32-bit and 64-bit operating systems. Below is a minimal working example. The main script is running, calculating values, interacting with GUI elements, and generating graphs. A user script processes the event of pressing the Plot Settings button.
Enjoy  :)

Updated: In the project1 object inspector, the DejaVu Sans Mono font is set for SynEdit1 and Memo1. If this font is missing  :(, set an available font for correct display. For cross-platform support, you can add the code:

Code: Pascal  [Select][+][-]
  1. const
  2.   {$IFDEF MSWINDOWS}
  3.     TARGET_FONT = 'Consolas';
  4.   {$ENDIF}
  5.   {$IFDEF LINUX}
  6.     TARGET_FONT = 'DejaVu Sans Mono';
  7.   {$ENDIF}
  8.  
  9. ...
  10. ...
  11. ...
  12.  
  13. SynEdit1.Font.Name := TARGET_FONT;
  14. Memo1.Font.Name := TARGET_FONT;
  15.  
Title: Re: 64 bits and PascalScript
Post by: maurog on June 08, 2025, 07:13:08 pm
@LV
Thanks for the example. What I mean is the following:
The example script below works correctly when the host application is 32-bit.
However, if the host is 64-bit, the button click is not detected or executed:
Code: Pascal  [Select][+][-]
  1. var Form1: TForm;
  2.     Button1: TButton;
  3.     Memo1: TMemo;
  4.  
  5.  
  6. procedure  OnButton1Click(Sender: TObject);
  7. begin
  8.   Memo1.Text := '';
  9.   Memo1.Text := 'Button1Click';
  10. end;
  11.  
  12. begin
  13.   Form1 := TForm.Create(nil);
  14.   Button1 := TButton.Create(Form1);
  15.   Button1.Parent := Form1;
  16.   Button1.OnClick := @OnButton1Click;
  17.   Memo1 := TMemo.Create(Form1);
  18.   Memo1.Top := 50;
  19.   Memo1.Parent := Form1;
  20.   Form1.Show;
  21. end.            
  22.  
Title: Re: 64 bits and PascalScript
Post by: LV on June 08, 2025, 08:15:36 pm
However, if the host is 64-bit, the button click is not detected or executed
For this reason, I refused to create a GUI on the script side in 64-bit applications. Pascal Script is used to save and then run the host program, which includes many settings for initial and boundary conditions of modeling, saving intermediate results at specified times, and data visualization, among other features. In other words, the user does not need to enter a lot of initial data and settings; he just runs the script, which is useful when performing serial calculations.
Title: Re: 64 bits and PascalScript
Post by: LV on June 14, 2025, 11:44:07 pm
However, if the host is 64-bit, the button click is not detected or executed
For this reason, I refused to create a GUI on the script side in 64-bit applications.

I changed my mind. Compile and run the attached project.  ;)


“Talk is cheap. Show me the code.”
— Linus Torvalds
Title: Re: 64 bits and PascalScript
Post by: maurog on June 15, 2025, 06:06:29 pm
Yes, that would be a possibility. Thank you very much.
Title: Re: 64 bits and PascalScript
Post by: LV on June 15, 2025, 07:17:33 pm
Sometimes I catch myself thinking that I stubbornly do some things in Pascal. Although it may be suitable, such as Lua (https://github.com/malcome/Lua4Lazarus), for these purposes.  :)
Title: Re: 64 bits and PascalScript
Post by: maurog on June 15, 2025, 08:27:56 pm
Yes, Pascal is simply more fun. Besides, if you use PascalScript, you don’t need any DLLs or shared objects.
And on top of that, for PascalScript there’s already a simple IDE with full debugger support implemented.
I’ll probably stick with PascalScript, and when I have more time (right now I’m busy with TurboBird), I plan to take a deep dive into the PascalScript source code.
Maybe I’ll be able to improve something. And if not, I’ll contact Carlo (the author). Maybe he’ll help us.

Title: Re: 64 bits and PascalScript
Post by: PascalDragon on June 16, 2025, 09:58:12 pm
Newer versions of Delphi have some API to make calls using RTTI... I don't know what is avail in FPC 3.3.1 (and I heard somewhere it may need some so/dll - but I may have mistaken something else).

The API is available even in FPC 3.2.2. Without a function call manager it can be used for register-functions on i386 as well as on x86_64-win64. For other platforms (and calling conventions) the use of libffi through the FFI.Manager unit is required (and thus the use of libffi.so).
Title: Re: 64 bits and PascalScript
Post by: LV on June 19, 2025, 11:17:30 pm
Hi

The suggestion above to solve this problem is a workaround. It consisted of writing wrappers and registering in the script engine for the host program procedures that ran additional event handler scripts. This is not very convenient. After getting to know the PascalScript source code a little, you can find another solution. The attached project shows the idea of ​​registering handlers that can be created in the main script code using a simple example.
The functionality has been tested using Lazarus 3.4; FPC 3.2.2 on Win32, Win64, and Deb64.

Title: Re: 64 bits and PascalScript
Post by: lhl on January 04, 2026, 02:03:27 pm
O https://github.com/pult
 helped me use this fork: https://github.com/pult/pascalscript (https://github.com/pult/pascalscript)
, in which the issue with x64 events started to work correctly.
Link to the discussion in 2020: https://github.com/remobjects/pascalscript/issues/230 (https://github.com/remobjects/pascalscript/issues/230)

However, I was not completely satisfied with this solution, as I believe the fix should be made by RemObjects itself and officially made available in the Lazarus distribution. For this reason, I raise this question on the Lazarus forum:
https://forum.lazarus.freepascal.org/index.php/topic,70668 (https://forum.lazarus.freepascal.org/index.php/topic,70668)
Title: Re: 64 bits and PascalScript
Post by: maurog on January 05, 2026, 07:33:38 pm
I have discussed this issue directly with the PascalScript developers on GitHub:
https://github.com/remobjects/pascalscript/issues/271

My original post there:

Hello, I have integrated PascalScript into LazCAD (a 2D CAD application based on Lazarus/FPC).
The problem with events in GUI scripts only occurs in 64-bit applications.
Hence the speculation in the Lazarus forum that the scripter always uses 32-bit pointers for events, regardless of whether it is compiled in 32- or 64-bit.
https://forum.lazarus.freepascal.org/index.php/topic,71170.0.html

In the 32-bit version of LazCAD, the events are triggered correctly.

Response from the PascalScript team:

This was fixed in commit 2ebc623 for Delphi.
The fix is not enabled for FPC, because it's not known if it works there.
I can enable it in FPC if you can confirm it works.

If I understand this correctly, Lazarus/FPC uses its own port of PascalScript, and this 64-bit fix is currently not enabled or integrated there.
Therefore, it seems that the FPC/Lazarus port may need to be adjusted or tested so that GUI events work correctly in 64-bit applications.

Best regards,
Maurog
Title: Re: 64 bits and PascalScript
Post by: Martin_fr on January 05, 2026, 09:25:48 pm
If I understand this correctly, Lazarus/FPC uses its own port of PascalScript, and this 64-bit fix is currently not enabled or integrated there.
Therefore, it seems that the FPC/Lazarus port may need to be adjusted or tested so that GUI events work correctly in 64-bit applications.

True, but as explained, the inclusion is made only to fulfill editor-macro needs, and they do not use events. So the current version fully works.

Its a long time, since I checked (probably a year, maybe longer), in the past most updates had commit messages marking them as Delphi only. And its been unclear which branch to follow.
I see that there have been cherry picks, since...

So when I find time, I may merge them into 4.99.



But even if, I will only test that it works for editor macros. And nothing else.

If the commit you look for is
Quote
86a057c8686143ce0443be6995d6515c5c9b705c
* 1)InvokeCall added, instead of all different callers (x86, x64, powerpc etc) for Delphi 2010+. (#207)

then I don't know if it will work... From hear say, if at all then only with fpc 3.3.1, and potentially some extra conditions on top (but I have no idea).

So for your own project, I am afraid, but I can't help you.
All that any update would do, is to save you from downloading a copy from remobjects, and you probably have done that already.

If it is InvokeCall, then ask on the fpc list (or the fpc section of the forum) what you need to use the function. Once that works, you should then be able to use it with PascalScript too. (I guess).
Title: Re: 64 bits and PascalScript
Post by: Martin_fr on January 05, 2026, 09:31:19 pm
As for the changes that are in lazarus, they are mostly removal of ifdef code.
Or fixing ifdef, for unsupported targets, ie. make it compile, even if it will not run.

There is an addition (that IIRC exists as PR to upstream) for an alternative registration of methods.


So using the upstream version should work just as well, if not better.
Title: Re: 64 bits and PascalScript
Post by: LV on January 05, 2026, 10:56:43 pm
Some time ago, I needed to create a cross-platform application that would work on both Windows 64-bit and Debian 64-bit. The integrated script had to manage various types of numerical and symbolic mathematics, plot graphs, interact with the host program's GUI, dynamically create forms and components, and handle events. To my surprise, Lazarus, along with the included Pascal Script, accomplished this task with ease and without any masochism. The challenge of event handling in a 64-bit environment was effectively addressed by managing events in child scripts.
Title: Re: 64 bits and PascalScript
Post by: franciscoluiz on January 06, 2026, 04:07:20 pm
Hi

The suggestion above to solve this problem is a workaround. It consisted of writing wrappers and registering in the script engine for the host program procedures that ran additional event handler scripts. This is not very convenient. After getting to know the PascalScript source code a little, you can find another solution. The attached project shows the idea of ​​registering handlers that can be created in the main script code using a simple example.
The functionality has been tested using Lazarus 3.4; FPC 3.2.2 on Win32, Win64, and Deb64.

estas granda plezuro renkonti iun, kiu scias esperanton.

en: it's a great pleasure to meet someone who knows esperanto
Title: Re: 64 bits and PascalScript
Post by: PascalDragon on January 06, 2026, 09:22:01 pm
estas granda plezuro renkonti iun, kiu scias esperanton.

Please only use English on the international part of the forum or at least provide an English translation.
Title: Re: 64 bits and PascalScript
Post by: maurog on January 07, 2026, 02:37:59 am
@Martin_fr
Quote
So when I find time, I may merge them into 4.99.

Thank you very much for your effort.

@lhl
Quote
O https://github.com/pult
 helped me use this fork: https://github.com/pult/pascalscript
, in which the issue with x64 events started to work correctly.

Thank you for the hint.
Does this fork also work under Linux?

If so, since I am already using a slightly modified version of PascalScript from the Lazarus repository (among other things for the debugger), I will most likely use the fork at
https://github.com/pult/pascalscript

for the time being.
Title: Re: 64 bits and PascalScript
Post by: LV on January 07, 2026, 09:15:41 am
estas granda plezuro renkonti iun, kiu scias esperanton.

en: it's a great pleasure to meet someone who knows esperanto

Mi ĵus lernas Esperanton. Ĝi estas eleganta lingvo, kiu mi esperas baldaŭ fariĝos la lingvo de la forumo Lazarus. 😉.

Kaj en programado, mi ĵus malkovris Lape, unu el la plej rapidaj enigitaj skriptoj por la Pascal-mondo.
- malpeza;
- plurplatforma;
- potenca (grandordo pli rapida ol PascalScript, pli rapida ol pura Python);
- povas fari ĉion, aŭ preskaŭ ĉion.

Mi finos per iu grava laboro kaj iom post iom reverkos la skriptajn partojn de miaj projektoj en Lape.

P.S. Dankon al @zamtmn, kiu prezentis al mi Lape en sia mallonga afiŝo https://forum.lazarus.freepascal.org/index.php/topic,16665.msg571301.html#msg571301 👌

En:
I'm just learning Esperanto. It's an elegant language that I hope will soon become the language of the Lazarus forum. 😉.

And in programming, I recently discovered Lape, one of the fastest embedded scripts for the Pascal world.
- lightweight;
- cross-platform;
- powerful (an order of magnitude faster than PascalScript, faster than pure Python);
- can do everything, or almost everything.

I'll finish up with some important work and gradually rewrite the scripting parts of my projects in Lape.

P.S. Thanks to @zamtmn, who introduced me to Lape in his short post https://forum.lazarus.freepascal.org/index.php/topic,16665.msg571301.html#msg571301 👌


Title: Re: 64 bits and PascalScript
Post by: DonAlfredo on January 07, 2026, 10:46:39 am
Lape looks very interesting. I have added it (as an installable module) into fpcupdeluxe.
Will be available next release.
Title: Re: 64 bits and PascalScript
Post by: PascalDragon on January 08, 2026, 09:56:17 pm
If the commit you look for is
Quote
86a057c8686143ce0443be6995d6515c5c9b705c
* 1)InvokeCall added, instead of all different callers (x86, x64, powerpc etc) for Delphi 2010+. (#207)

then I don't know if it will work... From hear say, if at all then only with fpc 3.3.1, and potentially some extra conditions on top (but I have no idea).

It should work with FPC 3.2.2 as well (except the one or other bug), please note however that on non-Windows platforms or for stdcall and cdecl on i386-win32 libffi is required (for now).
Title: Re: 64 bits and PascalScript
Post by: maurog on March 28, 2026, 03:55:44 pm
Hi,

I have extended the form designer from
https://github.com/havlicekp/form-designer

and integrated it with the RemObjects PascalScript Demo IDE.

Result:

Drag & Drop Form Designer – components can be placed directly using the mouse
Automatic Code Linking – double-clicking a designer element jumps directly to the corresponding code location
Debugger Support – breakpoints, Step Into / Step Over, etc.
Cross-platform – Linux & Windows

I have published the project on GitHub:
https://github.com/mdadali/PascalScript_IDE

There is an animated GIF right on the start page, so you don’t have to compile everything just to get an impression.

The project is still experimental, but I will definitely continue developing it.
Once you have such a tool, you can use it everywhere in the long term—especially where automation is needed.

Unfortunately, the well-known 64-bit bug in PascalScript is still present:
Events in GUI scripts are not triggered in the 64-bit version.

I have already slightly modified the JvDesign and PascalDebugger components for debugging purposes and provided them in the source/componente directory.
However, I currently lack the in-depth knowledge required to fix the 64-bit issue myself.

It would be great if someone with the necessary expertise could address this—then we would have a truly full-featured tool in the long run.

Best regards,
Maurog.
Title: Re: 64 bits and PascalScript
Post by: maurog on May 02, 2026, 01:10:53 pm
Hello,
I fixed the 64-bit bug (events were not being triggered) with the help of AI and this article:
https://stackoverflow.com/questions/33648962/compiling-pascalscript-in-64-bit-mode-events-not-working
The result can be found here:
https://github.com/mdadali/PascalScript_IDE
Best regards,
Maurog.
TinyPortal © 2005-2018