Recent

Author Topic: Pascal Script and Interfaces  (Read 1069 times)

Jan_

  • New Member
  • *
  • Posts: 17
Pascal Script and Interfaces
« on: March 18, 2022, 01:14:16 pm »
Hi,
i try to get into Pascal Script.
Code: Pascal  [Select][+][-]
  1.   mycar : Volkswagen;
  2.   mywifescar : BMW;
  3.   mywifessecondcar : driveable;
  4. begin
  5.   mycar := Volkswagen.create();
  6.   mycar.drive();
  7.   mywifescar := BMW.create();
  8.   mywifessecondcar :=  mywifescar.drive();
  9.   mywifessecondcar.drive();
  10. end.

Quote
Script:
Classname: Volkswagen
Funktion: Volkswagen --> Drive
Classname: BMW
Funktion: BMW --> Drive

Compiled:
Classname: Volkswagen
Funktion: Volkswagen --> Drive
Classname: BMW
Funktion: BMW --> Drive
Classname: BMW
Funktion: BMW --> Drive

My classes:
Code: Pascal  [Select][+][-]
  1. driveable = interface
  2.     function drive( ):driveable;
  3.   end;
  4. car = class Abstract (TInterfacedObject,driveable)
  5.   public
  6.     function drive( ):driveable;virtual;Abstract;
  7.   end;
  8. Volkswagen = class(car,driveable)
  9.   public
  10.     function drive( ):driveable;override;
  11.   end;
  12. BMW = class(car,driveable)
  13.   public
  14.     function drive( ):driveable;override;
  15.   end;
i do connect the pascal script with the classes:
Code: Pascal  [Select][+][-]
  1. procedure mypascalscript.OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
  2. begin
  3.   RIRegister_Std(x);
  4.   RIRegister_Classes(x,true);
  5.  
  6.   with x.Add(car) do
  7.   begin
  8.     RegisterVirtualAbstractMethod(car,@car.drive, 'drive');
  9.   end;
  10.  
  11.   with x.Add(Volkswagen) do
  12.   begin
  13.     RegisterMethod(@volkswagen.drive, 'drive');
  14.   end;
  15.  
  16.   with x.Add(BMW) do
  17.   begin
  18.     RegisterMethod(@BMW.drive, 'drive');
  19.   end;
  20. end;
  21.  
  22. procedure mypascalscript.OnCompile(Sender: TPSScript);
  23. begin
  24.   SIRegister_Std(Sender.Comp);
  25.   SIRegister_Classes(Sender.Comp,true);
  26.  
  27.   with (Sender.Comp.AddInterface(Sender.Comp.FindInterface('IUNKNOWN'), GetTypeData(TypeInfo(driveable))^.GUID,'driveable')) do
  28.   begin
  29.     RegisterMethod('Function drive( ):driveable', TPSCallingConvention(CdCdecl));
  30.   end;
  31.  
  32.  
  33.   with Sender.Comp.AddClassN(Sender.Comp.FindClass('TOBJECT'),'car') do
  34.   begin
  35. //    RegisterMethod('Function drive( ):driveable'); // is abstract --> may no decleration?
  36.   end;
  37.  
  38.  
  39.   with Sender.Comp.AddClassN(Sender.Comp.FindClass('car'),'Volkswagen') do
  40.   begin
  41.     RegisterMethod('Function drive( ):driveable');
  42.   end;
  43.  
  44.   with Sender.Comp.AddClassN(Sender.Comp.FindClass('car'),'BMW') do
  45.   begin
  46.     RegisterMethod('Function drive( ):driveable');
  47.   end;
  48. end;

may there is an Error, on the connection between my Classes and the APscal script or why does   mywifessecondcar.drive(); dont work on the Pascal script, but on the compiled source?

Thanks Jan



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11789
  • Debugger - SynEdit - and more
    • wiki
Re: Pascal Script and Interfaces
« Reply #1 on: March 18, 2022, 03:06:21 pm »
I assume RemObjects PascalScript?

I don't have the answer, but maybe some hints that can help investigating.
Note this is just general stuff about PascalScript. I have never used it with interfaces myself. I do not know if any of this applies to your issue.




1)
If you use the PascalScript from the Lazarus installer => try the upstream project instead.




2)
You need to mention your CPU, Bitness and OS.

PascalScript internally has to builds the stack for passing params (in some cases this includes the result too) to called functions. This code differs for each CPU, Bitness and OS.  (look for the .inc files, with target names)
In addition to  CPU/OS the stack layout also depends on the data-type. And sometimes the order of the params.

In other word, you may well be able to call some functions with certain param-signatures, yet other may fail if the stack-building code handles it wrong.

You may be able to debug this, by checking what your code (if it is called) receives for each param.
(If there is any issue, please be aware that reports need to go to the upstream project)


I believe they are replacing their own stack-building, with replacement functions from the compiler/rtl/packages. I don't know if (or to what degree) that is available with fpc. (I guess, if at all then only latest git 3.3.1).

An other way to avoid any issue with the stack building is to use the ".....Name" methods.
E.g. "RegisterMethodName" instead of "RegisterMethod".
But that is only available for a subset of functionality. (And a lot more work to implement. See package EditorMacroScript in the IDE for example code).

Jan_

  • New Member
  • *
  • Posts: 17
Re: Pascal Script and Interfaces
« Reply #2 on: March 20, 2022, 10:44:30 am »
Wich other ObjectPascal Scripting exsits for FreePascal?
i looked at DW-Script - it seems not to run with FPC 3.x.

Thaddy

  • Hero Member
  • *****
  • Posts: 18300
  • Here stood a man who saw the Elbe and jumped it.
Re: Pascal Script and Interfaces
« Reply #3 on: March 20, 2022, 12:07:41 pm »
Wich other ObjectPascal Scripting exsits for FreePascal?
i looked at DW-Script - it seems not to run with FPC 3.x.
It does, with some very slight modifications. ({$ifdef fpc}{$mode delphi}{$endif})
Do not expect too much from a scripting language, though. I usually bind to Python, when necessary. (previously to JS)
Do not have an unrealistic view about scripting.
« Last Edit: March 20, 2022, 01:48:09 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018