Recent

Author Topic: PascalScript can't use classes like TStringList  (Read 8656 times)

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
PascalScript can't use classes like TStringList
« on: July 10, 2016, 01:07:01 am »
Hi, when I try to use a TStringList for example, the application crashes and gives me a SIGSEV error.

I've already attached the Plugin to the Script, and also it compiles, so is not a compilation problem of the script but a problem executing it.

Attached an example. Just uncomment the lines in the SynEdit and try.

derek.john.evans

  • Guest
Re: PascalScript can't use classes like TStringList
« Reply #1 on: July 10, 2016, 04:16:12 am »
Ok, Ive had a look for what its worth. It seems all constructors are broken. I had a look in uPSRuntime & x86.inc which includes a lot of FPC specific edits to handle FPC constructor calling conventions, all of which are beyond my knowledge.

So, I came up with two work arounds:

1) Create an external function which creates the TStringList. ie:

Code: Pascal  [Select][+][-]
  1. function CreateStringList: TStringList;
  2. begin
  3.   Result := TStringList.Create;
  4. end;
  5.  
  6. procedure TForm1.PSScript1Compile(Sender: TPSScript);
  7. begin
  8.   PSScript1.AddFunction(@ShowMessage, 'procedure ShowMessage(m: string);');
  9.   PSScript1.AddFunction(@CreateStringList, 'function CreateStringList: TStringList;');
  10. end;  
  11.  

Then this code works:
Code: Pascal  [Select][+][-]
  1. var
  2.   s: TStringList;
  3. begin
  4.   ShowMessage('This line works.');
  5.   s := CreateStringList;
  6.   s.Add('Test');
  7.   ShowMessage(s[0]);
  8.   s.Free;
  9. end.  
  10.  
But, it still will crash if you call the constructor. So:

2) Register a constructor for TStringList which links to the CreateStringList function:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PSScript1CompImport(Sender: TObject; x: TPSPascalCompiler);
  2. begin
  3.   x.FindClass('TStringList').RegisterMethod('constructor Create');
  4. end;
  5.  
  6. procedure TForm1.PSScript1ExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
  7. begin
  8.   x.FindClass('TStringList').RegisterConstructor(@CreateStringList, 'Create');
  9. end;  
  10.  

This "shouldn't" work but it does for some reason.

Im guessing someone with more knowledge of FPC constructor calling needs to look at this issue. But, there are 2 workarounds.
« Last Edit: July 10, 2016, 04:19:06 am by Geepster »

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: PascalScript can't use classes like TStringList
« Reply #2 on: July 10, 2016, 08:34:33 am »
EDIT: My suggestion doesn't work anymore. I use Geepsters workaround now.

If I remember right, you should edit uPSC_classes.pas so that the constructor becomes available. There's a {$IFDEF DELPHI2005UP} that prevents the constructor from being registered. Dont forget to do the same for the TStrings class! Its in the same file, a bit up.

Code: Pascal  [Select][+][-]
  1. procedure SIRegisterTSTRINGLIST(Cl: TPSPascalCompiler);
  2. begin
  3.   with Cl.AddClassN(cl.FindClass('TStrings'), 'TStringList') do
  4.   begin
  5.  
  6. {$IFDEF DELPHI2005UP}
  7.     RegisterMethod('constructor Create;');
  8. {$ENDIF}
  9.  
  10.     RegisterMethod('function Find(S:String;var Index:Integer):Boolean');
  11.     RegisterMethod('procedure Sort');
  12.     RegisterProperty('Duplicates', 'TDuplicates', iptrw);
  13.     RegisterProperty('Sorted', 'Boolean', iptrw);
  14.     RegisterProperty('OnChange', 'TNotifyEvent', iptrw);
  15.     RegisterProperty('OnChanging', 'TNotifyEvent', iptrw);
  16.   end;
  17. end;
  18.  
« Last Edit: January 16, 2017, 01:05:50 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: PascalScript can't use classes like TStringList
« Reply #3 on: July 10, 2016, 03:39:50 pm »
Thanks Geepster, both workarounds are working.
Kapibara, commenting the ifdef does not works for me, you've tested it?

Edit: Reported here http://bugs.freepascal.org/view.php?id=30363
« Last Edit: July 10, 2016, 04:08:24 pm by lainz »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9913
  • Debugger - SynEdit - and more
    • wiki
Re: PascalScript can't use classes like TStringList
« Reply #4 on: July 10, 2016, 06:03:59 pm »
please test with the upstream/original source.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: PascalScript can't use classes like TStringList
« Reply #5 on: July 10, 2016, 06:46:53 pm »
please test with the upstream/original source.

Yes, it works with the PascalScript downloaded from GitHub.

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: PascalScript can't use classes like TStringList
« Reply #6 on: July 11, 2016, 07:17:55 am »
@Lainz Yes, a year ago I managed to import the TStringList and use from inside script, but it involved modifying a line or two of the source. Unfortunately I cant remember the exact steps.

Looking at the source for the app I wrote, this is how the stringlist was made available. But it required the modified pascalscript source which later got accidentally replaced by a fresh unmodified version. :-[

Code: Pascal  [Select][+][-]
  1. procedure TDataParser.OnCompImport(Sender: TObject; ACompiler: TPSPascalCompiler);
  2. begin
  3.   SIRegister_Std(ACompiler);
  4.   SIRegisterTObject(ACompiler);
  5.   SIRegisterTStrings(ACompiler, True); //Both are
  6.   SIRegisterTStringList(ACompiler);    //needed!!
  7. end;
  8.  

Code: Pascal  [Select][+][-]
  1. procedure TDataParser.OnExecImport(Sender: TObject; APSExec: TPSExec;
  2.   AClassImporter: TPSRuntimeClassImporter);
  3. begin
  4.   RIRegister_Std(AClassImporter);
  5.   RIRegisterTObject(AClassImporter);
  6.   RIRegisterTStrings(AClassImporter, True);  //Both are
  7.   RIRegisterTStringList(AClassImporter);     //Needed !!
  8. end;
« Last Edit: July 11, 2016, 07:51:08 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: PascalScript can't use classes like TStringList
« Reply #7 on: July 12, 2016, 07:07:15 pm »
I'm now using the GitHub version, I need only to comment one line of code that don't want to be compiled and that's all. My code works with no modifications and no need to do OnCompImport and OnExecImport.

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: PascalScript can't use classes like TStringList
« Reply #8 on: July 13, 2016, 03:35:29 am »
@lainz - Thats great! I want it up and running too and removed lazarus preinstalled pascalscript and installed the GIT version. For me there was no error on install except lazarus package EditorMacroScript needed something from the removed sources. Anyway, I just wanted to see if it worked, but got SIGSEG in the script on TStringList.Create even after adding the PSImport_classes plugin to PSScript.

Could you describe more closely what you did, or attach a small sample that you know works. Maybe its my installation that is wrong.
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: PascalScript can't use classes like TStringList
« Reply #9 on: July 13, 2016, 03:47:25 am »
I commented a line when compiling it and also the ide was using the old package. I deleted the folder under components and replaced with the git one i downloaded from github. Then i rebuild lazarus.

The first post here contains an attachment that works just uncomment the lines and run it.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: PascalScript can't use classes like TStringList
« Reply #10 on: July 13, 2016, 03:49:33 am »
Also im using lazarus release not trunk...

 

TinyPortal © 2005-2018